-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
231 lines (218 loc) · 7.05 KB
/
index.js
File metadata and controls
231 lines (218 loc) · 7.05 KB
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
/* eslint-disable indent */
import {
displayBoard,
displayMessage,
displayTextMessage,
} from "./event-handlers.js";
let GAME_STATE = {
boardSize: 4,
currentBoard: [
{
boardNumber: 1,
board: [
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
],
count: function count(letter) {
let numberOfLetter = 0;
for (const e of this.board.flat()) {
if (e === letter) {
numberOfLetter += 1;
}
}
return numberOfLetter;
},
},
{
boardNumber: 2,
board: [
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
],
count: function count(letter) {
let numberOfLetter = 0;
for (const e of this.board.flat()) {
if (e === letter) {
numberOfLetter += 1;
}
}
return numberOfLetter;
},
},
],
userShipPositions: [],
AIshipPositions: {},
numOfShips: 2,
numOfShipsConst: 2,
shootingPhase: false,
placementPhase: false,
turn: "user",
};
/**
* This function is called when you choose the game mode.
* The caller gives you the data about what kind of game
* the AI would like to play and where it places its ships.
* @param {String} gameDescription - An encoded string of the game data.
* You have to parse to use it.
*/
export function selectGame(gameDescription) {
// You may delete the following line as an example to see what the data looks like.
displayMessage(gameDescription, "black");
const parts = gameDescription.replace(/{|}/g, "").split(/,s:|,/);
GAME_STATE.boardSize = Number(parts.shift().charAt(5));
parts.forEach((part) => {
let [key, value] = part.split(":");
key = key.trim();
value = value.trim();
GAME_STATE.AIshipPositions[key] = value;
});
GAME_STATE.placementPhase = true;
console.log(`Size of the board: ${GAME_STATE.boardSize}`);
console.table(GAME_STATE.AIshipPositions);
}
/**
* Called whenever the player clicks on a cell.
* @param {Object} clickProperties - The clicked cell's properties.
* It contains x and y coordinates, clickType that can be 'left' or 'right',
* and source that indicates the number of the board where the click happened.
*/
export function handleClick(clickProperties) {
// You may delete the following line as an example to see what the data looks like.
//Use destructuring assignment to access object properties, improve code readability
const { x, y, source } = clickProperties;
const xCord = x.codePointAt(0) - "A".codePointAt(0);
const yCord = y - 1;
const board = source - 1;
const shoot = `${x.toLowerCase()}${y}`;
//source values: 1 (user grid), 2 (AI grid)
switch (source) {
case 1: //--> click source is form USER grid
// Placement phase
if (GAME_STATE.placementPhase && !GAME_STATE.shootingPhase) {
//Save coordinates of the placed ship
if (GAME_STATE.numOfShips > 0) {
GAME_STATE.userShipPositions.push(x + y);
GAME_STATE.numOfShips--;
displayTextMessage( `You've left ${GAME_STATE.numOfShips} ships to place.`, "red" );
//Put ship on the table
GAME_STATE.currentBoard[board].board[xCord][yCord] = "O";
displayBoard(GAME_STATE.currentBoard[board]);
}
if (GAME_STATE.numOfShips === 0) {
GAME_STATE.placementPhase = false;
GAME_STATE.shootingPhase = true;
displayTextMessage("Shooting phase started. It's your turn.", "green");
}
}
break;
case 2: //--> click source is form AI grid
// Shooting phase
if (GAME_STATE.shootingPhase && GAME_STATE.turn === "user") {
let hit = false;
for (const shipPosition of Object.values(GAME_STATE.AIshipPositions)) {
if (shipPosition === shoot) {
hit = true;
}
}
if (hit) {
GAME_STATE.currentBoard[board].board[xCord][yCord] = "X";
displayBoard(GAME_STATE.currentBoard[board]);
if (GAME_STATE.numOfShipsConst === GAME_STATE.currentBoard[board].count("X")) {
displayTextMessage("hit, You won. CONGRATULATION", "red");
GAME_STATE.shootingPhase = false;
displayMessage("Push Restart or choose a game mode.", "blue");
} else {
displayTextMessage("hit, Your turn again", "red");
}
} else {
GAME_STATE.currentBoard[board].board[xCord][yCord] = "m";
displayTextMessage("missed, AI's turn", "red");
GAME_STATE.turn = "ai";
displayBoard(GAME_STATE.currentBoard[1]);
}
}
break;
default:
break;
}
}
/**
* Called when the player clicks on the reset game button.
*/
export function resetGame() {
GAME_STATE = {
boardSize: 4,
currentBoard: [
{
boardNumber: 1,
board: [
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
],
},
{
boardNumber: 2,
board: [
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
],
},
],
userShipPositions: [],
AIshipPositions: {},
numOfShips: 2,
numOfShipsConst: 2,
shootingPhase: false,
placementPhase: false,
turn: "user",
};
displayBoard(GAME_STATE.currentBoard[0]);
displayBoard(GAME_STATE.currentBoard[1]);
displayTextMessage("Select new game mode", "blue");
displayMessage("");
}
/**
* This function is called when the player clicks on the AI shoot button.
* The caller gives you randomly generated coordinates.
* You may ignore the parameter later when you implement more intelligent AI.
* @param {Object} coordinates - Random generated coordinates (x and y),
* where the AI would like to shoot.
*/
export function aiShoot(coordinates) {
displayMessage(coordinates.x + coordinates.y, "green");
const x = coordinates.x.codePointAt(0) - "A".codePointAt(0);
const y = coordinates.y - 1;
const board = 0;
const shoot = GAME_STATE.currentBoard[board].board[x][y];
if (GAME_STATE.shootingPhase && GAME_STATE.turn === "ai") {
if (shoot === "O") {
GAME_STATE.currentBoard[board].board[x][y] = "X";
if (GAME_STATE.currentBoard[0].count("X") === GAME_STATE.numOfShipsConst) {
displayTextMessage("You lost, try again!", "red");
GAME_STATE.shootingPhase = false;
displayMessage("Push Restart or choose a game mode.", "blue");
} else {
displayTextMessage("hit AI's turn again", "red");
}
} else if (shoot === "") {
GAME_STATE.currentBoard[board].board[x][y] = "m";
displayTextMessage("missed, Your turn", "red");
GAME_STATE.turn = "user";
} else {
displayTextMessage("AI is an idiot, shot at the same spot. It's your turn", "red");
GAME_STATE.turn = "user";
}
}
displayBoard(GAME_STATE.currentBoard[0]);
}
displayBoard(GAME_STATE.currentBoard[0]);
displayBoard(GAME_STATE.currentBoard[1]);
displayMessage("Select game mode", "green");