From 412dc556d660127af8ad00a71124c5752a958077 Mon Sep 17 00:00:00 2001 From: Martin VanWinkle III Date: Sun, 2 Jan 2022 14:19:09 -0500 Subject: [PATCH] 31_Depth_Charge/ruby --- 31_Depth_Charge/ruby/.editorconfig | 69 +++++++++ 31_Depth_Charge/ruby/depthcharge.rb | 211 ++++++++++++++++++++++++++++ 2 files changed, 280 insertions(+) create mode 100644 31_Depth_Charge/ruby/.editorconfig create mode 100755 31_Depth_Charge/ruby/depthcharge.rb diff --git a/31_Depth_Charge/ruby/.editorconfig b/31_Depth_Charge/ruby/.editorconfig new file mode 100644 index 00000000..9d7e93d0 --- /dev/null +++ b/31_Depth_Charge/ruby/.editorconfig @@ -0,0 +1,69 @@ +# EditorConfig is awesome: https://EditorConfig.org +# .editorconfig + +# Please see doc/developer_notes.md +# If you find anything egregious or missing, please consider submitting a pull request +# to https://github.com/theias/ias_package_shell + + +# top-most EditorConfig file +root = true + +# Sensible defaults for everything +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true + +# JavaScript +[**.js] +indent_style = space +indent_size = 2 +insert_final_newline = true + +# Ruby +[**.rb] +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true + +# Python +[**.py] +charset = utf-8 +indent_style = space +indent_size = 4 +insert_final_newline = true + +# Perl +[**.pl] +charset = utf-8 +insert_final_newline = true +[**.pm] +charset = utf-8 +insert_final_newline = true + +# PHP +[**.php] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +trim_trailing_whitespace = true +insert_final_newline = true + +# Makefiles +[Makefile] +indent_style = tab + +[**.gmk] +indent_style = tab + +# Configuration Files +# Matches the exact files either package.json or .travis.yml +[{package.json,.travis.yml}] +indent_style = space +indent_size = 2 + +# Diff files +[*.{diff,patch}] +trim_trailing_whitespace = false diff --git a/31_Depth_Charge/ruby/depthcharge.rb b/31_Depth_Charge/ruby/depthcharge.rb new file mode 100755 index 00000000..5ba36ba6 --- /dev/null +++ b/31_Depth_Charge/ruby/depthcharge.rb @@ -0,0 +1,211 @@ +#!/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() + end + + # 420 PRINT "OK. HOPE YOU ENJOYED YOURSELF." : GOTO 600 + printf("OK. HOPE YOU ENJOYED YOURSELF.\n") + end + + def output_title + printf("--- DEPTH CHARGE ---\n") + printf("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n") + printf("\n") + end + + def get_input_y_or_n(message) + while true + print(message) + + value = gets.chomp + + if (value == 'Y' || value == 'y') + return true + elsif value == 'N' || value == 'n' + return false + end + + printf("PLEASE ENTER Y/y OR N/n...\n\n") + end + end + + def get_input_positive_integer(message) + + while true + print(message) + value = gets.chomp + if (value == 'd') + debug_game() + next + end + + the_input = Integer(value) rescue nil + + if the_input == nil || the_input < 0 + printf("PLEASE ENTER A POSITIVE NUMBER\n\n") + next + + end + + return the_input + end + end + + def get_search_area_dimension + # 20 INPUT "DIMENSION OF SEARCH AREA";G: PRINT + @search_area_dimension = get_input_positive_integer("DIMENSION OF SEARCH AREA: ") + # 30 N=INT(LOG(G)/LOG(2))+1 + + @num_tries = Integer( + Math.log(@search_area_dimension)/Math.log(2) + ) + + end + + def print_instructions + # 40 PRINT "YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER" + # 50 PRINT "AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR" + # 60 PRINT "MISSION IS TO DESTROY IT. YOU HAVE";N;"SHOTS." + # 70 PRINT "SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A" + # 80 PRINT "TRIO OF NUMBERS -- THE FIRST TWO ARE THE" + # 90 PRINT "SURFACE COORDINATES; THE THIRD IS THE DEPTH." + # 100 PRINT : PRINT "GOOD LUCK !": PRINT + 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. + +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 + +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") + end + + def setup_game + get_search_area_dimension() + setup_enemy() + end + + def setup_enemy + # 110 A=INT(G*RND(1)) : B=INT(G*RND(1)) : C=INT(G*RND(1)) + @enemy_x = rand(1..@search_area_dimension) + @enemy_y = rand(1..@search_area_dimension) + @enemy_z = rand(1..@search_area_dimension) + end + + def game_loop + # 120 FOR D=1 TO N : PRINT : PRINT "TRIAL #";D; : INPUT X,Y,Z + for @trial in 1..@num_tries do + output_game_status() + + @shot_x = get_input_positive_integer("X: ") + @shot_y = get_input_positive_integer("Y: ") + @shot_z = get_input_positive_integer("Z: ") + + # 130 IF ABS(X-A)+ABS(Y-B)+ABS(Z-C)=0 THEN 300 + if ( + (@enemy_x - @shot_x).abs \ + + (@enemy_y - @shot_y).abs \ + + (@enemy_z - @shot_z).abs \ + == 0 + ) + you_win() + return + else + # 140 GOSUB 500 : PRINT : NEXT D + missed_shot() + end + end + + printf("\n") + + you_lose() + + end + + def output_game_status + printf("YOU HAVE %d SHOTS REMAINING.\n", @num_tries - @trial + 1) + printf("TRIAL \#%d\n", @trial) + end + def you_win + printf("B O O M ! ! YOU FOUND IT IN %d TRIES!\n\n", @trial ) + end + def missed_shot + missed_directions = [] + + # 530 IF X>A THEN PRINT "EAST"; + # 540 IF X @enemy_x + missed_directions.push('TOO FAR EAST') + elsif @shot_x < @enemy_x + missed_directions.push('TOO FAR WEST') + end + + # 510 IF Y>B THEN PRINT "NORTH"; + # 520 IF Y @enemy_y + missed_directions.push('TOO FAR NORTH') + elsif @shot_y < @enemy_y + missed_directions.push('TOO FAR SOUTH') + end + + # 560 IF Z>C THEN PRINT " TOO LOW." + # 570 IF Z @enemy_z + missed_directions.push('TOO DEEP') + elsif @shot_z < @enemy_z + missed_directions.push('TOO SHALLOW') + end + + # 500 PRINT "SONAR REPORTS SHOT WAS "; + printf("SONAR REPORTS SHOT WAS: \n") + printf("%s\n", "\t" + missed_directions.join("\n\t")) + # 550 IF Y<>B OR X<>A THEN PRINT " AND"; + # 590 RETURN + end + + def you_lose + # You took too long! + printf("YOU HAVE BEEN TORPEDOED! ABANDON SHIP!\n") + printf("THE SUBMARINE WAS AT %d %d %d\n", @enemy_x, @enemy_y, @enemy_z) + + end + + def get_input_another_game + # 400 PRINT : PRINT: INPUT "ANOTHER GAME (Y OR N)";A$ + return get_input_y_or_n("ANOTHER GAME (Y OR N): ") + # 410 IF A$="Y" THEN 100 + end +end + +game = DepthCharge.new +game.run_game()