Chess
Chess
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplayer Chess</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background-color: #f4f4f4;
}
h1 {
margin-bottom: 20px;
}
.board {
display: grid;
grid-template-columns: repeat(8, 50px);
grid-template-rows: repeat(8, 50px);
gap: 0;
margin-bottom: 20px;
}
.square {
width: 50px;
height: 50px;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
cursor: pointer;
}
.light {
background-color: #f0d9b5;
}
.dark {
background-color: #b58863;
}
.selected {
outline: 2px solid yellow;
}
.chat {
width: 400px;
display: flex;
flex-direction: column;
gap: 10px;
}
.messages {
height: 200px;
overflow-y: auto;
padding: 10px;
border: 1px solid #ccc;
background: #fff;
}
.message-input {
display: flex;
gap: 10px;
}
.message-input input {
flex-grow: 1;
padding: 5px;
border: 1px solid #ccc;
border-radius: 4px;
}
.message-input button {
padding: 5px 10px;
border: none;
background-color: #007bff;
color: white;
border-radius: 4px;
cursor: pointer;
}
.message-input button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Multiplayer Chess</h1>
<div class="chat">
<h2>Chat</h2>
<div id="messages" class="messages"></div>
<div class="message-input">
<input id="chatInput" type="text" placeholder="Type a message...">
<button id="sendButton">Send</button>
</div>
</div>
<script>
const boardEl = document.getElementById('board');
const messagesEl = document.getElementById('messages');
const chatInput = document.getElementById('chatInput');
const sendButton = document.getElementById('sendButton');
const pieceEmojis = {
'r': '♜', 'n': '♞', 'b': '♝', 'q': '♛', 'k': '♚', 'p': '♟',
'R': '♖', 'N': '♘', 'B': '♗', 'Q': '♕', 'K': '♔', 'P': '♙'
};
const initialBoard = [
['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
];
let board = JSON.parse(JSON.stringify(initialBoard));
let selectedSquare = null;
let currentPlayer = 'white';
let move = null;
sendButton.addEventListener('click', () => {
const message = chatInput.value.trim();
if (message) {
const payload = { type: 'chat_message', message };
socket.send(JSON.stringify(payload));
addMessage(`You: ${message}`);
chatInput.value = '';
}
});
if (to.row < 0 || to.row >= 8 || to.col < 0 || to.col >= 8) return false;
switch (piece.toLowerCase()) {
case 'p':
if (from.col === to.col && targetPiece === '' &&
((currentPlayer === 'white' && to.row === from.row - 1) ||
(currentPlayer === 'black' && to.row === from.row + 1))) {
return true;
}
if (Math.abs(from.col - to.col) === 1 && targetPiece !== '' &&
((currentPlayer === 'white' && to.row === from.row - 1) ||
(currentPlayer === 'black' && to.row === from.row + 1))) {
return true;
}
return false;
case 'n':
const rowDiff = Math.abs(from.row - to.row);
const colDiff = Math.abs(from.col - to.col);
return (rowDiff === 2 && colDiff === 1) || (rowDiff === 1 && colDiff === 2);
case 'b':
return Math.abs(from.row - to.row) === Math.abs(from.col - to.col) && !isPathBlocked(from, to);
case 'r':
return (from.row === to.row || from.col === to.col) && !isPathBlocked(from, to);
case 'q':
return (Math.abs(from.row - to.row) === Math.abs(from.col - to.col) ||
from.row === to.row || from.col === to.col) && !isPathBlocked(from, to);
case 'k':
const kingRowDiff = Math.abs(from.row - to.row);
const kingColDiff = Math.abs(from.col - to.col);
return kingRowDiff <= 1 && kingColDiff <= 1;
default:
return false;
}
};
return false;
};
renderBoard();
</script>
</body>
</html>