-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.rb
43 lines (37 loc) · 899 Bytes
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require './Chess_board.rb'
class ChessGame
attr_accessor :board
def initialize
@board = ChessBoard.new
return nil
end
def game_loop
color = :w
board.show
until board.check_mated?(:b) || board.check_mated?(:w)
begin
start_pos, end_pos = get_user_input(color)
raise ArgumentError unless board[start_pos].color == color
rescue
board.show
puts "\tInvalid move!"
retry
end
board.move(start_pos,end_pos)
color = color == :w ? :b : :w
end
end
def validate_move #check format and make sure move is possible
return true
end
def get_user_input(color)
valid = false
until valid
puts "\t#{color}'s turn to move."
printf "\tFormat [0,0]-[0,1]: "
move = gets.chomp.split("-").map { |pos| eval(pos) } #DO NOT KEEP
valid = validate_move
end
move
end
end