-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.rb
147 lines (113 loc) · 2.93 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# encoding: utf-8
require_relative './board.rb'
require_relative './exceptions.rb'
class ChessGame
attr_reader :board
HORIZONTAL_POSITIONS = {
:A => 0,
:B => 1,
:C => 2,
:D => 3,
:E => 4,
:F => 5,
:G => 6,
:H => 7
}
def initialize
@board = Board.new
@player = :w
end
def switch_player
@player = @player == :w ? :b : :w
end
def player_name
return "White Player" if @player == :w
return "Black Player" if @player == :b
end
def play
until @board.won?
render
puts "Current player: #{player_name}\n\n"
begin
puts "Check!\n" if @board.in_check?(@player)
user_input = request_input
break if user_input == "exit"
move_piece(user_input[0], user_input[1])
rescue ImproperBoardMove => e
puts e.message + ", #{player_name}"
retry
rescue MoveIntoCheckError => e
puts e.message + ", #{player_name}"
retry
end
switch_player
end
render
winning_player = @board.in_check_mate?(:w) ? "Black" : "White"
puts "#{winning_player} won the game!"
if user_input == "exit"
puts "\nGoodbye, quitter!"
end
end
def request_input
instruction = 'Enter your start and end position in the following format: "A2, A4"'
begin
puts
puts instruction
user_input = gets.chomp
return user_input if user_input == "exit"
unless user_input =~ /^[a-h][1-8],\s*[a-h][1-8]$/
raise InvalidInputError.new('Invalid Input Format')
end
user_input = user_input.split(/,\s*/)
start_coord = user_input[0]
end_coord = user_input[1]
rescue InvalidInputError => e
puts e.message
retry
end
[start_coord, end_coord]
end
def render
@board.render_with_color
end
def move_piece(start, finish)
start, finish = parse_coordinates(start,finish)
if piece_matches_player_color?(start)
@board.move(start, finish)
else
raise ImproperBoardMove.new("\n#{player_name} doesn't have a piece at that coordinate\n")
end
end
def parse_coordinates(start, finish)
start = start.split("")
finish = finish.split("")
start[0] = HORIZONTAL_POSITIONS[start[0].upcase.to_sym]
start[1] = start[1].to_i - 1
start[0], start[1] = start[1], start[0]
finish[0] = HORIZONTAL_POSITIONS[finish[0].upcase.to_sym]
finish[1] = finish[1].to_i - 1
finish[0], finish[1] = finish[1], finish[0]
[start, finish]
end
def piece_matches_player_color?(start)
piece = @board[start[0], start[1]]
return false if piece.nil?
piece.color == @player
end
end
c = ChessGame.new
c.play
=begin
c.move_piece("f2", "f3")
c.move_piece("e7", "e5")
c.move_piece("g2", "g4")
p c.board.in_check?(:w)
p c.board.in_check_mate?(:w)
c.move_piece("d8", "h4")
c.render
p c.board.in_check?(:w)
p c.board.in_check_mate?(:w)
c.render
=end
# p c.board.render_available_moves(c.board[1, 7])