Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created April 27, 2017 14:06
Show Gist options
  • Save joennlae/f3d69c284f5f7cbc35ff62d9332b88a9 to your computer and use it in GitHub Desktop.
Save joennlae/f3d69c284f5f7cbc35ff62d9332b88a9 to your computer and use it in GitHub Desktop.
package u8a3;
import reversi.Coordinates;
import reversi.GameBoard;
import reversi.OutOfBoundsException;
import reversi.Utils;
public class CheckMove implements ICheckMove{
@Override
public boolean checkMove(GameBoard gb, int player, Coordinates coord) {
if(gb.validCoordinates(coord)){
try {
if(gb.getOccupation(coord)==GameBoard.EMPTY){
for( int i = -1; i<= 1; i++){
for( int j = -1; j <= 1; j++){
if(i != 0 || j!=0 ){
if(checkDirection(gb, player, coord, new Coordinates(i,j))){
return true;
}
}
}
}
return false;
}
} catch (OutOfBoundsException e) {
e.printStackTrace();
}
}
return false;
}
private boolean checkDirection(GameBoard gb, int player, Coordinates coord, Coordinates dir) throws OutOfBoundsException{
coord = new Coordinates(coord.getRow()+ dir.getRow(), coord.getCol()+ dir.getCol());
if(gb.validCoordinates(coord)&&gb.getOccupation(coord)==Utils.other(player)){
while(gb.validCoordinates(coord)){
if(gb.getOccupation(coord)==Utils.other(player)){
coord = new Coordinates(coord.getRow()+ dir.getRow(), coord.getCol()+ dir.getCol());
}
else if(gb.getOccupation(coord)==player){
return true;
}
else return false;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment