if-condition在Javascript中没有正确评估

时间:2014-11-28 04:54:43

标签: javascript conditional-statements

我正在尝试创建一个跳棋游戏,但是if-coditional没有正确评估为tryAgain()。invalidDiag();总是被称为:

else if(row1 + 1 != row2 || row1 - 1 != row2 && col1 + 1 != col2 || col1 - 1 != col2){
    tryAgain().invalidDiag();
    console.log(row1, col1, row2, col2)
  } 

条件应该检查​​所做的移动是否是对角线而且只有一个空格。为什么条件不能正确评估?

以下是完整代码:

var board, player1, player2, position1, position2, gameOn = false;


var resetBoard = function () {

  board = [
    [' X ', 'wht', ' X ', 'wht', ' X ', 'wht', ' X ', 'wht'],
    ['wht', ' X ', 'wht', ' X ', 'wht', ' X ', 'wht', ' X '],
    [' X ', 'wht', ' X ', 'wht', ' X ', 'wht', ' X ', 'wht'],
    [' X ', ' X ', ' X ', ' X ', ' X ', ' X ', ' X ', ' X '],
    [' X ', ' X ', ' X ', ' X ', ' X ', ' X ', ' X ', ' X '],
    ['red', ' X ', 'red', ' X ', 'red', ' X ', 'red', ' X '],
    [' X ', 'red', ' X ', 'red', ' X ', 'red', ' X ', 'red'],
    ['red', ' X ', 'red', ' X ', 'red', ' X ', 'red', ' X ']
  ];
  displayBoard();
}

var tryAgain = function(){

  return {
    pcolor : function() {
      position1 = prompt("chose a "+player1+" peice to move");
      position2 = prompt("Where would you like to move it?")
      attemptMove(getMove(position1,position2).startRow, getMove(position1,position2).startCol, getMove(position1,position2).endRow, getMove(position1,position2).endCol);
    },
    invalidX : function() {
      position2 = prompt("Can only move to open space, try again");
      attemptMove(getMove(position1,position2).startRow, getMove(position1,position2).startCol, getMove(position1,position2).endRow, getMove(position1,position2).endCol);
    },
    invalidDiag : function() {
      position2 = prompt("Can only move diagonally, try again");
      attemptMove(getMove(position1,position2).startRow, getMove(position1,position2).startCol, getMove(position1,position2).endRow, getMove(position1,position2).endCol);
    }
  }  
}

var attemptMove = function(row1, col1, row2, col2)
{
  if(board[row1][col1] != player1) {
    tryAgain().pcolor();
  }
  else if(board[row1][col1] != 'wht' && board[row1][col1] != 'red'){
    tryAgain().pcolor();
  }
  else if(board[row2][col2] != ' X '){
    tryAgain().invalidX();
  }
  else if(row1 + 1 != row2 || row1 - 1 != row2){
    tryAgain().invalidDiag();
    console.log(row1, col1, row2, col2)
  } 
  else 
  {
    makeMove(row1, col1, row2, col2);
  }
}

var makeMove = function(row1, col1, row2, col2) {

   var col=board[row1][col1]; 
   board[row1][col1]=" X "; 
   board[row2][col2]=col;

   displayBoard();
}

var removePiece = function(row, col) {

}
//test calls her


var play = function(){
  gameOn = true;
  resetBoard();

  player1 = prompt("what color player would you like you be")
  position1 = prompt("what peice would you like to move?");
  position2 = prompt("where would you like to move it?");

  player1 = player1 === 'red' ? 'red' : 'wht';
  player2 = player1 === 'red' ? 'wht' : 'red';

  attemptMove(getMove(position1,position2).startRow, getMove(position1,position2).startCol, getMove(position1,position2).endRow, getMove(position1,position2).endCol);
}

var getMove = function(sMove, eMove){
  var startMove = sMove.split('');
  var endMove = eMove.split('');

  return {
    startRow : charToNum[startMove[0]],
    startCol : parseInt(startMove[1]),
    endRow : charToNum[endMove[0]],
    endCol : parseInt(endMove[1]),
    quit : true
  }
}

3 个答案:

答案 0 :(得分:1)

  • A || B && C || D评估为A || (B && C) || D。在你的情况下,这很奇怪。

  • 更糟糕的是,您正试图查看row2是否与row1 + 1row1 - 1不同。如果row2row1 + 1,那么它与row1 - 1不同;无论row2是什么,如果不是两者,它们将至少与其中一个不同。您想测试位置不错的情况

    if (!(row1 + 1 != row2 && row1 - 1 != row2 || col1 + 1 != col2 && col1 - 1 != col2))
    

    你可以通过德摩根定律对其进行改造并获得:

    if ((row1 + 1 == row2 || row2 - 1 == row2) && (col1 + 1 == col2 || col1 - 1 == col2))
    

    这与你所拥有的相似。您是否有机会切换||&&的含义?

编辑:根据新要求:

if ((player1 == "wht" ? row1 + 1 == row2 : row2 - 1 == row2) && (col1 + 1 == col2 || col1 - 1 == col2))

您可能必须使用“red”而不是“wht”:p

答案 1 :(得分:0)

试试这个,给出适当的优先权

else if((row1 + 1 != row2 || row1 - 1 != row2) && (col1 + 1 != col2 || col1 - 1 != col2)){

答案 2 :(得分:0)

尝试放置一些额外的括号,以确保条件不会过早地短路:

else if((row1 + 1 != row2 || row1 - 1 != row2) && (col1 + 1 != col2 || col1 - 1 != col2)){