Update templates/client.html
Browse files- templates/client.html +91 -83
templates/client.html
CHANGED
|
@@ -1,100 +1,108 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
-
<html>
|
| 3 |
<head>
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
</head>
|
| 17 |
<body>
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
<h2 id="status"></h2>
|
| 22 |
-
<div id="board"></div>
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
let myColor = null, board = [], selected = null;
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
.then(res => res.json())
|
| 41 |
-
.then(data => { board = data.board; render(); });
|
| 42 |
-
});
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
for (let r = 0; r < 8; r++) {
|
| 60 |
-
for (let c = 0; c < 8; c++) {
|
| 61 |
-
const div = document.createElement("div");
|
| 62 |
-
div.className = `cell ${(r + c) % 2 ? "black" : "white"}`;
|
| 63 |
-
div.onclick = () => handleClick(r, c);
|
| 64 |
-
const piece = board[r][c];
|
| 65 |
-
if (piece) {
|
| 66 |
-
const p = document.createElement("div");
|
| 67 |
-
p.className = `piece ${piece}`;
|
| 68 |
-
div.appendChild(p);
|
| 69 |
-
}
|
| 70 |
-
boardDiv.appendChild(div);
|
| 71 |
-
}
|
| 72 |
-
}
|
| 73 |
-
}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
socket.emit("move", {
|
| 79 |
-
from: selected,
|
| 80 |
-
to: [r, c],
|
| 81 |
-
captured: null
|
| 82 |
-
});
|
| 83 |
-
selected = null;
|
| 84 |
-
} else if (piece && piece[0] === myColor[0]) {
|
| 85 |
-
selected = [r, c];
|
| 86 |
-
}
|
| 87 |
-
}
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
}
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
</body>
|
| 100 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<title>Online Checkers</title>
|
| 6 |
+
<style>
|
| 7 |
+
body { font-family: sans-serif; text-align: center; }
|
| 8 |
+
table { border-collapse: collapse; margin: 20px auto; }
|
| 9 |
+
td {
|
| 10 |
+
width: 60px; height: 60px; text-align: center; vertical-align: middle;
|
| 11 |
+
border: 1px solid #444; font-size: 24px;
|
| 12 |
+
}
|
| 13 |
+
.black { background-color: #444; color: white; }
|
| 14 |
+
.red { background-color: red; color: white; }
|
| 15 |
+
.empty { background-color: #eee; }
|
| 16 |
+
.highlight { background-color: yellow !important; }
|
| 17 |
+
</style>
|
| 18 |
</head>
|
| 19 |
<body>
|
| 20 |
+
<h1>Online Checkers</h1>
|
| 21 |
+
<p id="status">Connecting...</p>
|
| 22 |
+
<table id="board"></table>
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
| 25 |
+
<script>
|
| 26 |
+
const socket = io();
|
|
|
|
| 27 |
|
| 28 |
+
let myColor = null;
|
| 29 |
+
let selected = null;
|
| 30 |
+
const boardElement = document.getElementById('board');
|
| 31 |
+
const statusElement = document.getElementById('status');
|
| 32 |
|
| 33 |
+
socket.on('connect', () => {
|
| 34 |
+
const name = prompt("Enter your name:");
|
| 35 |
+
socket.emit('set_name', { name: name || 'Player' });
|
| 36 |
+
});
|
| 37 |
|
| 38 |
+
socket.on('match_found', data => {
|
| 39 |
+
myColor = data.color;
|
| 40 |
+
statusElement.innerText = `Matched! You are ${myColor}. Opponent: ${data.opponent}`;
|
| 41 |
+
});
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
socket.on('opponent_move', data => {
|
| 44 |
+
fetch('/state')
|
| 45 |
+
.then(res => res.json())
|
| 46 |
+
.then(updateBoard);
|
| 47 |
+
});
|
| 48 |
|
| 49 |
+
socket.on('multi_jump', data => {
|
| 50 |
+
selected = data.from;
|
| 51 |
+
renderBoard(); // highlight piece
|
| 52 |
+
});
|
| 53 |
|
| 54 |
+
socket.on('game_over', data => {
|
| 55 |
+
statusElement.innerText = `Game over! Winner: ${data.winner}`;
|
| 56 |
+
});
|
| 57 |
|
| 58 |
+
socket.on('opponent_disconnected', () => {
|
| 59 |
+
statusElement.innerText = "Opponent disconnected. Game ended.";
|
| 60 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
function updateBoard(data) {
|
| 63 |
+
renderBoard(data.board);
|
| 64 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
function renderBoard(boardData = null) {
|
| 67 |
+
boardElement.innerHTML = '';
|
| 68 |
+
fetch('/state')
|
| 69 |
+
.then(res => res.json())
|
| 70 |
+
.then(data => {
|
| 71 |
+
const board = data.board;
|
| 72 |
+
for (let i = 0; i < 8; i++) {
|
| 73 |
+
const row = document.createElement('tr');
|
| 74 |
+
for (let j = 0; j < 8; j++) {
|
| 75 |
+
const cell = document.createElement('td');
|
| 76 |
+
const piece = board[i][j];
|
| 77 |
+
cell.className = (i + j) % 2 === 1 ? 'black' : 'empty';
|
| 78 |
+
if (piece === 'r') cell.innerText = 'r';
|
| 79 |
+
if (piece === 'R') cell.innerText = 'R';
|
| 80 |
+
if (piece === 'b') cell.innerText = 'b';
|
| 81 |
+
if (piece === 'B') cell.innerText = 'B';
|
| 82 |
+
|
| 83 |
+
if (selected && selected[0] === i && selected[1] === j)
|
| 84 |
+
cell.classList.add('highlight');
|
| 85 |
+
|
| 86 |
+
cell.onclick = () => handleClick(i, j, board);
|
| 87 |
+
row.appendChild(cell);
|
| 88 |
}
|
| 89 |
+
boardElement.appendChild(row);
|
| 90 |
+
}
|
| 91 |
+
});
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
function handleClick(i, j, board) {
|
| 95 |
+
const piece = board[i][j];
|
| 96 |
+
if (selected) {
|
| 97 |
+
socket.emit('move', { from: selected, to: [i, j] });
|
| 98 |
+
selected = null;
|
| 99 |
+
} else if (piece && myColor[0] === piece.toLowerCase()) {
|
| 100 |
+
selected = [i, j];
|
| 101 |
+
renderBoard(); // highlight selected
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
setInterval(renderBoard, 1500); // Keep board in sync
|
| 106 |
+
</script>
|
| 107 |
</body>
|
| 108 |
</html>
|