From ac9a5bf9d9f27decc3538bf319a07a23b520a88e Mon Sep 17 00:00:00 2001 From: jay-0331 Date: Mon, 11 Apr 2022 13:24:16 +0530 Subject: [PATCH 1/5] Rust Translation for Dice --- 33_Dice/rust/Cargo.toml | 9 +++++ 33_Dice/rust/README.md | 3 ++ 33_Dice/rust/src/main.rs | 85 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 33_Dice/rust/Cargo.toml create mode 100644 33_Dice/rust/README.md create mode 100644 33_Dice/rust/src/main.rs diff --git a/33_Dice/rust/Cargo.toml b/33_Dice/rust/Cargo.toml new file mode 100644 index 00000000..3b1d02f5 --- /dev/null +++ b/33_Dice/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" diff --git a/33_Dice/rust/README.md b/33_Dice/rust/README.md new file mode 100644 index 00000000..fc6468b9 --- /dev/null +++ b/33_Dice/rust/README.md @@ -0,0 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Rust](https://www.rust-lang.org/) diff --git a/33_Dice/rust/src/main.rs b/33_Dice/rust/src/main.rs new file mode 100644 index 00000000..8c7e5885 --- /dev/null +++ b/33_Dice/rust/src/main.rs @@ -0,0 +1,85 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Dice +// +// From: BASIC Computer Games (1978) +// Edited by David H. Ahl +// +// "Not exactly a game, this program simulates rolling +// a pair of dice a large number of times and prints out +// the frequency distribution. You simply input the +// number of rolls. It is interesting to see how many +// rolls are necessary to approach the theoretical +// distribution: +// +// 2 1/36 2.7777...% +// 3 2/36 5.5555...% +// 4 3/36 8.3333...% +// etc. +// +// "Daniel Freidus wrote this program while in the +// seventh grade at Harrison Jr-Sr High School, +// Harrison, New York." +// +// Rust Port by Jay, 2022 +// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +use rand::Rng; +use std::io::{self, Write}; + +fn main() { + let mut frequency: [i32; 13] = [0; 13]; + println!( + "{: >38}\n{: >57}\n\n", + "DICE", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" + ); + // DANNY FREIDUS + println!("THIS PROGRAM SIMULATES THE ROLLING OF A"); + println!("PAIR OF DICE."); + println!("YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO"); + println!("'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE"); + println!("A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000."); + let mut playing = true; + while playing { + let n = match readinput(&"HOW MANY ROLLS").trim().parse::() { + Ok(num) => num, + Err(_) => { + println!("PLEASE ENTER A NUMBER"); + continue; + } + }; + // Dice Rolled n times + for _i in 0..n { + let die_1 = rand::thread_rng().gen_range(1..=6); + let die_2 = rand::thread_rng().gen_range(1..=6); + let total = die_1 + die_2; + frequency[total] += 1; + } + + // Results tabel + println!("\nTOTAL SPOTS NUMBER OF TIMES"); + for i in 2..13 { + println!("{:^4}\t\t{}", i, frequency[i]); + } + + // Continue the game + let reply = readinput("TRY AGAIN").to_ascii_uppercase(); + if reply.starts_with("Y") || reply.eq("YES") { + frequency = [0; 13]; + } else { + playing = false; + } + } +} + +// function for getting input on same line +fn readinput(str: &str) -> String { + print!("\n{}? ", str); + let mut input = String::new(); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut input) + .expect("Failed to get Input"); + input +} From d0c46689e118a6f4592fee87c3498a9dbd582a79 Mon Sep 17 00:00:00 2001 From: jay-0331 Date: Wed, 13 Apr 2022 14:21:35 +0530 Subject: [PATCH 2/5] Removed All HTML in Rust Folder --- 82_Stars/rust/Cargo.toml | 2834 +------------------------------- 82_Stars/rust/README.md | 2693 +------------------------------ 82_Stars/rust/src/main.rs | 3223 +------------------------------------ 3 files changed, 94 insertions(+), 8656 deletions(-) diff --git a/82_Stars/rust/Cargo.toml b/82_Stars/rust/Cargo.toml index 439dcb0e..8d4e044f 100644 --- a/82_Stars/rust/Cargo.toml +++ b/82_Stars/rust/Cargo.toml @@ -1,2829 +1,9 @@ +[package] +name = "stars" +version = "0.1.0" +edition = "2021" +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - basic-computer-games/Cargo.toml at main · coding-horror/basic-computer-games - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content - - - - - - - - - - - - - - -
- -
- - - - - - - -
- - - -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - coding-horror  /   - basic-computer-games  /   - -
-
- - - -
- - -
- - -
- - - -
-
- Tip: - Type # to search pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search discussions -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type ! to search projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search teams -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search people and organizations -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type > to activate command mode -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Go to your accessibility settings to change your keyboard shortcuts -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type author:@me to search your content -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:pr to filter to pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:issue to filter to issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:project to filter to projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:open to filter to open content -
-
- Type ? for help and tips -
-
-
- -
- -
-
- We’ve encountered an error and some results aren't available at this time. Type a new search or try again later. -
-
- - No results matched your search - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - Search for issues and pull requests - - # - - - - Search for issues, pull requests, discussions, and projects - - # - - - - Search for organizations, repositories, and users - - @ - - - - Search for projects - - ! - - - - Search for files - - / - - - - Activate command mode - - > - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Filter to pull requests - - # is:pr - - - - Filter to issues - - # is:issue - - - - Filter to discussions - - # is:discussion - - - - Filter to projects - - # is:project - - - - Filter to open issues, pull requests, and discussions - - # is:open - - - - - - - - - - - - - - - - -
-
-
- -
- - - - - - - - - - -
- - - - -
-
-
- - - - - - - - - -
- -
- -
-

