-3

http://jsfiddle.net/dXL2v/ <----K i think thats the entire code the instructor gave us, most of the messy code inside the functions were done by me so yea..but question still remains the same, the conditions are returning false even when the values are identical.

these are a couple small snippets of a larger code im supposed to program for a tic tac toe game, its supposed to check to see if each row or col has 3 of the same images and if there is, then u display a message saying the current player has won... what im having trouble with is the "topRow[0] == player" condition is returning false so the others dont even get compared...the value of topRow[0] is set to the relative path of an image ("Images/x.gif")

i have two players, var PLAYER_X is set to "Images/x.gif" and PLAYER_O is "Images/o.gif" so according to the code above, i thought if i was player x and i already had 2 X's on the board, if i clicked on the board and it went through the code and found that for each slot in the topRow array there is the value "Images/x.gif" it would output the message saying player X won.. instead it outputs the wrong message... when i run firebug with a breakpoint and paste in the express "topRow[0] == player" its returning false.. i checked the values and i dont know if this is the problem or not but the values inside of the topRow array is appearing as "lmages/x.gif" while the value of player is appearing as "Images/x.gif" but ive checked all my spelling and the cases are the same so im stumped....

EDIT:

i added the checkCol function, sorry new to this stuff..the instructor defined all the functions and the parameters to be passed were supposed to make stuff work based on his program

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Niag Ntawv
  • 197
  • 1
  • 4
  • 16
  • Try === , JS is particular about comparing that way. See this for a better explanation, might not fix your particular problem but is good to know about JS. http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – Will Buck Mar 01 '12 at 18:12
  • if this is homework, please add the tag. also, you are showing `checkRow...`, but you are calling `checkCol...`. that's confusing. – mindandmedia Mar 01 '12 at 18:13
  • have you tried console.log()'ing topRow[0] and player? – MyStream Mar 01 '12 at 18:13
  • yea sry, checkCol is in the entire file but the format is almost identical just the array indexes are changed to reflecting checking each column vs. each row – Niag Ntawv Mar 01 '12 at 18:23

2 Answers2

1

I am not sure what you are asking, that's why I rewrote your code to check for a win in the rows of a tic-tac-toe. finding out how to check for a win in the columns, I leave up to you or others...

var o = "Images/o.gif";
var x = "Images/x.gif";

var board = [[o,null,x],[x,x,x],[o,null,o]]; //for example

var currentPlayer = o;

function checkForVictory(currentPlayer){
    //check the rows
    for( var row = 0; row < 3; row++ )
    { 
       if( checkRowForVictory(board[row], currentPlayer) )
       {
         return true;
       }
    }
    return false;
}

function checkRowForVictory(row, player){
   for(var col = 0; col < row.length; col++ )
   { 
      if( row[col] != player ) return false;
   }
   return true;
}
mindandmedia
  • 6,800
  • 1
  • 24
  • 33
0

There is no function checkColForVictory.

Asken
  • 7,679
  • 10
  • 45
  • 77