-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
162 lines (147 loc) · 4.29 KB
/
scripts.js
File metadata and controls
162 lines (147 loc) · 4.29 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
//SETUP STATE VARIABLES
// GET GAME GRID
const GAME_GRID = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
];
let isGridFull = false;
let hasPlayerWon = false;
let hasGameEnded = false;
//DISPLAY GAME GRID
const displayGameGrid = () => {
for (let i of GAME_GRID) {
let output = `|`;
for (let ii of i) {
output += ` ${ii} |`;
}
console.log(output);
}
return;
};
const updateGameGrid = (row, column, value) => {
GAME_GRID[row][column] = value;
displayGameGrid();
};
const getRandomMove = () => {
const _random_row = Math.floor(Math.random() * 2);
const _random_column = Math.round(Math.random() * 2);
console.log("random", _random_row, _random_column);
return [_random_row, _random_column];
};
const isPositionEmpty = (_row, _column) => {
return GAME_GRID[_row][_column] === 0 ? true : false;
};
const gameStateCheck = () => {
let isDraw;
let isHorizontalWin;
let isVerticalWin;
let isDiagonalWin;
let winner;
console.log("MAKING GAME CHECK+++++++");
displayGameGrid();
//drawCheck
for (let i of GAME_GRID) {
console.log("DOING draw CHECK");
for (let ii of i) {
if (ii === 0) {
isDraw = false;
}
}
}
//horizontal win
for (let i = 0; i < 3; i++) {
console.log("DOING HORIZONTAL CHECK");
if (
GAME_GRID[i][0] != 0 &&
(GAME_GRID[i][0] === GAME_GRID[i][1]) === GAME_GRID[i][2]
) {
isHorizontalWin = true;
winner = GAME_GRID[i][0];
}
}
//vertical win
for (let i = 0; i < 3; i++) {
console.log("DOING verticAL CHECK");
if (
GAME_GRID[0][i] != 0 &&
(GAME_GRID[0][i] === GAME_GRID[1][i]) === GAME_GRID[2][i]
) {
isVerticalWin = true;
winner = GAME_GRID[0][i];
}
}
console.log("DOINGdiagONTAL CHECK");
if (
(GAME_GRID[1][1] != 0 &&
(GAME_GRID[0][0] === GAME_GRID[1][1]) === GAME_GRID[2][2]) ||
(GAME_GRID[1][1] != 0 &&
(GAME_GRID[0][2] === GAME_GRID[1][1]) === GAME_GRID[2][0])
) {
isDiagonalWin = true;
winner = GAME_GRID[0][2];
}
//DIAGONAL WIN
console.log("DONE WITH GAME CHEK");
console.log(isDraw, isHorizontalWin, isVerticalWin, isDiagonalWin, winner);
return { isDraw, isHorizontalWin, isVerticalWin, isDiagonalWin, winner };
};
const endGame = (winner) => {
if (winner === 2) {
console.log("Player has won");
} else {
console.log("Computer has won");
}
};
//START GAME
//USER MOVE = 2
const startGame = () => {
console.log("===========GAME STARTED+++++++++++++");
displayGameGrid();
//COLLECT USER INPUT
const _input_row = prompt("What row are you making your move ?");
const _input_column = prompt("What column are you making your move ?");
//DISPLAY USER MOVE ON GRID
updateGameGrid(_input_row, _input_column, 2);
//GET A RANDOM MOVE FOR COMPUTER
//CHECK IF POSITION ISNT FILLED YET
//DISPLAY THE RANDOM MOVE. ON GRID
let [_randomRow, _randomColumn] = getRandomMove();
// console.log("the random row is ", _randomRow);
// console.log("the random Column is ", _randomColumn);
while (!isPositionEmpty(_randomRow, _randomColumn)) {
[_randomRow, _randomColumn] = getRandomMove();
}
console.log("+++++++ COMPUTER HAS MADE HIS MOVE");
updateGameGrid(_randomRow, _randomColumn, 1);
//CHECK IF ITS WIN
const { isDraw, isHorizontalWin, isVerticalWin, isDiagonalWin, winner } =
gameStateCheck();
//HORIXONTAL CHECK
if (isHorizontalWin) {
console.log("Horizontal win", isHorizontalWin);
hasGameEnded = true;
endGame(winner);
}
//VERTICAL CHECK
if (isVerticalWin) {
console.log("Vertical win", isVerticalWin);
hasGameEnded = true;
endGame(winner);
}
//DIAGONAL CHECK
if (isDiagonalWin) {
console.log("Diagonal win", isDiagonalWin);
hasGameEnded = true;
endGame(winner);
}
//DRAW CHECK
if (isDraw) {
isGridFull = true;
hasGameEnded = true;
return;
}
//IF WIN ANNOUNCE GAME OVER ,
//ELSE ,, CONTINUE UNTIL ALL GRID IS FILLED UP
//IF GAME END RESET ALL DATA
};