Class: Rubykon::Game
- Inherits:
-
Object
- Object
- Rubykon::Game
- Defined in:
- lib/rubykon/game.rb
Constant Summary collapse
- DEFAULT_KOMI =
6.5
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#captures ⇒ Object
readonly
Returns the value of attribute captures.
-
#group_tracker ⇒ Object
readonly
Returns the value of attribute group_tracker.
-
#ko ⇒ Object
readonly
Returns the value of attribute ko.
-
#komi ⇒ Object
Returns the value of attribute komi.
-
#move_count ⇒ Object
readonly
Returns the value of attribute move_count.
Class Method Summary collapse
Instance Method Summary collapse
- #dup ⇒ Object
- #finished? ⇒ Boolean
-
#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
constructor
the freakish constructor is here so that we can have a decent dup.
- #next_turn_color ⇒ Object
- #no_moves_played? ⇒ Boolean
- #play(x, y, color) ⇒ Object
- #play!(x, y, color) ⇒ Object
- #safe_set_move(identifier, color) ⇒ Object
- #set_valid_move(identifier, color) ⇒ Object
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
#board ⇒ Object (readonly)
Returns the value of attribute board.
3 4 5 |
# File 'lib/rubykon/game.rb', line 3 def board @board end |
#captures ⇒ Object (readonly)
Returns the value of attribute captures.
3 4 5 |
# File 'lib/rubykon/game.rb', line 3 def captures @captures end |
#group_tracker ⇒ Object (readonly)
Returns the value of attribute group_tracker.
3 4 5 |
# File 'lib/rubykon/game.rb', line 3 def group_tracker @group_tracker end |
#ko ⇒ Object (readonly)
Returns the value of attribute ko.
3 4 5 |
# File 'lib/rubykon/game.rb', line 3 def ko @ko end |
#komi ⇒ Object
Returns the value of attribute komi.
4 5 6 |
# File 'lib/rubykon/game.rb', line 4 def komi @komi end |
#move_count ⇒ Object (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
77 78 79 |
# File 'lib/rubykon/game.rb', line 77 def self.pass?(identifier) identifier.nil? end |
Instance Method Details
#dup ⇒ Object
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
46 47 48 |
# File 'lib/rubykon/game.rb', line 46 def finished? @consecutive_passes >= 2 end |
#next_turn_color ⇒ Object
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
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 |