Skip to content

Commit 56e474f

Browse files
committed
Complete the main loop
1 parent 34a9b88 commit 56e474f

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

lib/game.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize
99
@chessboard = ChessBoard.new
1010

1111
end
12-
def play_with_two_humans
12+
def human_vs_human
1313
puts "Player-1 What is your name?"
1414
name1 = gets.chomp
1515
player_1 = Player.new(name1, "white")
@@ -26,6 +26,7 @@ def play_with_two_humans
2626

2727

2828
current_player = player_1
29+
do_break = false
2930
loop do
3031
@chessboard.display
3132
current_player.show_captured_piece
@@ -70,10 +71,12 @@ def play_with_two_humans
7071
the_attacked_piece = @chessboard.data.dig(array_indexes_of_new_positions[0], array_indexes_of_new_positions[1])
7172

7273
if the_attacked_piece.class.name == "King"
74+
do_break = true
7375
puts "CHECKMATE".light_green
7476
puts "Congratulations #{current_player.name}, you win!!!".light_green
75-
break 2
77+
break
7678
end
79+
7780

7881

7982
# Capture the enemy's piece
@@ -117,12 +120,13 @@ def play_with_two_humans
117120
redo
118121

119122
end
123+
break if do_break
120124
current_player == player_1 ? current_player = player_2 : current_player = player_1
121125
end
122126
end
123127

124128

125-
def play_between_human_and_ai
129+
def human_vs_ai
126130
puts "Player-1 What is your name?"
127131
name1 = gets.chomp
128132
player_1 = Player.new(name1, "white")
@@ -139,6 +143,7 @@ def play_between_human_and_ai
139143

140144

141145
current_player = player_1
146+
do_break = false
142147
loop do
143148
@chessboard.display
144149
current_player.show_captured_piece
@@ -191,11 +196,12 @@ def play_between_human_and_ai
191196
the_attacked_piece = @chessboard.data.dig(array_indexes_of_new_positions[0], array_indexes_of_new_positions[1])
192197

193198
if the_attacked_piece.class.name == "King"
199+
do_break = true
194200
puts "CHECKMATE".light_green
195201
puts "Congratulations #{current_player.name}, you win!!!".light_green
196-
break 2
202+
break
197203
end
198-
204+
199205

200206
# Capture the enemy's piece
201207
@chessboard.remove(the_attacked_piece)
@@ -238,12 +244,13 @@ def play_between_human_and_ai
238244
redo
239245

240246
end
247+
break if do_break
241248
current_player == player_1 ? current_player = player_2 : current_player = player_1
242249
end
243250
end
244251

245252

246-
def play_with_two_ai
253+
def ai_vs_ai
247254

248255
player_1 = AI.new("Siri", "white")
249256

@@ -257,6 +264,7 @@ def play_with_two_ai
257264

258265
puts "Let's start playing chess right now".light_blue
259266
current_player = player_1
267+
do_break = false
260268
loop do
261269
@chessboard.display
262270
current_player.show_captured_piece
@@ -270,7 +278,6 @@ def play_with_two_ai
270278

271279
@chessboard.display
272280
current_player.show_captured_piece
273-
do_break = false
274281
loop do
275282
new_position = current_player.choose_a_position_to_move(@chessboard.active_piece)
276283
sleep(1)

main.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,37 @@
1+
require_relative 'lib/game.rb'
2+
loop do
3+
puts "Please choose from the following: 1) Human vs Human
4+
2) Human vs AI
5+
3) AI vs AI
6+
4) Load an old game
7+
5) View a game from the collection library
8+
"
9+
answer = gets.chomp
10+
game = ChessGame.new
11+
if answer == "1"
12+
game.human_vs_human
13+
14+
elsif answer == "2"
15+
game.human_vs_ai
16+
elsif answer == "3"
17+
game.ai_vs_ai
18+
elsif answer == "4"
19+
20+
elsif answer == "5"
21+
22+
else
23+
puts "This option doesn't exist"
24+
redo
25+
end
26+
puts "Do you want to play again[y/n]"
27+
play_again = gets.chomp
28+
if play_again == "y"
29+
redo
30+
else
31+
puts "Thank you for playing!"
32+
puts "Don't hesitate to follow me on GitHub: odilsonjs"
33+
break
34+
end
35+
end
136

237

0 commit comments

Comments
 (0)