-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
135 lines (115 loc) · 3.3 KB
/
Copy pathscript.js
File metadata and controls
135 lines (115 loc) · 3.3 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
// Game board/displayController is a MODULE
const gameBoard = (() => {
// private methods
let board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]];
function clearBoard() {
gameBoard.board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]];
}
function checkWin(board) {
// Check for wins horizontally
for (let i = 0; i<3; i++) {
if (board[i][0] == board[i][1] && board[i][1]== board[i][2]) {
return true;
}
}
// Check for wins vertically
for (let i = 0; i<3; i++) {
if (board[0][i] == board[1][i] && board[1][i]== board[2][i]) {
return true;
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] || board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
return true
}
}
function update(i, j, turn) {
board[i][j] = turn;
}
return {
// public methods
board,
update,
checkWin,
clearBoard
};
})();
const displayController = (() => {
// private methods
function _turn(board) {
let x = 0;
let o = 0;
for (let i = 0; i<3; i++) {
for (let j = 0; j<3; j++) {
if (board[i][j] === "X") {
x++;
}
else if (board[i][j] === "O") {
o++;
}
}
}
if (x > o) {
return "O";
}
else {return "X"};
}
function renderBoard(board) {
const gBoard = document.getElementsByClassName("gBoard")[0];
gBoard.innerHTML = '';
for (let i = 0; i < 3; i++) {
const row = document.createElement('div');
row.classList.add("row");
for (let j = 0; j < 3; j++) {
const button = document.createElement('button');
button.classList.add("btn");
button.textContent = `${board[i][j]}`
button.addEventListener('click', (e) => {
if (button.textContent == " ") {
const turn = _turn(board);
button.textContent = turn;
gameBoard.update(i, j, turn);
console.table(gameBoard.board);
}
})
row.appendChild(button);
}
gBoard.appendChild(row);
};
}
return {
// return an object of the public methods
renderBoard
};
})();
// Players are FACTORIES
const playerFactory = (name, turn) => {
// Stuff they are/can do
function win(name) {
const winBox = document.getElementsByClassName('winBox')[0];
winBox.textContent = `${name} Wins!`
}
// return all relavent propeties and methods
return {
name,
win
};
}
// Main Script
const addPlayer1 = document.getElementById('addPlayer1');
addPlayer1.addEventListener('click', (e) => {
const plrName1 = document.getElementById("names1").value;
window.plr1 = playerFactory(plrName1);
document.getElementById('plr1').innerHTML = plrName1;
})
const addPlayer2 = document.getElementById('addPlayer2');
addPlayer2.addEventListener('click', (e) => {
const plrName2 = document.getElementById("names2").value;
window.plr2 = playerFactory(plrName2);
document.getElementById('plr2').innerHTML = plrName2;
})
const start = document.getElementById('go');
start.addEventListener('click', (e) => {
//Trying to clear the board for a new game, screws everything up
// I think it has to do with the function scope, and how board = [...] doesn't apply everywhere
displayController.renderBoard(gameBoard.board);
});