Question: CODE IN JAVASCRIPT WITH CONSOLE.LOG AS OUTPUT, ONLY CAN USE LOOPS NO FUNCTIONS: NO HTML OR CSS PURE JS This question has two parts. Part
CODE IN JAVASCRIPT WITH CONSOLE.LOG AS OUTPUT, ONLY CAN USE LOOPS NO FUNCTIONS: NO HTML OR CSS PURE JS
This question has two parts.
- Part 1 requires you to write some pseudo-code to solve the problem. Answer this inside a multi-line block comment.
- Part 2 requires you to write some code based on the answer to Part 1. Answer this below your answer to Part 1.
The game connect four is a two person connection board game, with each player having different colours (i.e. red / yellow) and take turns dropping the coloured disk into a seven column, six row vertical grid. The objective of the game is to make an unbroken horizontal, vertical or diagonal line of four coloured disks.
The data for the game is stored as an array of arrays, and shown below:
let gameState = [ [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0] ];
The data in the array has three states:
- 0 representing empty
- 1 representing player 1
- 2 representing player 2
Each item in the outer array represents a row, with gameState[0] being the top-most row, and gameState[5] being the bottom-most row (i.e. the data representation shown above reflects the physical orientation of the board game suspended vertically).
Each item in the inner array represents cells in the respective columns from left to right, i.e. gameState[0][0] represents the left (first) cell in the top row, and gameState[5][6] represents the right end cell in the bottom row.
You have been tasked to determine the winning game logic - that is, solve the game state to determine which player (if any) has won the game by detecting an unbroken horizontal, vertical or diagonal line of four coloured disks of that player.
To simplify your task, you are only required to identify an unbroken horizontal line of player disks.
You can alter the data in the gameState variable to test your solution. For example, by setting one row to be [0,1,1,1,1,2,0] to represent a horizontal line of four connecting disks from player 1.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
