Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/ari_chess/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#current_player ⇒ Object
Returns the value of attribute current_player.
Instance Method Summary collapse
- #get_move ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #move_in_range?(row, col) ⇒ Boolean
- #parse(move) ⇒ Object
- #parse_positions(input) ⇒ Object
- #play ⇒ Object
- #play_turn ⇒ Object
- #prompt ⇒ Object
- #switch_player ⇒ Object
- #to_i(row) ⇒ Object
- #validate_move(start_pos) ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 |
# File 'lib/ari_chess/game.rb', line 8 def initialize @board = Board.new @board.setup_pieces @current_player = :W end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
5 6 7 |
# File 'lib/ari_chess/game.rb', line 5 def board @board end |
#current_player ⇒ Object
Returns the value of attribute current_player.
6 7 8 |
# File 'lib/ari_chess/game.rb', line 6 def current_player @current_player end |
Instance Method Details
#get_move ⇒ Object
40 41 42 43 |
# File 'lib/ari_chess/game.rb', line 40 def get_move input = prompt parse_positions(input) end |
#move_in_range?(row, col) ⇒ Boolean
95 96 97 |
# File 'lib/ari_chess/game.rb', line 95 def move_in_range?(row, col) col.between?("a", "h") && row.between?(1, 8) end |
#parse(move) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ari_chess/game.rb', line 77 def parse(move) raise InvalidMoveError if move.length != 2 col, row = move.split("") row = to_i(row) unless move_in_range?(row, col) raise InvalidMoveError.new, "Invalid move! Not a valid board position." end [(-1 * row) + 8, col.ord - 97] end |
#parse_positions(input) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/ari_chess/game.rb', line 45 def parse_positions(input) matched_input = /\s*(\w\d),*\s*(\w\d)\s*/.match(input) raise InvalidMoveError if matched_input.nil? positions = matched_input.captures parsed_positions = positions.map { |position| parse(position) } validate_move(parsed_positions.first) parsed_positions end |
#play ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ari_chess/game.rb', line 14 def play until board.checkmate?(current_player) system("clear") board.render play_turn switch_player end system("clear") board.render switch_player puts "Game over! #{current_player} wins!" end |
#play_turn ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/ari_chess/game.rb', line 32 def play_turn board.move(*get_move) rescue InvalidMoveError => e print "\n" puts e. retry end |
#prompt ⇒ Object
70 71 72 73 74 75 |
# File 'lib/ari_chess/game.rb', line 70 def prompt print "\n" puts "#{current_player}'s turn. Enter a move (e.g. 'a2 a3'):" print "> " input = gets.chomp.downcase end |
#switch_player ⇒ Object
28 29 30 |
# File 'lib/ari_chess/game.rb', line 28 def switch_player self.current_player = (current_player == :W ? :B : :W) end |
#to_i(row) ⇒ Object
89 90 91 92 93 |
# File 'lib/ari_chess/game.rb', line 89 def to_i(row) row = Integer(row) rescue ArgumentError raise InvalidMoveError end |
#validate_move(start_pos) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ari_chess/game.rb', line 55 def validate_move(start_pos) = nil piece = board[start_pos] if piece.nil? = "Invalid move! Start position is empty." elsif current_player != board[start_pos].color = "Invalid move! Chosen piece is not yours." end raise InvalidMoveError.new, unless .nil? nil end |