我如何得到这个来检测胜利?

我正在努力使这款终端游戏(井字游戏)能够检测出胜利,但我不太确定该怎么做。我需要游戏通过节点测试。我已经尝试了一些不同的方法,但是还没有任何东西可以工作。我需要将剩余的代码放置在“ function checkForWin”中,以使其正常工作。

'use strict';

const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,output: process.stdout
});
let board = [
  [' ',' ',' '],[' ',' ']
];

let playerTurn = 'X';

function printBoard() {
  console.log('   0  1  2');
  console.log('0 ' + board[0].join(' | '));
  console.log('  ---------');
  console.log('1 ' + board[1].join(' | '));
  console.log('  ---------');
  console.log('2 ' + board[2].join(' | '));
}

function horizontalWin() {
  if (
      board [0] [0] === playerTurn && 
      board [0] [1] === playerTurn &&
      board [0] [2] === playerTurn
      ) {
        return true;
      } else if (
        board [1] [0] === playerTurn && 
        board [1] [1] === playerTurn &&
        board [1] [2] === playerTurn 
      ) {
        return true;
      } else if (
        board [2] [0] === playerTurn && 
        board [2] [1] === playerTurn &&
        board [2] [2] === playerTurn 
      ) {
        return true;
      } 
        return false;


}

function verticalWin() {

  if (
    board [0] [0] === playerTurn && 
    board [1] [0] === playerTurn &&
    board [2] [0] === playerTurn
    ) {
      return true;
    } else if (
      board [0] [1] === playerTurn && 
      board [1] [1] === playerTurn &&
      board [2] [1] === playerTurn 
    ) {
      return true;
    } else if (
      board [0] [2] === playerTurn && 
      board [1] [2] === playerTurn &&
      board [2] [2] === playerTurn 
    ) {
      return true;
    } 
      return false;
}

function diagonalWin() {

  if (
    board [0] [0] === playerTurn && 
    board [1] [1] === playerTurn &&
    board [2] [2] === playerTurn
    ) {
      return true;
    } else if (
      board [0] [2] === playerTurn && 
      board [1] [1] === playerTurn &&
      board [2] [0] === playerTurn 
    ) {
      return true;
    } 
      return false;
}

function checkForWin() {


}

function ticTactoe(row,column) {

  let selectedRow = board[row];
  selectedRow[column] = playerTurn;

  if (playerTurn === 'X') {
    playerTurn = 'O';
  } else if (playerTurn === 'O') {
    playerTurn = 'X';
  }



}

function getPrompt() {
  printBoard();
  console.log("It's Player " + playerTurn + "'s turn.");
  rl.question('row: ',(row) => {
    rl.question('column: ',(column) => {
      ticTactoe(row,column);
      getPrompt();
    });
  });

}



// Tests

if (typeof describe === 'function') {

  describe('#ticTactoe()',() => {
    it('should place mark on the board',() => {
      ticTactoe(1,1);
      assert.deepEqual(board,[ [' ','X',' '] ]);
    });
    it('should alternate between players',() => {
      ticTactoe(0,0);
      assert.deepEqual(board,[ ['O',' '] ]);
    });
    it('should check for vertical wins',() => {
      board = [ [' ',' '] ];
      assert.equal(verticalWin(),true);
    });
    it('should check for horizontal wins',() => {
      board = [ ['X','X'],' '] ];
      assert.equal(horizontalWin(),true);
    });
    it('should check for diagonal wins','X'] ];
      assert.equal(diagonalWin(),true);
    });
    it('should detect a win',() => {
      assert.equal(checkForWin(),true);
    });
  });
} else {

  getPrompt();

}
wyg159753159753 回答:我如何得到这个来检测胜利?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3169638.html

大家都在问