本文介绍了如果是"O",如何更改?已经放了棋子然后轮到X放了棋子并在OOP javascript中检查数组2D的获胜者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是功能控制表.

/**
     * @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 () {
};


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


This is the function print to console .

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控制表和播放器.


The GamePlay Control Table and Player.

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.


This is 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"来指示玩家需要做什么.现在,您的主要功能中已有以下代码:

At some point you need to set the variable "ch" to indicate what player needs to go. Right now you have this code in your main function:

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

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

One thing you could try doing is returning whether putChessman succeeded.

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".

You can capture the result in your main function, and use it to decide what "ch" should be.

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';
  }
}

希望这会有所帮助!

这篇关于如果是"O",如何更改?已经放了棋子然后轮到X放了棋子并在OOP javascript中检查数组2D的获胜者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 00:45