In: Computer Science
How to make tic tac toe game in javascript with vue.js
//styling the game appearance.
master.css
#game-view{
width: 500px;
margin: 0 auto;
border: 1px solid #000;
}
#game-view-info{
padding: 15px;
font-family: sans-serif;
font-size: 20px;
font-weight: bold;
text-align: center;
background-color: #eee;
}
#game-view-squares{
height: 500px;
display: flex;
flex-wrap: wrap;
padding: 25px;
box-sizing: border-box;
}
.game-view-square{
width: 33.33%;
height: 33.33%;
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
font-family: cursive;
font-size: 75px;
text-transformation: uppercase;
border-radius: 15px;
cursor: pointer;
user-select: none;
-moz-user-select: none;
}
.game-view-square.hightlighted { color: green}
.game-view-square:hover { background-color: #eee }
.game-view-square:nth-child(-n + 6) { border-bottom: 15px solid brown }
.game-view-square:nth-child(3n + 1),
.game-view-square:nth-child(3n+2) {border-right: 15px solid brown}
create Game.js file with the following code
Class Game{
constructor(){
this.inProgress=true;
this.winner=null;//'O'or'x'
this.currentTurn=Game.O;//'O'or'X'
this.movesMode=0;
this.squares=new
Array(9).fill().map(s => new Square() );
}
makeMove(i){
if(this.inProgress &&
!this.squares[i].value){
this.squares[i].value=this.currentTurn;
this.movesMade++;
this.checkForWinner();
this.currenTurn=(this.currentTurn
== Game.O) ? Game.X : Game.O;
}
}
checkForWinner(){
const winningCombinations = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],//a==0,b=3,c=6
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
];
winningCombinations.forEach(wc)={
const [a,b,c]
=wc;
const
sqA=this.squares[a];
const
sqB=this.squares[b];
const
sqC=this.squares[c];
if(sqA.value
&& sqA.value ==sqB.value && sqA.value ==
sqC.value){
this.inProgress=false;
this.winner=sqA.value;//'O' or 'X'
sqA.isHighlighted=sqB.isHighlighted=sqC.isHighlighted=true;
}
});
//check for tie
if (this.movesMade ==
this.squares.length){
this.inProgress=false;//inProgress=false AND winner=null
}
}
}
Game.O='O';
Game.X='X';
create Square.js with the following code
class Square {
constructor(){
this.value=null;
this.isHighlighted-false;
}
}
create index.html with the following code:-
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tic Tac
Toe</title>
<link rel="stylesheet"
href="CSS/master.css">
</head>
<body>
<div id="game-view">
<div
id="game-view-info">
{{ infoMessage
}}
</div>
<div
id="game-view-squares">
</div>
<div
id="game-view-squares">
<div
v-for="(square,i) in squares"
v-on:click="activeGame.makeMove(i)"
v-bind:class="{highlighted: square.isHighlighted}"
class="game-view-square">
{{ square.value
}}
</div>
</div>
<script
src="https://cdn.jsdeliver.net/npm/vue"
charset="utf-8"></script>
<script src="js/Square.js"
charset="utf-8"></script>
<script src="js/Game.js"
charset="utf-8"></script>
<script
type="text/javascript">
let activeGame=new Game();
let gameVue =new Vue({
el: '#game-view',
data: activeGame
computed: {
infoMessage:
function(){
if(this.inprogress){
return 'It is' + this.currentTurn + '\'s
turn!';
}
if(this.winner){
return this.winner + 'wins!';
}
if(this.winner
&& !this.inProgress){
return 'It was a draw!' ;
}
}
}
});
</script>
</body>
</html>
Launch it in a browser.