<commit_before>/**
 * @fileOverview Game module definition
 */

/**
 * A game engine for tic tac toe
 *
 * @class Game
 * @constructor
 * @param {Object} player1
 * @param {Object} player2
 */
var Game = function(player1, player2) {
  /**
   * A reference to the player1 object instance
   *
   * @property player1
   * @type {Object}
   * @default undefined
   */
  this.player1 = player1 || {};

  /**
   * A reference to the player2 object instance
   *
   * @property player2
   * @type {Object}
   * @default undefined
   */
  this.player2 = player2 || {};

  /**
   * An array representing the tic tac toe game board
   *
   * @property gameBoard
   * @type {Array}
   * @default [0,0,0,0,0,0,0,0,0]
   */
  this.gameBoard = [0,0,0,0,0,0,0,0,0];

  /**
   * Tracks whether the game is won by either player
   *
   * @property gameIsWon
   * @type {Boolean}
   * @default true
   */
  this.gameIsWon = false;

  /**
   * A reference to the PubSub object instance
   *
   * @property pubSub
   * @type {Object}
   * @default undefined
   */
  this.pubSub = new PubSub() || {};

  this.init();
};

/**
 * Initializes the game engine
 *
 * @method init
 * @returns Game
 */
Game.prototype.init = function() {
  this.assignSides()
      .runGame(this.player1);

  return this;
};

/**
 * Assigns tic tac toe sides to the players
 *
 * @method assignSides
 * @returns Game
 * @chainable
 */
Game.prototype.assignSides = function() {
  this.player1.side = 1;
  this.player2.side = 2;

  return this;
};

/**
 * Recursively runs getMove methods on the players until
 * there is a winner
 *
 * @method runGame
 * @param {Object} currentPlayer The current player of the game
 */
Game.prototype.runGame = function(currentPlayer) {
  var move;
  var nextPlayer;

  if (currentPlayer === this.player1) {
    nextPlayer = this.player2;
  } else {
    nextPlayer = this.player1;
  }

  if (!this.gameIsWon) {
    move = currentPlayer.getMove(this.gameBoard, currentPlayer.side);
    this.updateGameBoard(move, currentPlayer);
    this.checkForWin();
    this.runGame(nextPlayer);
  } else {
    this.publishWin(currentPlayer.side);
  }
};

/**
 * Updates the game board with the current player move
 *
 * @method updateGameBoard
 * @param {int} move The index of the game board array where the move is made
 * @param {Object} currentPlayer The current player of the game
 * @chainable
 */
Game.prototype.updateGameBoard = function(move, currentPlayer) {
  this.gameBoard[move] = currentPlayer.side;

  return this;
};

/**
 * Checks the game board to see if the current player's move wins the game
 * or draws the game
 *
 * @method checkForWin
 * @chainable
 */
Game.prototype.checkForWin = function() {
  switch(true) {
    case this.gameBoard[0] === this.gameBoard[1] && this.gameBoard[1] === this.gameBoard[2]:
    case this.gameBoard[3] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[5]:
    case this.gameBoard[6] === this.gameBoard[7] && this.gameBoard[7] === this.gameBoard[8]:
    case this.gameBoard[0] === this.gameBoard[3] && this.gameBoard[3] === this.gameBoard[6]:
    case this.gameBoard[1] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[7]:
    case this.gameBoard[2] === this.gameBoard[5] && this.gameBoard[5] === this.gameBoard[8]:
    case this.gameBoard[0] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[8]:
    case this.gameBoard[2] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[6]:
      this.gameIsWon = true;
      break;
  }

  return this;
};

Game.prototype.checkForDraw = function() {

  return this;
};

/**
 * Publishes to a PubSub object with winner information for use by other modules
 *
 * @method publishWin
 * @param {Int} winningSide The winning side
 * @chainable
 */
Game.prototype.publishWin = function(winningSide) {
  this.pubSubs.publish('Game is Won', {
    winner: winningSide
  });

  return this;
};<commit_msg>Added check for a draw. Added more PubSub publishing<commit_after>/**
 * @fileOverview Game module definition
 */

/**
 * A game engine for tic tac toe
 *
 * @class Game
 * @constructor
 * @param {Object} player1
 * @param {Object} player2
 */