- - - / - - basic-computer-games - - - Public -

- -
- -
    - - - -
  • - -
    - - - - - - - Watch - - - 126 - - - - -
    -
    -

    Notifications

    - -
    - -
    -
    - - - - - - - - -
    - - -
    - - - - - Get push notifications on iOS or Android. - -
    -
    -
    -
    - - - - -
    -
    -
    - - - -
  • - -
  • -
    -
    - Fork - 1k - - - -
    - -

    Fork basic-computer-games

    -
    - -
    - - - - - -

    If this dialog fails to load, you can visit the fork page directly.

    -
    -
    - -
    -
    -
    - -
  • - -
  • - - -
    -
    - - -
    -
    - -
    -
    - - - - -
    - -
    -
    - - - - - - - -
    - -
    -
    -
    -
    -
    -
  • - - - -
- -
- -
-
- - - - -
- - - -
- Open in github.dev - Open in a new github.dev tab - - - - - - -
- - -
- - - - - - -Permalink - -
- -
-
- - - main - - - - -
-
-
- Switch branches/tags - -
- - - -
- -
- -
- - -
- -
- - - - - - - - - - - - - - - - -
- - -
-
-
-
- -
- -
- - - Go to file - - -
- - - - - -
-
-
- - - - - - - - - -
- -
-
- - @Jay-0331 - -
- - - - - - -
-
- - Latest commit - 34baeec - Apr 1, 2022 - - - - - - History - - -
-
- -
- -
-
- - - 1 - - contributor - - -
- -

- Users who have contributed to this file -

