-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.rb
99 lines (79 loc) · 2.05 KB
/
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
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
class Game
attr_reader :board
def self.setup_game
new_board = ChessBoard.new.setup_board
puts "Please enter a name for player 1"
player1 = HumanPlayer.new(gets.strip, :white, new_board)
puts "Please enter a name for player 2"
player2 = HumanPlayer.new(gets.strip, :black, new_board)
Game.new(player1, player2, new_board)
end
def initialize(player1, player2, board)
@board = board
@player1 = player1
@player2 = player2
end
def turn(curr_player, next_player, turn_color)
puts "#{curr_player.name}'s turn:"
curr_player.play_turn(turn_color)
@board.render
puts "Check!" if @board.in_check?(next_player.color)
end
def play
puts " Let the games begin!"
puts
@board.render
loop do
break if @board.checkmate?(@player1.color)
turn(@player1, @player2, @player1.color)
break if @board.checkmate?(@player2.color)
turn(@player2, @player1, @player2.color)
end
@board.checkmate?(@player1.color) ? win(@player2) : win(@player1)
end
def win(player)
puts "Checkmate! #{player.name} wins!"
end
end
class HumanPlayer
USER_INPUT = {
"a" => 0,
"b" => 1,
"c" => 2,
"d" => 3,
"e" => 4,
"f" => 5,
"g" => 6,
"h" => 7,
"8" => 0,
"7" => 1,
"6" => 2,
"5" => 3,
"4" => 4,
"3" => 5,
"2" => 6,
"1" => 7
}
attr_reader :name, :color
def initialize(name, color, board)
@name = name
@color = color
@board = board
end
def play_turn(turn_color)
begin
puts "Please input your starting and ending positions, Ex: f2,f3"
user_input = gets.chomp.downcase
unless user_input =~ /^[a-h][1-8],*[a-h][1-8]$/
raise InvalidMove.new("Letter-number, letter-number, it's not hard!")
end
start_pos = [USER_INPUT[user_input[1]], USER_INPUT[user_input[0]]]
end_pos = [USER_INPUT[user_input[3]], USER_INPUT[user_input[2]]]
@board.move(start_pos, end_pos, turn_color)
rescue InvalidMove => e
puts "Invalid move: #{e.message}"
retry
end
puts
end
end