Class: Game

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

Defined Under Namespace

Classes: IllegalMove

Constant Summary collapse

LETTERS =
('a'..'z').to_a

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board: 19) ⇒ Game

Returns a new instance of Game.



6
7
8
9
# File 'lib/game.rb', line 6

def initialize(board: 19)
  @board = Board.new(board)
  @moves = []
end

Instance Attribute Details

#boardObject (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

#capturesObject



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

#passObject



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

def pass
  @moves << {stone: NullStone.new(), captures: [], pass: true} 
end

#passesObject



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_sgfObject



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

#undoObject



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

#viewObject



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