var Game = function(player1, player2) {
  /**
   * A reference to the player1 object instance
   *
   * @property player1
   * @type {Object}
   * @default undefined
   */
  this.player1 = player1 || {};

  /**
   * A reference to the player2 object instance
   *
   * @property player2
   * @type {Object}
   * @default undefined
   */
  this.player2 = player2 || {};

  /**
   * An array representing the tic tac toe game board
   *
   * @property gameBoard
   * @type {Array}
   * @default [0,0,0,0,0,0,0,0,0]
   */
  this.gameBoard = [0,0,0,0,0,0,0,0,0];

  /**
   * Tracks whether the game is won by either player
   *
   * @property gameIsWon
   * @type {Boolean}
   * @default true
   */
  this.gameIsWon = false;

  /**
   * Tracks whether the game is a draw
   *
   * @property gameIsDrawn
   * @type {Boolean}
   * @default true
   */
  this.gameIsDrawn = false;

  /**
   * A reference to the PubSub object instance
   *
   * @property pubSub
   * @type {Object}
   * @default undefined
   */
  this.pubSub = new PubSub() || {};

  this.init();
};

/**
 * Initializes the game engine
 *
 * @method init
 * @returns Game
 */
Game.prototype.init = function() {
  this.assignSides()
      .runGame(this.player1);

  return this;
};

/**
 * Assigns tic tac toe sides to the players
 *
 * @method assignSides
 * @returns Game
 * @chainable
 */
Game.prototype.assignSides = function() {
  this.player1.side = 1;
  this.player2.side = 2;

  return this;
};

/**
 * Recursively runs getMove methods on the players until
 * there is a winner
 *
 * @method runGame
 * @param {Object} currentPlayer The current player of the game
 */
Game.prototype.runGame = function(currentPlayer) {
  var move;
  var nextPlayer;

  if (currentPlayer === this.player1) {
    nextPlayer = this.player2;
  } else {
    nextPlayer = this.player1;
  }

  if (!this.gameIsWon && !this.gameIsDrawn) {
    move = currentPlayer.getMove(this.gameBoard, currentPlayer.side);
    this.updateGameBoard(move, currentPlayer);
    this.checkForWin();
    this.checkForDraw();
    this.runGame(nextPlayer);
  } else if (this.gameIsWon){
    this.publishWin(currentPlayer.side);
  } else if (this.gameIsDrawn){
    this.publishDraw();
  }
};

/**
 * Updates the game board with the current player move
 *
 * @method updateGameBoard
 * @param {int} move The index of the game board array where the move is made
 * @param {Object} currentPlayer The current player of the game
 * @chainable
 */
Game.prototype.updateGameBoard = function(move, currentPlayer) {
  this.gameBoard[move] = currentPlayer.side;

  this.pubSub.publish('gameUpdate', {
    gameBoard: this.gameBoard
  });

  return this;
};

/**
 * Checks the game board to see if the current player's move wins the game
 *
 * @method checkForWin
 * @chainable
 */
Game.prototype.checkForWin = function() {
  switch(true) {
    case this.gameBoard[0] === this.gameBoard[1] && this.gameBoard[1] === this.gameBoard[2]:
    case this.gameBoard[3] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[5]:
    case this.gameBoard[6] === this.gameBoard[7] && this.gameBoard[7] === this.gameBoard[8]:
    case this.gameBoard[0] === this.gameBoard[3] && this.gameBoard[3] === this.gameBoard[6]:
    case this.gameBoard[1] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[7]:
    case this.gameBoard[2] === this.gameBoard[5] && this.gameBoard[5] === this.gameBoard[8]:
    case this.gameBoard[0] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[8]:
    case this.gameBoard[2] === this.gameBoard[4] && this.gameBoard[4] === this.gameBoard[6]:
      this.gameIsWon = true;
      break;
  }

  return this;
};

/**
 * Checks the game board to see if the current player's move draws the game
 *
 * @method checkForDraw
 * @chainable
 */
Game.prototype.checkForDraw = function() {
  if (this.gameBoard.indexOf(0) < 0) {
    this.gameIsDrawn = true;
  }
  return this;
};

/**
 * Publishes to a PubSub object with winner information for use by other modules
 *
 * @method publishWin
 * @param {Int} winningSide The winning side
 * @chainable
 */
Game.prototype.publishWin = function(winningSide) {
  this.pubSubs.publish('gameWon', {
    winner: winningSide
  });

  return this;
};

/**
 * Publishes to a PubSub object with winner information for use by other modules
 *
 * @method publishDraw
 * @chainable
 */
Game.prototype.publishDraw = function() {
  this.pubSubs.publish('gameDrawn', {
    draw: true
  });

  return this;
};
{'fraction_non_alphanumeric': 0.14495944380069525, 'fraction_numerical': 0.01645422943221321, 'mean_word_length': 3.289761431411531, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7094592', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2872, 'n_words': 745}