Skip to content

Commit dfeb48d

Browse files
Merge pull request #930 from marquesrs/depth_charge-rust-implementation
Rust implementation for the game Depth Charge
2 parents 6106e03 + cef6574 commit dfeb48d

File tree

3 files changed

+395
-0
lines changed

3 files changed

+395
-0
lines changed

Diff for: 31_Depth_Charge/rust/Cargo.lock

+243
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: 31_Depth_Charge/rust/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rand = "0.9.0"

Diff for: 31_Depth_Charge/rust/src/main.rs

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/** DEPTH CHARGE GAME
2+
* https://github.com/marquesrs/basic-computer-games/blob/main/31_Depth_Charge/depthcharge.bas
3+
* Direct conversion from BASIC to Rust by Pablo Marques (marquesrs).
4+
* No additional features or improvements were added. As a faithful translation,
5+
* many of the code here are done in an unrecommended way by today's standards.
6+
* 03/03/25
7+
*/
8+
9+
use std::io::Write;
10+
use rand::Rng;
11+
12+
fn input(msg: &str) -> String {
13+
print!("{}", msg);
14+
let _ =std::io::stdout().flush().unwrap();
15+
let mut input = String::new();
16+
std::io::stdin().read_line(&mut input).unwrap();
17+
return input.trim().to_uppercase();
18+
}
19+
20+
fn main() {
21+
// 2 PRINT TAB(30);"DEPTH CHARGE"
22+
// 4 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
23+
// 6 PRINT: PRINT: PRINT
24+
print!("{}", format!("{}{}\n{}{}\n\n\n\n",
25+
" ".repeat(29),
26+
"DEPTH CHARGE",
27+
" ".repeat(14),
28+
"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
29+
));
30+
31+
// 20 INPUT "DIMENSION OF SEARCH AREA";G: PRINT
32+
let g = input("DIMENSION OF SEARCH AREA: ").parse::<i32>().unwrap();
33+
34+
// 30 N=INT(LOG(G)/LOG(2))+1
35+
let n = (f32::ln(g as f32) / f32::ln(2.0) + 1.0) as i32;
36+
37+
// 40 PRINT "YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER"
38+
// 50 PRINT "AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR"
39+
// 60 PRINT "MISSION IS TO DESTROY IT. YOU HAVE";N;"SHOTS."
40+
// 70 PRINT "SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A"
41+
// 80 PRINT "TRIO OF NUMBERS -- THE FIRST TWO ARE THE"
42+
// 90 PRINT "SURFACE COORDINATES; THE THIRD IS THE DEPTH."
43+
// 100 PRINT : PRINT "GOOD LUCK !": PRINT
44+
let a = (g * rand::rng().random_range(0..=1)) as i32;
45+
let b = (g * rand::rng().random_range(0..=1)) as i32;
46+
let c = (g * rand::rng().random_range(0..=1)) as i32;
47+
print!("{}", format!("{}{}{}{}{}{}{}{}{}{}{}",
48+
"\nYOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER\n",
49+
"AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR\n",
50+
"MISSION IS TO DESTROY IT. YOU HAVE ",
51+
n,
52+
" SHOTS.\n",
53+
"SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A\n",
54+
"TRIO OF NUMBERS -- THE FIRST TWO ARE THE\n",
55+
"SURFACE COORDINATES; THE THIRD IS THE DEPTH.\n",
56+
format!("EXAMPLE FOR DIMENSION {}: ", g),
57+
format!("{}, {}, {}", a, b, c),
58+
"\nGOOD LUCK !\n"
59+
));
60+
61+
'main: loop {
62+
// 110 A=INT(G*RND(1)) : B=INT(G*RND(1)) : C=INT(G*RND(1))
63+
let a = (g * rand::rng().random_range(0..=1)) as i32;
64+
let b = (g * rand::rng().random_range(0..=1)) as i32;
65+
let c = (g * rand::rng().random_range(0..=1)) as i32;
66+
// 120 FOR D=1 TO N : PRINT : PRINT "TRIAL #";D; : INPUT X,Y,Z
67+
let mut x;
68+
let mut y;
69+
let mut z;
70+
for d in 1..=n {
71+
print!("\nTRIAL #{}\n", d);
72+
x = input("X: ").parse::<i32>().unwrap();
73+
y = input("Y: ").parse::<i32>().unwrap();
74+
z = input("Z: ").parse::<i32>().unwrap();
75+
// 130 IF ABS(X-A)+ABS(Y-B)+ABS(Z-C)=0 THEN 300
76+
if i32::abs(x-a) + i32::abs(y-b) + i32::abs(z-c) == 0 {
77+
// 300 PRINT : PRINT "B O O M ! ! YOU FOUND IT IN";D;"TRIES!"
78+
print!("{}", format!("{}{}{}{}{}",
79+
"\n",
80+
"B O O M ! ! YOU FOUND IT IN ",
81+
d,
82+
" TRIES!\n",
83+
"\n"
84+
));
85+
// 400 PRINT : PRINT: INPUT "ANOTHER GAME (Y OR N)";A$
86+
if replay() { continue 'main; }
87+
else { break 'main; }
88+
}
89+
90+
// 140 GOSUB 500 : PRINT : NEXT D
91+
subroutine(x, y, z, a, b, c);
92+
println!();
93+
}
94+
// 200 PRINT : PRINT "YOU HAVE BEEN TORPEDOED! ABANDON SHIP!"
95+
// 210 PRINT "THE SUBMARINE WAS AT";A;",";B;",";C : GOTO 400
96+
print!("{}", format!("{}{}{}{}{}{}{}{}",
97+
"\nYOU HAVE BEEN TORPEDOED! ABANDON SHIP!\n",
98+
"THE SUBMARINE WAS AT ",
99+
a,
100+
",",
101+
b,
102+
",",
103+
c,
104+
"\n"
105+
));
106+
107+
replay();
108+
}
109+
// 600 END
110+
}
111+
112+
// 500 PRINT "SONAR REPORTS SHOT WAS ";
113+
// 510 IF Y>B THEN PRINT "NORTH";
114+
// 520 IF Y<B THEN PRINT "SOUTH";
115+
// 530 IF X>A THEN PRINT "EAST";
116+
// 540 IF X<A THEN PRINT "WEST";
117+
// 550 IF Y<>B OR X<>A THEN PRINT " AND";
118+
// 560 IF Z>C THEN PRINT " TOO LOW."
119+
// 570 IF Z<C THEN PRINT " TOO HIGH."
120+
// 580 IF Z=C THEN PRINT " DEPTH OK."
121+
// 590 RETURN
122+
fn subroutine(x: i32, y: i32, z:i32, a:i32, b:i32, c:i32) {
123+
print!("SONAR REPORTS SHOT WAS ");
124+
if y>b { print!("NORTH"); };
125+
if y<b { print!("SOUTH"); };
126+
if x>a { print!("EAST"); };
127+
if x<a { print!("WEST"); };
128+
if y!=b || x!=a { print!(" AND"); };
129+
if z>c { print!(" TOO LOW."); };
130+
if z<c { print!(" TOO HIGH."); };
131+
if z==c { print!(" DEPTH OK."); };
132+
}
133+
134+
fn replay() -> bool {
135+
let r = input("ANOTHER GAME (Y OR N): ");
136+
// 410 IF A$="Y" THEN 100
137+
if r == "Y" {
138+
return true;
139+
}
140+
else {
141+
// 420 PRINT "OK. HOPE YOU ENJOYED YOURSELF." : GOTO 600
142+
println!("OK. HOPE YOU ENJOYED YOURSELF.");
143+
return false;
144+
}
145+
}

0 commit comments

Comments
 (0)