mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Merge pull request #484 from robmiller/depth-charge-idiomatic-ruby
Depth Charge: use more idiomatic Ruby
This commit is contained in:
@@ -1,59 +1,60 @@
|
||||
#!/usr/bin/ruby
|
||||
|
||||
class DepthCharge
|
||||
|
||||
def run_game
|
||||
output_title()
|
||||
while true
|
||||
printf("----------\n")
|
||||
print_instructions()
|
||||
setup_game()
|
||||
printf("\n")
|
||||
game_loop()
|
||||
break if ! get_input_another_game()
|
||||
output_title
|
||||
|
||||
loop do
|
||||
puts "----------"
|
||||
print_instructions
|
||||
setup_game
|
||||
puts
|
||||
game_loop
|
||||
break unless get_input_another_game
|
||||
end
|
||||
|
||||
printf("OK. HOPE YOU ENJOYED YOURSELF.\n")
|
||||
puts "OK. HOPE YOU ENJOYED YOURSELF."
|
||||
end
|
||||
|
||||
def output_title
|
||||
printf("--- DEPTH CHARGE ---\n")
|
||||
printf("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
|
||||
printf("\n")
|
||||
puts "--- DEPTH CHARGE ---"
|
||||
puts "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
puts
|
||||
end
|
||||
|
||||
def get_input_y_or_n(message)
|
||||
while true
|
||||
print(message)
|
||||
loop do
|
||||
print message
|
||||
|
||||
value = gets.chomp
|
||||
|
||||
if (value == 'Y' || value == 'y')
|
||||
if value.downcase == "y"
|
||||
return true
|
||||
elsif value == 'N' || value == 'n'
|
||||
elsif value.downcase == "n"
|
||||
return false
|
||||
end
|
||||
|
||||
printf("PLEASE ENTER Y/y OR N/n...\n\n")
|
||||
puts "PLEASE ENTER Y/y OR N/n..."
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
def get_input_positive_integer(message)
|
||||
|
||||
while true
|
||||
print(message)
|
||||
loop do
|
||||
print message
|
||||
value = gets.chomp
|
||||
if (value == 'd')
|
||||
debug_game()
|
||||
|
||||
if value == "d"
|
||||
debug_game
|
||||
next
|
||||
end
|
||||
|
||||
the_input = Integer(value) rescue nil
|
||||
the_input = Integer(value) rescue 0
|
||||
|
||||
if the_input == nil || the_input < 1
|
||||
printf("PLEASE ENTER A POSITIVE NUMBER\n\n")
|
||||
if the_input < 1
|
||||
puts "PLEASE ENTER A POSITIVE NUMBER"
|
||||
puts
|
||||
next
|
||||
|
||||
end
|
||||
|
||||
return the_input
|
||||
@@ -61,42 +62,39 @@ class DepthCharge
|
||||
end
|
||||
|
||||
def print_instructions
|
||||
printf( <<~INSTRUCTIONS
|
||||
YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER
|
||||
AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR
|
||||
MISSION IS TO DESTROY IT.
|
||||
puts <<~INSTRUCTIONS
|
||||
YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER
|
||||
AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR
|
||||
MISSION IS TO DESTROY IT.
|
||||
|
||||
SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A
|
||||
TRIO OF NUMBERS -- THE FIRST TWO ARE THE
|
||||
SURFACE COORDINATES (X, Y):
|
||||
WEST < X < EAST
|
||||
SOUTH < Y < NORTH
|
||||
SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A
|
||||
TRIO OF NUMBERS -- THE FIRST TWO ARE THE
|
||||
SURFACE COORDINATES (X, Y):
|
||||
WEST < X < EAST
|
||||
SOUTH < Y < NORTH
|
||||
|
||||
THE THIRD IS THE DEPTH (Z):
|
||||
SHALLOW < Z < DEEP
|
||||
THE THIRD IS THE DEPTH (Z):
|
||||
SHALLOW < Z < DEEP
|
||||
|
||||
GOOD LUCK !
|
||||
GOOD LUCK !
|
||||
|
||||
INSTRUCTIONS
|
||||
)
|
||||
end
|
||||
|
||||
def debug_game
|
||||
printf("@enemy_x: %d\n", @enemy_x)
|
||||
printf("@enemy_y: %d\n", @enemy_y)
|
||||
printf("@enemy_z: %d\n", @enemy_z)
|
||||
printf("@num_tries: %d\n", @num_tries)
|
||||
printf("@trial: %d\n", @trial)
|
||||
printf("\n")
|
||||
puts "@enemy_x: %d" % @enemy_x
|
||||
puts "@enemy_y: %d" % @enemy_y
|
||||
puts "@enemy_z: %d" % @enemy_z
|
||||
puts "@num_tries: %d" % @num_tries
|
||||
puts "@trial: %d" % @trial
|
||||
puts
|
||||
end
|
||||
|
||||
def setup_game
|
||||
@search_area_dimension = get_input_positive_integer("DIMENSION OF SEARCH AREA: ")
|
||||
|
||||
@num_tries = Integer(
|
||||
Math.log(@search_area_dimension)/Math.log(2) + 1
|
||||
)
|
||||
setup_enemy()
|
||||
@num_tries = Integer(Math.log(@search_area_dimension) / Math.log(2) + 1)
|
||||
setup_enemy
|
||||
end
|
||||
|
||||
def setup_enemy
|
||||
@@ -113,32 +111,34 @@ GOOD LUCK !
|
||||
@shot_y = get_input_positive_integer("Y: ")
|
||||
@shot_z = get_input_positive_integer("Z: ")
|
||||
|
||||
if (
|
||||
(@enemy_x - @shot_x).abs \
|
||||
+ (@enemy_y - @shot_y).abs \
|
||||
+ (@enemy_z - @shot_z).abs \
|
||||
== 0
|
||||
)
|
||||
you_win()
|
||||
|
||||
distance = (@enemy_x - @shot_x).abs +
|
||||
(@enemy_y - @shot_y).abs +
|
||||
(@enemy_z - @shot_z).abs
|
||||
|
||||
if distance == 0
|
||||
you_win
|
||||
return
|
||||
else
|
||||
missed_shot()
|
||||
missed_shot
|
||||
end
|
||||
end
|
||||
|
||||
printf("\n")
|
||||
|
||||
you_lose()
|
||||
puts
|
||||
|
||||
you_lose
|
||||
end
|
||||
|
||||
def output_game_status
|
||||
printf("YOU HAVE %d SHOTS REMAINING.\n", @num_tries - @trial + 1)
|
||||
printf("TRIAL \#%d\n", @trial)
|
||||
puts "YOU HAVE %d SHOTS REMAINING." % @num_tries - @trial + 1
|
||||
puts "TRIAL \#%d" % @trial
|
||||
end
|
||||
|
||||
def you_win
|
||||
printf("\nB O O M ! ! YOU FOUND IT IN %d TRIES!\n\n", @trial )
|
||||
puts "\nB O O M ! ! YOU FOUND IT IN %d TRIES!" % @trial
|
||||
puts
|
||||
end
|
||||
|
||||
def missed_shot
|
||||
missed_directions = []
|
||||
|
||||
@@ -160,14 +160,13 @@ GOOD LUCK !
|
||||
missed_directions.push('TOO SHALLOW')
|
||||
end
|
||||
|
||||
printf("SONAR REPORTS SHOT WAS: \n")
|
||||
printf("%s\n", "\t" + missed_directions.join("\n\t"))
|
||||
puts "SONAR REPORTS SHOT WAS: "
|
||||
puts "\t#{missed_directions.join("\n\t")}"
|
||||
end
|
||||
|
||||
def you_lose
|
||||
printf("YOU HAVE BEEN TORPEDOED! ABANDON SHIP!\n")
|
||||
printf("THE SUBMARINE WAS AT %d %d %d\n", @enemy_x, @enemy_y, @enemy_z)
|
||||
|
||||
puts "YOU HAVE BEEN TORPEDOED! ABANDON SHIP!"
|
||||
puts "THE SUBMARINE WAS AT %d %d %d" % [@enemy_x, @enemy_y, @enemy_z]
|
||||
end
|
||||
|
||||
def get_input_another_game
|
||||
@@ -176,4 +175,4 @@ GOOD LUCK !
|
||||
end
|
||||
|
||||
game = DepthCharge.new
|
||||
game.run_game()
|
||||
game.run_game
|
||||
|
||||
Reference in New Issue
Block a user