Greetings to the StackOverflow Community!
Currently, I am working on developing a JavaScript version of the well-known game Pentago as a personal side project before resuming my studies in the fall. However, I have hit a roadblock and need some guidance on how to proceed further with this project.
Initial Strategy
Initially, I decided to create four matrices and store them in a list, with each matrix representing a section of the Pentago board. However, implementing this approach posed several challenges when it came to iterating over the matrices and checking for a winning scenario where a row of five game tokens is placed.
Revising My Approach
Seeking an alternative perspective, I was advised to use a single matrix instead of four. While this simplifies certain aspects, rotating each section becomes slightly more complex, albeit manageable.
The Problem at Hand...
My main concern revolves around determining whether a player has won the game upon placing a token in a specific position. Here’s what I believe I understand:
The function to determine the game's conclusion triggers after a player rotates a quadrant.
Subsequently, identifying the player who placed the last token enables focusing solely on their tokens for consideration.
I need to traverse the matrix starting from the location of the last token placed and check for adjacent tokens recursively until a sequence of five tokens is found diagonally, horizontally, or vertically.
Below is the code I have developed for this game so far. Any assistance would be greatly valued as I'm stuck at this particular juncture! Thanks in advance!
Current Code Progress
https://github.com/jakenewby/pentagojs/tree/master/www/app
var game = (function (players){
var self = {};
self.numTokensPlaced = 0;
// initialize the empty pentago board
self.board = [];
for(var i=0; i<6; i++)
{
self.board[i] = [];
for(var j=0; j<6; j++)
{
self.board[i][j] = null;
}
}
// rotate one of the quadrants of the board
self.rotateSection = function (section, direction)
{
if(isGameOver())
{
}
else
{
if(direction == 'clockwise')
{
/*
var newGrid = [];
var rowLength = Math.sqrt(section.length);
newGrid.length = section.length
for (var i = 0; i < section.length; i++)
.........
}
else
{
// code for rotation counterclockwise
}
switchTurns();
}
}
// place a token on the board given the player id,
// section, and coordinates where the token is being placed
self.placeToken = function(playerid, location)
{
if (self.board[location[0]][location[1]] != null)
{
return location.toString() + ' is already taken';
}
else
{
// logic for placing token and checking game status
}
}
.......
return self;
}());
Game Instructions
To play, insert a marble in a quadrant and subsequently rotate one of the quadrants by 90 degrees in either direction. Achieve a line of five marbles either horizontally, vertically, or diagonally to win!