使用数组和基本JS知识创建SPA井字游戏

时间:2019-07-01 22:58:46

标签: javascript jquery arrays

我正在尝试制作SPA井字游戏。我的逻辑有些不对劲,我真的需要帮助。这是我的第一个项目,并且尝试进行编码,所以请轻柔=)

const store = require('./../store')

const board = ['', '', '', '', '', '', '', '', '']
// select user token
const newGame = []

const updateGame = function setToken(id) {
  if (id === 'x') {
    board.user = '"x"'
    board.opponent = '"o"'
  } else if (id === 'o') {
    board.user = '"o"'
    board.opponent = '"x"'
  }

  // set current player
  function setCurrentPlayer(player) {
    board.currentPlayer = 'player'
  }

  // mave move and switch player
  function makeMove(id) {
    for (let i = 0; i < 9; i++) {
      $(`#${i}`).text('')
    }
    // function to make move on the id passed
    if (board.currentPlayer === 'user') {
      // get element by id
      $('cell' + id).click(board.currentPlayer)
      // if current player selects a cell add id
      $('cell' + id).removeAttr('onClick')
      // if current player selects a cell on get game switch current Player
      onGetGame()
      setCurrentPlayer('opponent')
    } else if (board.currentPlayer === 'opponent') {
      $('cell' + id).click(board.user)
      $('cell' + id).removeAttr('onClick')
      onGetGame()
      setCurrentPlayer('user')
    }
    board.moves++
    // next player move ??
  }

  const checkforWin = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [6, 4, 2]
  ]
  // possible cells for winning
  function playerWin(id) {
    return board.checkforWin.some(function(cell) {
      return board.every(function(cell) {
        return board[cell] === id
      })
    })
  }

  // illegal move
  const illegalMove = function cellTaken(id) {
    if (id.cell.value.length === 0) {
      // if length of id is longer than 0
      return !
        //return false
        console.log('Space taken, try another space!')
    } else if (id.value.length === 1) {
      return true
      // if else return true
    }
    // tied game
    let tieGame = function(cellTaken) {
      return (board.every.(cellTaken))
      return true {
        console.log('DRAW!')
      }
    }
  }
}

我在这条线上有一个linter错误。我正在尝试使用.every方法来运行cellTaken函数,并验证是否已填充所有单元格以调用绘制。

 return (board.every.(cellTaken))

1 个答案:

答案 0 :(得分:0)

要解决特定的棉绒错误,请删除。在每个括号之间。

return (board.every(cellTaken))