1

I am currently building a Reversi game where the board is a 8x8 series of integers (0 for empty space, 1 for white, and 2 for black). I have figured out how to get the vertical and horizontal to check the move and make the move, but cannot figure out how to get the diagonals working.

package OthelloTesting;

public class OthelloGameHandler {
    private int[][] gameBoard = new int[8][8];

    public OthelloGameHandler() {

    }

    public void setBoard(int[][] newBoard) {
        gameBoard = newBoard;
    }

    public int[][] getBoard() {
        return gameBoard;
    }




    private final boolean inBounds(int row, int col) {
        return row >= 0 && col >= 0 && row < gameBoard.length && col < gameBoard.length;
    }

    /*
     * Color is: 0 - Empty | 1 - White | 2 - Black Return Messages: 0 - Okay | 1
     * - Piece in Spot | 2 - Not Valid Placement
     */
    public int move(int color, int row, int col) {
        // Check if spot is full
        int oppColor = (color == 1) ? 2 : 1;
        int returnCode = 2;
        if (gameBoard[row][col] != 0) {
            returnCode = 1;
        }
        if (!inBounds(row, col)) {
            returnCode = 2;
        }
        // Check if move is Valid...

        // Check Right Horizontal
        if (col < 6 && gameBoard[row][col + 1] != 0 &&
            gameBoard[row][col + 1] == oppColor) {
            for (int pos = col + 2; pos < 8; pos++) {
                if (gameBoard[row][pos] == 0) {
                    break;
                }
                if (gameBoard[row][pos] == color) {
                    fill(color, row, col, row, pos);
                    returnCode = 0;
                }
            }
        }

        // Check Left Horizontal
        if (col > 1 && gameBoard[row][col - 1] != 0 &&
            gameBoard[row][col - 1] == oppColor) {
            for (int pos = col - 2; pos > -1; pos--) {
                if (gameBoard[row][pos] == 0) {
                    break;
                }
                if (gameBoard[row][pos] == color) {
                    fill(color, row, pos, row, col);
                    returnCode = 0;
                }
            }
        }

        // Check Bottom Vertical
        if (row < 6 && gameBoard[row + 1][col] != 0 && gameBoard[row + 1][col] == oppColor) {
            for (int pos = row + 2; pos < 8; pos++) {
                System.out.println("did");
                if (gameBoard[pos][col] == 0) {
                    break;
                }
                if (gameBoard[pos][col] == color) {
                    fill(color, row, col, pos, col);
                    returnCode = 0;
                }
            }
        }

        // Check Top Vertical
        if (row > 1 && gameBoard[row - 1][col] != 0 && gameBoard[row - 1][col] == oppColor) {
            for (int pos = row - 2; pos > -1; pos++) {
                System.out.println("did");
                if (gameBoard[pos][col] == 0) {
                    break;
                }
                if (gameBoard[pos][col] == color) {
                    fill(color, pos, col, row, col);
                    returnCode = 0;
                }
            }
        }

        // Check Upper Right Diagonal


        // Check Upper Left Diagonal


        // Check Lower Left Diagonal


        // Check Lower Right Diagonal

        return returnCode;
    }

    private void fill(int color, int r1, int c1, int r2, int c2) {

        // Horizontal Filling
        if (r1 == r2) {
            for (int pos = c1; pos <= c2; pos++) {
                gameBoard[r1][pos] = color;
            }
        }
        if (c1 == c2) {
            for (int pos = r1; pos <= r2; pos++) {
                gameBoard[pos][c1] = color;
            }
        }


    }
}
alex
  • 10,900
  • 15
  • 70
  • 100
Helithumper
  • 11
  • 1
  • 2
  • 6
    Not all of us are likely to know the rules for Reversi/Othello/whatever the game is called, so you may want to make sure that either a) knowledge of the rules is unnecessary or b) **all** relevant rules are [edit]ed into your post. – Nathan Tuggy Jul 27 '15 at 02:35
  • Do you need to learn how to traverse a 2D array diagonally? Look here: https://www.google.com/search?q=traverse+a+2d+array+diagonally&ie=utf-8&oe=utf-8 – Donotalo Jul 27 '15 at 03:58
  • I think you should have pos-- in // Check Top Vertical – Herokiller Jul 27 '15 at 04:03

1 Answers1

0

I know this is C++ code and you are doing Java. But I think you can extract the main idea.

// flips discs in one direction
uint64_t flip_dir(const uint64_t P, const uint64_t O, const uint8_t move, const int dX, const int dY)
{
    uint64_t flips = 0;
    int i = (move % 8) + dX; // Starting index in x direction
    int j = (move / 8) + dY; // Starting index in y direction

    while ((i >= 0) && (i < 8) && (j >= 0) && (j < 8)) // In between boundaries
    {
        const uint64_t bit = 1ULL << (j * 8 + i); // The bit to look at
        if (O & bit) // The bit belongs to the opponent
            flips |= bit; // Add to possible flips
        else if (P & bit) // The bit belongs to the player
            return flips; // All possible flips become real flips
        else // The bit belongs to no player
            return 0; // There are no possible flips
        i += dX; // Advance in direction
        j += dY; // Advance in direction
    }
    return 0;
}

uint64_t flip(const uint64_t P, const uint64_t O, const uint8_t move)
{
    return move == 64 ? 0ULL :
           flip_dir(P, O, move, -1, -1)
         | flip_dir(P, O, move, -1,  0)
         | flip_dir(P, O, move, -1, +1)
         | flip_dir(P, O, move,  0, -1)
         | flip_dir(P, O, move,  0, +1)
         | flip_dir(P, O, move, +1, -1)
         | flip_dir(P, O, move, +1,  0)
         | flip_dir(P, O, move, +1, +1);
}

Happy disc flipping ;-)

Dominic Hofer
  • 5,751
  • 4
  • 18
  • 23