Results 1 to 9 of 9
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,039
    Rep Power
    10

    Default 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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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.

  4. #4
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Have you Played othello/reversi/go before. Finding valid move help prease.

    Java 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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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

  6. #6
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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?

  7. #7
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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.

  8. #8
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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?

  9. #9
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default 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.




    Java 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;
        }

Similar Threads

  1. Replies: 6
    Last Post: 06-13-2013, 04:36 PM
  2. Replies: 1
    Last Post: 01-15-2012, 03:50 PM
  3. Problem in Minimax search in othello program
    By caabool_oo in forum NetBeans
    Replies: 0
    Last Post: 03-08-2011, 06:38 AM
  4. highlight word when audio is played
    By Saran185 in forum AWT / Swing
    Replies: 4
    Last Post: 02-09-2011, 11:57 AM
  5. Othello/Reversi flip checkers
    By chielt in forum Java Applets
    Replies: 10
    Last Post: 01-03-2010, 12:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •