I am a beginner programmer. In a prototype function of the game function, called game.prototype.restart
, I repeated some lines of code to reset variables I used to keep track of scores that were in its parent function. I'm not really sure right now how I could've made a restart button for my game another way, but it works.
'use strict'
function game () {
this.player = null;
this.computer = null;
this.turns = 0;
this.playerChoices = [];
this.computerChoices = [];
this.squares = document.querySelectorAll('.play');
this.spotsLeft = [0,1,2,3,4,5,6,7,8];
this.winningCombos = [
[0, 1, 2],[6, 7, 8],[0, 3, 6],[1, 4, 7],[2, 5, 8],
[0, 4, 8],[2, 4, 6], [3,4,5]
];
for(let i = 0; i < this.squares.length; i++){
this.squares[i].addEventListener('click', (function() {
this.playerMove(i);
}).bind(this));
}
}
game.prototype.chooseSymbol = function (symbol) {
this.player = symbol;
//choose computers symbol
this.computer = symbol === 'x' ? 'o' : 'x';
document.querySelector('.sign').style.visibility = 'hidden';
}
game.prototype.playerMove = function (i) {
if(this.squares[i].classList.contains('play')){
this.squares[i].textContent = this.player;
this.turns++; console.log(this.turns);
//remove to prevent overwriting text content on computer move and make player move array
this.squares[i].classList.remove('play');
//remove spot player chose to prevent computer from moving here
let index = this.spotsLeft.indexOf(i);
this.spotsLeft.splice(index, 1);
//push to array to compare for win
this.playerChoices.push(i);
this.checkWin(this.playerChoices, 'Player');
if(this.checkWin(this.playerChoices, 'Player') === false){
this.computerMove();
}
}
}
//chooses spot randomly
game.prototype.computerMove = function () {
let random = this.spotsLeft[Math.floor(Math.random() * this.spotsLeft.length)]
this.squares[random].textContent = this.computer;
//remove to prevent choosing again
let index = this.spotsLeft.indexOf(random);
this.spotsLeft.splice(index, 1);
this.squares[random].classList.remove('play');
this.computerChoices.push(random);
this.checkWin(this.computerChoices, 'Computer');
}
//compare player or computer arrays to check for win or tie
game.prototype.checkWin = function (player, name) {
console.log(name);
let win = this.winningCombos.some((ar) => ar.every((c) => player.includes(c)));
if( win === true){
document.querySelector('.result').style.visibility = 'visible';
document.querySelector('#tie').textContent = `${name} wins!`
} else if (this.turns === 5){
document.querySelector('.result').style.visibility = 'visible';
document.querySelector('#tie').textContent = `Tie!`
}
return Boolean(win);
}
game.prototype.restart = function () {
document.querySelector('.result').style.visibility = 'hidden';
document.querySelector('.sign').style.visibility = 'visible';
this.player = null;
this.computer = null;
this.turns = 0;
this.playerChoices = [];
this.computerChoices = [];
this.spotsLeft = [0,1,2,3,4,5,6,7,8];
for(let i = 0; i < this.squares.length; i++){
this.squares[i].classList.add('play');
this.squares[i].textContent = ' ';
}
}
const ticTacToe = new game();
document.querySelector('#playx').addEventListener('click', () => {
ticTacToe.chooseSymbol('x')
});
document.querySelector('#playo').addEventListener('click', () => {
ticTacToe.chooseSymbol('o')
});
document.querySelector('#replay').addEventListener('click', () => {
ticTacToe.restart();
})
'use strict'
function game() {
this.player = null;
this.computer = null;
this.turns = 0;
this.playerChoices = [];
this.computerChoices = [];
this.squares = document.querySelectorAll('.play');
this.spotsLeft = [0, 1, 2, 3, 4, 5, 6, 7, 8];
this.winningCombos = [
[0, 1, 2],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
[3, 4, 5]
];
for (let i = 0; i < this.squares.length; i++) {
this.squares[i].addEventListener('click', (function() {
this.playerMove(i);
}).bind(this));
}
}
game.prototype.chooseSymbol = function(symbol) {
this.player = symbol;
//choose computers symbol
this.computer = symbol === 'x' ? 'o' : 'x';
document.querySelector('.sign').style.visibility = 'hidden';
}
game.prototype.playerMove = function(i) {
if (this.squares[i].classList.contains('play')) {
this.squares[i].textContent = this.player;
this.turns++;
console.log(this.turns);
//remove to prevent overwriting text content on computer move and make player move array
this.squares[i].classList.remove('play');
//remove spot player chose to prevent computer from moving here
let index = this.spotsLeft.indexOf(i);
this.spotsLeft.splice(index, 1);
//push to array to compare for win
this.playerChoices.push(i);
this.checkWin(this.playerChoices, 'Player');
if (this.checkWin(this.playerChoices, 'Player') === false) {
this.computerMove();
}
}
}
//chooses spot randomly
game.prototype.computerMove = function() {
let random = this.spotsLeft[Math.floor(Math.random() * this.spotsLeft.length)]
this.squares[random].textContent = this.computer;
//remove to prevent choosing again
let index = this.spotsLeft.indexOf(random);
this.spotsLeft.splice(index, 1);
this.squares[random].classList.remove('play');
this.computerChoices.push(random);
this.checkWin(this.computerChoices, 'Computer');
}
//compare player or computer arrays to check for win or tie
game.prototype.checkWin = function(player, name) {
console.log(name);
let win = this.winningCombos.some((ar) => ar.every((c) => player.includes(c)));
if (win === true) {
document.querySelector('.result').style.visibility = 'visible';
document.querySelector('#tie').textContent = `${name} wins!`
} else if (this.turns === 5) {
document.querySelector('.result').style.visibility = 'visible';
document.querySelector('#tie').textContent = `Tie!`
}
return Boolean(win);
}
game.prototype.restart = function() {
document.querySelector('.result').style.visibility = 'hidden';
document.querySelector('.sign').style.visibility = 'visible';
this.player = null;
this.computer = null;
this.turns = 0;
this.playerChoices = [];
this.computerChoices = [];
this.spotsLeft = [0, 1, 2, 3, 4, 5, 6, 7, 8];
for (let i = 0; i < this.squares.length; i++) {
this.squares[i].classList.add('play');
this.squares[i].textContent = ' ';
}
}
const ticTacToe = new game();
document.querySelector('#playx').addEventListener('click', () => {
ticTacToe.chooseSymbol('x')
});
document.querySelector('#playo').addEventListener('click', () => {
ticTacToe.chooseSymbol('o')
});
document.querySelector('#replay').addEventListener('click', () => {
ticTacToe.restart();
})
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: rgba(244, 100, 0, 0.1);
}
.centered {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* choose x or o block */
.shade {
z-index: 1;
position: absolute;
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.box {
padding: 1.250rem 1.875rem;
height: auto;
background-color: azure;
top: 6em;
}
/* tic tac toe Board */
table {
z-index: 0;
border-collapse: collapse;
table-layout: fixed;
display: flex;
justify-content: center;
}
td {
width: 188.8px;
height: 188.8px;
border: 6px solid #222;
text-align: center;
vertical-align: middle;
font-size: 100px;
}
td {
border: 6px solid #222;
}
td:first-of-type {
border-left-color: transparent;
border-top-color: transparent;
}
td:nth-of-type(2) {
border-top-color: transparent;
}
td:nth-of-type(3) {
border-right-color: transparent;
border-top-color: transparent;
}
tr:nth-of-type(3) td {
border-bottom-color: transparent;
}
/* replay button */
.result {
visibility: hidden;
}
.replayBox {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
flex-wrap: nowrap
}
<!-- choose sign -->
<div class='shade sign'>
<div class='box'>
<button id='playx'>X</button>
<button id='playo'>o</button>
</div>
</div>
<!-- win or tie -->
<div class='shade result'>
<div class='box replayBox'>
<h3 id='tie'></h3>
<button id='replay'>Replay?</button>
</div>
</div>
<!-- board -->
<table id='board' class='centered'>
<tr>
<td id='0' class='play'></td>
<td id='1' class='play'></td>
<td id='2' class='play'></td>
</tr>
<tr>
<td id='3' class='play'></td>
<td id='4' class='play'></td>
<td id='5' class='play'></td>
</tr>
<tr>
<td id='6' class='play'></td>
<td id='7' class='play'></td>
<td id='8' class='play'></td>
</tr>
</table>