-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameModel.js
152 lines (137 loc) · 3.57 KB
/
GameModel.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
import { SingleGame, AddPlayerStatus } from "./SingleGame.js";
/**
* @typedef {Object} AddPlayerResponse
* @property {AddPlayerStatus} status
* @property {string} roomId
*/
export default class GameModel {
constructor() {
this.playerRoomMap = new Map();
this.rooms = [];
}
/**
* @returns {AddPlayerResponse}
*/
addPlayer(id) {
// console.log(id, this.playerRoomMap, this.rooms);
if (this.rooms.length !== 0) {
const [lastRoom] = this.rooms.slice(-1);
const res = lastRoom.addPlayer(id);
if (res === AddPlayerStatus.PlayerO || res === AddPlayerStatus.PlayerX) {
this.playerRoomMap.set(id, this.rooms.length - 1);
return {
status: res,
roomId: "room" + String(this.rooms.length - 1),
};
}
}
const room = new SingleGame();
const res = room.addPlayer(id);
this.rooms.push(room);
this.playerRoomMap.set(id, this.rooms.length - 1);
return {
status: res,
roomId: "room" + String(this.rooms.length - 1),
};
}
playTurn(id, player, row, col) {
const roomid = this.playerRoomMap.get(id);
const room = this.rooms[roomid];
return room.playTurn(player, row, col);
}
removePlayer(id) {
const roomid = this.playerRoomMap.get(id);
const room = this.rooms[roomid];
room.removePlayer(id);
}
}
// // Supporting only one game
// addPlayerSingleGame(id) {
// if ([this.playerO, this.playerX].includes(id)) {
// return 1; //"You are already in the game"
// }
// if (this.playerO != null && this.playerX != null) {
// return 0; //"Game full, you cannot be added";
// }
// if (this.playerX == null) {
// this.playerX = id;
// return "X";
// }
// this.playerO = id;
// return "O"; //"You are added in the game now";
// }
// removePlayerSingleGame(id) {
// if (this.playerO == id) {
// this.playerO = null;
// } else if (this.playerX == id) {
// this.playerX = null;
// }
// }
// playTurn(player, row, col) {
// this.game[row][col] = player;
// if (this.evaluate() === true) {
// return 0; // Game Over
// }
// return 1;
// }
// evaluate() {
// for (let i = 0; i < 3; i++) {
// if (this.evalRow(i) === true) {
// return true;
// }
// }
// for (let i = 0; i < 3; i++) {
// if (this.evalCol(i) === true) {
// return true;
// }
// }
// return this.evalDiagonal1() || this.evalDiagonal2();
// }
// evalRow(rowNum) {
// for (let j = 0; j < 3 - 1; j++) {
// if (
// this.game[rowNum][j] == null ||
// this.game[rowNum][j] !== this.game[rowNum][j + 1]
// ) {
// return false;
// }
// }
// return true;
// }
// evalCol(colNum) {
// for (let i = 0; i < 3 - 1; i++) {
// if (
// this.game[i][colNum] == null ||
// this.game[i][colNum] !== this.game[i + 1][colNum]
// ) {
// return false;
// }
// }
// return true;
// }
// evalDiagonal1() {
// for (let i = 0; i < 3 - 1; i++) {
// if (
// this.game[i][i] == null ||
// this.game[i][i] !== this.game[i + 1][i + 1]
// ) {
// return false;
// }
// }
// return true;
// }
// evalDiagonal2() {
// let j = 3;
// for (let i = 0; i < 3 - 1; i++) {
// j -= 1;
// if (
// this.game[i][j] == null ||
// this.game[i][j] !== this.game[i + 1][j - 1]
// ) {
// return false;
// }
// }
// return true;
// }
// }
// module.exports = GameModel;