-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
277 lines (247 loc) · 7.57 KB
/
server.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const express = require("express");
const sqlite3 = require("sqlite3").verbose();
const app = express();
const port = 3000;
// Middleware
app.use(express.static("public"));
app.use(express.json());
// Establish SQLite database connection
const db = new sqlite3.Database("game.db", (err) => {
if (err) {
console.error("Error opening database:", err);
} else {
console.log("Connected to the SQLite database.");
// Create table if it doesn't exist
db.run(
`
CREATE TABLE IF NOT EXISTS highscores (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player_name TEXT NOT NULL,
moves INTEGER NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`,
(err) => {
if (err) {
console.error("Error creating table:", err);
} else {
console.log("Highscores table ready.");
}
}
);
}
});
// Class for storing a game status
class GameBoard {
constructor() {
this.rows = 6;
this.cols = 7;
// Array with 6 rows and 7 columns, set all to empty
this.board = Array(this.rows)
.fill()
.map(() => Array(this.cols).fill(0));
}
// Helper function to check if a stone can be added to a column
isValidMove(col) {
// True if the top element in the column is empty
return this.board[0][col] === 0;
}
// Player add stone to column col
makeMove(col, player) {
// Go through all rows from bottom to top
for (let row = this.rows - 1; row >= 0; row--) {
// If no stone set in this row and column
if (this.board[row][col] === 0) {
// Set stone and return coordinates of set stone
this.board[row][col] = player;
return { row, col };
}
}
// No free space could be found -> return null
return null;
}
// Check if player has won
checkWin(row, col, player) {
// Check for 4 stones in a row
for (let c = 0; c <= this.cols - 4; c++) {
// If 4 stones in row are set, return true
if (
this.board[row][c] === player &&
this.board[row][c + 1] === player &&
this.board[row][c + 2] === player &&
this.board[row][c + 3] === player
) {
return true;
}
}
// Check for 4 stones in a column
for (let r = 0; r <= this.rows - 4; r++) {
if (
this.board[r][col] === player &&
this.board[r + 1][col] === player &&
this.board[r + 2][col] === player &&
this.board[r + 3][col] === player
) {
return true;
}
}
// Check for 4 stones diagnonally (/)
for (let r = 3; r < this.rows; r++) {
for (let c = 0; c <= this.cols - 4; c++) {
if (
this.board[r][c] === player &&
this.board[r - 1][c + 1] === player &&
this.board[r - 2][c + 2] === player &&
this.board[r - 3][c + 3] === player
) {
return true;
}
}
}
// Check for 4 stones diagnonally (\)
for (let r = 0; r <= this.rows - 4; r++) {
for (let c = 0; c <= this.cols - 4; c++) {
if (
this.board[r][c] === player &&
this.board[r + 1][c + 1] === player &&
this.board[r + 2][c + 2] === player &&
this.board[r + 3][c + 3] === player
) {
return true;
}
}
}
// Player has not won
return false;
}
// Get an array with the columns where a stone can be put
getValidMoves() {
const validMoves = [];
for (let col = 0; col < this.cols; col++) {
if (this.isValidMove(col)) {
// If there is space, add column to array
validMoves.push(col);
}
}
// Return array of valid moves
return validMoves;
}
// Calculate move of computer
computerMove() {
// Get array with columns where a stone can be placed
const validMoves = this.getValidMoves();
// If no move possible return null --> game over
if (validMoves.length === 0) return null;
// Return a random entry of the column arrays --> set stone on random column
const randomIndex = Math.floor(Math.random() * validMoves.length);
return validMoves[randomIndex];
}
}
// Global map with gameID --> Gameboard entries
const games = new Map();
// Endpoint to start a game, passing in the player name
app.post("/api/game/start", (req, res) => {
// Get player name from request
const { playerName } = req.body;
// Create a new game id
const gameId = Date.now().toString();
// Add new game data to games map
games.set(gameId, {
// The game board status
board: new GameBoard(),
// The player name
playerName,
// The move count
moves: 0,
});
// Return the gameId to the client
res.json({ gameId });
});
// Player makes a move
app.post("/api/game/:gameId/move", (req, res) => {
// Get the game id
const { gameId } = req.params;
// Get the column of the added stone
const { column } = req.body;
// Get the game data
const game = games.get(gameId);
// If the game cannot be found, return an error
if (!game) {
return res.status(404).json({ error: "Spiel nicht gefunden" });
}
// Check if the player move to column is valid
if (!game.board.isValidMove(column)) {
// If invalid move, return http error
return res.status(400).json({ error: "Ungültiger Zug" });
}
// Make the move of the player so that the board gets updated
const playerMove = game.board.makeMove(column, 1);
// Increase the move counter
game.moves++;
// Check if the player has 4 in a row
if (game.board.checkWin(playerMove.row, playerMove.col, 1)) {
// If won, store the core to the high scores table
db.run("INSERT INTO highscores (player_name, moves) VALUES (?, ?)", [game.playerName, game.moves]);
// Delete the game state from the map
games.delete(gameId);
// Return status "win" with board status and move count to the GUI
return res.json({
status: "win",
board: game.board.board,
moves: game.moves,
});
}
// Calculate the computer move
const computerColumn = game.board.computerMove();
// If no computer move is possible, return a draw status
if (computerColumn === null) {
games.delete(gameId);
return res.json({ status: "draw", board: game.board.board });
}
// Process the computer move to the board
const computerMove = game.board.makeMove(computerColumn, 2);
// Check if the computer has won
if (game.board.checkWin(computerMove.row, computerMove.col, 2)) {
// If computer has won, delete game status
games.delete(gameId);
// Then return status "lose" and board data
return res.json({
status: "lose",
board: game.board.board,
});
}
// No one has one --> return status "ongoing", board and computer move column
res.json({
status: "ongoing",
board: game.board.board,
computerMove: computerColumn,
});
});
// REST endpoint to get highscores
app.get("/api/highscores", (req, res) => {
// Select all entries from highscores table, oder by move count
db.all("SELECT player_name, moves FROM highscores ORDER BY moves ASC LIMIT 10", (err, rows) => {
if (err) {
// Error handling
res.status(500).json({ error: "Datenbankfehler" });
} else {
// Return data
res.json(rows);
}
});
});
// Shutdown database if process has been killed
process.on("SIGINT", () => {
db.close((err) => {
if (err) {
console.error("Error closing database:", err);
} else {
console.log("Database connection closed.");
}
process.exit(0);
});
});
// Start the express REST server
app.listen(port, () => {
console.log(`Server läuft auf Port ${port}`);
});