Class: RubyGo::Game

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

Defined Under Namespace

Classes: IllegalMove

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board: 19) ⇒ Game

Returns a new instance of Game.



5
6
7
8
# File 'lib/ruby-go/game.rb', line 5

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

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/ruby-go/game.rb', line 3

def board
  @board
end

Instance Method Details

#black(x, y) ⇒ Object



35
36
37
# File 'lib/ruby-go/game.rb', line 35

def black(x, y)
  play(Stone.new(x, y, :black))
end

#black_passObject



43
44
45
# File 'lib/ruby-go/game.rb', line 43

def black_pass
  pass(:black)
end

#capturesObject



64
65
66
# File 'lib/ruby-go/game.rb', line 64

def captures
  moves.capture_count
end

#passesObject



60
61
62
# File 'lib/ruby-go/game.rb', line 60

def passes
  moves.pass_count
end

#save(name = "my_go_game") ⇒ Object



12
13
14
15
# File 'lib/ruby-go/game.rb', line 12

def save(name="my_go_game")
  tree = SGF::Parser.new.parse(to_sgf)
  tree.save(name + '.sgf')
end

#to_sgfObject



17
18
19
20
21
22
23
24
25
# File 'lib/ruby-go/game.rb', line 17

def to_sgf
  sgf = "(;GM[1]FF[4]CA[UTF-8]AP[jphager2]SZ[#{board.size}]PW[White]PB[Black]"

  moves.each do |move|
    sgf << move.played.to_sgf
  end

  sgf << ')'
end

#undoObject



51
52
53
54
55
56
57
58
# File 'lib/ruby-go/game.rb', line 51

def undo
  move = moves.pop

  board.remove(move.played)
  move.captures.each do |stone|
    board.place(stone)
  end
end

#viewObject



27
28
29
30
31
32
33
# File 'lib/ruby-go/game.rb', line 27

def view 
  puts  board
  puts  "   " + "_"*(board.size * 2)
  print "   Prisoners || White: #{captures[:black]} |"
  puts  " Black: #{captures[:white]}"
  puts  "   " + "-"*(board.size * 2)
end

#white(x, y) ⇒ Object



39
40
41
# File 'lib/ruby-go/game.rb', line 39

def white(x, y)
  play(Stone.new(x, y, :white))
end

#white_passObject



47
48
49
# File 'lib/ruby-go/game.rb', line 47

def white_pass
  pass(:white)
end