-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (92 loc) · 3.03 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
const gameboard = {
board: ['1', ' | ', '2', ' | ', '3',
'4', ' | ', '5', ' | ', '6',
'7', ' | ', '8', ' | ', '9'],
display() {
console.log(`
${this.board.slice(0, 5).join('')}\n
${this.board.slice(5, 10).join('')}\n
${this.board.slice(10).join('')}
`);
},
};
const player = {
create(values) {
const instance = Object.create(this);
Object.assign(instance, values);
return instance;
},
};
const game = {
playerTurn: true, // true for X and false for O
putMarker(index) {
if (this.playerTurn) {
gameboard.board[index] = playerOne.marker;
} else {
gameboard.board[index] = playerTwo.marker;
}
},
whosTurn() {
if (this.playerTurn) {
console.log('Player One\'s turn');
} else {
console.log('Player Two\'s turn');
}
},
didSomeoneWin() {
const locationsToCheck = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
const isItDraw = locationsToCheck.every(num => !gameboard.board.includes(num));
const WINNING_COMBINATIONS = [
[0, 2, 4], // Top row
[5, 7, 9], // Middle row
[10, 12, 14], // Bottom row
[0, 5, 10], // Left column
[2, 7, 12], // Middle column
[4, 9, 14], // Right column
[0, 7, 14], // Diagonal
[10, 7, 4], // Diagonal
];
const winnerFound = WINNING_COMBINATIONS.some((combo) => {
const [a, b, c] = combo;
return (
gameboard.board[a] === gameboard.board[b] &&
gameboard.board[b] === gameboard.board[c] &&
(gameboard.board[a] === playerOne.marker || gameboard.board[a] === playerTwo.marker)
);
});
if (winnerFound) {
console.log(this.playerTurn ? 'Player One won!' : 'Player Two won!');
return true;
}
if (isItDraw) {
console.log('It\'s a draw.');
return true;
}
return false; // If no win or draw, continue the game
},
};
const playerOne = player.create({name: "John", marker: "X", score: 0});
const playerTwo = player.create({name: "Jane", marker: "O", score: 0});
(() => {
while (true) {
game.whosTurn();
const userInput = prompt('Position (1-9): ');
if (userInput > 0 && userInput < 10) {
if (!gameboard.board.includes(userInput)) {
console.log('Position is already taken, try again.');
continue;
}
const index = gameboard.board.indexOf(userInput);
game.putMarker(index);
gameboard.display();
if (game.didSomeoneWin()) {
break
}
game.playerTurn = !game.playerTurn;
} else if (userInput === null) {
break
} else {
console.log('Invalid position, try again.')
}
}
})();