检查winMap以查看是否有赢家

时间:2019-07-09 20:42:15

标签: javascript arrays

我的项目几乎完成了,我知道有些事情离我远了。很好,这是我的第一个大项目!在makeMove中,我试图编写如何检查我的winMap。我不确定是否应该像makePlayer一样使它成为makeMove之外的函数并在makeMove中调用。

但是,如果我不确定我做错了什么,那我会有一个linter错误。


let currentPlayer = 'X'
const board = ['', '', '', '', '', '', '', '', '']
// check for win array
const winMap = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [6, 4, 2]
]
// switch players
const switchPlayer = function() {
  if (currentPlayer === 'X') {
    currentPlayer = 'O'
  } else {
    currentPlayer = 'X'
  }
}


function tieGame() {
  for (let i = 0; i < board.lenght; i++) {
    if (board[i] === '') {
      return true
    } else {
      return false
      $('.message').text('Draw!')
    }
  }
}

const winner = function(cell, id) {
  //      return board.checkforWin.some(function(cell) {
  return board.every(function() {
    return board[cell] === id
  })
}

var all = function(list) {
    for (var i = 0; i < list.length; i++)
        if (!list[i])
            return false;

    return true;
}

// mave move and switch player

const makeMove = (event) => {
  event.preventDefault()
  const $cell = $(event.target)
  console.log('Target is: ', $cell)
  const index = $(event.target).data('cellindex')
  console.log(index)
  if (!$cell.text()) {
    $cell.text(currentPlayer)
  } else {(all(winMap[i][j].map(index => board[index] === "X")))
      alert("X wins") || (all(winMap[i][j].map(index => board[index] === "O")))
          alert("O wins")
          tiegame()
    switchPlayer()
  } else if {
    $('.message').text('Try another space!')
  }
  store.board[index].push(currentPlayer)

    $('.message').text(currentPlayer + ' Wins!!')
}

0 个答案:

没有答案