-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
85 lines (76 loc) · 2.17 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import WebSocket from "ws";
import readline from "readline";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const ws = new WebSocket("ws://localhost:8080");
let playerId: string;
let playerSymbol: string;
let isMyTurn = false;
ws.on("open", () => {
console.log("Connected to server");
playerId = "player1";
// Join the game
ws.send(JSON.stringify({ type: "join", playerId }));
});
ws.on("message", (message) => {
const data = JSON.parse(message.toString());
if (data.type === "joined") {
playerSymbol = data.symbol;
console.log(`Player joined as ${data.symbol}`);
} else if (data.type === "start") {
console.log("Both players are connected. Game start!");
if (playerSymbol === "X") {
isMyTurn = true;
promptMove();
}
} else if (data.type === "update") {
printBoard(data.board);
if (data.winner) {
console.log(`Winner: ${data.winner}`);
isMyTurn = false; // End current game
} else {
isMyTurn = data.currentPlayer === playerSymbol;
if (isMyTurn) {
promptMove();
}
}
} else if (data.type === "reset") {
console.log("Game reset. Symbols switched.");
playerSymbol = data.newSymbol;
isMyTurn = playerSymbol === "X";
if (isMyTurn) {
promptMove();
}
} else if (data.type === "error") {
console.log(`Error: ${data.message}`);
if (isMyTurn) {
promptMove();
}
}
});
ws.on("close", () => {
console.log("Disconnected from server");
rl.close();
});
function promptMove() {
rl.question("Enter your move (0-8): ", (input) => {
const position = parseInt(input);
if (!isNaN(position) && position >= 0 && position <= 8) {
ws.send(JSON.stringify({ type: "move", playerId, position }));
} else {
console.log("Invalid move, please enter a number between 0 and 8.");
promptMove();
}
});
}
function printBoard(board: (string | null)[]) {
console.log(`
${board[0] || " "} | ${board[1] || " "} | ${board[2] || " "}
---------
${board[3] || " "} | ${board[4] || " "} | ${board[5] || " "}
---------
${board[6] || " "} | ${board[7] || " "} | ${board[8] || " "}
`);
}