Geek7 commited on
Commit
df03365
·
verified ·
1 Parent(s): e230ca8

Update templates/client.html

Browse files
Files changed (1) hide show
  1. templates/client.html +91 -83
templates/client.html CHANGED
@@ -1,100 +1,108 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
4
- <title>Checkers</title>
5
- <style>
6
- body { font-family: Arial; text-align: center; }
7
- #board { display: grid; grid-template: repeat(8, 60px) / repeat(8, 60px); margin: auto; }
8
- .cell { width: 60px; height: 60px; display: flex; align-items: center; justify-content: center; }
9
- .black { background: #769656; }
10
- .white { background: #EEEED2; }
11
- .piece { width: 40px; height: 40px; border-radius: 50%; }
12
- .r { background: red; }
13
- .b { background: black; }
14
- .R, .B { border: 2px solid gold; }
15
- </style>
 
 
16
  </head>
17
  <body>
18
- <h1>Checkers</h1>
19
- <input id="nameInput" placeholder="Enter name" />
20
- <button onclick="setName()">Join</button>
21
- <h2 id="status"></h2>
22
- <div id="board"></div>
23
 
24
- <script src="https://cdn.socket.io/4.6.1/socket.io.min.js"></script>
25
- <script>
26
- const socket = io();
27
- let myColor = null, board = [], selected = null;
28
 
29
- socket.on("connect", () => console.log("Connected"));
 
 
 
30
 
31
- function setName() {
32
- const name = document.getElementById("nameInput").value;
33
- socket.emit("set_name", { name });
34
- }
35
 
36
- socket.on("match_found", ({ color, opponent }) => {
37
- myColor = color;
38
- document.getElementById("status").innerText = `You are ${color}, playing vs ${opponent}`;
39
- fetch("/state")
40
- .then(res => res.json())
41
- .then(data => { board = data.board; render(); });
42
- });
43
 
44
- socket.on("opponent_move", move => {
45
- applyMove(move);
46
- render();
47
- });
 
48
 
49
- socket.on("multi_jump", ({ from }) => {
50
- selected = from;
51
- });
 
52
 
53
- socket.on("game_over", ({ winner }) => alert(`${winner} wins!`));
54
- socket.on("opponent_disconnected", () => alert("Opponent disconnected."));
 
55
 
56
- function render() {
57
- const boardDiv = document.getElementById("board");
58
- boardDiv.innerHTML = "";
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
- function handleClick(r, c) {
76
- const piece = board[r][c];
77
- if (selected) {
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
- function applyMove({ from, to, captured }) {
90
- const [fr, fc] = from, [tr, tc] = to;
91
- board[tr][tc] = board[fr][fc];
92
- board[fr][fc] = null;
93
- if (captured) {
94
- const [cr, cc] = captured;
95
- board[cr][cc] = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  }
97
- }
98
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>