# Have you Played othello/reversi/go before. Finding valid move help prease.

• 10-07-2013, 08:15 PM
NoobieCode
Have you Played othello/reversi/go before. Finding valid move help prease.
Hi. I was wondering if someone can give me the algorithm to see if a move can be made. I am using a board 2D array. In this method, the position is passed in. I have to determine if the position aBoard[row][col] is valid. In order for it to be valid...the move, when made should trap at least one piece or another color.

1. I know ill have to ur 2 for-loops twice for each direction (horizontal, vertical, diagnol) to go back and forth starting from the given row,col
2. I am thinking of having a counter, so while going through, if the position that we're tyring to find hits goes to a cell where it is the same color piece, counter becomes 1 . And then i say YAY its legal for that direction, then check again backwards.

Thanks for giving me help. No code please.
• 10-07-2013, 08:28 PM
KevinWorkman
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
You mentioned an approach in your post. What happened when you tried that?
• 10-08-2013, 05:40 AM
NoobieCode
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
I got stuck... say for examplle im going to check the East of the position passed in. i hava for loop. I know how to say, go through column starting at passed in row, col and increment col by 1. If the passed in color = the color while going through , have some counter incrmeemnt and then return true if counter == 1. What I don't know how to say is.... If the adjacent box is Black and the color is black, this doen'st work and you dont increment.
• 10-08-2013, 05:43 AM
NoobieCode
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
Code:

``` private boolean checkHorizR2L  (color color, int row, int col){                     int indexRow = row;                 int counterHorizR2L = 0;                 for (int indexCol = col; indexCol >= 0; indexCol--){                         if(color.equals(Piece.BLACK)){                                 if (board[indexRow][indexCol].equals(Piece.BLACK)                                                 && counterHorizR2L == 0){                                         counterHorizR2L ++;                                 }                         }                         else {                                 if (board[indexRow][indexCol].equals(Piece.WHITE)                                                 && counterHorizR2L== 0){                                         counterHorizR2L ++;                                 }                         }                 }                 if (counterHorizR2L == 1) return true;                 else return false;         }```
'

Or could i keep track of both black and white peices in each loop and count them and if count of opposite color = 0 then adjacent peices wont cause counter to increment
• 10-08-2013, 05:46 AM
Junky
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
There are always two ways to look at things logically.

adjacent box is black and current colour is black : don't increment

Is the same as

adjacent box is black and current colour is white : increment

You can even simplify the condition to comparing adjacent box to current colour
• 10-08-2013, 06:08 AM
NoobieCode
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
this is what i did, I had a white counter and black counter, while the board elements == white or black, increment their respective counters. So when checking if passed in position and that direction is a valid move, I said if the elements color = position, and white or black counter > 0, then increment the horizontalCounter.... would that work?
• 10-08-2013, 06:14 AM
NoobieCode
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
Ok. I know my counter idea is flawed, it doens't actually check adjacency.
• 10-08-2013, 06:19 AM
NoobieCode
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
im going to use 2 for-loops
for horizontal

for (int i = 0; i < 8; i++)
for (int j = i +1; j < 8; j ++)

if (aBoard[row][i] == black/white && aBoard[row][j] == black/white return false no counter other wise count. if there are other colors between them. is this ok?
• 10-10-2013, 07:27 PM
NoobieCode
Re: Have you Played othello/reversi/go before. Finding valid move help prease.
So Update, I got east, west, north, and south directions to work. For some reason my diagonals work only if 1 peice would be trapped. For more than one, it fails. I dont know what wrong with my logic. Please save me from this agony.

Code:

``` public boolean validMove(Color color, int row, int col) {         if (row < 0 || row > 7) return false;         if (col < 0 || col > 7) return false;         if (board[row][col].equals(BLACK) || board[row][col].equals(WHITE))             return false;         //NorthEast check         if (chkDir(color, row, col, -1 , 1) == true){             int R = row - 2;             for (int C = col + 2 ; C < 8 ; C ++){                 if (color.equals(BLACK)){                      if (board[R][C].equals(EMPTY)) return false;                     if (board[R][C].equals(WHITE)) continue;                     if (board[R][C].equals(BLACK)) return true;                 }                 else if (color.equals(Piece.WHITE)){                     if (board[R][C].equals(EMPTY)) return false;                     if (board[R][C].equals(WHITE)) return true;                     if (board[R][C].equals(BLACK)) continue;                 }                 R --;             }         }         //SouthWest check         if (chkDir(color, row, col, 1 , -1) == true){             int R = row + 2;             for (int C = col - 2 ; C >= 0 ; C --){                 if (color.equals(BLACK)){                      if (board[R][C].equals(EMPTY)) return false;                     if (board[R][C].equals(WHITE)) continue;                     if (board[R][C].equals(BLACK)) return true;                 }                 else if (color.equals(WHITE)){                     if (board[R][C].equals(EMPTY)) return false;                     if (board[R][C].equals(WHITE)) return true;                     if (board[R][C].equals(BLACK)) continue;                 }                 R++;             }         }         return false;     }     private boolean chkDir (Color color, int row, int col, int rowTwo, int colTwo){         if (color.equals(BLACK)                 && board[row + rowTwo][col + colTwo].equals(WHITE)) return true;         else if (color.equals(WHITE)                 && board[row + rowTwo][col + colTwo].equals(BLACK)) return true;         else return false;     }```