Class: Shogi::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = :csa, turn = "+") ⇒ Game

Returns a new instance of Game.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
# File 'lib/shogi/game.rb', line 5

def initialize(format=:csa, turn="+")
  raise ArgumentError, "Undefined format: #{format}" unless /\Acsa\z/ =~ format
  raise ArgumentError, "Invalid turn: #{turn}" unless /\A[+-]\z/ =~ turn

  @default_format = format
  @board = Shogi::Board.new(@default_format)
  @turn = turn
  @kifu = []
  @states_of_the_game = [] << Marshal.dump(self)
end

Instance Attribute Details

#default_formatObject

Returns the value of attribute default_format.



3
4
5
# File 'lib/shogi/game.rb', line 3

def default_format
  @default_format
end

#turnObject (readonly)

Returns the value of attribute turn.



4
5
6
# File 'lib/shogi/game.rb', line 4

def turn
  @turn
end

Instance Method Details

#at(num_of_moves) ⇒ Object



44
45
46
# File 'lib/shogi/game.rb', line 44

def at(num_of_moves)
  Marshal.load(@states_of_the_game[num_of_moves])
end

#kifuObject



31
32
33
# File 'lib/shogi/game.rb', line 31

def kifu
  @kifu.join("\n") << "\n"
end

#move(movement_lines, format = @default_format) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/shogi/game.rb', line 20

def move(movement_lines, format=@default_format)
  movement_lines.each_line do |movement|
    movement.chomp!
    @board.move(movement, format)
    @kifu << movement
    @turn = (@turn == "+") ? "-" : "+"
    @states_of_the_game << Marshal.dump(self)
  end
  self
end

#show(format = @default_format) ⇒ Object



35
36
37
# File 'lib/shogi/game.rb', line 35

def show(format=@default_format)
  $stdout.puts __send__("to_#{format}")
end

#show_all(format = @default_format) ⇒ Object



39
40
41
42
# File 'lib/shogi/game.rb', line 39

def show_all(format=@default_format)
  show
  $stdout.puts kifu
end

#to_csaObject



16
17
18
# File 'lib/shogi/game.rb', line 16

def to_csa
  @board.to_csa << turn << "\n"
end