-
Notifications
You must be signed in to change notification settings - Fork 1
/
tictactoe.js
67 lines (60 loc) · 1.94 KB
/
tictactoe.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
console.log("Hello Youtube!");
let player = 'X';
let board = [];
let gameOver = false;
let moveIndex = 1;
function initializeBoard(boardSize) {
for(let row=0; row < boardSize; row++){
let boardRow = [];
for(let column=0; column < boardSize; column++){
boardRow.push('-');
}
board.push(boardRow);
}
console.table(board);
}
function playTicTacToe(boardSize) {
initializeBoard(boardSize);
do {
let position = prompt(`Enter row and column for player ${player}`);
if(position == null) {
break;
}
let positionX = position.split(',')[0];
let positionY = position.split(',')[1];
if(positionX > boardSize-1 || positionY > boardSize-1) {
alert("Wrong move");
continue;
}
board[positionX][positionY] = player;
console.table(board);
checkWinner(boardSize, positionX, positionY);
player = (player == 'X') ? 'O' : 'X';
moveIndex++;
} while(!gameOver)
}
function checkWinner(boardSize, positionX, positionY) {
if(moveIndex == (boardSize*boardSize)) {
gameOver = true;
alert("It is a draw");
}
let rowValues = new Set();
let columnValues = new Set();
let diagonal1Values = new Set();
let diagonal2Values = new Set();
for(let index = 0; index < boardSize; index++){
rowValues.add(board[positionX][index]);
columnValues.add(board[index][positionY]);
diagonal1Values.add(board[index][index]);
diagonal2Values.add(board[index][boardSize-index-1]);
}
if((rowValues.size == 1 && !rowValues.has('-'))
|| (columnValues.size == 1 && !columnValues.has('-'))
|| (diagonal1Values.size == 1 && !diagonal1Values.has('-'))
|| (diagonal2Values.size == 1 && !diagonal2Values.has('-'))) {
gameOver = true;
alert(`Winner is ${player}`);
}
}
let size = prompt("Enter board size");
playTicTacToe(size);