3
\$\begingroup\$

I've written a gameBoard class for a Reversi/Othello which is the following:

import java.io.Serializable;

public class GameBoard implements Serializable {


private static final long serialVersionUID = 1034285059488509251L;
public char[][] board;

public GameBoard(){
    board = new char[8][8];
    setUpBoard();
}


  public void printBoard() {

      //prints numbers at the top of the board
        System.out.print("  ");
        for(int i = 0; i < board.length; ++i){
            System.out.print(i +  "   ");
        }

        //prints column icons and numbers and side of board
        System.out.println();       
        for (int i = 0; i < board.length; i++) {
            System.out.print("| ");
            for (int j = 0; j < board.length; j++) {

                System.out.print(board[i][j] + " | ");
            }
                System.out.print(i);
                System.out.println();
        }

    }

public void setUpBoard(){

      // Loop through rows
    for (int i = 0; i < 8; i++) {

        // Loop through columns
        for (int j = 0; j < 8; j++) {
            board[i][j] = '-';
            System.out.println();
        }
    }

    //sets the default piece positions;
    board[3][3] = 'W';
    board[3][4] =  'B';
    board[4][3] = 'B';
    board[4][4] = 'W';

}   

public String toString(){
    StringBuilder b = new StringBuilder();
    for(int i = 0; i < board.length; ++i){
        b.append(i +  "   ");
    }

    b.append("\r\n");       
    for (int i = 0; i < board.length; i++) {
        b.append("| ");
        for (int j = 0; j < board.length; j++) {

            b.append(board[i][j] + " | ");
        }
        b.append(i);
        b.append("\r\n");
    }
    return b.toString();
 }
}

This class as you can see handles basics of the game board. Such as printing the board and setting up the default positions. It is also serializable and I have a saving and loading method to store the game state and an overriden toString method so that I can print the board again.

I have another class called boardLogic and I've been messing around in this class for about 4 hours now trying to figure out how to check what is a valid move in Reversi/Othello or not.

The simple rules are: -You cannot place a piece on any random place -If you are to place a piece it must me adjacent to another piece of your color Horizontally, vertically or diagonally and it doesn't matter how many pieces are in between.

So, can anyone help me out on how I would go about checking if the player had made a valid move or not.

\$\endgroup\$
0
\$\begingroup\$

Given then two dimension array board[][], when a player places a piece, loop over the adjacent elements and check if there is a piece in one of them that matches the player's colour. If so it is a valid move.

The difficulty is in the edge cases - you need to make sure if a piece is placed near the edge of the board you don't check outside the board boundary.

\$\endgroup\$
  • 1
    \$\begingroup\$ Yeah, I actually just done something similar. How would I go about checking if say a piece is adjacent to another of the same colour, the only catch being that there has to be a piece of a different colour in the middle? \$\endgroup\$ – Yatoniea Dec 13 '15 at 6:48
  • \$\begingroup\$ If there's another piece in the middle, I don't think they're adjacent. If what you want is to find is if there is a line of opposing pieces between your pieces, then you can just do a loop for each direction and scan until you find an empty or friendly piece. \$\endgroup\$ – Silent137 Jan 15 '16 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.