-
- - - - - - -
-
-
-
- - - - - - - - - - - - -
- -
- - -
- - 9 lines (7 sloc) - - 188 Bytes -
- -
- - - - -
- - - - - - - - - - - - - - - -
- -
-
- -
-
- -
- -
-
- - - -
- - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[package]
name = "stars"
version = "0.1.0"
edition = "2021"
-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
-
[dependencies]
rand = "0.8.3"
-
- - - -
- -
- - - - -
- - -
- - -
-
- - -
- -
- - -
- -
-
- -
- - - - - - - - - - - - - - - - - - - - - - +[dependencies] +rand = "0.8.5" diff --git a/82_Stars/rust/README.md b/82_Stars/rust/README.md index 334493b2..fc6468b9 100644 --- a/82_Stars/rust/README.md +++ b/82_Stars/rust/README.md @@ -1,2692 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - basic-computer-games/README.md at main · coding-horror/basic-computer-games - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content - - - - - - - - - - - - - - -
- -
- - - - - - - -
- - - -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - coding-horror  /   - basic-computer-games  /   - -
-
- - - -
- - -
- - -
- - - -
-
- Tip: - Type # to search pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search discussions -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type ! to search projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search teams -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search people and organizations -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type > to activate command mode -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Go to your accessibility settings to change your keyboard shortcuts -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type author:@me to search your content -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:pr to filter to pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:issue to filter to issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:project to filter to projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:open to filter to open content -
-
- Type ? for help and tips -
-
-
- -
- -
-
- We’ve encountered an error and some results aren't available at this time. Type a new search or try again later. -
-
- - No results matched your search - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - Search for issues and pull requests - - # - - - - Search for issues, pull requests, discussions, and projects - - # - - - - Search for organizations, repositories, and users - - @ - - - - Search for projects - - ! - - - - Search for files - - / - - - - Activate command mode - - > - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Filter to pull requests - - # is:pr - - - - Filter to issues - - # is:issue - - - - Filter to discussions - - # is:discussion - - - - Filter to projects - - # is:project - - - - Filter to open issues, pull requests, and discussions - - # is:open - - - - - - - - - - - - - - - - -
-
-
- -
- - - - - - - - - - -
- - - - -
-
-
- - - - - - - - - -
- -
- -
-

- - - / - - basic-computer-games - - - Public -

- -
- -
    - - - -
  • - -
    - - - - - - - Watch - - - 126 - - - - -
    -
    -

    Notifications

    - -
    - -
    -
    - - - - - - - - -
    - - -
    - - - - - Get push notifications on iOS or Android. - -
    -
    -
    -
    - - - - -
    -
    -
    - - - -
  • - -
  • -
    -
    - Fork - 1k - - - -
    - -

    Fork basic-computer-games

    -
    - -
    - - - - - -

    If this dialog fails to load, you can visit the fork page directly.

    -
    -
    - -
    -
    -
    - -
  • - -
  • - - -
    -
    - - -
    -
    - -
    -
    - - - - -
    - -
    -
    - - - - - - - -
    - -
    -
    -
    -
    -
    -
  • - - - -
- -
- -
-
- - - - -
- - - -
- Open in github.dev - Open in a new github.dev tab - - - - - - -
- - -
- - - - - - -Permalink - -
- -
-
- - - main - - - - -
-
-
- Switch branches/tags - -
- - - -
- -
- -
- - -
- -
- - - - - - - - - - - - - - - - -
- - -
-
-
-
- -
- -
- - - Go to file - - -
- - - - - -
-
-
- - - - - - - - - -
- -
-
-
 
-
- -
-
 
