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
# 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 = []
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



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

def at(num_of_moves)
  Shogi::Game.new.move(@kifu[0, num_of_moves].join("\n") << "\n")
end

#kifuObject



29
30
31
# File 'lib/shogi/game.rb', line 29

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

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



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

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

#show(format = @default_format) ⇒ Object



33
34
35
# File 'lib/shogi/game.rb', line 33

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

#show_all(format = @default_format) ⇒ Object



37
38
39
40
# File 'lib/shogi/game.rb', line 37

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

#to_csaObject



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

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