Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Defined Under Namespace
Classes: IllegalMove
Constant Summary collapse
- LETTERS =
('a'..'z').to_a
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
Instance Method Summary collapse
- #black(x, y) ⇒ Object
- #captures ⇒ Object
-
#initialize(board: 19) ⇒ Game
constructor
A new instance of Game.
- #pass ⇒ Object
- #passes ⇒ Object
- #save(name = "my_go_game") ⇒ Object
- #to_sgf ⇒ Object
- #undo ⇒ Object
- #view ⇒ Object
- #white(x, y) ⇒ Object
Constructor Details
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
5 6 7 |
# File 'lib/game.rb', line 5 def board @board end |
Instance Method Details
#black(x, y) ⇒ Object
34 35 36 |
# File 'lib/game.rb', line 34 def black(x, y) play(BlackStone.new(x,y)) end |
#captures ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/game.rb', line 56 def captures @moves.each_with_object({black: 0, white: 0}) do |move, total| move[:captures].each do |capture| total[capture.color] += 1 end end end |
#pass ⇒ Object
42 43 44 |
# File 'lib/game.rb', line 42 def pass @moves << {stone: NullStone.new(), captures: [], pass: true} end |
#passes ⇒ Object
52 53 54 |
# File 'lib/game.rb', line 52 def passes @moves.inject(0) {|total, move| move[:pass] ? total + 1 : 0} end |
#save(name = "my_go_game") ⇒ Object
11 12 13 14 |
# File 'lib/game.rb', line 11 def save(name="my_go_game") tree = SGF::Parser.new.parse(to_sgf) tree.save(name + '.sgf') end |
#to_sgf ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/game.rb', line 16 def to_sgf sgf = "(;GM[1]FF[4]CA[UTF-8]AP[jphager2]SZ[19]PW[White]PB[Black]" @moves.each do |move| sgf << move[:stone].to_sgf end sgf << ')' end |
#undo ⇒ Object
46 47 48 49 50 |
# File 'lib/game.rb', line 46 def undo move = @moves.pop @board.remove(move[:stone]) move[:captures].each {|stone| @board.place(stone)} end |
#view ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/game.rb', line 26 def view puts @board.to_s puts " " + "_"*(@board.size * 2) print " Prisoners || White: #{captures[:black]} |" puts " Black: #{captures[:white]}" puts " " + "-"*(@board.size * 2) end |
#white(x, y) ⇒ Object
38 39 40 |
# File 'lib/game.rb', line 38 def white(x, y) play(WhiteStone.new(x,y)) end |