Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Examine the code and identify two different types of module coupling that are happening in the code. Put in extracts of the code that illustrate
Examine the code and identify two different types of module coupling that are happening in the code. Put in extracts of the code that illustrate the coupling and explain why you think that type of coupling is happening there. The code is given below: 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;
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