mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 07:40:50 -08:00
reverse game initial rust implementation
This commit is contained in:
243
73_Reverse/rust/Cargo.lock
generated
Normal file
243
73_Reverse/rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,243 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# 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.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
|
||||
[[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",
|
||||
]
|
||||
7
73_Reverse/rust/Cargo.toml
Normal file
7
73_Reverse/rust/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.9.0"
|
||||
190
73_Reverse/rust/src/main.rs
Normal file
190
73_Reverse/rust/src/main.rs
Normal file
@@ -0,0 +1,190 @@
|
||||
/** REVERSE GAME
|
||||
* https://github.com/marquesrs/basic-computer-games/blob/main/73_Reverse/reverse.bas
|
||||
* Direct conversion from BASIC to Rust by Pablo Marques (marquesrs).
|
||||
* No additional features or improvements were added. As a faithful translation,
|
||||
* many of the code here are done in an unrecommended way by today's standards.
|
||||
* 03/03/25
|
||||
*/
|
||||
|
||||
use rand::Rng;
|
||||
use std::io::Write;
|
||||
|
||||
fn input(msg: &str) -> String {
|
||||
print!("{}", msg);
|
||||
let _ =std::io::stdout().flush().unwrap();
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).unwrap();
|
||||
return input.trim().to_uppercase();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// 10 PRINT TAB(32);"REVERSE"
|
||||
// 20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
// 30 PRINT:PRINT:PRINT
|
||||
// 100 PRINT "REVERSE -- A GAME OF SKILL": PRINT
|
||||
print!("{}", format!("{}{}{}{}{}{}{}{}",
|
||||
" ".repeat(31),
|
||||
"REVERSE",
|
||||
"\n",
|
||||
" ".repeat(14),
|
||||
"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n",
|
||||
"\n\n\n",
|
||||
"REVERSE -- A GAME OF SKILL\n",
|
||||
"\n"
|
||||
));
|
||||
|
||||
// 130 DIM A(20)
|
||||
let mut a = Vec::with_capacity(20);
|
||||
|
||||
// 140 REM *** N=NUMBER OF NUMBERS
|
||||
// 150 N=9
|
||||
let n = 9;
|
||||
|
||||
// 160 PRINT "DO YOU WANT THE RULES";
|
||||
// 170 INPUT A$
|
||||
let opt = input("DO YOU WANT THE RULES (YES OR NO)? ");
|
||||
|
||||
|
||||
if opt == "YES" {
|
||||
// 190 GOSUB 710
|
||||
sub1(n);
|
||||
}
|
||||
// 180 IF A$="NO" THEN 210
|
||||
'c : loop {
|
||||
// 200 REM *** MAKE A RANDOM LIST A(1) TO A(N)
|
||||
// 210 A(1)=INT((N-1)*RND(1)+2)
|
||||
a[1-1] = ((n-1) as f32 * rand::rng().random_range(0.0..=1.0) + 2.0) as i32;
|
||||
|
||||
// 220 FOR K=2 TO N
|
||||
'a : for k in 2..=n {
|
||||
// 230 A(K)=INT(N*RND(1)+1)
|
||||
a[k-1] = (n as f32 * rand::rng().random_range(0.0..=1.0) + 1.0) as i32;
|
||||
// 240 FOR J=1 TO K-1
|
||||
for j in 1..k {
|
||||
if a[k-1] == a[j-1] {
|
||||
// 250 IF A(K)=A(J) THEN 230
|
||||
continue 'a;
|
||||
}
|
||||
// 260 NEXT J:
|
||||
}
|
||||
//NEXT K
|
||||
}
|
||||
// 280 REM *** PRINT ORIGINAL LIST AND START GAME
|
||||
// 290 PRINT: PRINT "HERE WE GO ... THE LIST IS:"
|
||||
print!("\nHERE WE GO ... THE LIST IS: ");
|
||||
|
||||
// 310 T=0
|
||||
let mut t = 0;
|
||||
|
||||
'b : loop {
|
||||
// 330 PRINT "HOW MANY SHALL I REVERSE";
|
||||
// 340 INPUT R
|
||||
let r = input("HOW MANY SHALL I REVERSE: ").parse::<usize>().unwrap();
|
||||
|
||||
// 350 IF R=0 THEN 520
|
||||
if r == 0 {
|
||||
// 520 PRINT
|
||||
// 530 PRINT "TRY AGAIN (YES OR NO)";
|
||||
// 540 INPUT A$
|
||||
let opt = input("\nTRY AGAIN (YES OR NO): ");
|
||||
// 550 IF A$="YES" THEN 210
|
||||
if opt == "YES" {
|
||||
continue 'c;
|
||||
}
|
||||
// 560 PRINT: PRINT "O.K. HOPE YOU HAD FUN!!":GOTO 999
|
||||
print!("\nO.K. HOPE YOU HAD FUN!!");
|
||||
break 'c;
|
||||
}
|
||||
// 360 IF R<=N THEN 390
|
||||
if r <= n {
|
||||
// 390 T=T+1
|
||||
t = t + 1;
|
||||
|
||||
// 400 REM *** REVERSE R NUMBERS AND PRINT NEW LIST
|
||||
// 410 FOR K=1 TO INT(R/2)
|
||||
for k in 1..=((r/2) as usize) {
|
||||
// 420 Z=A(K)
|
||||
let z = a[k-1];
|
||||
// 430 A(K)=A(R-K+1)
|
||||
a[k-1] = a[r-k];
|
||||
// 440 A(R-K+1)=Z
|
||||
a[r-k] = z;
|
||||
|
||||
// 450 NEXT K
|
||||
}
|
||||
// 460 GOSUB 610
|
||||
sub2(&a, n);
|
||||
|
||||
// 470 REM *** CHECK FOR A WIN
|
||||
// 480 FOR K=1 TO N
|
||||
for k in 1..=n {
|
||||
// 490 IF A(K)<>K THEN 330
|
||||
if a[k-1] != k as i32 {
|
||||
continue 'b;
|
||||
}
|
||||
// 500 NEXT K
|
||||
}
|
||||
// 510 PRINT "YOU WON IT IN";T;"MOVES!!!":PRINT
|
||||
print!("{}{}{}", "YOU WON IT IN ", t, " MOVES!!!\n\n");
|
||||
|
||||
}
|
||||
// 370 PRINT "OOPS! TOO MANY! I CAN REVERSE AT MOST";N:GOTO 330
|
||||
print!("OOPS! TOO MANY! I CAN REVERSE AT MOST {n}");
|
||||
|
||||
// 320 GOSUB 610
|
||||
sub2(&a, n);
|
||||
}
|
||||
}
|
||||
// 999 END
|
||||
}
|
||||
|
||||
// 600 REM *** SUBROUTINE TO PRINT LIST
|
||||
fn sub2(a: &Vec<i32>, n: usize) {
|
||||
// 610 PRINT:FOR K=1 TO N:PRINT A(K);:NEXT K
|
||||
// 650 PRINT:PRINT:RETURN
|
||||
for k in 1..=n {
|
||||
print!("{}", a[k-1]);
|
||||
}
|
||||
print!("\n\n");
|
||||
}
|
||||
|
||||
// 700 REM *** SUBROUTINE TO PRINT THE RULES
|
||||
fn sub1(n: usize) {
|
||||
// 710 PRINT:PRINT "THIS IS THE GAME OF 'REVERSE'. TO WIN, ALL YOU HAVE"
|
||||
// 720 PRINT "TO DO IS ARRANGE A LIST OF NUMBERS (1 THROUGH";N;")"
|
||||
// 730 PRINT "IN NUMERICAL ORDER FROM LEFT TO RIGHT. TO MOVE, YOU"
|
||||
// 740 PRINT "TELL ME HOW MANY NUMBERS (COUNTING FROM THE LEFT) TO"
|
||||
// 750 PRINT "REVERSE. FOR EXAMPLE, IF THE CURRENT LIST IS:"
|
||||
// 760 PRINT:PRINT "2 3 4 5 1 6 7 8 9"
|
||||
// 770 PRINT:PRINT "AND YOU REVERSE 4, THE RESULT WILL BE:"
|
||||
// 780 PRINT:PRINT "5 4 3 2 1 6 7 8 9"
|
||||
// 790 PRINT:PRINT "NOW IF YOU REVERSE 5, YOU WIN!"
|
||||
// 800 PRINT:PRINT "1 2 3 4 5 6 7 8 9":PRINT
|
||||
// 810 PRINT "NO DOUBT YOU WILL LIKE THIS GAME, BUT"
|
||||
// 820 PRINT "IF YOU WANT TO QUIT, REVERSE 0 (ZERO).":PRINT: RETURN
|
||||
|
||||
print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
|
||||
"\n",
|
||||
"THIS IS THE GAME OF 'REVERSE'. TO WIN, ALL YOU HAVE\n",
|
||||
"TO DO IS ARRANGE A LIST OF NUMBERS (1 THROUGH ",
|
||||
n,
|
||||
")\n",
|
||||
"IN NUMERICAL ORDER FROM LEFT TO RIGHT. TO MOVE, YOU\n",
|
||||
"TELL ME HOW MANY NUMBERS (COUNTING FROM THE LEFT) TO\n",
|
||||
"REVERSE. FOR EXAMPLE, IF THE CURRENT LIST IS:\n",
|
||||
"\n",
|
||||
"2 3 4 5 1 6 7 8 9\n",
|
||||
"\n",
|
||||
"AND YOU REVERSE 4, THE RESULT WILL BE:\n",
|
||||
"\n",
|
||||
"5 4 3 2 1 6 7 8 9\n",
|
||||
"\n",
|
||||
"NOW IF YOU REVERSE 5, YOU WIN!\n",
|
||||
"\n",
|
||||
"1 2 3 4 5 6 7 8 9\n",
|
||||
"\n",
|
||||
"NO DOUBT YOU WILL LIKE THIS GAME, BUT\n",
|
||||
"IF YOU WANT TO QUIT, REVERSE 0 (ZERO).\n",
|
||||
"\n",
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user