-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
174 lines (114 loc) · 3.67 KB
/
script2.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
function Gameboard () {
let rows = 3;
let cols = 3;
let board = [];
for (let i = 0; i < rows; i++) {
board[i] = [];
for (let j = 0; j < cols; j++) {
board[i].push(Cell());
}
}
const getBoard = () => board;
const markSpot = (player, row, col) => {
if (board[row][col].getValue() === 0) {
board[row][col].changeValue(player);
return;
}
else {
console.log(row, col);
alert("A player has already placed a marker there.\n");
}
}
const printBoard = () => {
const boardWithCellValues = board.map((row) => row.map((cell) => cell.getValue()))
console.log(boardWithCellValues);
};
return {getBoard, markSpot, printBoard};
}
function Cell () {
let value = 0;
const changeValue = (player) => value = player.marker;
const getValue = ()=> value;
return {changeValue, getValue};
}
function GameController () {
const playerOneName = 'Player One';
const playerTwoName = 'Player Two';
const playerOneMarker = '1';
const playerTwoMarker = '2';
players = [
{
name: playerOneName,
marker: playerOneMarker
},
{
name: playerTwoName,
marker: playerTwoMarker
}
];
let board = Gameboard();
let activePlayer = players[0]
let winner =0;
const position = (() => {
let col = 0;
let row = 0;
const askPosition = () => {
row = prompt("Enter the row");
col = prompt("Enter the col\n");
row = +row;
col = +col;
}
const getRow = ()=> row;
const getCol = () => col;
return {askPosition, getRow, getCol};
})();
const getActivePlayer = () => activePlayer;
const getWinner = () => winner;
const switchPlayerTurn = () => activePlayer === players[0] ? activePlayer = players[1] : activePlayer = players[0];
const printNewRound = () => {
board.printBoard();
console.log(`${getActivePlayer().name}'s turn.\n`);
}
const checkWinner = (player, row, col) => {
// Check the row
if (board.getBoard()[row].every(cell => cell.getValue() === player.marker)) {
return player;
}
// Check the column
let column = board.getBoard().map(row => row[col]);
if (column.every(cell => cell.getValue() === player.marker)) {
return player;
}
// Check diagonals
if ((board.getBoard()[0][0].getValue() === player.marker &&
board.getBoard()[1][1].getValue() === player.marker &&
board.getBoard()[2][2].getValue() === player.marker) ||
(board.getBoard()[0][2].getValue() === player.marker &&
board.getBoard()[1][1].getValue() === player.marker &&
board.getBoard()[2][0].getValue() === player.marker)) {
return player;
}
return false;
}
function playRound () {
printNewRound();
position.askPosition();
board.markSpot(getActivePlayer(), position.getRow(), position.getCol());
winner = checkWinner(getActivePlayer(), position.getRow(), position.getCol());
if (winner) {
return;
}
switchPlayerTurn();
}
return {playRound, getWinner};
}
function Game () {
const game = GameController();
let counter = 0;
while (!game.getWinner()) {
game.playRound();
counter++;
}
console.log(`Winner: ${game.getWinner().name}\n`);
}
Game();