Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here is the javaScript code: / / Initial references const container = document.querySelector ( . container ) ; const playerTurn = document.getElementById (
Here is the javaScript code:
Initial references
const container document.querySelectorcontainer";
const playerTurn document.getElementByIdplayerTurn;
const startScreen document.querySelectorstartScreen";
const startButton document.getElementByIdstart;
const message document.getElementByIdmessage;
let initialMatrix
;
let currentPlayer;
Random Number Between Range
const generateRandomNumber min max
Math.floorMathrandommax min min;
Loop through array and check for same values
const verifyArray arrayElement
let bool false;
let elementCount ;
arrayElement.forEachelement index
if element currentPlayer
elementCount ;
if elementCount
bool true;
else
elementCount ;
;
return bool;
;
Check for game overLast step
const gameOverCheck
let truthCounnt ;
for let innerArray of initialMatrix
if innerArrayeveryval val
truthCounnt ;
else
return false;
if truthCounnt
message.innerText "Game Over";
startScreen.classList.removehide;
;
Check rows
const checkAdjacentRowValues row
return verifyArrayinitialMatrixrow;
;
Check columns
const checkAdjacentColumnValues column
let colWinCount
colWinBool false;
initialMatrix.forEachelement index
if elementcolumn currentPlayer
colWinCount ;
if colWinCount
colWinBool true;
else
colWinCount ;
;
no match
return colWinBool;
;
Get Right diagonal values
const getRightDiagonal row column, rowLength, columnLength
let rowCount row;
let columnCount column;
let rightDiagonal ;
while rowCount
if columnCount columnLength
break;
rowCount ;
columnCount ;
rightDiagonal.unshiftinitialMatrixrowCountcolumnCount;
rowCount row;
columnCount column;
while rowCount rowLength
if columnCount
break;
rightDiagonal.pushinitialMatrixrowCountcolumnCount;
rowCount ;
columnCount ;
return rightDiagonal;
;
const getLeftDiagonal row column, rowLength, columnLength
let rowCount row;
let columnCount column;
let leftDiagonal ;
while rowCount
if columnCount
break;
rowCount ;
columnCount ;
leftDiagonal.unshiftinitialMatrixrowCountcolumnCount;
rowCount row;
columnCount column;
while rowCount rowLength
if columnCount columnLength
break;
leftDiagonal.pushinitialMatrixrowCountcolumnCount;
rowCount ;
columnCount ;
return leftDiagonal;
;
Check diagonal
const checkAdjacentDiagonalValues row column
let diagWinBool false;
let tempChecks
leftTop:
rightTop:
;
let columnLength initialMatrixrowlength;
let rowLength initialMatrix.length;
Store left and right diagonal array
tempChecks.leftTop
getLeftDiagonalrow column, rowLength, columnLength
;
tempChecks.rightTop
getRightDiagonalrow column, rowLength, columnLength
;
check both arrays for similarities
diagWinBool verifyArraytempChecksrightTop;
if diagWinBool
diagWinBool verifyArraytempChecksleftTop;
return diagWinBool;
;
Win check logic
const winCheck row column
if any of the functions return true we return true
return checkAdjacentRowValuesrow
true
: checkAdjacentColumnValuescolumn
true
: checkAdjacentDiagonalValuesrow column
true
: false;
;
Sets the circle to exact points
const setPiece startCount colValue
let rows document.querySelectorAllgridrow";
Initially it will place the circles in the last row else if no place availabke we will decrement the count until we find empty slot
if initialMatrixstartCountcolValue
startCount ;
setPiecestartCount colValue;
else
place circle
let currentRow rowsstartCountquerySelectorAllgridbox";
currentRowcolValueclassList.addfilledplayer$currentPlayer;
Update Matrix
initialMatrixstartCountcolValue currentPlayer;
Check for wins
if winCheckstartCount colValue
message.innerHTML Player $currentPlayer wins;
startScreen.classList.removehide;
return false;
Check if all are full
gameOverCheck;
;
When user clicks on a box
const fillBox e
get column value
let colValue parseIntetarget.getAttributedatavalue";
because we have rows
setPiece colValue;
currentPlayer currentPlayer : ;
playerTurn.innerHTML Player $currentPlayers turn;
;
Create Matrix
const matrixCreator
for let innerArray in initialMatrix
let outerDiv document.createElementdiv;
outerDiv.classList.addgridrow";
outerDiv.setAttributedatavalue", innerArray;
for let j in initialMatrixinnerArray
Set all matrix values to
initialMatrixinnerArrayj;
let innerDiv document.createElementdiv;
innerDiv.classList.addgridbox";
innerDiv.setAttributedatavalue", j;
innerDiv.addEventListenerclicke
fillBoxe;
< Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started