- Cannot retrieve contributors at this time -
-
- - - - - - - - - - - - -
- -
- - -
- - 3 lines (2 sloc) - - 139 Bytes -
- -
- - - - - -
- - - - - - - - - - - - - - - -
- -
-
- -
-
- -
- -
-
- - -
- -
- -
- - - - -
- - -
- - -
-
- - - - -
- -
- - -
- -
-
- -
- - - - - - - - - - - - - - - - - - - - - - +Conversion to [Rust](https://www.rust-lang.org/) diff --git a/82_Stars/rust/src/main.rs b/82_Stars/rust/src/main.rs index 9cf285da..429646b3 100644 --- a/82_Stars/rust/src/main.rs +++ b/82_Stars/rust/src/main.rs @@ -1,3138 +1,85 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - basic-computer-games/main.rs at main · coding-horror/basic-computer-games - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content - - - - - - - - - - - - - - -
- -
- - - - - - - -
- - - -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - coding-horror  /   - basic-computer-games  /   - -
-
- - - -
- - -
- - -
- - - -
-
- Tip: - Type # to search pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search discussions -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type ! to search projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search teams -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search people and organizations -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type > to activate command mode -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Go to your accessibility settings to change your keyboard shortcuts -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type author:@me to search your content -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:pr to filter to pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:issue to filter to issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:project to filter to projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:open to filter to open content -
-
- Type ? for help and tips -
-
-
- -
- -
-
- We’ve encountered an error and some results aren't available at this time. Type a new search or try again later. -
-
- - No results matched your search - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - Search for issues and pull requests - - # - - - - Search for issues, pull requests, discussions, and projects - - # - - - - Search for organizations, repositories, and users - - @ - - - - Search for projects - - ! - - - - Search for files - - / - - - - Activate command mode - - > - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Filter to pull requests - - # is:pr - - - - Filter to issues - - # is:issue - - - - Filter to discussions - - # is:discussion - - - - Filter to projects - - # is:project - - - - Filter to open issues, pull requests, and discussions - - # is:open - - - - - - - - - - - - - - - - -
-
-
- -
- - - - - - - - - - -
- - - - -
-
-
- - - - - - - - - -
- -
- -
-

- - - / - - basic-computer-games - - - Public -

- -
- -
    - - - -
  • - -
    - - - - - - - Watch - - - 126 - - - - -
    -
    -

    Notifications

    - -
    - -
    -
    - - - - - - - - -
    - - -
    - - - - - Get push notifications on iOS or Android. - -
    -
    -
    -
    - - - - -
    -
    -
    - - - -
  • - -
  • -
    -
    - Fork - 1k - - - -
    - -

    Fork basic-computer-games

    -
    - -
    - - - - - -

    If this dialog fails to load, you can visit the fork page directly.

    -
    -
    - -
    -
    -
    - -
  • - -
  • - - -
    -
    - - -
    -
    - -
    -
    - - - - -
    - -
    -
    - - - - - - - -
    - -
    -
    -
    -
    -
    -
  • - - - -
- -
- -
-
- - - - -
- - - -
- Open in github.dev - Open in a new github.dev tab - - - - - - -
- - -
- - - - - - -Permalink - -
- -
-
- - - main - - - - -
-
-
- Switch branches/tags - -
- - - -
- -
- -
- - -
- -
- - - - - - - - - - - - - - - - -
- - -
-
-
-
- -
- -
- - - Go to file - - -
- - - - - -
-
-
- - - - - - - - - -
- -
-
- - @Jay-0331 - -
- - - - - - -
-
- - Latest commit - 34baeec - Apr 1, 2022 - - - - - - History - - -
-
- -
- -
-
- - - 1 - - contributor - - -
- -

- Users who have contributed to this file -

-
- - - - - - -
-
-
-
- - - - - - - - - - - - -
- -
- - -
- - 85 lines (78 sloc) - - 2.63 KB -
- -
- - - - -
- - - - - - - - - - - - - - - -
- -
-
- -
-
- -
- -
-
- - - -
- - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
use rand::Rng;
use std::io;
-
fn main() {
println!(
"{: >39}\n{: >57}\n\n\n",
"STARS", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
);
// STARS - PEOPLE'S COMPUTER CENTER, MENLO PARK, CA
// A IS LIMIT ON NUMBER, M IS NUMBER OF GUESSES
let a: u32 = 101;
let m: u32 = 7;
let mut need_instrut = String::new();
-
println!("DO YOU WANT INSTRUCTIONS?");
io::stdin()
.read_line(&mut need_instrut)
.expect("Failed to get input");
-
if need_instrut[..1].to_ascii_lowercase().eq("y") {
println!("I AM THINKING OF A WHOLE NUMBER FROM 1 TO {}", a - 1);
println!("TRY TO GUESS MY NUMBER. AFTER YOU GUESS, I");
println!("WILL TYPE ONE OR MORE STARS (*). THE MORE");
println!("STARS I TYPE, THE CLOSER YOU ARE TO MY NUMBER.");
println!("ONE STAR (*) MEANS FAR AWAY, SEVEN STARS (*******)");
println!("MEANS REALLY CLOSE! YOU GET {} GUESSES.\n\n", m);
}
-
loop {
println!("\nOK, I AM THINKING OF A NUMBER, START GUESSING.\n");
let rand_number: i32 = rand::thread_rng().gen_range(1..a) as i32; // generates a random number between 1 and 100
-
// GUESSING BEGINS, HUMAN GETS M GUESSES
for i in 0..m {
let mut guess = String::new();
println!("YOUR GUESS?");
io::stdin()
.read_line(&mut guess)
.expect("Failed to get input");
let guess: i32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("PLEASE ENTER A NUMBER VALUE.\n");
continue;
}
};
if guess == rand_number {
print!("");
for _i in 0..50 {
print!("*");
}
println!("!!!");
println!("YOU GOT IT IN {} GUESSES!!! LET'S PLAY AGAIN...\n", i + 1);
break;
} else {
match_guess(rand_number - guess);
}
-
if i == 6 {
println!(
"SORRY, THAT'S {} GUESSES. THE NUMBER WAS {}",
m, rand_number
);
}
}
}
}
-
fn match_guess(diff: i32) {
if diff.abs() >= 64 {
println!("*\n");
} else if diff.abs() >= 32 {
println!("**\n");
} else if diff.abs() >= 16 {
println!("***\n");
} else if diff.abs() >= 8 {
println!("****\n");
} else if diff.abs() >= 4 {
println!("*****\n");
} else if diff.abs() >= 2 {
println!("******\n");
} else {
println!("*******\n");
}
}
-
- - - -
- -
- - - - -
- - -
- - -
-
- - -
- -
- - -
- -
-
- -
- - - - - - - - - - - - - - - - - - - - - - +use rand::Rng; +use std::io; + +fn main() { + println!( + "{: >39}\n{: >57}\n\n\n", + "STARS", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" + ); + // STARS - PEOPLE'S COMPUTER CENTER, MENLO PARK, CA + // A IS LIMIT ON NUMBER, M IS NUMBER OF GUESSES + let a: u32 = 101; + let m: u32 = 7; + let mut need_instrut = String::new(); + + println!("DO YOU WANT INSTRUCTIONS?"); + io::stdin() + .read_line(&mut need_instrut) + .expect("Failed to get input"); + + if need_instrut[..1].to_ascii_lowercase().eq("y") { + println!("I AM THINKING OF A WHOLE NUMBER FROM 1 TO {}", a - 1); + println!("TRY TO GUESS MY NUMBER. AFTER YOU GUESS, I"); + println!("WILL TYPE ONE OR MORE STARS (*). THE MORE"); + println!("STARS I TYPE, THE CLOSER YOU ARE TO MY NUMBER."); + println!("ONE STAR (*) MEANS FAR AWAY, SEVEN STARS (*******)"); + println!("MEANS REALLY CLOSE! YOU GET {} GUESSES.\n\n", m); + } + + loop { + println!("\nOK, I AM THINKING OF A NUMBER, START GUESSING.\n"); + let rand_number: i32 = rand::thread_rng().gen_range(1..a) as i32; // generates a random number between 1 and 100 + + // GUESSING BEGINS, HUMAN GETS M GUESSES + for i in 0..m { + let mut guess = String::new(); + println!("YOUR GUESS?"); + io::stdin() + .read_line(&mut guess) + .expect("Failed to get input"); + let guess: i32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => { + println!("PLEASE ENTER A NUMBER VALUE.\n"); + continue; + } + }; + if guess == rand_number { + print!(""); + for _i in 0..50 { + print!("*"); + } + println!("!!!"); + println!("YOU GOT IT IN {} GUESSES!!! LET'S PLAY AGAIN...\n", i + 1); + break; + } else { + match_guess(rand_number - guess); + } + + if i == 6 { + println!( + "SORRY, THAT'S {} GUESSES. THE NUMBER WAS {}", + m, rand_number + ); + } + } + } +} + +fn match_guess(diff: i32) { + if diff.abs() >= 64 { + println!("*\n"); + } else if diff.abs() >= 32 { + println!("**\n"); + } else if diff.abs() >= 16 { + println!("***\n"); + } else if diff.abs() >= 8 { + println!("****\n"); + } else if diff.abs() >= 4 { + println!("*****\n"); + } else if diff.abs() >= 2 { + println!("******\n"); + } else { + println!("*******\n"); + } +} From 882117758dde6347cd9d65da37e2e978a454bb73 Mon Sep 17 00:00:00 2001 From: Joe Nellis Date: Wed, 13 Apr 2022 02:57:15 -0700 Subject: [PATCH 3/5] Bagels in Lua. --- 05_Bagels/lua/Bagels.lua | 121 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 05_Bagels/lua/Bagels.lua diff --git a/05_Bagels/lua/Bagels.lua b/05_Bagels/lua/Bagels.lua new file mode 100644 index 00000000..57c35297 --- /dev/null +++ b/05_Bagels/lua/Bagels.lua @@ -0,0 +1,121 @@ +--- +--- Bagels +--- Ported by Joe Nellis. +--- Text displayed is altered slightly from the original program to allow for +--- more (or less) than three digits to be guessed. Change the difficulty to +--- the number of digits you wish in the secret code. +--- + +--- difficult is number of digits to use +local difficulty = 3 + +print [[ + BAGELS + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + +]] + +function getInput(prompt) + io.write(prompt) + io.flush() + local input = io.read("l") + if not input then --- test for EOF + print("GOODBYE") + os.exit(0) + end + return input +end + +local needsRules = getInput("WOULD YOU LIKE THE RULES? (YES OR NO) ") +print() +if needsRules:match("[yY].*") then + print(string.format( [[ +I AM THINKING OF A %u DIGIT NUMBER. TRY TO GUESS +MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS: + PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION + FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION + BAGELS - NO DIGITS + ]], difficulty)) +end + +function play(numDigits, score) + local digits = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" } + --- secret number must not have duplicate digits + --- randomly swap numDigits at the head of this list to create secret number + for i = 1, numDigits do + local j = math.random(1, 10) + digits[i], digits[j] = digits[j], digits[i] + end + + print "O.K. I HAVE A NUMBER IN MIND." + for guessNum = 1, 20 do + :: GUESS_AGAIN :: --- 0) then + print("A", score, "POINT BAGELS BUFF!!") + end + print "HOPE YOU HAD FUN. BYE." + end +end + +play(difficulty, 0) --- default is numDigits=3, score=0 + From 1f02b11c7463a7b7dddd9c942bfaa32ac31ec306 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Wed, 13 Apr 2022 22:16:46 +0200 Subject: [PATCH 4/5] fix(#700): fix terminal emulator on mobile --- .../javascript/WebTerminal/HtmlTerminal.css | 16 ++++-- .../javascript/WebTerminal/HtmlTerminal.js | 53 +++++++------------ .../javascript/WebTerminal/terminal.html | 2 +- 00_Common/javascript/common.mjs | 6 ++- index.html | 2 +- 5 files changed, 37 insertions(+), 42 deletions(-) diff --git a/00_Common/javascript/WebTerminal/HtmlTerminal.css b/00_Common/javascript/WebTerminal/HtmlTerminal.css index 0150bb9a..eaeea966 100644 --- a/00_Common/javascript/WebTerminal/HtmlTerminal.css +++ b/00_Common/javascript/WebTerminal/HtmlTerminal.css @@ -17,7 +17,7 @@ overflow-y: scroll; width: 100%; - max-width: 60rem; + max-width: 640px; margin: 0 auto; } @@ -32,10 +32,7 @@ padding: 0; } -/* The "terminal" has one "prompt" element. - * This prompt is not any kind of input, but just a simple - * with an id "prompt" and a - */ +/* The "terminal" has one "prompt" input-element. */ @keyframes prompt-blink { 100% { opacity: 0; @@ -57,6 +54,15 @@ width: 0.75rem; opacity: 1; } +.terminal input#prompt { + text-transform: uppercase; + background: none; + border: none; + outline: none; + caret-color: var(--text); + color: var(--text); + font: var(--terminal-font); +} /* Terminal scrollbar */ diff --git a/00_Common/javascript/WebTerminal/HtmlTerminal.js b/00_Common/javascript/WebTerminal/HtmlTerminal.js index c8d529d9..a2aa2365 100644 --- a/00_Common/javascript/WebTerminal/HtmlTerminal.js +++ b/00_Common/javascript/WebTerminal/HtmlTerminal.js @@ -26,7 +26,7 @@ class HtmlTerminal { * @private * @type {HTMLElement} */ - #$prompt = undefined; + #$prompt; /** * Constructor @@ -45,13 +45,18 @@ class HtmlTerminal { this.$output.classList.add('terminal'); // Create a prompt element. - // This element gets added if input is needed - this.#$prompt = document.createElement("span"); + // This element gets added if input is needed. + this.#$prompt = document.createElement("input"); this.#$prompt.setAttribute("id", "prompt"); - this.#$prompt.innerText = ""; + this.#$prompt.setAttribute("type", "text"); + this.#$prompt.setAttribute("length", "50"); + this.#$prompt.addEventListener("keydown", this.#handleKey.bind(this)); - //TODO: this handler shouls be only on the propt element and only active if cursor is visible - document.addEventListener("keyup", this.#handleKey.bind(this)); + // Force focus on the promt on each click. + // This is needed for mobile support. + document.body.addEventListener('click', () => { + this.#$prompt.focus(); + }); } /** @@ -77,37 +82,16 @@ class HtmlTerminal { * @param {*} e */ #handleKey(e) { - // if no input-callback is defined + // if no input-callback is defined just return if (!this.#inputCallback) { return; } - if (e.keyCode === 13 /* ENTER */) { - // create a new line with the text input and remove the prompt - const text = this.#$prompt.innerText; - this.write(text + "\n"); - this.#$prompt.innerText = ""; + if (e.keyCode == 13) { + const text = this.#$prompt.value; + this.#$prompt.value = ''; this.#$prompt.remove(); - - // return the inputed text - this.#inputCallback(text); - - // remove the callback and the key handler - this.#inputCallback = undefined; - } else if (e.keyCode === 8 /* BACKSPACE */) { - this.#$prompt.innerText = this.#$prompt.innerText.slice(0, -1); - } else if ( - e.keyCode == 16 // "Shift" - || e.keyCode == 17 // "Control" - || e.keyCode == 20 // "CapsLock" - || !e.key.match(/^[a-z0-9!"§#$%&'()*+,.\/:;<=>?@\[\] ^_`{|}~-]$/i) - ) { - // ignore non-visible characters - return e; - } else { - this.#$prompt.innerHtml = ''; - const key = e.shiftKey ? e.key.toUpperCase() : e.key; - this.#$prompt.innerText = this.#$prompt.innerText + key; + this.#inputCallback(text + '\n'); } } @@ -122,7 +106,7 @@ class HtmlTerminal { } /** - * TODO: + * Create a new div and add html content. * * @public * @param {*} htmlContent @@ -189,7 +173,8 @@ class HtmlTerminal { */ input(callback) { // show prompt with a blinking prompt - this.$output.appendChild(this.#$prompt); this.#inputCallback = callback; + this.$output.appendChild(this.#$prompt); + this.#$prompt.focus(); } } diff --git a/00_Common/javascript/WebTerminal/terminal.html b/00_Common/javascript/WebTerminal/terminal.html index d9fab1d6..ae68bedb 100644 --- a/00_Common/javascript/WebTerminal/terminal.html +++ b/00_Common/javascript/WebTerminal/terminal.html @@ -1,7 +1,7 @@ Minimal node.js terminal - + BASIC Computer Games

BASIC Computer Games

\ No newline at end of file +BASIC Computer Games

BASIC Computer Games

\ No newline at end of file From 4d4aea42fc0231aa8d4a5054a55c254976816512 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Wed, 13 Apr 2022 22:59:20 +0200 Subject: [PATCH 5/5] fix(#707): removed css animation for performance --- 00_Utilities/javascript/style_terminal.css | 136 +-------------------- 1 file changed, 1 insertion(+), 135 deletions(-) diff --git a/00_Utilities/javascript/style_terminal.css b/00_Utilities/javascript/style_terminal.css index dce94bb9..c0a23ac5 100644 --- a/00_Utilities/javascript/style_terminal.css +++ b/00_Utilities/javascript/style_terminal.css @@ -84,143 +84,10 @@ a:hover { } /* add all the face flicker effects (only on desktop) */ -@media screen and (min-width: 960px) { +@media not screen and (max-width: 960px) and (prefers-reduced-motion) { main { padding: 3rem; } - @keyframes flicker { - 0% { - opacity: 0.27861; - } - 5% { - opacity: 0.34769; - } - 10% { - opacity: 0.23604; - } - 15% { - opacity: 0.90626; - } - 20% { - opacity: 0.18128; - } - 25% { - opacity: 0.83891; - } - 30% { - opacity: 0.65583; - } - 35% { - opacity: 0.67807; - } - 40% { - opacity: 0.26559; - } - 45% { - opacity: 0.84693; - } - 50% { - opacity: 0.96019; - } - 55% { - opacity: 0.08594; - } - 60% { - opacity: 0.20313; - } - 65% { - opacity: 0.71988; - } - 70% { - opacity: 0.53455; - } - 75% { - opacity: 0.37288; - } - 80% { - opacity: 0.71428; - } - 85% { - opacity: 0.70419; - } - 90% { - opacity: 0.7003; - } - 95% { - opacity: 0.36108; - } - 100% { - opacity: 0.24387; - } - } - @keyframes textShadow { - 0% { - text-shadow: 0.4389924193300864px 0 1px rgba(0,30,255,0.5), -0.4389924193300864px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 5% { - text-shadow: 2.7928974010788217px 0 1px rgba(0,30,255,0.5), -2.7928974010788217px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 10% { - text-shadow: 0.02956275843481219px 0 1px rgba(0,30,255,0.5), -0.02956275843481219px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 15% { - text-shadow: 0.40218538552878136px 0 1px rgba(0,30,255,0.5), -0.40218538552878136px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 20% { - text-shadow: 3.4794037899852017px 0 1px rgba(0,30,255,0.5), -3.4794037899852017px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 25% { - text-shadow: 1.6125630401149584px 0 1px rgba(0,30,255,0.5), -1.6125630401149584px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 30% { - text-shadow: 0.7015590085143956px 0 1px rgba(0,30,255,0.5), -0.7015590085143956px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 35% { - text-shadow: 3.896914047650351px 0 1px rgba(0,30,255,0.5), -3.896914047650351px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 40% { - text-shadow: 3.870905614848819px 0 1px rgba(0,30,255,0.5), -3.870905614848819px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 45% { - text-shadow: 2.231056963361899px 0 1px rgba(0,30,255,0.5), -2.231056963361899px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 50% { - text-shadow: 0.08084290417898504px 0 1px rgba(0,30,255,0.5), -0.08084290417898504px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 55% { - text-shadow: 2.3758461067427543px 0 1px rgba(0,30,255,0.5), -2.3758461067427543px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 60% { - text-shadow: 2.202193051050636px 0 1px rgba(0,30,255,0.5), -2.202193051050636px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 65% { - text-shadow: 2.8638780614874975px 0 1px rgba(0,30,255,0.5), -2.8638780614874975px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 70% { - text-shadow: 0.48874025155497314px 0 1px rgba(0,30,255,0.5), -0.48874025155497314px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 75% { - text-shadow: 1.8948491305757957px 0 1px rgba(0,30,255,0.5), -1.8948491305757957px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 80% { - text-shadow: 0.0833037308038857px 0 1px rgba(0,30,255,0.5), -0.0833037308038857px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 85% { - text-shadow: 0.09769827255241735px 0 1px rgba(0,30,255,0.5), -0.09769827255241735px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 90% { - text-shadow: 3.443339761481782px 0 1px rgba(0,30,255,0.5), -3.443339761481782px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 95% { - text-shadow: 2.1841838852799786px 0 1px rgba(0,30,255,0.5), -2.1841838852799786px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - 100% { - text-shadow: 2.6208764473832513px 0 1px rgba(0,30,255,0.5), -2.6208764473832513px 0 1px rgba(255,0,80,0.3), 0 0 3px; - } - } - #output { - animation: textShadow 1.6s infinite; - } #output::before { content: " "; display: block; @@ -246,6 +113,5 @@ a:hover { opacity: 0; z-index: 2; pointer-events: none; - animation: flicker 0.15s infinite; } } \ No newline at end of file