make this more modern <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">…
- User
make this more modern
code - Assistant
make this more modern
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #2c3e50, #34495e);
color: #ecf0f1;
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
padding: 20px;
transition: background 0.5s ease;
}
h1 {
margin-bottom: 20px;
color: #ecf0f1;
animation: fadeIn 1s ease-in-out;
}
/* Room Interface */
#room-interface {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}
#room-interface input {
padding: 10px;
margin: 5px 0;
width: 220px;
border: 1px solid #7f8c8d;
border-radius: 5px;
background-color: #34495e;
color: #ecf0f1;
transition: border 0.3s ease;
}
#room-interface input:focus {
border: 1px solid #2980b9;
outline: none;
}
#room-interface button {
padding: 10px 20px;
margin-top: 10px;
background-color: #2980b9;
border: none;
color: white;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
}
#room-interface button:hover {
background-color: #3498db;
transform: scale(1.05);
}
/* Status Message */
#status {
margin-bottom: 20px;
font-size: 1.2em;
color: #bdc3c7;
animation: fadeIn 1s ease-in-out;
}
/* Current Room */
#current-room {
display: none;
margin-bottom: 10px;
font-size: 1em;
color: #bdc3c7;
animation: fadeIn 1s ease-in-out;
}
/* Game Grid */
#game {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 15px;
margin-bottom: 20px;
animation: fadeIn 1s ease-in-out;
}
.cell {
width: 100px;
height: 100px;
background-color: #2c3e50;
border: 2px solid #7f8c8d;
border-radius: 10px;
font-size: 2.5em;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.1s ease, color 0.3s ease;
position: relative;
}
.cell::after {
content: '';
position: absolute;
width: 0;
height: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: width 0.3s ease, height 0.3s ease;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 50%;
z-index: 0;
}
.cell:hover::after {
width: 120%;
height: 120%;
}
.cell:hover {
background-color: #34495e;
}
.cell.clicked {
animation: pop 0.3s ease;
z-index: 1;
}
.cell.disabled {
cursor: not-allowed;
background-color: #1a252f;
color: #7f8c8d;
}
/* Restart Button */
#restart {
padding: 10px 20px;
font-size: 1em;
background-color: #c0392b;
border: none;
color: white;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
animation: fadeIn 1s ease-in-out;
}
#restart:hover {
background-color: #e74c3c;
transform: scale(1.05);
}
#restart:disabled {
background-color: #7f8c8d;
cursor: not-allowed;
transform: none;
}
/* Responsive Design */
@media (max-width: 500px) {
#game {
grid-template-columns: repeat(3, 80px);
grid-gap: 10px;
}
.cell {
width: 80px;
height: 80px;
font-size: 2em;
}
#room-interface input {
width: 160px;
}
}
/* Keyframes */
@keyframes pop {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
Here's an updated version of your Multiplayer Tic Tac Toe game with a more modern design, including a refresh button that appears when disconnected from the server and modern in-page notifications instead of traditional alert dialogs.
Modernized Design:
Modern Notifications:
alert dialogs with sleek, dismissible toast notifications that appear at the top-right corner.Connection Refresh:
Improved User Experience:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Multiplayer Tic Tac Toe</title>
<style>
/* CSS Variables for Theme Management */
:root {
--primary-color: #2c3e50;
--secondary-color: #34495e;
--accent-color: #2980b9;
--accent-hover: #3498db;
--error-color: #c0392b;
--success-color: #27ae60;
--background-gradient: linear-gradient(135deg, #2c3e50, #34495e);
--font-family: 'Roboto', sans-serif;
--transition-speed: 0.3s;
--toast-width: 300px;
}
/* Reset and Basic Styles */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: var(--font-family);
background: var(--background-gradient);
color: #ecf0f1;
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
padding: 20px;
transition: background 0.5s ease;
}
h1 {
margin-bottom: 20px;
color: #ecf0f1;
animation: fadeIn 1s ease-in-out;
}
/* Room Interface */
#room-interface {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}
#room-interface input {
padding: 12px 15px;
margin: 8px 0;
width: 240px;
border: none;
border-radius: 8px;
background-color: #3b4c59;
color: #ecf0f1;
font-size: 1em;
transition: box-shadow var(--transition-speed) ease;
}
#room-interface input::placeholder {
color: #7f8c8d;
}
#room-interface input:focus {
outline: none;
box-shadow: 0 0 10px var(--accent-color);
}
#room-interface button {
padding: 12px 25px;
margin-top: 10px;
background-color: var(--accent-color);
border: none;
color: white;
border-radius: 8px;
cursor: pointer;
font-size: 1em;
transition: background-color var(--transition-speed) ease, transform 0.2s ease;
}
#room-interface button:hover {
background-color: var(--accent-hover);
transform: translateY(-2px);
}
#room-interface button:disabled {
background-color: #7f8c8d;
cursor: not-allowed;
transform: none;
}
/* Status Message */
#status {
margin-bottom: 20px;
font-size: 1.2em;
color: #bdc3c7;
animation: fadeIn 1s ease-in-out;
}
/* Current Room */
#current-room {
display: none;
margin-bottom: 10px;
font-size: 1em;
color: #bdc3c7;
animation: fadeIn 1s ease-in-out;
}
/* Game Grid */
#game {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 20px;
margin-bottom: 20px;
animation: fadeIn 1s ease-in-out;
}
.cell {
width: 100px;
height: 100px;
background-color: var(--primary-color);
border: 2px solid #7f8c8d;
border-radius: 12px;
font-size: 2.5em;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background-color var(--transition-speed) ease, transform 0.1s ease, color var(--transition-speed) ease;
position: relative;
}
.cell::after {
content: '';
position: absolute;
width: 0;
height: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: width 0.3s ease, height 0.3s ease;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 50%;
z-index: 0;
}
.cell:hover::after {
width: 120%;
height: 120%;
}
.cell:hover {
background-color: var(--secondary-color);
}
.cell.clicked {
animation: pop 0.3s ease;
z-index: 1;
}
.cell.disabled {
cursor: not-allowed;
background-color: #1a252f;
color: #7f8c8d;
}
/* Restart Button */
#restart {
padding: 12px 25px;
font-size: 1em;
background-color: #c0392b;
border: none;
color: white;
border-radius: 8px;
cursor: pointer;
transition: background-color var(--transition-speed) ease, transform 0.2s ease;
animation: fadeIn 1s ease-in-out;
}
#restart:hover {
background-color: #e74c3c;
transform: translateY(-2px);
}
#restart:disabled {
background-color: #7f8c8d;
cursor: not-allowed;
transform: none;
}
/* Refresh Button */
#refresh {
padding: 12px 25px;
font-size: 1em;
background-color: var(--accent-color);
border: none;
color: white;
border-radius: 8px;
cursor: pointer;
transition: background-color var(--transition-speed) ease, transform 0.2s ease;
display: none;
margin-top: 20px;
}
#refresh:hover {
background-color: var(--accent-hover);
transform: translateY(-2px);
}
/* Toast Notifications */
#toast-container {
position: fixed;
top: 20px;
right: 20px;
width: var(--toast-width);
z-index: 1000;
}
.toast {
background-color: #333;
color: #ecf0f1;
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
display: flex;
justify-content: space-between;
align-items: center;
animation: slideIn 0.5s ease forwards;
opacity: 0;
}
.toast.success {
border-left: 5px solid var(--success-color);
}
.toast.error {
border-left: 5px solid var(--error-color);
}
.toast .close-btn {
background: none;
border: none;
color: #ecf0f1;
font-size: 1.2em;
cursor: pointer;
}
/* Responsive Design */
@media (max-width: 500px) {
#game {
grid-template-columns: repeat(3, 80px);
grid-gap: 15px;
}
.cell {
width: 80px;
height: 80px;
font-size: 2em;
}
#room-interface input {
width: 180px;
}
#toast-container {
width: 250px;
}
}
/* Keyframes */
@keyframes pop {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
</style>
</head>
<body>
<h1>Multiplayer Tic Tac Toe</h1>
<div id="room-interface">
<button id="create-room">Create Room</button>
<div style="margin: 10px 0; font-weight: bold; color: #bdc3c7;">OR</div>
<input type="text" id="room-code-input" placeholder="Enter Room Code" maxlength="6">
<button id="join-room">Join Room</button>
</div>
<div id="current-room">
Room Code: <span id="current-room-code"></span>
</div>
<div id="status">Please create or join a room to start the game.</div>
<div id="game" style="display: none;">
<!-- 9 cells -->
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<button id="restart" disabled>Restart</button>
<button id="refresh">Refresh Connection</button>
<!-- Toast Notification Container -->
<div id="toast-container"></div>
<script>
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');
const refreshButton = document.getElementById('refresh');
const toastContainer = document.getElementById('toast-container');
let socket;
let mySymbol;
let myTurn = false;
let gameActive = false;
let roomCode = null;
/**
* Display a toast notification.
* @param {string} message - The message to display.
* @param {string} type - 'success' or 'error'.
*/
function showToast(message, type = 'success') {
const toast = document.createElement('div');
toast.classList.add('toast', type);
toast.innerHTML = `
<span>${message}</span>
<button class="close-btn">×</button>
`;
toastContainer.appendChild(toast);
const closeBtn = toast.querySelector('.close-btn');
closeBtn.addEventListener('click', () => {
toast.remove();
});
// Automatically remove toast after 5 seconds
setTimeout(() => {
toast.remove();
}, 5000);
}
/**
* Initialize WebSocket connection.
*/
function connect() {
// Replace 'localhost' and port '6789' with your server's address and port if different
socket = new WebSocket('ws://localhost:6789');
socket.onopen = () => {
console.log('Connected to server');
showToast('Connected to server!', 'success');
refreshButton.style.display = 'none';
createRoomButton.disabled = false;
joinRoomButton.disabled = false;
roomCodeInput.disabled = false;
};
socket.onmessage = (event) => {
const message = JSON.parse(event.data);
switch(message.type) {
case 'created':
roomCode = message.room;
currentRoomCodeSpan.textContent = roomCode;
currentRoomDiv.style.display = 'block';
statusDiv.textContent = 'Waiting for an opponent to join...';
gameDiv.style.display = 'none';
showToast(`Room created! Share code: ${roomCode}`, 'success');
break;
case 'start':
mySymbol = message.symbol;
roomCode = message.room;
currentRoomCodeSpan.textContent = roomCode;
currentRoomDiv.style.display = 'block';
gameDiv.style.display = 'grid';
gameActive = true;
myTurn = mySymbol === 'X'; // X always starts
updateStatus();
showToast(`Game started! You are '${mySymbol}'.`, 'success');
break;
case 'turn':
const symbol = message.symbol;
myTurn = (symbol === mySymbol);
updateStatus();
break;
case 'move':
updateBoard(message.index, message.symbol);
break;
case 'end':
gameActive = false;
statusDiv.textContent = message.result;
restartButton.disabled = false;
showToast(message.result, message.result.includes('won') ? 'success' : 'error');
break;
case 'reset':
resetGame();
showToast('Game has been reset.', 'success');
break;
case 'error':
showToast(message.message, 'error');
createRoomButton.disabled = false;
joinRoomButton.disabled = false;
roomCodeInput.disabled = false;
break;
default:
console.log('Unknown message type:', message.type);
}
};
socket.onclose = () => {
showToast('Disconnected from server.', 'error');
statusDiv.textContent = 'Disconnected from server.';
gameActive = false;
gameDiv.style.display = 'none';
currentRoomDiv.style.display = 'none';
refreshButton.style.display = 'block';
createRoomButton.disabled = true;
joinRoomButton.disabled = true;
roomCodeInput.disabled = true;
};
socket.onerror = (error) => {
console.error('WebSocket error:', error);
showToast('WebSocket error occurred.', 'error');
};
}
/**
* Update the status message based on the current turn.
*/
function updateStatus() {
if (!gameActive) return;
if (myTurn) {
statusDiv.textContent = `Your turn (${mySymbol})`;
} else {
statusDiv.textContent = `Opponent's turn`;
}
}
/**
* Update the game board with the opponent's move.
* @param {number} index - The cell index.
* @param {string} symbol - 'X' or 'O'.
*/
function updateBoard(index, symbol) {
const cell = document.querySelector(`.cell[data-index='${index}']`);
if (cell && cell.textContent === '') {
cell.textContent = symbol;
cell.classList.add('disabled', 'clicked');
}
}
/**
* Reset the game board for a new game.
*/
function resetGame() {
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('disabled', 'clicked');
});
restartButton.disabled = true;
statusDiv.textContent = 'Game reset. Waiting for opponent...';
myTurn = mySymbol === 'X';
gameActive = true;
updateStatus();
}
// Event Listeners
createRoomButton.addEventListener('click', () => {
socket.send(JSON.stringify({ type: 'create' }));
createRoomButton.disabled = true;
joinRoomButton.disabled = true;
roomCodeInput.disabled = true;
showToast('Creating room...', 'success');
});
joinRoomButton.addEventListener('click', () => {
const code = roomCodeInput.value.trim().toUpperCase();
if (code.length !== 6) {
showToast('Please enter a valid 6-character room code.', 'error');
return;
}
socket.send(JSON.stringify({ type: 'join', room: code }));
createRoomButton.disabled = true;
joinRoomButton.disabled = true;
roomCodeInput.disabled = true;
showToast(`Joining room: ${code}`, 'success');
});
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), room: roomCode }));
cell.textContent = mySymbol;
cell.classList.add('disabled', 'clicked');
myTurn = false;
updateStatus();
});
cell.addEventListener('animationend', () => {
cell.classList.remove('clicked');
});
});
restartButton.addEventListener('click', () => {
socket.send(JSON.stringify({ type: 'reset', room: roomCode }));
restartButton.disabled = true;
showToast('Restarting game...', 'success');
});
refreshButton.addEventListener('click', () => {
showToast('Reconnecting...', 'success');
connect();
});
window.addEventListener('load', () => {
connect();
});
</script>
</body>
</html>
CSS Enhancements:
JavaScript Functionality:
showToast function to handle in-page notifications, removing the need for alert dialogs. These notifications auto-dismiss after 5 seconds but can also be closed manually.User Experience Improvements:
Accessibility:
Font Integration: If you prefer the Roboto font, consider adding a link to Google Fonts in the <head> section:
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
Server Configuration: Ensure that your WebSocket server is correctly configured to handle connections from the updated client, especially if hosted on a different domain or port.
Security Enhancements: Implement necessary security measures for WebSocket communications, such as using wss:// in production environments.
Feature Extensions: Consider adding features like player usernames, score tracking, or animations for victories to further enhance the game.
This updated version should provide a more modern and user-friendly experience for players. Feel free to further customize the styles and functionalities to better suit your preferences!
Not yet included in a published release.