Class: Shogi::Game
- Inherits:
-
Object
- Object
- Shogi::Game
- Defined in:
- lib/shogi/game.rb
Instance Attribute Summary collapse
-
#default_format ⇒ Object
Returns the value of attribute default_format.
-
#turn ⇒ Object
readonly
Returns the value of attribute turn.
Instance Method Summary collapse
- #at(num_of_moves) ⇒ Object
-
#initialize(format = :csa, turn = "+") ⇒ Game
constructor
A new instance of Game.
- #kifu ⇒ Object
- #move(movement_lines, format = @default_format) ⇒ Object
- #show(format = @default_format) ⇒ Object
- #show_all(format = @default_format) ⇒ Object
- #to_csa ⇒ Object
Constructor Details
#initialize(format = :csa, turn = "+") ⇒ Game
Returns a new instance of Game.
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_format ⇒ Object
Returns the value of attribute default_format.
3 4 5 |
# File 'lib/shogi/game.rb', line 3 def default_format @default_format end |
#turn ⇒ Object (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 |
#kifu ⇒ Object
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_csa ⇒ Object
15 16 17 |
# File 'lib/shogi/game.rb', line 15 def to_csa @board.to_csa << turn << "\n" end |