-1

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?

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). You might also want to learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 09 '15 at 06:03
  • I have split your question so that it is easier to read. And I have bolded a few keywords. – Rohit Gupta Oct 11 '15 at 06:12
  • I would start by changing the board representation. You'll find less repetition if you represent the board as a linear array of lines - then you can advance by ±1 left and right, ±cols up and down, and ±cols±1 diagonally. – Toby Speight Dec 12 '22 at 14:42

2 Answers2

0

One way that strikes me as simple is to use variables xoffset and yoffset which take values 1, 0, -1. Use a for loop on the values of those variables, then a while loop to follow consecutive pieces.

John Perry
  • 2,497
  • 2
  • 19
  • 28
  • I see what you mean but would I have to create 8 different loops for each direction? – lastlightinthewest Oct 09 '15 at 06:17
  • No; suppose you're at position (a,b). You have an outer loop for `xoffset`, ranging from -1 to 1, an inner loop for `offset`, ranging the same. Initialize (c,d) to be the same as (a,b), then put a do/while loop where you add `xoffset` to `c`, and `yoffset` to `d`. The innermost loop terminates when you arrive either at a piece of your color or an empty location. – John Perry Oct 09 '15 at 12:28
  • 1
    `for (auto std::pair dir: {{-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}})` – Toby Speight Dec 12 '22 at 14:45
0

Great question!

I'd suggest you start by thinking about how exactly you determine if a piece should be flipped. In any of your eight directions, you flip any pieces of the opposite color between your piece and the first piece of the same color, and don't flip any if there are any empty spaces before you reach that piece. (Note that this is a pretty handy way to deal with even the case where there are 0 pieces of the opposite color between the two, because you'll flip "all 0" of them.)

There are cool things you can do when you use +/- 1, too -- check the product of values to see whether the colors match (+1) or mismatch (-1), or to flip the token multiply by -1.

Quick mockup of this conceptually, where we're checking the lines of options as well as to make sure we don't leave the board -- I'm using != for not equal, in case that's not familiar to you:

set current_player // +1 or -1
set current_space // say [3, 4]
directions = [[-1, -1], [-1, 0], ...]
for direction in directions:
    spaces_away = 1
    check_space = direction*spaces_away + current_space
    while (check_space has values between 1 and 8 for both x and y) and (check_space != 0)
        if current_player * (direction*spaces_away + current_space) = -1
            spaces_away += 1 // add one so we can check the next space out
            check_space = direction*spaces_away + current_space
        else
            for i from 1 to spaces_away
                flip_space = direction*spaces_away + current_space
                flip_space * -1 // this will change the color

There's some implementation to think about here, mainly the fact that I ignore how you're assigning a -1, 0, or 1 to each of the 64 spaces. Hopefully the pseudocode is clear enough without this.

If the for direction in directions doesn't make sense, take some time to look up what an iterable is. Super useful concept, regardless of what you do here!

Best of luck!