mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-02-04 11:07:59 -08:00
depth charge game initial rust implementation
This commit is contained in:
236
31_Depth_Charge/rust/Cargo.lock
generated
236
31_Depth_Charge/rust/Cargo.lock
generated
@@ -2,6 +2,242 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.170"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
||||
dependencies = [
|
||||
"zerocopy 0.7.35",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"zerocopy 0.8.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.99"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e02e925281e18ffd9d640e234264753c43edc62d64b2d4cf898f1bc5e75f3fc2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.13.3+wasi-0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
|
||||
dependencies = [
|
||||
"wit-bindgen-rt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"zerocopy-derive 0.7.35",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcf01143b2dd5d134f11f545cf9f1431b13b749695cb33bcce051e7568f99478"
|
||||
dependencies = [
|
||||
"zerocopy-derive 0.8.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712c8386f4f4299382c9abee219bee7084f78fb939d88b6840fcc1320d5f6da2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
@@ -4,3 +4,4 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.9.0"
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::io::Write;
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
/** DEPTH CHARGE GAME
|
||||
* https://github.com/marquesrs/basic-computer-games/blob/main/31_Depth_Charge/depthcharge.bas
|
||||
* Direct conversion from BASIC to Rust by Pablo Marques (marquesrs).
|
||||
@@ -28,10 +30,10 @@ fn main() {
|
||||
));
|
||||
|
||||
// 20 INPUT "DIMENSION OF SEARCH AREA";G: PRINT
|
||||
let mut g = input("DIMENSION OF SEARCH AREA");
|
||||
let g = input("DIMENSION OF SEARCH AREA: ").parse::<i32>().unwrap();
|
||||
|
||||
// 30 N=INT(LOG(G)/LOG(2))+1
|
||||
let mut n = INT(LOG(G)/LOG(2)) + 1;
|
||||
let n = (f32::ln(g as f32) / f32::ln(2.0) + 1.0) as i32;
|
||||
|
||||
// 40 PRINT "YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER"
|
||||
// 50 PRINT "AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR"
|
||||
@@ -40,54 +42,50 @@ fn main() {
|
||||
// 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
|
||||
print!("{}", format!("{}{}{}{}{}{}{}{}{}",
|
||||
"YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER\n",
|
||||
"AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR\n",
|
||||
"MISSION IS TO DESTROY IT. YOU HAVE",
|
||||
let a = (g * rand::rng().random_range(0..=1)) as i32;
|
||||
let b = (g * rand::rng().random_range(0..=1)) as i32;
|
||||
let c = (g * rand::rng().random_range(0..=1)) as i32;
|
||||
print!("{}", format!("{}{}{}{}{}{}{}{}{}{}{}",
|
||||
"\nYOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER\n",
|
||||
"AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR\n",
|
||||
"MISSION IS TO DESTROY IT. YOU HAVE ",
|
||||
n,
|
||||
"SHOTS.\n",
|
||||
" SHOTS.\n",
|
||||
"SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A\n",
|
||||
"TRIO OF NUMBERS -- THE FIRST TWO ARE THE\n",
|
||||
"SURFACE COORDINATES; THE THIRD IS THE DEPTH.\n",
|
||||
format!("EXAMPLE FOR DIMENSION {}: ", g),
|
||||
format!("{}, {}, {}", a, b, c),
|
||||
"\nGOOD LUCK !\n"
|
||||
));
|
||||
|
||||
'main: loop {
|
||||
// 110 A=INT(G*RND(1)) : B=INT(G*RND(1)) : C=INT(G*RND(1))
|
||||
let mut a = INT(G*RND(1));
|
||||
let mut b = INT(G*RND(1));
|
||||
let mut c = INT(G*RND(1));
|
||||
|
||||
let a = (g * rand::rng().random_range(0..=1)) as i32;
|
||||
let b = (g * rand::rng().random_range(0..=1)) as i32;
|
||||
let c = (g * rand::rng().random_range(0..=1)) as i32;
|
||||
// 120 FOR D=1 TO N : PRINT : PRINT "TRIAL #";D; : INPUT X,Y,Z
|
||||
let mut x;
|
||||
let mut y;
|
||||
let mut z;
|
||||
for d in 1..=n {
|
||||
print!("\nTRIAL #{}", d);
|
||||
x = input("X: ");
|
||||
y = input("X: ");
|
||||
z = input("X: ");
|
||||
print!("\nTRIAL #{}\n", d);
|
||||
x = input("X: ").parse::<i32>().unwrap();
|
||||
y = input("Y: ").parse::<i32>().unwrap();
|
||||
z = input("Z: ").parse::<i32>().unwrap();
|
||||
// 130 IF ABS(X-A)+ABS(Y-B)+ABS(Z-C)=0 THEN 300
|
||||
if i32::abs(x-a) + i32::abs(y-b) + i32::abs(z-c) == 0 {
|
||||
// 300 PRINT : PRINT "B O O M ! ! YOU FOUND IT IN";D;"TRIES!"
|
||||
// 400 PRINT : PRINT: INPUT "ANOTHER GAME (Y OR N)";A$
|
||||
print!("{}", format!("{}{}{}{}{}",
|
||||
"\n",
|
||||
"B O O M ! ! YOU FOUND IT IN",
|
||||
"B O O M ! ! YOU FOUND IT IN ",
|
||||
d,
|
||||
"TRIES!\n",
|
||||
"\n\n"
|
||||
" TRIES!\n",
|
||||
"\n"
|
||||
));
|
||||
let a = input("ANOTHER GAME (Y OR N): ");
|
||||
// 410 IF A$="Y" THEN 100
|
||||
if a == "Y" {
|
||||
continue 'main;
|
||||
}
|
||||
else {
|
||||
// 420 PRINT "OK. HOPE YOU ENJOYED YOURSELF." : GOTO 600
|
||||
print!("OK. HOPE YOU ENJOYED YOURSELF.");
|
||||
break 'main;
|
||||
}
|
||||
// 400 PRINT : PRINT: INPUT "ANOTHER GAME (Y OR N)";A$
|
||||
if replay() { continue 'main; }
|
||||
else { break 'main; }
|
||||
}
|
||||
|
||||
// 140 GOSUB 500 : PRINT : NEXT D
|
||||
@@ -96,15 +94,18 @@ fn main() {
|
||||
}
|
||||
// 200 PRINT : PRINT "YOU HAVE BEEN TORPEDOED! ABANDON SHIP!"
|
||||
// 210 PRINT "THE SUBMARINE WAS AT";A;",";B;",";C : GOTO 400
|
||||
print!("{}", format!("{}{}{}{}{}{}{}",
|
||||
print!("{}", format!("{}{}{}{}{}{}{}{}",
|
||||
"\nYOU HAVE BEEN TORPEDOED! ABANDON SHIP!\n",
|
||||
"THE SUBMARINE WAS AT ",
|
||||
a,
|
||||
",",
|
||||
b,
|
||||
",",
|
||||
c
|
||||
c,
|
||||
"\n"
|
||||
));
|
||||
|
||||
replay();
|
||||
}
|
||||
// 600 END
|
||||
}
|
||||
@@ -130,3 +131,16 @@ fn subroutine(x: i32, y: i32, z:i32, a:i32, b:i32, c:i32) {
|
||||
if z<c { print!(" TOO HIGH."); };
|
||||
if z==c { print!(" DEPTH OK."); };
|
||||
}
|
||||
|
||||
fn replay() -> bool {
|
||||
let r = input("ANOTHER GAME (Y OR N): ");
|
||||
// 410 IF A$="Y" THEN 100
|
||||
if r == "Y" {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// 420 PRINT "OK. HOPE YOU ENJOYED YOURSELF." : GOTO 600
|
||||
println!("OK. HOPE YOU ENJOYED YOURSELF.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user