-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
87 lines (83 loc) · 2.38 KB
/
game.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
import Score from './score.js';
import Board from './board.js';
import Message from './message.js';
import PlayAgain from './playAgain.js';
export default {
template: `
<div class="game">
<Score :player1="score.player1" :player2="score.player2" />
<Board :board="board" :currentPlayer="currentPlayer" @tile-clicked="handleTileClick" />
<Message :text="message" />
<PlayAgain v-if="gameOver" @click="resetGame" />
</div>
`,
components: {
Score,
Board,
Message,
PlayAgain,
},
data() {
return {
board: Array(9).fill(''),
currentPlayer: this.randomPlayer(),
score: {
player1: 0,
player2: 0,
},
message: '',
gameOver: false,
playerMapping: {
'o': '1',
'x': '2'
}
};
},
methods: {
randomPlayer() {
return Math.random() < 0.5 ? 'o' : 'x';
},
handleTileClick(index) {
if (this.board[index] !== '' || this.gameOver) {
return; // Tile already filled or game over
}
// Directly update the board array
this.board[index] = this.currentPlayer;
if (this.checkWin()) {
this.message = `Player ${this.playerMapping[this.currentPlayer]} wins!`;
this.gameOver = true;
if (this.currentPlayer === 'o') {
this.score.player1 += 1;
} else {
this.score.player2 += 1;
}
} else if (this.board.every(tile => tile !== '')) {
this.message = "It's a draw!";
this.gameOver = true;
} else {
this.currentPlayer = this.currentPlayer === 'o' ? 'x' : 'o';
this.message = `It's player ${this.playerMapping[this.currentPlayer]}'s turn`;
}
}
,
checkWin() {
const winningCombinations = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns
[0, 4, 8], [2, 4, 6] // Diagonals
];
return winningCombinations.some(combination =>
combination.every(index => this.board[index] === this.currentPlayer)
);
},
resetGame() {
this.board = Array(9).fill('');
this.currentPlayer = this.randomPlayer();
this.message = `It's player ${this.playerMapping[this.currentPlayer]}'s turn`;
this.gameOver = false;
}
},
created() {
this.message = `It's player ${this.playerMapping[this.currentPlayer]}'s turn`;
}
};