//
const board = [
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]
];
var vm = new Vue({
el: '#app',
data() {
return {
plays: 0,
player: true,
winner: '',
board: JSON.parse(JSON.stringify(board))
}
},
methods: {
resetGame() {
this.plays = 0
this.winner = ''
this.board = JSON.parse(JSON.stringify(board))
},
isBool(value) {
return typeof(value) === typeof(true)
},
play(x, y) {
if (!this.isBool(this.board[x][y]) && this.winner === '') {
this.board[x][y] = this.player
this.plays++
this.checkWin(this.player)
this.player = !this.player
}
},
slot(x, y) {
return {
'fa-circle-o': this.isBool(this.board[x][y]) && this.board[x][y] === true,
'fa-times': this.isBool(this.board[x][y]) && this.board[x][y] === false
}
},
endGame(player) {
this.winner = player
},
checkWin(player) {
// check horizontal win
for (var i = 0; i <= 2; i++) {
if (this.board[i][0] === player &&
this.board[i][1] === player &&
this.board[i][2] === player) {
this.endGame(player);
}
}
// check vertical win
for (var i = 0; i <= 2; i++) {
if (this.board[0][i] === player &&
this.board[1][i] === player &&
this.board[2][i] === player) {
this.endGame(player);
}
}
// check diagonal win
if ((this.board[0][0] === player &&
this.board[1][1] === player &&
this.board[2][2] === player) ||
this.board[0][2] === player &&
this.board[1][1] === player &&
this.board[2][0] === player) {
this.endGame(player);
}
if (this.plays === 9) {
this.endGame(-1);
}
}
}
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.14/vue.min.js"></script>
<style>
table tr td {
min-width: 20px;
min-height: 20px;
padding: 3px;
text-align: center;
}
</style>
<div id="app">
<h3>Tic-Tac-Vue</h3>
<p>It's player <i :class="['fa', {'fa-circle-o': player, 'fa-times': !player}]"></i>'s go!</p>
<table border="1">
<tr>
<td @click="play(0,0)"><i :class="['fa', slot(0,0)]"></i></td>
<td @click="play(0,1)"><i :class="['fa', slot(0,1)]"></i></td>
<td @click="play(0,2)"><i :class="['fa', slot(0,2)]"></i></td>
</tr>
<tr>
<td @click="play(1,0)"><i :class="['fa', slot(1,0)]"></i></td>
<td @click="play(1,1)"><i :class="['fa', slot(1,1)]"></i></td>
<td @click="play(1,2)"><i :class="['fa', slot(1,2)]"></i></td>
</tr>
<tr>
<td @click="play(2,0)"><i :class="['fa', slot(2,0)]"></i></td>
<td @click="play(2,1)"><i :class="['fa', slot(2,1)]"></i></td>
<td @click="play(2,2)"><i :class="['fa', slot(2,2)]"></i></td>
</tr>
</table>
<p v-if="winner === true || winner === false">Player <i :class="['fa', {'fa-circle-o': !player, 'fa-times': player}]"></i> is the winner!!</p>
<p v-if="winner === -1">Game Over, Draw!!</p>
<button @click="resetGame()">Reset Game</button>
</div>