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 - + 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 + 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 +} 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"); + } +} diff --git a/index.html b/index.html index a14d61f6..cb9ea8d3 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ -BASIC Computer Games

BASIC Computer Games

\ No newline at end of file +BASIC Computer Games

BASIC Computer Games

\ No newline at end of file