<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Multiplayer Tic…
- Usercode
- Assistant
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #121212; /* Dark background */
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
padding: 20px;
color: #ffffff; /* White text */
}
h1 {
margin-bottom: 20px;
color: #ffffff;
}
/* Room Interface */
#room-interface {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}
#room-interface input {
padding: 10px;
margin: 5px 0;
width: 200px;
background-color: #2c3e50;
color: #ecf0f1;
border: 1px solid #34495e;
border-radius: 5px;
}
#room-interface button {
padding: 10px 20px;
margin-top: 10px;
background-color: #1abc9c;
border: none;
color: #ffffff;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#room-interface button:hover {
background-color: #16a085;
}
/* Status Message */
#status {
margin-bottom: 20px;
font-size: 1.2em;
color: #cccccc;
}
/* Game Grid */
#game {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 10px;
margin-bottom: 20px;
}
.cell {
width: 100px;
height: 100px;
background-color: #2c3e50; /* Dark cell background */
border: 2px solid #34495e; /* Slightly lighter border */
border-radius: 10px;
font-size: 2.5em;
color: #ecf0f1; /* Light cell text color */
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.1s ease, box-shadow 0.3s ease;
}
.cell:hover {
background-color: #3b5998;
box-shadow: 0 0 10px rgba(59, 89, 152, 0.5);
}
.cell.disabled {
cursor: not-allowed;
background-color: #34495e; /* Dark background */
color: #7f8c8d; /* Muted text color */
}
.symbol-placed {
animation: pop-in 0.3s ease;
}
@keyframes pop-in {
0% {
transform: scale(0.5);
opacity: 0;
}
100% {
transform: scale(1);
opacity: 1;
}
}
/* Restart Button */
#restart {
padding: 10px 20px;
font-size: 1em;
background-color: #e74c3c;
border: none;
color: #ffffff;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#restart:hover {
background-color: #c0392b;
}
/* Responsive Design */
@media (max-width: 500px) {
#game {
grid-template-columns: repeat(3, 80px);
grid-gap: 8px;
}
.cell {
width: 80px;
height: 80px;
font-size: 2em;
}
#room-interface input {
width: 160px;
}
}
</style>
make this more modern
To modernize your Multiplayer Tic Tac Toe web application, we'll make several enhancements to the HTML, CSS, and JavaScript parts of your code. The improvements will focus on:
Below is the revamped version of your application incorporating these improvements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Multiplayer Tic Tac Toe</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Google Fonts for Modern Typography -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<!-- Font Awesome for Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/kTcxyQSj1+JXrItj3tb0UMFQsSJnOzHWbzqv6Hh5CZfz10vJg6oK1np4e3XtzKcM6L6m0CkP0Izow==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
/* CSS will be updated in the next section */
</style>
</head>
<body>
<header>
<h1>Multiplayer Tic Tac Toe</h1>
</header>
<main>
<section id="room-interface" aria-labelledby="room-interface-heading">
<h2 id="room-interface-heading">Room Management</h2>
<div class="buttons">
<button id="create-room" class="btn primary-btn"><i class="fas fa-plus"></i> Create Room</button>
<span class="separator">OR</span>
<div class="join-room">
<input type="text" id="room-code-input" placeholder="Enter Room Code" maxlength="6" aria-label="Room Code">
<button id="join-room" class="btn secondary-btn"><i class="fas fa-sign-in-alt"></i> Join Room</button>
</div>
</div>
</section>
<section id="current-room" aria-live="polite">
<p>Room Code: <strong id="current-room-code"></strong></p>
</section>
<section id="status" aria-live="polite">Please create or join a room to start the game.</section>
<section id="game" aria-label="Tic Tac Toe Board">
<div class="grid">
<!-- 9 cells -->
<button class="cell" data-index="0" aria-label="Cell 1"></button>
<button class="cell" data-index="1" aria-label="Cell 2"></button>
<button class="cell" data-index="2" aria-label="Cell 3"></button>
<button class="cell" data-index="3" aria-label="Cell 4"></button>
<button class="cell" data-index="4" aria-label="Cell 5"></button>
<button class="cell" data-index="5" aria-label="Cell 6"></button>
<button class="cell" data-index="6" aria-label="Cell 7"></button>
<button class="cell" data-index="7" aria-label="Cell 8"></button>
<button class="cell" data-index="8" aria-label="Cell 9"></button>
</div>
</section>
<button id="restart" class="btn danger-btn" disabled><i class="fas fa-redo"></i> Restart</button>
</main>
<footer>
<p>© 2023 Multiplayer Tic Tac Toe</p>
</footer>
<script>
// JavaScript will be updated in the next section
</script>
</body>
</html>
<header>, <main>, <section>, and <footer> for better structure.aria-label, aria-labelledby, and aria-live for assistive technologies.ws or wss) based on the page's protocol.const and let appropriately.aria-disabled attributes to cells when they are inactive.Server Integration: Ensure your WebSocket server is properly set up to handle the defined message types (create, join, move, reset, etc.). Update the WebSocket URL in the JavaScript if your server is hosted elsewhere.
Security Measures:
wss) in production environments.Enhancements:
Testing: Thoroughly test the application across different browsers and devices to ensure compatibility and responsiveness.
Deployment: Host your application on a reliable platform (e.g., Vercel, Netlify) and ensure your WebSocket server is accessible over the internet.
By implementing these modernizations, your Multiplayer Tic Tac Toe application will offer a better user experience, enhanced accessibility, and a more maintainable codebase. Feel free to customize the design further to match your branding or personal preferences!
Not yet included in a published release.
/* CSS Variables for Theming */
:root {
--background-color: #121212;
--primary-color: #1abc9c;
--primary-hover: #16a085;
--secondary-color: #3498db;
--secondary-hover: #2980b9;
--danger-color: #e74c3c;
--danger-hover: #c0392b;
--text-color: #ecf0f1;
--muted-text: #7f8c8d;
--border-color: #34495e;
--cell-hover: #3b5998;
--font-family: 'Roboto', sans-serif;
}
/* Global Styles */
* {
box-sizing: border-box;
}
body {
font-family: var(--font-family);
background-color: var(--background-color);
color: var(--text-color);
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
}
header, footer {
text-align: center;
margin-bottom: 20px;
}
h1 {
font-size: 2.5rem;
margin: 0;
}
h2 {
font-size: 1.5rem;
margin-bottom: 10px;
}
main {
width: 100%;
max-width: 500px;
}
.buttons {
display: flex;
flex-direction: column;
align-items: center;
}
.btn {
display: flex;
align-items: center;
gap: 8px;
padding: 12px 20px;
font-size: 1rem;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
margin: 5px 0;
width: 100%;
max-width: 250px;
}
.primary-btn {
background-color: var(--primary-color);
color: #fff;
}
.primary-btn:hover {
background-color: var(--primary-hover);
}
.secondary-btn {
background-color: var(--secondary-color);
color: #fff;
}
.secondary-btn:hover {
background-color: var(--secondary-hover);
}
.danger-btn {
background-color: var(--danger-color);
color: #fff;
width: auto;
padding: 10px 20px;
align-self: center;
}
.danger-btn:hover {
background-color: var(--danger-hover);
}
.btn:active {
transform: scale(0.98);
}
.separator {
margin: 10px 0;
color: var(--muted-text);
font-weight: bold;
}
.join-room {
display: flex;
gap: 10px;
width: 100%;
max-width: 250px;
}
.join-room input {
flex: 1;
padding: 10px;
border: 1px solid var(--border-color);
border-radius: 8px;
background-color: #2c3e50;
color: var(--text-color);
}
.join-room input:focus {
outline: none;
border-color: var(--primary-color);
}
#current-room {
text-align: center;
margin: 15px 0;
font-size: 1.1rem;
}
#status {
text-align: center;
margin: 15px 0;
font-size: 1.1rem;
color: var(--muted-text);
}
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
margin: 20px 0;
}
.cell {
background-color: #2c3e50;
border: 2px solid var(--border-color);
border-radius: 12px;
font-size: 2.5rem;
color: var(--text-color);
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.1s ease, box-shadow 0.3s ease;
width: 100%;
padding-top: 100%; /* For square cells */
position: relative;
}
.cell::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.cell:hover:not(.disabled) {
background-color: var(--cell-hover);
box-shadow: 0 0 10px rgba(59, 89, 152, 0.5);
}
.cell.disabled {
cursor: not-allowed;
background-color: var(--border-color);
color: var(--muted-text);
}
.symbol-placed {
animation: pop-in 0.3s ease;
}
@keyframes pop-in {
from {
transform: scale(0.5);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
footer p {
font-size: 0.9rem;
color: var(--muted-text);
}
/* Responsive Design */
@media (max-width: 600px) {
.grid {
gap: 10px;
}
.btn {
max-width: 100%;
}
.join-room {
flex-direction: column;
}
.join-room input, .join-room button {
max-width: 100%;
}
}
document.addEventListener('DOMContentLoaded', () => {
const createRoomButton = document.getElementById('create-room');
const joinRoomButton = document.getElementById('join-room');
const roomCodeInput = document.getElementById('room-code-input');
const currentRoomDiv = document.getElementById('current-room');
const currentRoomCodeSpan = document.getElementById('current-room-code');
const statusDiv = document.getElementById('status');
const gameDiv = document.getElementById('game');
const cells = document.querySelectorAll('.cell');
const restartButton = document.getElementById('restart');
let socket;
let mySymbol = '';
let myTurn = false;
let gameActive = false;
let roomCode = '';
const connectWebSocket = () => {
// Update the WebSocket URL as per your server configuration
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
const socketUrl = `${protocol}://${window.location.hostname}:6789`;
socket = new WebSocket(socketUrl);
socket.addEventListener('open', () => {
console.log('Connected to server');
});
socket.addEventListener('message', (event) => {
try {
const message = JSON.parse(event.data);
handleServerMessage(message);
} catch (error) {
console.error('Error parsing message:', error);
}
});
socket.addEventListener('close', () => {
statusDiv.textContent = 'Disconnected from server.';
disableGameInterface();
});
socket.addEventListener('error', (error) => {
console.error('WebSocket error:', error);
statusDiv.textContent = 'WebSocket error. Please try refreshing the page.';
});
};
const handleServerMessage = (message) => {
switch (message.type) {
case 'created':
roomCode = message.room;
displayCurrentRoom();
statusDiv.textContent = 'Waiting for an opponent to join...';
break;
case 'start':
initializeGame(message);
break;
case 'turn':
myTurn = (message.symbol === mySymbol);
updateStatus();
break;
case 'move':
updateBoard(message.index, message.symbol);
break;
case 'end':
endGame(message.result);
break;
case 'reset':
resetGame();
break;
case 'error':
alert(message.message);
enableRoomInterface();
break;
default:
console.warn('Unknown message type:', message.type);
}
};
const displayCurrentRoom = () => {
currentRoomCodeSpan.textContent = roomCode;
currentRoomDiv.style.display = 'block';
gameDiv.style.display = 'none';
};
const initializeGame = (message) => {
mySymbol = message.symbol;
roomCode = message.room;
displayCurrentRoom();
gameDiv.style.display = 'grid';
gameActive = true;
myTurn = mySymbol === 'X'; // X always starts
updateStatus();
// Clear any existing board
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('disabled', 'symbol-placed');
cell.removeAttribute('aria-disabled');
});
restartButton.disabled = true;
};
const updateStatus = () => {
if (!gameActive) return;
if (myTurn) {
statusDiv.textContent = `Your turn (${mySymbol})`;
} else {
statusDiv.textContent = 'Opponent\'s turn';
}
};
const updateBoard = (index, symbol) => {
const cell = document.querySelector(`.cell[data-index='${index}']`);
if (cell && cell.textContent === '') {
cell.textContent = symbol;
cell.classList.add('disabled', 'symbol-placed');
cell.setAttribute('aria-disabled', 'true');
}
};
const endGame = (result) => {
gameActive = false;
statusDiv.textContent = result;
restartButton.disabled = false;
};
const resetGame = () => {
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('disabled', 'symbol-placed');
cell.removeAttribute('aria-disabled');
});
restartButton.disabled = true;
statusDiv.textContent = 'Game reset. Waiting for opponent...';
myTurn = mySymbol === 'X';
gameActive = true;
updateStatus();
};
const disableGameInterface = () => {
gameActive = false;
gameDiv.style.display = 'none';
currentRoomDiv.style.display = 'none';
enableRoomInterface();
};
const enableRoomInterface = () => {
createRoomButton.disabled = false;
joinRoomButton.disabled = false;
roomCodeInput.disabled = false;
};
const enableRoomButtons = () => {
createRoomButton.disabled = false;
joinRoomButton.disabled = false;
roomCodeInput.disabled = false;
};
createRoomButton.addEventListener('click', () => {
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ type: 'create' }));
createRoomButton.disabled = true;
joinRoomButton.disabled = true;
roomCodeInput.disabled = true;
} else {
alert('Unable to connect to the server. Please try again later.');
}
});
joinRoomButton.addEventListener('click', () => {
const code = roomCodeInput.value.trim().toUpperCase();
const codePattern = /^[A-Z0-9]{6}$/;
if (!codePattern.test(code)) {
alert('Please enter a valid 6-character alphanumeric room code.');
return;
}
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ type: 'join', room: code }));
createRoomButton.disabled = true;
joinRoomButton.disabled = true;
roomCodeInput.disabled = true;
} else {
alert('Unable to connect to the server. Please try again later.');
}
});
cells.forEach(cell => {
cell.addEventListener('click', () => {
if (!gameActive || !myTurn || cell.textContent !== '') return;
const index = cell.getAttribute('data-index');
socket.send(JSON.stringify({ type: 'move', index: parseInt(index, 10), room: roomCode }));
cell.textContent = mySymbol;
cell.classList.add('disabled', 'symbol-placed');
cell.setAttribute('aria-disabled', 'true');
myTurn = false;
updateStatus();
});
});
restartButton.addEventListener('click', () => {
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ type: 'reset', room: roomCode }));
restartButton.disabled = true;
} else {
alert('Unable to communicate with the server. Please try again later.');
}
});
// Initialize WebSocket connection
connectWebSocket();
});