I’m working on a part of a minimax AI for a Connect Four board.
I have a parallel gameboard to test that looks like this:
let parallelBoard = [
[1,2,3,4,5,6,7],
[8,9,10,11,12,13,14],
[15,16,17,18,19,20,21],
[22,23,24,25,26,27,28],
[29,30,31,32,33,34,35],
[36,37,38,39,40,41,42]
];
The findAvailableSpots(board)
function gives me an array consisting of the numbers in the first row that are still available for play. I use the first row to determine this because I splice “Red” or “Yellow” into the array whenever a spot is taken, and if the first row for that column is occupied, it cannot be selected anymore.
Then I loop through the length of available spots, and take each one as the index of the column I want to put a marker into and test.
Next, for each one of these available spots, I loop upwards from the bottom or sixth row to place the token where there is no “red” or “yellow” string in that spot.
I would like it to stop once it finds a spot and move on to the next number in the parallelAvailable
array.
But if I don’t put in a return, it puts a token in every row of that column.
If I do put in a return, it breaks
after splicing [parallelAvailable][0]
.
I would like it to run the i
loop to see which row it can put a token into, and then move on to the next in [parallelAvailable][s]
.
What should I do? Am I placing the return
in the wrong place, or is there a better way to do this altogether, maybe with another if
condition?
function pickBestMove() {
// let bestScore
// let bestColumn
let parallelAvailable = findAvailableSpots(parallelBoard)
console.log(parallelAvailable)
for (s=0; s<parallelAvailable.length; s++) {
let i;
let j = parseInt(parallelAvailable[s] - 1)
console.log(j)
for (i = 5; i > -1; i--)
{if (Number.isInteger(parallelBoard[i][j])) {
parallelBoard[i].splice((j), 1, currentPlayer)
}
// let positionScore = scorePosition (parallelBoard, currentPlayer)
// console.log(gameboard[i][j] + " gets " + positionScore)
// parallelBoard[i].splice((indexPick), 1, gameboard[i][j])
// if (positionScore > bestScore) {
// bestScore = score
// console.log(bestScore)
// bestColumn = s
// console.log(bestColumn)
// }
// // return
// }
// }
}
// return bestColumn
console.log(parallelBoard)}
};