ga-wdi-boston / game-project

Other
7 stars 102 forks source link

can't check for wins #829

Closed jingrid closed 7 years ago

jingrid commented 7 years ago

I can't seem to log any wins.

I defined each box and combination of wins:

let boxA = $('#a').text()
let boxB = $('#b').text()
let boxC = $('#c').text()
let boxD = $('#d').text()
let boxE = $('#e').text()
let boxF = $('#f').text()
let boxG = $('#g').text()
let boxH = $('#h').text()
let boxI = $('#i').text()
const checkForWin = function () {
  if (
    (boxA === boxB && boxB === boxC && boxA !== '') ||
    (boxD === boxE && boxE === boxF && boxD !== '') ||
    (boxG === boxH && boxH === boxI && boxG !== '') ||
    (boxA === boxD && boxD === boxG && boxA !== '') ||
    (boxB === boxE && boxE === boxH && boxB !== '') ||
    (boxC === boxF && boxF === boxI && boxC !== '') ||
    (boxA === boxE && boxE === boxI && boxA !== '') ||
    (boxC === boxE && boxE === boxG && boxC !== '')
  ) {
    console.log('winner')
  } else {
    console.log('not winner')
  }
}

But when I call the function, it always logs 'not winner.'

const checkMarker = function (event) {
  if ($(event.target).text() === '') {
    console.log('Empty')
    console.log(event.target.index)
    if (x === true) {
      $(event.target).text('x')
      // store.game.cells[$(this).data('id')] = 'x'
      onUpdateGame('x', $(this).data('id'), false)
      // console.log(store.game.cells)
    } else {
      $(event.target).text('o')
      // store.game.cells[$(this).data('id')] = 'o'
      // console.log(store.game.cells)
      onUpdateGame('o', $(this).data('id'), false)
      // set up condition to change false to true when game is over
    }
  } else {
    console.log('Not Empty')
  }
  checkForWin()
jordanallain commented 7 years ago

when are you setting these variables?

let boxA = $('#a').text()
let boxB = $('#b').text()
let boxC = $('#c').text()
let boxD = $('#d').text()
let boxE = $('#e').text()
let boxF = $('#f').text()
let boxG = $('#g').text()
let boxH = $('#h').text()
let boxI = $('#i').text()

because if you set them before the boxes are clicked they will all be set to an empty string.

jingrid commented 7 years ago

oh I see, but I'm not sure how to define them after the boxes are clicked.

jordanallain commented 7 years ago

i would argue that you don't even need them. you have an array that represents the state of the board already correct?

jingrid commented 7 years ago

Yes, from the patch requests. I've been trying to retrieve the array, but I'm unable to.

jingrid commented 7 years ago

Changed all instances of (boxA === boxB && boxB === boxC && boxA !== '')....

to ($('#a').text() === $('#b').text() && $('#b').text() === $('#c').text() && $('#a').text() !== '').....

which works.