-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
170 lines (144 loc) · 4.04 KB
/
script.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
const GameBoard = (() => {
const size = 3;
let gameboard = [["", "", ""], ["", "", ""], ["", "", ""]];
const getGameBoard = () => gameboard;
const displayGameBoard = () => {
let rows = gameboard.length;
let cols = gameboard[0].length;
let gameboardDiv = document.querySelector("#gameboard");
for(var row = 0; row < rows; row++){
for(var col = 0; col < cols; col++){
var cell = document.createElement("div");
if(gameboard[row][col] === ""){
cell.innerHTML = gameboard[row][col];
} else {
cell.appendChild(gameboard[row][col])
}
cell.id = [row, col];
cell.setAttribute("class", "cell");
gameboardDiv.appendChild(cell);
cell.style.top = row * 110 + "px";
cell.style.left = col * 110 + "px";
}
}
};
const play = (player, row, col) => {
symbol = player.getSymbol();
gameboard[row][col] = symbol;
displayGameBoard();
if(GameBoard.gameOver(symbol, row, col) == 0){
document.getElementById("winner").innerHTML = ("The game is over! " + player.getName() + " is the winner!");
} else if (GameBoard.gameOver(symbol, row, col) == 1){
document.getElementById("winner").innerHTML = ("It is a draw!")
} else {
Game.listen();
Game.increaseMoveCount();
}
};
const gameOver = (symbol, row, col) => {
for(var i = 0; i<size; i++){
if(gameboard[row][i].alt != symbol.alt){
break;
}
if(i == size-1){
return 0;
}
}
for(var i = 0; i<size; i++){
if(gameboard[i][col].alt != symbol.alt){
break;
}
if(i == size-1){
return 0;
}
}
if(row == col){
for(var i = 0; i<size; i++){
if(gameboard[i][i].alt != symbol.alt){
break;
}
if(i == size-1){
return 0;
}
}
}
if(row + col == size-1){
for(var i = 0; i<size; i++){
if(gameboard[i][size-1-i].alt != symbol.alt){
break;
}
if(i == size-1){
return 0;
}
}
}
if(Game.getMoveCount() >= (size*size-1)){
return 1
}
}
const clearBoard = () => {
gameboard = [["", "", ""], ["", "", ""], ["", "", ""]];
}
return {getGameBoard, displayGameBoard, play, gameOver, clearBoard};
})();
const Player = (symbol, name) => {
const getSymbol = () => symbol;
const getName = () => name;
return {getSymbol, getName};
};
const Game = (() => {
let turn = false;
let moveCount = 0;
GameBoard.displayGameBoard();
const getMoveCount = () => {
return moveCount;
};
const increaseMoveCount = () => {
moveCount++;
};
const listen = () => {
player1Name = document.getElementById("player1").value;
player2Name = document.getElementById("player2").value;
symbolX = document.createElement("img");
symbolX.src="images/tic-tac-toe-x.png"
symbolX.width = 100;
symbolX.alt = "x"
symbolO = document.createElement("img");
symbolO.src="images/tic-tac-toe-o.png"
symbolO.width = 90;
symbolO.alt = "o"
const player1 = Player(symbolX, player1Name);
const player2 = Player(symbolO, player2Name);
const cells = document.getElementsByClassName("cell");
if(turn){
player = player2;
turn = false;
} else {
player = player1;
turn = true;
}
Array.from(cells).forEach((cell) => {
cell.addEventListener("click", function(){
if(GameBoard.getGameBoard()[cell.id[0]][cell.id[2]] === ""){
GameBoard.play(player, parseInt(cell.id[0]), parseInt(cell.id[2]))
}
})
});
};
const reset = () => {
window.location.reload();
}
return {listen, moveCount, getMoveCount, increaseMoveCount, reset};
})();
document.getElementById("start").addEventListener("click", function(){
player1Name = document.getElementById("player1").value;
player2Name = document.getElementById("player2").value;
if(player1Name === '' || player2Name === ''){
alert("Please input your names!");
} else {
Game.listen();
}
})
document.getElementById("reset").addEventListener("click", function(){
Game.reset();
})