-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.rb
83 lines (72 loc) · 1.99 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
require "./board.rb"
require "./player.rb"
class Game
attr_reader :chess_board
def play(board = Board.new)
@chess_board = board
players = [HumanPlayer.new(:white), HumanPlayer.new(:black)]
while true
players.each do |player|
new_pos = []
puts @chess_board.display
puts "#{player.color.to_s.capitalize} is in check!" if @chess_board.check?(player.color)
begin
curr_pos, new_pos = player.play_turn
end until @chess_board.move(curr_pos, new_pos, player.color)
if @chess_board.pawn_promote?
puts @chess_board.display
@chess_board.pawn_promote!(new_pos, promo_pieces(player.promotion_choice, player.color))
end
return end_of_game(player.color) if game_over?(player.color)
end
end
end
def promo_pieces(player_choice, color)
promo_pieces = {
"q" => Queen.new(color, @chess_board),
"r" => Rook.new(color, @chess_board),
"n" => Knight.new(color, @chess_board),
"b" => Bishop.new(color, @chess_board)
}
return promo_pieces[player_choice]
end
def game_over?(color)
@chess_board.checkmate?(Board::flip(color)) ||
@chess_board.stalemate?(Board::flip(color))
#add stalemate later
end
def end_of_game(color)
puts @chess_board.display
if @chess_board.check?(Board::flip(color))
puts "#{color.to_s.capitalize} has won. Congratulations!"
else
puts "Stalemate!"
end
end
end
Game.new.play
# b = Board.new
# puts b.display
#
# b.move([6,4],[5,4], :white)
# puts b.display
#
# b.move([1,5], [2,5], :black)
# puts b.display
#
# b.move([1,6],[3,6], :black)
# puts b.display
# #
# # b.move([2,6],[3,6])
# # puts b.display
# #
# b.move([7,3],[3,7], :white)
# puts b.display
# #
# #
# # puts "Is White in check? #{b.check?(:white)}"
# # puts "is White in checkmate? #{b.checkmate?(:white)}"
# #
# puts "Is black in check? #{b.check?(:black)}"
# puts "is black in checkmate? #{b.checkmate?(:black)}"
# Game.new.play(Board.test_pawn_promote)