mercredi 18 mars 2015

Is it possible to consolidate my If statements here?

I am developing an 8x8 grid game. Similar to checkers I guess. Players can move in any direction and jump/take a piece in any direction.


I am individually writing out EVERY single move.. including invalid moves. Is there a way for me to clean this up? I believe there is but I just dont see it at the moment, I cant seem to think up a simpler solution.


Everything works fine, I just am curious if its possible to shorten it.


Heres some of the moves:



//If player attempts to move 2 squares within the same column
if(checkRow == 2 && checkCol == 0){

//Checks if row is OOB. If not, Checks to see if there is a player 2 one position to the right.
//If yes checks to see if row - 2 equals the initial player. This avoids methods getting called
// When surrounded by multiple pieces.
if(fromRow+1 < 8 && boardGame[fromRow+1][fromCol].getPlayer()== 2 && boardGame[toRow-2][toCol].getPlayer() == 1){
board[fromRow+1][fromCol] = 0;
valid = true;
updateTurnCount();
}
//Checks if row is OOB. If not, Checks to see if there is a player 2 one position to the left.
else if(fromRow-1 >= 0 && boardGame[fromRow-1][fromCol].getPlayer()== 2 && boardGame[toRow+2][toCol].getPlayer() == 1){
board[fromRow-1][fromCol] = 0;
valid = true;
updateTurnCount();
}

else if(fromRow+1 < 8 && boardGame[fromRow+1][fromCol].getPlayer()== 1 && boardGame[toRow-2][toCol].getPlayer() == 2){
board[fromRow+1][fromCol] = 0;
valid = true;
updateTurnCount();
}

else if(fromRow-1 >= 0 && boardGame[fromRow-1][fromCol].getPlayer()== 1 && boardGame[toRow+2][toCol].getPlayer() == 2){
board[fromRow-1][fromCol] = 0;
valid = true;
updateTurnCount();
}
}

Aucun commentaire:

Enregistrer un commentaire