Class: Rubykon::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/rubykon/game.rb

Constant Summary collapse

DEFAULT_KOMI =
6.5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = 19, komi = DEFAULT_KOMI, board = Board.new(size), move_count = 0, consecutive_passes = 0, ko = nil, captures = initial_captures, move_validator = MoveValidator.new, group_tracker = GroupTracker.new) ⇒ Game

the freakish constructor is here so that we can have a decent dup



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubykon/game.rb', line 9

def initialize(size = 19, komi = DEFAULT_KOMI, board = Board.new(size),
               move_count = 0, consecutive_passes = 0,
               ko = nil, captures = initial_captures,
               move_validator = MoveValidator.new,
               group_tracker = GroupTracker.new)
  @board              = board
  @komi               = komi
  @move_count         = move_count
  @consecutive_passes = consecutive_passes
  @ko                 = ko
  @captures           = captures
  @move_validator     = move_validator
  @group_tracker      = group_tracker
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/rubykon/game.rb', line 3

def board
  @board
end

#capturesObject (readonly)

Returns the value of attribute captures.



3
4
5
# File 'lib/rubykon/game.rb', line 3

def captures
  @captures
end

#group_trackerObject (readonly)

Returns the value of attribute group_tracker.



3
4
5
# File 'lib/rubykon/game.rb', line 3

def group_tracker
  @group_tracker
end

#koObject (readonly)

Returns the value of attribute ko.



3
4
5
# File 'lib/rubykon/game.rb', line 3

def ko
  @ko
end

#komiObject

Returns the value of attribute komi.



4
5
6
# File 'lib/rubykon/game.rb', line 4

def komi
  @komi
end

#move_countObject (readonly)

Returns the value of attribute move_count.



3
4
5
# File 'lib/rubykon/game.rb', line 3

def move_count
  @move_count
end

Class Method Details

.from(string) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rubykon/game.rb', line 81

def self.from(string)
  game = new(string.index("\n") / Board::CHARS_PER_GLYPH)
  Board.each_move_from(string) do |identifier, color|
    game.safe_set_move(identifier, color)
  end
  game
end

.other_color(color) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rubykon/game.rb', line 69

def self.other_color(color)
  if color == :black
    :white
  else
    :black
  end
end

.pass?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubykon/game.rb', line 77

def self.pass?(identifier)
  identifier.nil?
end

Instance Method Details

#dupObject



64
65
66
67
# File 'lib/rubykon/game.rb', line 64

def dup
  self.class.new @size, @komi, @board.dup, @move_count, @consecutive_passes,
                 @ko, @captures.dup, @move_validator, @group_tracker.dup
end

#finished?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rubykon/game.rb', line 46

def finished?
  @consecutive_passes >= 2
end

#next_turn_colorObject



42
43
44
# File 'lib/rubykon/game.rb', line 42

def next_turn_color
  move_count.even? ? Board::BLACK : Board::WHITE
end

#no_moves_played?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rubykon/game.rb', line 38

def no_moves_played?
  @move_count == 0
end

#play(x, y, color) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rubykon/game.rb', line 24

def play(x, y, color)
  identifier = @board.identifier_for(x, y)
  if valid_move?(identifier, color)
    set_valid_move(identifier, color)
    true
  else
    false
  end
end

#play!(x, y, color) ⇒ Object



34
35
36
# File 'lib/rubykon/game.rb', line 34

def play!(x, y, color)
  raise IllegalMoveException unless play(x, y, color)
end

#safe_set_move(identifier, color) ⇒ Object



59
60
61
62
# File 'lib/rubykon/game.rb', line 59

def safe_set_move(identifier, color)
  return if color == Board::EMPTY
  set_valid_move(identifier, color)
end

#set_valid_move(identifier, color) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/rubykon/game.rb', line 50

def set_valid_move(identifier, color)
  @move_count += 1
  if Game.pass?(identifier)
    @consecutive_passes += 1
  else
    set_move(color, identifier)
  end
end