-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.rb
81 lines (66 loc) · 1.91 KB
/
chess.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require "./board.rb"
require "./pieces.rb"
class Chess
def initialize
@board = Board.new
end
def play
@board.setup_pieces
turn = [:white, :black]
erred = false
loop do
system('clear')
puts
puts @board
puts
if erred
puts "Invalid move!"
erred = false
end
puts "#{turn.first.to_s.capitalize}'s turn!"
puts "Castle possible? #{@board.castle_possible(turn.first)}"
move = prompt_user_for_move
if move != nil && @board.is_valid_move?(move[0], move[1], turn.first)
@board.make_move(move[0], move[1])
if upgrade_pawn = @board.get_pawn_in_back_row
type = prompt_upgrade(upgrade_pawn)
# board.upgrade(piece, type)
end
turn.reverse!
if @board.checkmate?(turn.first)
puts "Checkmate! #{turn.first.to_s.capitalize} loses!"
return
end
else
erred = true
end
end
end
def prompt_upgrade piece
puts "Your pawn is upgradeable. Please enter Q, K, B, or R"
new_piece = gets.chomp.upcase
new_piece = case new_piece
when "Q" then Queen.new(piece.color, piece.position)
when "K" then Knight.new(piece.color, piece.position)
when "B" then Bishop.new(piece.color, piece.position)
when "R" then Rook.new(piece.color, piece.position)
end
@board.board[piece.position[0]][piece.position[1]] = new_piece
end
def prompt_user_for_move
print "Please enter the next move (e.g. a2 a3): "
sanitize_input(gets)
end
def sanitize_input input
input = input.strip.upcase
return nil if input.empty?
start_pos, end_pos = *input.split
start_pos = start_pos.each_char.to_a
start_pos = [start_pos[1].to_i - 1, start_pos[0].ord - "A".ord]
end_pos = end_pos.each_char.to_a
end_pos = [end_pos[1].to_i - 1, end_pos[0].ord - "A".ord]
[start_pos, end_pos]
end
end
game = Chess.new
game.play