这是功能控制表。

/**
     * @constructor
     * @param {Number} width - dimension width for table
     * @param {Number} height - dimension height for table
     */
function Table(width, height) {
  this.table = [];
  for (var x = 0; x < height; x++) {
    this.table[x] = [];
    for (var y = 0; y < width; y++) {
      this.table[x][y] = ' ';
    }
  }

  console.log('test', this.table);
  this.width = width;
  this.height = height;
}

Table.prototype = {
  /**
   * Representation for get width of table
   */
  getWidth: function () {
    return this.width;
  },

  /**
   * Representation for get height of table
   */
  getHeight: function () {
    return this.height;
  },

  /**
   * Representation for get table
   */
  getTable: function () {
    var x = new Array(this.getHeight());
    for (var i = 0; i < this.getHeight(); i++) {
      x[i] = new Array(this.getWidth());
    };
  },

  /**
   * Representation for set position of table
   */
  setPosition: function (x, y, ch) {
    this.table[x][y] = ch;
  },

  /**
   * Representation for get value detail at cell of table
   */
  getValueAt: function (x, y) {
    return this.table[x][y];
  },

  /**
   * Representation for check empty conditional
   */
  isEmptyAt: function (x, y) {
    return !this.table[x][y];
  },
};

/**
 * @constructor
 * @param {String} character - X or O
 */
function Player(name, ch) {
  this.name = name;
  this.ch = ch;
}

var Printer = function () {
};




这是打印到控制台的功能。

Printer.prototype = {
  /**
   * Representation print table
   */
  printTable: function (table) {
    var str = '';
    for (var i = 0; i < table.width; i++) {
      var x = i;
      for (var j = 0; j < table.height; j++) {
        var y = j;
        str += ' ' + table.getValueAt(x, y) +  ' |';
      }

      str += '\n------------\n';
    }

    console.log(str);
  },

  /**
   * Representation check winner conditional
   */
  printWinner: function (player) {

  },
};

/**
 * @param newTable [array] : The array two-direction table
 * @param player [object] : the object contain player X and O
 */
var GamePlay = function (table, playerOne, playerTwo) {
  this.table = table;
  this.playerOne = playerOne;
  this.playerTwo = playerTwo;
  this.printer = new Printer();

};




GamePlay控制表和播放器。

GamePlay.prototype = {

  run: function () {
    console.log('Game start ...!');
    var x = Math.floor(Math.random() * 3);
    var y = Math.floor(Math.random() * 3);
    this.putChessman(x, y, this.playerOne.ch);
    this.printer.printTable(this.table);

    if (ch === 'O') {
      this.putChessman(x, y, this.playerTwo.ch);
    } else {
      this.putChessman(x, y, this.playerOne.ch);
    }
  },




这是CheckWin。

  /**
   * @param player [keywork] : the keywork X and O
   */


   checkWin: function (table) {
    var winner = 0;

    //check each row for a winner
    if(table[0][0] == table[0][1] && table[0][0] == table[0][2] && table[0][0] != 0) {
      winner = table[0][0];
    }

    // check row 2 for winner
    if (table[1][0] == table[1][1] && table[1][0] == table[1][2] && table[1][0] != 0 ){

        winner = table[1][0];
    }

    // check row 3 for winner
    if (table[2][0] == table[2][1] && table[2][0] == table[2][2] && table[2][0] != 0 ){

      winner = table[2][0];
    };

    // check column 1 for winner
    if (table[0][0] == table[1][0] && table[0][0] == table[2][0] && table[0][0] != 0 ){

      winner = table[0][0];
    }
    // check column 2 for winner
    if (table[0][1] == table[1][1] && table[0][1] == table[2][1] && table[0][1] != 0 ){

      winner = table[0][1];
    }
    // check column 3 for winner
    if (table[0][2] == table[1][2] && table[0][2] == table[2][2] && table[0][2] != 0 ){

      winner = table[0][2];
    }

    //check each diagnoal for a winner
    if (table[0][0] == table[1][1] && table[0][0] == table[2][2] && table[0][0] != 0 ){

      winner = table[0][0];
    }

    // check second diagonal for winner
    if (table[0][2] == table[1][1] && table[0][2] == table[2][0] && table[0][2] != 0 ){

      winner = table[0][2];

    }

    return winner;
  },




这是功能放棋子的人。但是它总是转为else

    putChessman: function (x, y, ch) {
    if (this.table.isEmptyAt(x, y) === true) {
      console.log('@ player ' + ch + ' put');
      this.table.setPosition(x, y, ch);
      if (ch === 'O') {
        result = this.putChessman(x, y, this.playerTwo.ch);
        if (result) {
          ch = 'X';
        }
      } else {
        result = this.putChessman(x, y, this.playerOne.ch);
        if (result) {
          ch = 'O';
        }
      }
    } else {
      console.log('@ Other player already put on it');
    }
  },

};

var table = new Table(3, 3);
var playerOne = new Player('playerOne', 'O');
var playerTwo = new Player('playerTwo', 'X');
var game = new GamePlay(table, playerOne, playerTwo);
game.run();

最佳答案

在某些时候,您需要设置变量“ ch”以指示玩家需要去什么。现在,您的主要功能中包含以下代码:

if (ch === 'O') {
  this.putChessman(x, y, this.playerTwo.ch);
} else {
  this.putChessman(x, y, this.playerOne.ch);
}


您可以尝试做的一件事是返回putChessman是否成功。

putChessman: function (x, y, ch) {
  if (this.table.isEmptyAt(x, y) === true) {
    console.log('@ Other player already put on it');
    return true;   // CHANGED FROM THE CODE ABOVE
  } else {
    console.log('@ player ' + ch + ' put');
    this.table.setPosition(x, y, ch);
    return false;   // CHANGED FROM THE CODE ABOVE
  }


您可以在主函数中捕获结果,并使用它来确定应为“ ch”。

if (ch === 'O') {
  result = this.putChessman(x, y, this.playerTwo.ch);
  if(result) {
      ch = 'X';
  }
} else {
  result = this.putChessman(x, y, this.playerOne.ch);
  if(result) {
      ch = 'O';
  }
}


希望这可以帮助!

09-20 23:31