Hi. I was wondering if anyone could help me with checking for a valid move in Othello. I was thinking about hard=coding everything but that takes too long.

 this is what i have so far:
1. Check which direction is valid (adjacent piece is different from piece being placed)
2. Go in that direction(s) and if:
1. hit the end, or hit a empty space return false
2. hit another piece, keep going
3. hit my own piece, stop


All 9 Replies

There's no real alternative to writing that code. Yu may be able to avoid repeating code for each direction by putting that code into a method you can call repeatedly.

//This is a psuedo code-ish idea of what I was thinking, do i have the
//right idea?

private boolean isEast(Color color, int row, int col){

//Makes sure adjacent and passed in position is not same color
if(board[row][col + 1].equals(WHITE)
&& color.equals(BLACK)){

//Start from adjacent + 1 and go to end
for (int newCol = col + 2; newCol < 8; newCol ++){
//If elements is empty return false
if (board[row][newCol].equals(NONE)) return false;
//if it hits black return true
if (board[row][newCol].equals(BLACK)) return true;
}

}

else if (board[row][col+1].equals(BLACK)
&& color.equals(WHITE)){

for (int newCol = col + 2; newCol < 8; newCol ++){
if (board[row][newCol].equals(NONE)) return false;
if (board[row][newCol].equals(WHITE)) return true;
}
}
else {
return false;
}

return false;

}


I'm not familiar with the rules of the game, so I can't comment on the logic, but the overall style looks OK to me.
Maybe you can generalise that code for any directiopn by passing in a couple of variables to define the direction (delta-row and delta-col)? So instead of isEast you could have something like

isAnyDirection((Piece color, int row, int col, int dRow, int dCol){
if(board[row + dRow][col + dCol] ... etc


Then you could call it for East by passing 0,1 for the deltas, etc
Just a thought...

Ok. thanks.

My diagonals arent working. Can someone help me?
It works if i have to trap one piece in between, but when 2 or more are diagonally in between, it doesnt work.

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


anyone there

hi I am stuck on the same probblme did you manage to solve it in the end

The OP hasn't been on this site in 5 years so don't expect a reply.

Start your own Topic... explain how far you have got and what's blocking you. Show any relevant code, or at least say what language you are using. Help us to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.