rahul2001
rahul2001

Reputation: 1657

if-condition not evaluating properly in Javascript

I am trying to create a checkers game but else if-coditional is not evaluating properly as tryAgain().invalidDiag(); is always called:

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

The conditional is supposed to check if the move made was diagonal and only one space. Why is the conditional not evaluating properly?

Here is the full code:

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

Upvotes: 0

Views: 102

Answers (3)

Amadan
Amadan

Reputation: 198486

  • A || B && C || D evaluates like A || (B && C) || D. This is weird in your case.

  • Worse, you are trying to see if row2 is different from row1 + 1 or row1 - 1. If row2 is row1 + 1, then it is different from row1 - 1; whatever row2 is, it will be different from at least one of them, if not both. You want to test for the situation where the position is not bad:

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

    You can transform it by De Morgan's law and get this:

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

    which is rather similar to what you had. Did you switch the meanings of || and &&, by any chance?

EDIT: Based on new requirements:

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

You might have to use "red" instead of "wht" :p

Upvotes: 1

Andrei Fecioru
Andrei Fecioru

Reputation: 106

Try to place some additional parentheses to make sure that the conditional is not short-circuited prematurely:

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

Upvotes: 0

ssilas777
ssilas777

Reputation: 9764

Try this, to give proper precedence

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

Upvotes: 0

Related Questions