<!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 in one file
Certainly! Here's an updated version of your Multiplayer Tic Tac Toe game with a more modern design and enhanced functionality. This single-file approach leverages modern HTML5, CSS3 (including CSS variables for easier theming), and ES6+ JavaScript features for cleaner and more efficient code. Additionally, it incorporates improved responsiveness, enhanced accessibility, and smoother animations to provide a better user experience.
<!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">
<style>
/* CSS Variables for Easy Theming */
:root {
--background-color: #121212;
--text-color: #ffffff;
--primary-color: #1abc9c;
--primary-hover: #16a085;
--secondary-color: #2c3e50;
--secondary-hover: #34495e;
--accent-color: #e74c3c;
--accent-hover: #c0392b;
--cell-hover: #3b5998;
--cell-active: #34495e;
--font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
/* Reset and Basic Styles */
*, *::before, *::after {
box-sizing: border-box;
}
body {
margin: 0;
padding: 20px;
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;
}
h1 {
margin-bottom: 1.5rem;
font-size: 2rem;
text-align: center;
}
/* Room Interface */
#room-interface {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
max-width: 400px;
gap: 1rem;
margin-bottom: 2rem;
}
#room-interface button,
#room-interface input {
width: 100%;
padding: 0.75rem 1rem;
font-size: 1rem;
border-radius: 8px;
border: none;
outline: none;
transition: background-color 0.3s ease, transform 0.2s ease;
}
#create-room,
#join-room {
background-color: var(--primary-color);
color: var(--text-color);
cursor: pointer;
}
#create-room:hover,
#join-room:hover {
background-color: var(--primary-hover);
}
#room-code-input {
background-color: var(--secondary-color);
color: var(--text-color);
}
#room-code-input::placeholder {
color: #bdc3c7;
}
/* Current Room Display */
#current-room {
display: none;
margin-bottom: 1rem;
font-size: 1rem;
text-align: center;
}
/* Status Message */
#status {
margin-bottom: 2rem;
font-size: 1.2rem;
text-align: center;
min-height: 1.5em;
}
/* Game Grid */
#game {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 1rem;
width: 100%;
max-width: 320px;
margin-bottom: 2rem;
display: none;
}
.cell {
background-color: var(--secondary-color);
border: 2px solid var(--secondary-hover);
border-radius: 12px;
font-size: 2.5rem;
color: var(--text-color);
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
position: relative;
transition: background-color 0.3s ease, transform 0.1s ease, box-shadow 0.3s ease;
}
.cell::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 12px;
transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
.cell:hover::after {
background-color: var(--cell-hover);
box-shadow: 0 0 15px rgba(59, 89, 152, 0.5);
}
.cell.disabled {
cursor: not-allowed;
background-color: var(--cell-active);
color: #7f8c8d;
}
.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: 0.75rem 1.5rem;
font-size: 1rem;
background-color: var(--accent-color);
color: var(--text-color);
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
disabled: true;
}
#restart:disabled {
background-color: #7f8c8d;
cursor: not-allowed;
}
#restart:hover:not(:disabled) {
background-color: var(--accent-hover);
}
/* Responsive Design */
@media (max-width: 500px) {
h1 {
font-size: 1.5rem;
}
#game {
max-width: 240px;
grid-gap: 0.75rem;
}
.cell {
font-size: 2rem;
}
#room-interface button,
#room-interface input {
padding: 0.6rem 0.8rem;
font-size: 0.9rem;
}
#restart {
padding: 0.6rem 1.2rem;
font-size: 0.9rem;
}
}
</style>
</head>
<body>
<h1>Multiplayer Tic Tac Toe</h1>
<div id="room-interface" aria-label="Room Interface">
<button id="create-room">Create Room</button>
<div style="font-weight: bold;">OR</div>
<input type="text" id="room-code-input" placeholder="Enter Room Code" maxlength="6" aria-label="Room Code Input">
<button id="join-room">Join Room</button>
</div>
<div id="current-room" aria-live="polite">
Room Code: <span id="current-room-code"></span>
</div>
<div id="status" aria-live="polite">Please create or join a room to start the game.</div>
<div id="game" aria-label="Tic Tac Toe Game Grid">
<!-- 9 cells -->
<div class="cell" data-index="0" tabindex="0" aria-label="Cell 1"></div>
<div class="cell" data-index="1" tabindex="0" aria-label="Cell 2"></div>
<div class="cell" data-index="2" tabindex="0" aria-label="Cell 3"></div>
<div class="cell" data-index="3" tabindex="0" aria-label="Cell 4"></div>
<div class="cell" data-index="4" tabindex="0" aria-label="Cell 5"></div>
<div class="cell" data-index="5" tabindex="0" aria-label="Cell 6"></div>
<div class="cell" data-index="6" tabindex="0" aria-label="Cell 7"></div>
<div class="cell" data-index="7" tabindex="0" aria-label="Cell 8"></div>
<div class="cell" data-index="8" tabindex="0" aria-label="Cell 9"></div>
</div>
<button id="restart" disabled>Restart</button>
<script>
// Selectors
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');
// Game State Variables
let socket;
let mySymbol;
let myTurn = false;
let gameActive = false;
let roomCode = null;
// Initialize WebSocket Connection
function connect() {
// Update the WebSocket URL if your server is hosted elsewhere
socket = new WebSocket('ws://localhost:6789');
socket.addEventListener('open', () => {
console.log('Connected to the server');
});
socket.addEventListener('message', (event) => {
try {
const message = JSON.parse(event.data);
handleServerMessage(message);
} catch (error) {
console.error('Invalid JSON:', error);
}
});
socket.addEventListener('close', () => {
statusDiv.textContent = 'Disconnected from server.';
gameActive = false;
gameDiv.style.display = 'none';
currentRoomDiv.style.display = 'none';
createRoomButton.disabled = false;
joinRoomButton.disabled = false;
roomCodeInput.disabled = false;
});
socket.addEventListener('error', (error) => {
console.error('WebSocket error:', error);
});
}
// Handle Messages from Server
function handleServerMessage(message) {
switch(message.type) {
case 'created':
roomCode = message.room;
displayRoomCode();
statusDiv.textContent = 'Waiting for an opponent to join...';
break;
case 'start':
startGame(message.symbol, message.room);
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);
resetInterface();
break;
default:
console.warn('Unknown message type:', message.type);
}
}
// Display Current Room Code
function displayRoomCode() {
currentRoomCodeSpan.textContent = roomCode;
currentRoomDiv.style.display = 'block';
gameDiv.style.display = 'none';
}
// Start the Game
function startGame(symbol, room) {
mySymbol = symbol;
roomCode = room;
currentRoomCodeSpan.textContent = roomCode;
currentRoomDiv.style.display = 'block';
gameDiv.style.display = 'grid';
gameActive = true;
myTurn = mySymbol === 'X'; // X always starts
updateStatus();
}
// Update the Status Message
function updateStatus() {
if (!gameActive) return;
statusDiv.textContent = myTurn ? `Your turn (${mySymbol})` : `Opponent's turn`;
}
// Update the Game Board
function updateBoard(index, symbol) {
const cell = document.querySelector(`.cell[data-index='${index}']`);
if (cell && cell.textContent === '') {
cell.textContent = symbol;
cell.classList.add('disabled', 'symbol-placed');
}
}
// End the Game
function endGame(result) {
gameActive = false;
statusDiv.textContent = result;
restartButton.disabled = false;
}
// Reset the Game
function resetGame() {
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('disabled', 'symbol-placed');
});
restartButton.disabled = true;
statusDiv.textContent = 'Game reset. Waiting for opponent...';
myTurn = mySymbol === 'X';
gameActive = true;
updateStatus();
}
// Reset Interface on Errors or Disconnections
function resetInterface() {
createRoomButton.disabled = false;
joinRoomButton.disabled = false;
roomCodeInput.disabled = false;
currentRoomDiv.style.display = 'none';
gameDiv.style.display = 'none';
statusDiv.textContent = 'Please create or join a room to start the game.';
restartButton.disabled = true;
gameActive = false;
}
// Event Listeners for Room Creation and Joining
createRoomButton.addEventListener('click', () => {
socket.send(JSON.stringify({ type: 'create' }));
createRoomButton.disabled = true;
joinRoomButton.disabled = true;
roomCodeInput.disabled = true;
statusDiv.textContent = 'Creating room...';
});
joinRoomButton.addEventListener('click', () => {
const code = roomCodeInput.value.trim().toUpperCase();
if (!/^[A-Z0-9]{6}$/.test(code)) {
alert('Please enter a valid 6-character room code.');
return;
}
socket.send(JSON.stringify({ type: 'join', room: code }));
createRoomButton.disabled = true;
joinRoomButton.disabled = true;
roomCodeInput.disabled = true;
statusDiv.textContent = 'Joining room...';
});
// Event Listeners for Game Cells
cells.forEach(cell => {
cell.addEventListener('click', () => handleCellClick(cell));
// Enable keyboard accessibility
cell.addEventListener('keypress', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
handleCellClick(cell);
}
});
});
// Handle Cell Click
function handleCellClick(cell) {
if (!gameActive || !myTurn || cell.textContent !== '') return;
const index = cell.getAttribute('data-index');
socket.send(JSON.stringify({ type: 'move', index: parseInt(index), room: roomCode }));
cell.textContent = mySymbol;
cell.classList.add('disabled', 'symbol-placed');
myTurn = false;
updateStatus();
}
// Event Listener for Restart Button
restartButton.addEventListener('click', () => {
socket.send(JSON.stringify({ type: 'reset', room: roomCode }));
restartButton.disabled = true;
statusDiv.textContent = 'Restarting game...';
});
// Initialize WebSocket Connection on Page Load
window.addEventListener('load', connect);
</script>
</body>
</html>
Modern CSS Features:
:root for defining color schemes and fonts, making theming easier.pop-in animation and added smooth hover effects.Semantic HTML & Accessibility:
aria-label, aria-live, and tabindex attributes to improve accessibility.Enter or Space keys.Clean and Efficient JavaScript:
const and let for variable declarations, arrow functions, and template literals for better readability.Enhanced User Interface:
Accessibility Enhancements:
aria-live regions to announce dynamic content changes to assistive technologies.Optimized WebSocket Handling:
Secure WebSocket (WSS):
wss://) instead of ws:// to encrypt data transmission.Room Code Generation:
Server Validation:
Error Messaging:
Visual Enhancements:
Sound Effects:
Hosting:
This updated version should provide a more modern, accessible, and user-friendly experience for players. Feel free to further customize the design and functionality to better suit your needs!
Not yet included in a published release.