0

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

3 Answers 3

1
  • 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

Sign up to request clarification or add additional context in comments.

4 Comments

Or (row1 + 1 == row2 || row1 - 1 == row2) && (col1 + 1 == col2 || col1 - 1 == col2), which might be slightly easier to understand.
@minitech: Yup! I type slowly... :p
(row1 + 1 == row2 || row1 - 1 == row2) && (col1 + 1 == col2 || col1 - 1 == col2) will still allow for a move forward and backward. The idea with this conditional is to only allow diagonal movement that is forward. So, if you are a red player you can only move up the board and if you are a wht player you can only move down.
@rahul2001: And you wrote this where? Your original post just says "diagonal and only one space". Anyway, check the update.
0

Try this, to give proper precedence

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

2 Comments

Also note that (row1 + 1 != row2 || row1 - 1 != row2) is always true.
minitech is correct -- it will not throw an error as it is always true
0

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)){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.