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_passObject



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

def black_pass
  pass(:black)
end

#capturesObject



56
57
58
# File 'lib/ruby-go/game.rb', line 56

def captures
  moves.capture_count
end

#passesObject



52
53
54
# File 'lib/ruby-go/game.rb', line 52

def passes
  moves.pass_count
end

#place_black(x, y) ⇒ Object



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

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

#place_white(x, y) ⇒ Object



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

def place_white(x, y)
  play(Stone.new(x, y, :white))
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



43
44
45
46
47
48
49
50
# File 'lib/ruby-go/game.rb', line 43

def undo
  move = moves.pop

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

#white_passObject



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

def white_pass
  pass(:white)
end