-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (130 loc) · 4.39 KB
/
script.js
File metadata and controls
150 lines (130 loc) · 4.39 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
let boardSize;
let gameBoard;
let gameMode;
let gameStatus = '';
let currPlayer = 'X';
function startGame() {
currPlayer = 'X';
boardSize = parseInt(document.getElementById('board-size').value);
gameBoard = Array.from({ length: boardSize }, () => Array(boardSize).fill(''));
gameMode = document.querySelector('input[name=gameMode]:checked').value;
// If the game status is different of empty string, that means that
// the button was pressed to restart the game
// thus, it's needed to remove the last message of the game.
if(gameStatus != '') {
let gameContainer = document.getElementById('game-container');
const message = document.getElementById('message-game');
if(gameContainer.contains(message)) {
gameContainer.removeChild(message);
}
}
gameStatus = '';
renderBoard();
}
function renderBoard() {
const board = document.getElementById('board');
board.innerHTML = '';
for (let i = 0; i < boardSize; i++) {
const row = document.createElement('div');
row.className = 'row';
for (let j = 0; j < boardSize; j++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.innerText = gameBoard[i][j];
cell.addEventListener('click', () => cellClick(i, j));
row.appendChild(cell);
}
board.appendChild(row);
}
// Similarly, adding the end game message.
if(gameStatus != '') {
gameContainer = document.getElementById('game-container');
const message = document.createElement('p');
message.id = 'message-game';
message.innerText = gameStatus;
gameContainer.appendChild(message);
}
}
function cellClick(row, col) {
if(gameStatus === '' && gameBoard[row][col] === '') {
gameBoard[row][col] = currPlayer;
currPlayer = currPlayer === 'X' ? 'O' : 'X';
verifyEndGame(row,col);
if (gameMode === "single" && gameStatus === '') {
computerLogic();
}
renderBoard();
}
}
// Function to select a random valid space for the computer set a
// its move.
function computerLogic(){
while(true) {
let row = Math.floor(Math.random() * boardSize);
let col = Math.floor(Math.random() * boardSize);
if(gameBoard[row][col] === '') {
gameBoard[row][col] = currPlayer;
currPlayer = currPlayer === 'X' ? 'O' : 'X';
verifyEndGame(row,col);
break;
}
}
}
function verifyEndGame(row, col){
if (checkVictory(row, col)) {
if (gameMode === "multi") {
if (currPlayer === 'O') {
gameStatus = "Jogador 1 venceu!!"
} else {
gameStatus = "Jogador 2 venceu!!"
}
} else {
if (currPlayer === 'O') {
gameStatus = "Jogador venceu!!"
} else {
gameStatus = "Computador venceu!!"
}
}
} else if (checkTie())
gameStatus = "Deu velha!!";
}
function checkVictory(row, col){
let victory = false;
// Verify main diagonal
if(row === col) victory = verifyMainDiagonal();
// Verify secondary diagonal
if(row === boardSize - 1 - col && victory === false) victory = verifySecondaryDiagonal();
// Verify for both rows and columns
if(victory === false) victory = verifyRowAndCol(row, col);
return victory;
}
function checkTie(){
for(let row = 0; row < boardSize; ++row){
for(let col = 0; col < boardSize; ++col){
if(gameBoard[row][col] == '') return false;
}
}
return true;
}
function verifyMainDiagonal(){
let count = 0;
for(let i = 1; i < boardSize; ++i) {
if(gameBoard[i-1][i-1] === gameBoard[i][i]) ++count;
}
return count === boardSize-1 ? true : false;
}
function verifySecondaryDiagonal(){
let count = 0;
for(let i = 0; i < boardSize; ++i) {
if(gameBoard[i][boardSize - 1 - i] == gameBoard[0][boardSize - 1]) ++count;
}
return count == boardSize ? true : false;
}
function verifyRowAndCol(row, col){
let countRow = countCol = 0;
for(let i = 1; i < boardSize; ++i) {
if(gameBoard[row][0] == gameBoard[row][i]) ++countRow;
if(gameBoard[0][col] == gameBoard[i][col]) ++countCol;
}
return (countRow == boardSize - 1) || (countCol == boardSize - 1) ? true : false;
}