Class: NeuroGammon::GameEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/neuro_gammon/game_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(white_player, black_player) ⇒ GameEngine

Returns a new instance of GameEngine.



30
31
32
33
34
35
36
# File 'lib/neuro_gammon/game_engine.rb', line 30

def initialize white_player,black_player
  @board=Board.new
  @dice=Dice.new
  @black_player=black_player
  @white_player=white_player
  @colour=Board::WHITE
end

Instance Attribute Details

#black_playerObject (readonly)

Returns the value of attribute black_player.



26
27
28
# File 'lib/neuro_gammon/game_engine.rb', line 26

def black_player
  @black_player
end

#boardObject

Returns the value of attribute board.



23
24
25
# File 'lib/neuro_gammon/game_engine.rb', line 23

def board
  @board
end

#colourObject

Returns the value of attribute colour.



25
26
27
# File 'lib/neuro_gammon/game_engine.rb', line 25

def colour
  @colour
end

#diceObject

Returns the value of attribute dice.



24
25
26
# File 'lib/neuro_gammon/game_engine.rb', line 24

def dice
  @dice
end

#game_dataObject (readonly)

Returns the value of attribute game_data.



27
28
29
# File 'lib/neuro_gammon/game_engine.rb', line 27

def game_data
  @game_data
end

#white_playerObject (readonly)

Returns the value of attribute white_player.



26
27
28
# File 'lib/neuro_gammon/game_engine.rb', line 26

def white_player
  @white_player
end

#winnerObject (readonly)

Returns the value of attribute winner.



28
29
30
# File 'lib/neuro_gammon/game_engine.rb', line 28

def winner
  @winner
end

Instance Method Details

#play_gameObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/neuro_gammon/game_engine.rb', line 38

def play_game
  game=Game.new white_player,black_player
  while !(board.piece_count(Board::BLACK)==0 || board.piece_count(Board::WHITE)==0)
    dice.roll
    while dice.to_a.size>0
      game.add_dice_state(dice.to_a)
      game.add_board_state(board.state) #TODO final board state isn't recorded (i.e. always ends with 1 piece remainining)
      player=player_to_move
      move=player.suggest_move(board,dice,@colour)
      if (move!=nil) 
        board.move!(move,colour,dice)
        game.add_move(move)
      else #can't move
        #TODO this needs some more careful thought, and unit tests. Do we want to record when a move couldn't be made??
        game.dice_states.delete(game.dice_states.last)
        game.board_states.delete(game.board_states.last)        
        break
      end
    end
    toggle_player
  end
  game.winner_colour=board.winner
  @winner=(board.winner==Board::WHITE ? white_player : black_player)
  
  return game
end