I am making a Reversi game in C++ but I am having trouble figuring out how to check for consecutive enemy board pieces once the player places their piece on the board.
The board is an 8x8 2D array with values 0, -1, or 1.
- -1 is a White piece,
- 1 is a black piece and
- 0 is a vacant cell.
The function ApplyMove will simply place the current player's piece on the board and check if there is a run of enemy pieces in any direction. It will then flip all the pieces in that direction.
My function will have these parameters:
void ApplyMove(char board[BOARD_SIZE][BOARD_SIZE], int row, int col, char currentPlayer)
I want the program to use loops to iterate through all 8 possible directions starting from the piece that was placed by the player. How would this be accomplished in the simplest way possible without a bunch of ifs and elses?
One way that strikes me as simple is to use variables
xoffsetandyoffsetwhich take values 1, 0, -1. Use aforloop on the values of those variables, then awhileloop to follow consecutive pieces.