是否可以在此处合并我的If语句?

时间:2015-03-19 04:20:29

标签: if-statement

我正在开发一个8x8网格游戏。与猜测类似,我猜。玩家可以向任何方向移动并向任何方向跳跃/拿走一块。

我单独写出每一个动作......包括无效动作。有没有办法让我清理它?我相信有,但我现在还没有看到它,我似乎无法想出一个更简单的解决方案。

一切正常,我很好奇是否有可能缩短它。

继承了一些举措:

 //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(); 
                 }
           }

2 个答案:

答案 0 :(得分:0)

请不要在这里得到你的要求,但你当然可以将大部分逻辑推广到涵盖所有案例的简单陈述中。

    if ( newRow <0 || newRow >8 ) {
            disallowMove();
            return;
        }

   if ( board[newRow][newCol].player() != board[oldRow][oldCol].player() ){
            disallowMove();
            return;     
   }

    board[newRow][newCol] == somethingElse;
    updateTurnCount();

答案 1 :(得分:0)

lambdas可以大大缩短代码:例如,在主循环之前立即声明

auto from = [boardGame,fromRow,fromCol](int deltaRow, int deltaCol) {
  return boardGame[fromRow+deltaRow][fromCol+deltaCol];
};
auto to = [boardGame,toRow,toCol](int deltaRow, int deltaCol) {
  return boardGame[toRow+deltaRow][toCol+deltaCol];
};

条件可以写成

if(fromRow+1 < 8 && from(+1,0) == 2 && to(-2,0) == 1){...}

进一步清理可以从重复代码的因子分解开始:所采取的操作总是相同的,并使用相同的变量模式:然后lambda(假设boardGame等是成员变量)可以

auto action = [this](int df_Row, int df_Col, int dt_Row, int dt_Col) {
 int f_row = fromRow+deltaRow, f_col = fromCol+deltaCol;
 int t_row = toRow+dt_Row, t_col = toCol+dt_Col;
 if (f_row < 8 && boardGame[f_row][f_col].getPlayer()== 2 &&
     boardGame[t_row][t_col].getPlayer() == 1){
   boardGame[f_row][f_col] = 0;
   valid = true;
   return true;
 }
 return false;
};

并且逻辑变得更简单:

if (action(+1,0,-2,0)) updateTurnCount(); 
...