Class: Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

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

#boardObject (readonly)

Returns the value of attribute board.



5
6
7
# File 'lib/ari_chess/game.rb', line 5

def board
  @board
end

#current_playerObject

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_moveObject



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

Returns:

  • (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

Raises:



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

Raises:



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

#playObject



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_turnObject



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.message
  retry
end

#promptObject



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_playerObject



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

Raises:



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)
  error_message = nil

  piece = board[start_pos]
  if piece.nil?
    error_message = "Invalid move! Start position is empty."
  elsif current_player != board[start_pos].color
    error_message = "Invalid move! Chosen piece is not yours."
  end

  raise InvalidMoveError.new, error_message unless error_message.nil?

  nil
end