From c49283c7601ad35e0f26d90089a0414f181c68bd Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Wed, 23 Feb 2022 23:39:06 -0800 Subject: [PATCH 01/13] rust port of Amazing --- 02_Amazing/rust/Cargo.toml | 9 ++ 02_Amazing/rust/src/main.rs | 197 ++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 02_Amazing/rust/Cargo.toml create mode 100644 02_Amazing/rust/src/main.rs diff --git a/02_Amazing/rust/Cargo.toml b/02_Amazing/rust/Cargo.toml new file mode 100644 index 00000000..276ea543 --- /dev/null +++ b/02_Amazing/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" \ No newline at end of file diff --git a/02_Amazing/rust/src/main.rs b/02_Amazing/rust/src/main.rs new file mode 100644 index 00000000..cef8496c --- /dev/null +++ b/02_Amazing/rust/src/main.rs @@ -0,0 +1,197 @@ +use rand::{Rng, thread_rng, prelude::SliceRandom}; +use std::{io, collections::HashSet}; + +fn main() { + //DATA + enum Direction { + LEFT=0, + UP=1, + RIGHT=2, + DOWN=3, + } + impl Direction { + fn val(&self) -> usize { + match self { + Direction::LEFT=>0, + Direction::UP=>1, + Direction::RIGHT=>2, + Direction::DOWN=>3, + } + } + } + const EXIT_DOWN:usize = 1; + const EXIT_RIGHT:usize = 2; + let mut rng = thread_rng(); //rng + /* + vector of: + vectors of: + integers + Initially set to 0, unprocessed cells. + Filled in with consecutive non-zero numbers as cells are processed + */ + let mut used; //2d vector + /* + vector of: + vectors of: + integers + Remains 0 if there is no exit down or right + Set to 1 if there is an exit down + Set to 2 if there is an exit right + Set to 3 if there are exits down and right + */ + let mut walls; //2d vector + let width; + let height; + let entrance_column; //rng, column of entrance + let mut row; + let mut col; + let mut count; + + + + //print welcome message + println!(" + AMAZING PROGRAM + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n"); + + //prompt for input + width = get_user_input("What is your width?"); + print!("\n"); //one blank line below + height = get_user_input("What is your height?"); + print!("\n\n\n\n");//4 blank lines below + + //generate maze + //initialize used and wall vectors + //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) + used = vec![0; (width * height) as usize]; + let mut used: Vec<_> = used.as_mut_slice().chunks_mut(width as usize).collect(); + let used = used.as_mut_slice(); //accessible as used[][] + //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) + walls = vec![0; (width * height) as usize]; + let mut walls: Vec<_> = walls.as_mut_slice().chunks_mut(width as usize).collect(); + let walls = walls.as_mut_slice(); //accessible as walls[][] + + entrance_column=rng.gen_range(0..width-1); + row = 0; + col = entrance_column; + count = 1; + used[row][col] = count; + count += 1; + + while count != width*height + 1 { + //remove possible directions that are blocked or + //hit cells already processed + let mut possible_directions: HashSet = vec![Direction::LEFT.val(),Direction::UP.val(),Direction::RIGHT.val(),Direction::DOWN.val()].into_iter().collect(); + if col==0 || used[row][col-1]!=0 { + possible_directions.remove(&Direction::LEFT.val()); + } + if row==0 || used[row-1][col]!=0 { + possible_directions.remove(&Direction::UP.val()); + } + if col==width-1 || used[row][col+1]!=0 { + possible_directions.remove(&Direction::RIGHT.val()); + } + if row==height-1 || used[row+1][col]!=0 { + possible_directions.remove(&Direction::DOWN.val()); + } + + //If we can move in a direction, move and make opening + if possible_directions.len() != 0 { //all values in possible_directions are not NONE + let pos_dir_vec: Vec<_> = possible_directions.into_iter().collect(); // convert the set to a vector to get access to the choose method + //select a random direction + match pos_dir_vec.choose(&mut rng).expect("error") { + 0=> { + col -= 1; + walls[row][col] = EXIT_RIGHT; + }, + 1=> { + row -= 1; + walls[row][col] = EXIT_DOWN; + }, + 2=>{ + walls[row][col] = walls[row][col] + EXIT_RIGHT; + col += 1; + }, + 3=>{ + walls[row][col] = walls[row][col] + EXIT_DOWN; + row += 1; + }, + _=>{}, + } + used[row][col]=count; + count += 1; + } + //otherwise, move to the next used cell, and try again + else { + loop { + if col != width-1 {col += 1;} + else if row != height-1 {row+=1; col=0;} + else {row=0;col=0;} + + if used[row][col] != 0 {break;} + } + } + + } + // Add a random exit + col=rng.gen_range(0..width); + row=height-1; + walls[row][col]+=1; + + //print maze + //first line + for c in 0..width { + if c == entrance_column { + print!(". "); + } + else { + print!(".--"); + } + } + println!("."); + //rest of maze + for r in 0..height { + print!("I"); + for c in 0..width { + if walls[r][c]<2 {print!(" I");} + else {print!(" ");} + } + println!(); + for c in 0..width { + if walls[r][c] == 0 || walls[r][c]==2 {print!(":--");} + else {print!(": ");} + } + println!("."); + } + + + + + + +} + +fn get_user_input(prompt: &str) -> usize { + //DATA + let mut raw_input = String::new(); // temporary variable for user input that can be parsed later + + //input loop + return loop { + + //print prompt + println!("{}", prompt); + + //read user input from standard input, and store it to raw_input + raw_input.clear(); //clear input + io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); + + //from input, try to read a number + match raw_input.trim().parse::() { + Ok(i) => break i, // this escapes the loop, returning i + Err(e) => { + println!("MEANINGLESS DIMENSION. TRY AGAIN. {}", e.to_string().to_uppercase()); + continue; // run the loop again + } + }; + } +} From 842c85ffde8db51ed4cca5a004ff9c984067fdfc Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Wed, 23 Feb 2022 23:39:06 -0800 Subject: [PATCH 02/13] rust port of Amazing --- 02_Amazing/rust/Cargo.toml | 9 ++ 02_Amazing/rust/src/main.rs | 197 ++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 02_Amazing/rust/Cargo.toml create mode 100644 02_Amazing/rust/src/main.rs diff --git a/02_Amazing/rust/Cargo.toml b/02_Amazing/rust/Cargo.toml new file mode 100644 index 00000000..276ea543 --- /dev/null +++ b/02_Amazing/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" \ No newline at end of file diff --git a/02_Amazing/rust/src/main.rs b/02_Amazing/rust/src/main.rs new file mode 100644 index 00000000..cef8496c --- /dev/null +++ b/02_Amazing/rust/src/main.rs @@ -0,0 +1,197 @@ +use rand::{Rng, thread_rng, prelude::SliceRandom}; +use std::{io, collections::HashSet}; + +fn main() { + //DATA + enum Direction { + LEFT=0, + UP=1, + RIGHT=2, + DOWN=3, + } + impl Direction { + fn val(&self) -> usize { + match self { + Direction::LEFT=>0, + Direction::UP=>1, + Direction::RIGHT=>2, + Direction::DOWN=>3, + } + } + } + const EXIT_DOWN:usize = 1; + const EXIT_RIGHT:usize = 2; + let mut rng = thread_rng(); //rng + /* + vector of: + vectors of: + integers + Initially set to 0, unprocessed cells. + Filled in with consecutive non-zero numbers as cells are processed + */ + let mut used; //2d vector + /* + vector of: + vectors of: + integers + Remains 0 if there is no exit down or right + Set to 1 if there is an exit down + Set to 2 if there is an exit right + Set to 3 if there are exits down and right + */ + let mut walls; //2d vector + let width; + let height; + let entrance_column; //rng, column of entrance + let mut row; + let mut col; + let mut count; + + + + //print welcome message + println!(" + AMAZING PROGRAM + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n"); + + //prompt for input + width = get_user_input("What is your width?"); + print!("\n"); //one blank line below + height = get_user_input("What is your height?"); + print!("\n\n\n\n");//4 blank lines below + + //generate maze + //initialize used and wall vectors + //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) + used = vec![0; (width * height) as usize]; + let mut used: Vec<_> = used.as_mut_slice().chunks_mut(width as usize).collect(); + let used = used.as_mut_slice(); //accessible as used[][] + //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) + walls = vec![0; (width * height) as usize]; + let mut walls: Vec<_> = walls.as_mut_slice().chunks_mut(width as usize).collect(); + let walls = walls.as_mut_slice(); //accessible as walls[][] + + entrance_column=rng.gen_range(0..width-1); + row = 0; + col = entrance_column; + count = 1; + used[row][col] = count; + count += 1; + + while count != width*height + 1 { + //remove possible directions that are blocked or + //hit cells already processed + let mut possible_directions: HashSet = vec![Direction::LEFT.val(),Direction::UP.val(),Direction::RIGHT.val(),Direction::DOWN.val()].into_iter().collect(); + if col==0 || used[row][col-1]!=0 { + possible_directions.remove(&Direction::LEFT.val()); + } + if row==0 || used[row-1][col]!=0 { + possible_directions.remove(&Direction::UP.val()); + } + if col==width-1 || used[row][col+1]!=0 { + possible_directions.remove(&Direction::RIGHT.val()); + } + if row==height-1 || used[row+1][col]!=0 { + possible_directions.remove(&Direction::DOWN.val()); + } + + //If we can move in a direction, move and make opening + if possible_directions.len() != 0 { //all values in possible_directions are not NONE + let pos_dir_vec: Vec<_> = possible_directions.into_iter().collect(); // convert the set to a vector to get access to the choose method + //select a random direction + match pos_dir_vec.choose(&mut rng).expect("error") { + 0=> { + col -= 1; + walls[row][col] = EXIT_RIGHT; + }, + 1=> { + row -= 1; + walls[row][col] = EXIT_DOWN; + }, + 2=>{ + walls[row][col] = walls[row][col] + EXIT_RIGHT; + col += 1; + }, + 3=>{ + walls[row][col] = walls[row][col] + EXIT_DOWN; + row += 1; + }, + _=>{}, + } + used[row][col]=count; + count += 1; + } + //otherwise, move to the next used cell, and try again + else { + loop { + if col != width-1 {col += 1;} + else if row != height-1 {row+=1; col=0;} + else {row=0;col=0;} + + if used[row][col] != 0 {break;} + } + } + + } + // Add a random exit + col=rng.gen_range(0..width); + row=height-1; + walls[row][col]+=1; + + //print maze + //first line + for c in 0..width { + if c == entrance_column { + print!(". "); + } + else { + print!(".--"); + } + } + println!("."); + //rest of maze + for r in 0..height { + print!("I"); + for c in 0..width { + if walls[r][c]<2 {print!(" I");} + else {print!(" ");} + } + println!(); + for c in 0..width { + if walls[r][c] == 0 || walls[r][c]==2 {print!(":--");} + else {print!(": ");} + } + println!("."); + } + + + + + + +} + +fn get_user_input(prompt: &str) -> usize { + //DATA + let mut raw_input = String::new(); // temporary variable for user input that can be parsed later + + //input loop + return loop { + + //print prompt + println!("{}", prompt); + + //read user input from standard input, and store it to raw_input + raw_input.clear(); //clear input + io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); + + //from input, try to read a number + match raw_input.trim().parse::() { + Ok(i) => break i, // this escapes the loop, returning i + Err(e) => { + println!("MEANINGLESS DIMENSION. TRY AGAIN. {}", e.to_string().to_uppercase()); + continue; // run the loop again + } + }; + } +} From 8865fcdc53c3fc8baeb59a78d464ac0fb8ea6dd9 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Fri, 25 Feb 2022 13:29:29 -0800 Subject: [PATCH 03/13] start rust port of blackjack --- 10_Blackjack/rust/Cargo.toml | 9 +++ 10_Blackjack/rust/src/main.rs | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 10_Blackjack/rust/Cargo.toml create mode 100644 10_Blackjack/rust/src/main.rs diff --git a/10_Blackjack/rust/Cargo.toml b/10_Blackjack/rust/Cargo.toml new file mode 100644 index 00000000..1c2935d4 --- /dev/null +++ b/10_Blackjack/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" \ No newline at end of file diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs new file mode 100644 index 00000000..8db2a0b3 --- /dev/null +++ b/10_Blackjack/rust/src/main.rs @@ -0,0 +1,105 @@ +use rand::{Rng, prelude::thread_rng}; +use std::{io}; + +//DATA +struct CARD { + name: String, + value: u8, +} +impl CARD { + +} +struct HAND { + cards: Vec, + total: u8, +} +impl HAND { + fn drawCard(deck: DECK) -> CARD { + //pull a random card from deck + + } +} +struct DECK { + cards: Vec, +} +struct PLAYER { + hand: HAND, +} + +struct GAME { + human_player: PLAYER, + computer_players: Vec, + dealer: PLAYER, + + deck: DECK, + discard_pile: DECK, +} +impl GAME { + fn start(num_players:u8) { + //ask user how many players there should be, not including them + let num_players = loop { + //input loop + + let mut raw_input = String::new(); // temporary variable for user input that can be parsed later + //print prompt + println!("How many other players should there by?"); + + //read user input from standard input, and store it to raw_input + raw_input.clear(); //clear input + io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); + + //from input, try to read a number + match raw_input.trim().parse::() { + Ok(i) => break i, // this escapes the loop, returning i + Err(e) => { + println!("MEANINGLESS NUMBER. TRY AGAIN. {}", e.to_string().to_uppercase()); + continue; // run the loop again + } + }; + }; + + + } +} + +const card_names: [&str;13] = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]; + +fn main() { + //DATA + //P(I,J) IS THE JTH CARD IN HAND I, Q(I) IS TOTAL OF HAND I + //C IS THE DECK BEING DEALT FROM, D IS THE DISCARD PILE, + //T(I) IS THE TOTAL FOR PLAYER I, S(I) IS THE TOTAL THIS HAND FOR + //PLAYER I, B(I) IS TH BET FOR HAND I + //R(I) IS THE LENGTH OF P(I,*) + + //print welcome message + welcome(); + + +} + +fn welcome() { + //welcome message + println!(" + BLACK JACK + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + \n\n\n"); +} + +fn instructions() { + println!(" + THIS IS THE GAME OF 21. AS MANY AS 7 PLAYERS MAY PLAY THE + GAME. ON EACH DEAL, BETS WILL BE ASKED FOR, AND THE + PLAYERS' BETS SHOULD BE TYPED IN. THE CARDS WILL THEN BE + DEALT, AND EACH PLAYER IN TURN PLAYS HIS HAND. THE + FIRST RESPONSE SHOULD BE EITHER 'D', INDICATING THAT THE + PLAYER IS DOUBLING DOWN, 'S', INDICATING THAT HE IS + STANDING, 'H', INDICATING HE WANTS ANOTHER CARD, OR '/', + INDICATING THAT HE WANTS TO SPLIT HIS CARDS. AFTER THE + INITIAL RESPONSE, ALL FURTHER RESPONSES SHOULD BE 'S' OR + 'H', UNLESS THE CARDS WERE SPLIT, IN WHICH CASE DOUBLING + DOWN IS AGAIN PERMITTED. IN ORDER TO COLLECT FOR + BLACKJACK, THE INITIAL RESPONSE SHOULD BE 'S'. + NUMBER OF PLAYERS + "); +} \ No newline at end of file From 383ca2c3e6d746fa544df3e246c22d550f050360 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Fri, 25 Feb 2022 22:51:47 -0800 Subject: [PATCH 04/13] todo list --- 10_Blackjack/rust/src/main.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index 8db2a0b3..8ad4cac3 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -1,6 +1,19 @@ use rand::{Rng, prelude::thread_rng}; use std::{io}; +/** + * todo list: + * + * make a 2 player game functional + * + * add more players + * + * use a 3 deck pool of cards instead of an infinite amount + * + * keep track of player bets + */ + + //DATA struct CARD { name: String, @@ -24,6 +37,12 @@ struct DECK { } struct PLAYER { hand: HAND, + balance: usize, +} +impl PLAYER { + fn new() -> PLAYER { + + } } struct GAME { @@ -35,7 +54,7 @@ struct GAME { discard_pile: DECK, } impl GAME { - fn start(num_players:u8) { + fn start(num_players:u8) -> GAME { //ask user how many players there should be, not including them let num_players = loop { //input loop @@ -59,6 +78,12 @@ impl GAME { }; + + + //return a game + return GAME { human_player: PLAYER::new(), computer_players: (), dealer: (), deck: (), discard_pile: () } + + } } From 0ca97eae9e98d6edf1f24031fcaf82c4ea39ffe3 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 18:42:05 -0800 Subject: [PATCH 05/13] rust port of blackjack (missing features) todo list: * allow splitting * keep track of player bets * allow doubling down --- 10_Blackjack/rust/src/main.rs | 572 ++++++++++++++++++++++++++++++---- 1 file changed, 506 insertions(+), 66 deletions(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index 8ad4cac3..73ed5134 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -1,108 +1,473 @@ -use rand::{Rng, prelude::thread_rng}; -use std::{io}; +use rand::{prelude::{thread_rng, SliceRandom}}; +use std::{io, collections::HashSet}; /** * todo list: * - * make a 2 player game functional - * - * add more players - * - * use a 3 deck pool of cards instead of an infinite amount + * allow splitting * * keep track of player bets + * + * allow doubling down */ //DATA -struct CARD { - name: String, +//enums +enum PlayerType { + Player, + Dealer, +} +enum Play { + Stand, + Hit, +} +impl ToString for Play { + fn to_string(&self) -> String { + match self { + &Play::Hit => return String::from("Hit"), + &Play::Stand => return String::from("Stand"), + } + } +} +//structs +struct CARD<'a> { + name: &'a str, value: u8, } -impl CARD { +impl<'a> CARD<'a> { + /** + * creates a new card from the passed card name + */ + fn new(card_name: &str) -> CARD { + return CARD { name: card_name, value: CARD::determine_value_from_name(card_name) }; + } + + /** + * returns the value associated with a card with the passed name + * return 0 if the passed card name doesn't exist + */ + fn determine_value_from_name(card_name: &str) -> u8 { + //DATA + let value:u8; + + match card_name.to_ascii_uppercase().as_str() { + "ACE" => value = 11, + "2" => value = 2, + "3" => value = 3, + "4" => value = 4, + "5" => value = 5, + "6" => value = 6, + "7" => value = 7, + "8" => value = 8, + "9" => value = 9, + "10" => value = 10, + "JACK" => value = 10, + "QUEEN" => value = 10, + "KING" => value = 10, + _ => value = 0, + } + + return value; + } +} +struct HAND<'a> { + cards: Vec>, +} +impl<'a> HAND<'a> { + /** + * returns a new empty hand + */ + fn new() -> HAND<'a> { + return HAND { cards: Vec::new()}; + } -} -struct HAND { - cards: Vec, - total: u8, -} -impl HAND { - fn drawCard(deck: DECK) -> CARD { - //pull a random card from deck + /** + * add a passed card to this hand + */ + fn add_card(&mut self, card: CARD<'a>) { + self.cards.push(card); + } + /** + * returns the total points of the cards in this hand + */ + fn get_total(&self) -> usize { + let mut total:usize = 0; + for card in &self.cards { + total += card.value as usize; + } + + //if there is an ACE, and the hand would otherwise bust, treat the ace like it's worth 1 + if total > 21 && self.cards.iter().any(|c| -> bool {*c.name == *"ACE"}) { + total -= 10; + } + + return total; + } + + /** + * adds the cards in hand into the discard pile + */ + fn discard_hand(&mut self, deck: &mut DECKS<'a>) { + let len = self.cards.len(); + for _i in 0..len { + deck.discard_pile.push(self.cards.pop().expect("hand empty")); + } } } -struct DECK { - cards: Vec, +struct DECKS<'a> { + deck: Vec>, //cards in the deck + discard_pile: Vec> //decks discard pile } -struct PLAYER { - hand: HAND, - balance: usize, -} -impl PLAYER { - fn new() -> PLAYER { +impl<'a> DECKS<'a> { + /** + * creates a new full and shuffled deck, and an empty discard pile + */ + fn new() -> DECKS<'a> { + //returns a number of full decks of 52 cards, shuffles them + //DATA + let mut deck = DECKS{deck: Vec::new(), discard_pile: Vec::new()}; + let number_of_decks = 3; + //fill deck + for _n in 0..number_of_decks { //fill deck with number_of_decks decks worth of cards + for card_name in CARD_NAMES { //add 4 of each card, totaling one deck with 4 of each card + deck.deck.push( CARD::new(card_name) ); + deck.deck.push( CARD::new(card_name) ); + deck.deck.push( CARD::new(card_name) ); + deck.deck.push( CARD::new(card_name) ); + } + } + + //shuffle deck + deck.shuffle(); + + //return deck + return deck; } -} -struct GAME { - human_player: PLAYER, - computer_players: Vec, - dealer: PLAYER, + /** + * shuffles the deck + */ + fn shuffle(&mut self) { + self.deck.shuffle(&mut thread_rng()); + } - deck: DECK, - discard_pile: DECK, -} -impl GAME { - fn start(num_players:u8) -> GAME { - //ask user how many players there should be, not including them - let num_players = loop { - //input loop + /** + * draw card from deck, and return it + * if deck is empty, shuffles discard pile into it and tries again + */ + fn draw_card(&mut self) -> CARD<'a> { + match self.deck.pop() { + Some(card) => return card, + None => { + let len = self.discard_pile.len(); - let mut raw_input = String::new(); // temporary variable for user input that can be parsed later - //print prompt - println!("How many other players should there by?"); - - //read user input from standard input, and store it to raw_input - raw_input.clear(); //clear input - io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); - - //from input, try to read a number - match raw_input.trim().parse::() { - Ok(i) => break i, // this escapes the loop, returning i - Err(e) => { - println!("MEANINGLESS NUMBER. TRY AGAIN. {}", e.to_string().to_uppercase()); - continue; // run the loop again + if len > 0 {//deck is empty, shuffle discard pile into deck and try again + println!("deck is empty, shuffling"); + for _i in 0..len { + self.deck.push( self.discard_pile.pop().expect("discard pile empty") ) + } + self.shuffle(); + return self.draw_card(); + } else { //discard pile and deck are empty, should never happen + panic!("discard pile empty"); } - }; - }; + } + } + } +} +struct PLAYER<'a> { + hand: HAND<'a>, + _balance: usize, + wins: usize, + player_type: PlayerType, +} +impl<'a> PLAYER<'a> { + /** + * creates a new player of the given type + */ + fn new(player_type: PlayerType) -> PLAYER<'a> { + return PLAYER { hand: HAND::new(), _balance: STARTING_BALANCE, wins: 0, player_type: player_type}; + } + /** + * returns a string of the players hand + * + * if player is a dealer, returns the first card in the hand followed by *'s for every other card + * if player is a player, returns every card and the total + */ + fn print_hand(&self, hide_dealer:bool) -> String { + if !hide_dealer { + return format!( + "{}\n\ttotal points = {}", //message + { //cards in hand + let mut s:String = String::new(); + for cards_in_hand in self.hand.cards.iter().rev() { + s += format!("{}\t", cards_in_hand.name).as_str(); + } + s + }, + self.hand.get_total() //total points in hand + ); + } + match &self.player_type { + &PlayerType::Dealer => { //if this is a dealer + return format!( + "{}*",//message + { //*'s for other cards + let mut s:String = String::new(); + let mut cards_in_hand = self.hand.cards.iter(); + cards_in_hand.next();//consume first card drawn + for c in cards_in_hand.rev() { + s += format!("{}\t", c.name).as_str(); + } + s + } + ); + }, + &PlayerType::Player => { //if this is a player + return format!( + "{}\n\ttotal points = {}", //message + { //cards in hand + let mut s:String = String::new(); + for cards_in_hand in self.hand.cards.iter().rev() { + s += format!("{}\t", cards_in_hand.name).as_str(); + } + s + }, + self.hand.get_total() //total points in hand + ); + } + } + } + + /** + * get the players 'play' + */ + fn get_play(&self) -> Play { + /* + do different things depending on what type of player this is: + if it's a dealer, use an algorithm to determine the play + if it's a player, ask user for input + */ + match &self.player_type { + &PlayerType::Dealer => { + if self.hand.get_total() > 16 { // if total value of hand is greater than 16, stand + return Play::Stand; + } else { //otherwise hit + return Play::Hit; + } + }, + &PlayerType::Player => { + let play = get_char_from_user_input("\tWhat is your play?", &vec!['s','S','h','H']); + match play { + 's' | 'S' => return Play::Stand, + 'h' | 'H' => return Play::Hit, + _ => panic!("get_char_from_user_input() returned invalid character"), + } + }, + } + } +} + +struct GAME<'a> { + players: Vec>, //last item in this is the dealer + decks: DECKS<'a>, + rounds:usize, + games_played:usize, +} +impl<'a> GAME<'a> { + /** + * creates a new game + */ + fn new(num_players:usize) -> GAME<'a> { + //DATA + let mut players: Vec = Vec::new(); + + //add dealer + players.push(PLAYER::new(PlayerType::Dealer)); + //create human player(s) (at least one) + players.push(PLAYER::new(PlayerType::Player)); + for _i in 1..num_players { //one less than num_players players + players.push(PLAYER::new(PlayerType::Player)); + } + + //ask if they want instructions + if let 'y'|'Y' = get_char_from_user_input("Do you want instructions? (y/n)", &vec!['y','Y','n','N']) { + instructions(); + } + println!(); //return a game - return GAME { human_player: PLAYER::new(), computer_players: (), dealer: (), deck: (), discard_pile: () } - - + return GAME { players: players, decks: DECKS::new(), rounds: 0, games_played: 0} } + + /** + * prints the score of every player + */ + fn print_wins(&self) { + println!("Scores:"); + for player in self.players.iter().enumerate() { + match player.1.player_type { + PlayerType::Player => println!("Player{} wins:\t{}", player.0, player.1.wins), + PlayerType::Dealer => println!("Dealer wins:\t{}", player.1.wins) + } + } + } + + /** + * plays a round of blackjack + */ + fn play_game(&mut self) { + //deal cards to each player + for _i in 0..2 { // do this twice + //draw card for each player + self.players.iter_mut().for_each(|player| {player.hand.add_card( self.decks.draw_card() );}); + } + + let mut players_playing: HashSet = HashSet::new(); // the numbers presenting each player still active in the round + for i in 0..self.players.len() { //runs number of players times + players_playing.insert(i); + } + //round loop (each player either hits or stands) + loop { + //increment rounds + self.rounds += 1; + + //print game state + println!("\n\t\t\tGame {}\tRound {}", self.games_played, self.rounds); + + self.players.iter().enumerate().for_each(|player| { + match player.1.player_type { + PlayerType::Player => println!("Player{} Hand:\t{}", player.0, player.1.print_hand(true)), + PlayerType::Dealer => println!("Dealer Hand:\t{}", player.1.print_hand(true)) + } + }); + + print!("\n"); //empty line + + //get the moves of all remaining players + for player in self.players.iter_mut().enumerate() { + match player.1.player_type {//print the player name + PlayerType::Player => print!("Player{}:", player.0), + PlayerType::Dealer => print!("Dealer:"), + } + + //check their hand value for a blackjack(21) or bust + let score = player.1.hand.get_total(); + if score >= 21 { + if score == 21 { // == 21 + println!("\tBlackjack (21 points)"); + } else { // > 21 + println!("\tBust ({} points)", score); + } + players_playing.remove(&player.0);//remove player from playing list + continue; + } + + //get play + let play = player.1.get_play(); + + //process play + match play { + Play::Stand => { + println!("\t{}s", play.to_string()); + players_playing.remove(&player.0);//remove player from playing list + }, + Play::Hit => { + println!("\t{}s", play.to_string()); + //give them a card + player.1.hand.add_card( self.decks.draw_card() ); + }, + } + } + + //loop end condition + if players_playing.is_empty() {break;} + } + + //determine winner + let mut top_score = 0; //player with the highest points + let mut num_winners = 1; + for player in self.players.iter_mut().enumerate().filter( |x| -> bool {x.1.hand.get_total()<=21}) { //players_who_didnt_bust + let score = player.1.hand.get_total(); + if score > top_score { + top_score = score; + num_winners = 1; + } else if score == top_score { + num_winners += 1; + } + } + + //print everyones hand + println!(""); + self.players.iter().enumerate().for_each(|player| { + match player.1.player_type { + PlayerType::Player => println!("Player{} Hand:\t{}", player.0, player.1.print_hand(false)), + PlayerType::Dealer => println!("Dealer Hand:\t{}", player.1.print_hand(false)) + } + }); + println!(""); + //for each player with the top score + self.players.iter_mut().enumerate().filter(|x|->bool{x.1.hand.get_total()==top_score}).for_each(|x| { + match x.1.player_type { + PlayerType::Player => print!("Player{}\t", x.0), + PlayerType::Dealer => print!("Dealer\t"), + } + //increment their wins + x.1.wins += 1; + }); + if num_winners > 1 {println!("all tie with {}\n\n\n", top_score);} + else {println!("wins with {}!\n\n\n",top_score);} + + //discard hands + self.players.iter_mut().for_each(|player| {player.hand.discard_hand(&mut self.decks);}); + + //increment games_played + self.games_played += 1; + } + + + } -const card_names: [&str;13] = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]; +const CARD_NAMES: [&str;13] = ["ACE","2","3","4","5","6","7","8","9","10","JACK","QUEEN","KING"]; +const STARTING_BALANCE: usize = 100; fn main() { //DATA - //P(I,J) IS THE JTH CARD IN HAND I, Q(I) IS TOTAL OF HAND I - //C IS THE DECK BEING DEALT FROM, D IS THE DISCARD PILE, - //T(I) IS THE TOTAL FOR PLAYER I, S(I) IS THE TOTAL THIS HAND FOR - //PLAYER I, B(I) IS TH BET FOR HAND I - //R(I) IS THE LENGTH OF P(I,*) + let mut game: GAME; //print welcome message welcome(); - + //create game + game = GAME::new( get_number_from_user_input("How many players should there be (at least 1)?", 1, 7) ); + + //game loop, play game until user wants to stop + loop { + //print score of every user + game.print_wins(); + + //play round + game.play_game(); + + //ask if they want to play again + match get_char_from_user_input("Play Again? (y/n)", &vec!['y','Y','n','N']) { + 'y' | 'Y' => continue, + 'n' | 'N' => break, + _ => break, + } + } } +/** + * prints the welcome screen + */ fn welcome() { //welcome message println!(" @@ -111,6 +476,9 @@ fn welcome() { \n\n\n"); } +/** + * prints the instructions + */ fn instructions() { println!(" THIS IS THE GAME OF 21. AS MANY AS 7 PLAYERS MAY PLAY THE @@ -126,5 +494,77 @@ fn instructions() { DOWN IS AGAIN PERMITTED. IN ORDER TO COLLECT FOR BLACKJACK, THE INITIAL RESPONSE SHOULD BE 'S'. NUMBER OF PLAYERS + + NOTE: Currently only H and S are implemented properly "); -} \ No newline at end of file +} + +/** + * gets a usize integer from user input + */ +fn get_number_from_user_input(prompt: &str, min:usize, max:usize) -> usize { + //input loop + return loop { + let mut raw_input = String::new(); // temporary variable for user input that can be parsed later + + //print prompt + println!("{}", prompt); + + //read user input from standard input, and store it to raw_input + //raw_input.clear(); //clear input + io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); + + //from input, try to read a number + match raw_input.trim().parse::() { + Ok(i) => { + if i < min || i > max { //input out of desired range + println!("INPUT OUT OF VALID RANGE. TRY AGAIN."); + continue; // run the loop again + } + else { + println!(); + break i;// this escapes the loop, returning i + } + }, + Err(e) => { + println!("INVALID INPUT. TRY AGAIN. {}", e.to_string().to_uppercase()); + continue; // run the loop again + } + }; + }; +} + +/** + * gets a character from user input + * returns the first character they type + */ +fn get_char_from_user_input(prompt: &str, valid_results: &Vec) -> char { + //input loop + return loop { + let mut raw_input = String::new(); // temporary variable for user input that can be parsed later + + //print prompt + println!("{}", prompt); + + //read user input from standard input, and store it to raw_input + //raw_input.clear(); //clear input + io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); + + //from input, try to read a valid character + match raw_input.trim().chars().nth(0) { + Some(i) => { + if !valid_results.contains(&i) { //input out of desired range + println!("INPUT IS NOT VALID CHARACTER. TRY AGAIN."); + continue; // run the loop again + } + else { + break i;// this escapes the loop, returning i + } + }, + None => { + println!("INVALID INPUT. TRY AGAIN."); + continue; // run the loop again + } + }; + }; +} From 1a353c90a4a317af0cd76a9419adae32dc665328 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 20:06:17 -0800 Subject: [PATCH 06/13] readme's added --- 02_Amazing/rust/README.md | 3 +++ 10_Blackjack/rust/README.md | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 02_Amazing/rust/README.md create mode 100644 10_Blackjack/rust/README.md diff --git a/02_Amazing/rust/README.md b/02_Amazing/rust/README.md new file mode 100644 index 00000000..f84e546c --- /dev/null +++ b/02_Amazing/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/) by Anthony Rubick [AnthonyMichaelTDM](https://github.com/AnthonyMichaelTDM) \ No newline at end of file diff --git a/10_Blackjack/rust/README.md b/10_Blackjack/rust/README.md new file mode 100644 index 00000000..f84e546c --- /dev/null +++ b/10_Blackjack/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/) by Anthony Rubick [AnthonyMichaelTDM](https://github.com/AnthonyMichaelTDM) \ No newline at end of file From 277f8364fa50bd25084aaaacadcfcefbee3dbe92 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 20:31:46 -0800 Subject: [PATCH 07/13] 10_blackjack rust more improvements --- 10_Blackjack/rust/src/main.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index 73ed5134..72d3188e 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -323,12 +323,16 @@ impl<'a> GAME<'a> { * plays a round of blackjack */ fn play_game(&mut self) { + //print score of every user + self.print_wins(); + //deal cards to each player for _i in 0..2 { // do this twice //draw card for each player self.players.iter_mut().for_each(|player| {player.hand.add_card( self.decks.draw_card() );}); } + //keep track of player who haven't busted or stood yet let mut players_playing: HashSet = HashSet::new(); // the numbers presenting each player still active in the round for i in 0..self.players.len() { //runs number of players times players_playing.insert(i); @@ -369,6 +373,9 @@ impl<'a> GAME<'a> { continue; } + //check if player is still playing + if !players_playing.contains(&player.0) {print!("\n");continue;}//print a line and skip to the next iteration of the loop + //get play let play = player.1.get_play(); @@ -429,6 +436,8 @@ impl<'a> GAME<'a> { //increment games_played self.games_played += 1; + //reset rounds + self.rounds = 0; } @@ -450,9 +459,6 @@ fn main() { //game loop, play game until user wants to stop loop { - //print score of every user - game.print_wins(); - //play round game.play_game(); From fa9bb6b81788b52f610d5d203856eeb054eb81ed Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 23:27:23 -0800 Subject: [PATCH 08/13] 10_blackjack rust implemented betting --- 10_Blackjack/rust/src/main.rs | 268 +++++++++++++++++++--------------- 1 file changed, 149 insertions(+), 119 deletions(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index 72d3188e..0302527f 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -1,13 +1,11 @@ use rand::{prelude::{thread_rng, SliceRandom}}; -use std::{io, collections::HashSet}; +use std::{io, io::{stdout, Write}}; /** * todo list: * * allow splitting * - * keep track of player bets - * * allow doubling down */ @@ -18,6 +16,14 @@ enum PlayerType { Player, Dealer, } +impl ToString for PlayerType { + fn to_string(&self) -> String { + match self { + &PlayerType::Dealer => return String::from("Dealer"), + &PlayerType::Player => return String::from("Player"), + } + } +} enum Play { Stand, Hit, @@ -181,16 +187,36 @@ impl<'a> DECKS<'a> { struct PLAYER<'a> { hand: HAND<'a>, - _balance: usize, + balance: usize, + bet: usize, wins: usize, player_type: PlayerType, + index: usize, + } impl<'a> PLAYER<'a> { /** * creates a new player of the given type */ - fn new(player_type: PlayerType) -> PLAYER<'a> { - return PLAYER { hand: HAND::new(), _balance: STARTING_BALANCE, wins: 0, player_type: player_type}; + fn new(player_type: PlayerType, index: usize) -> PLAYER<'a> { + return PLAYER { hand: HAND::new(), balance: STARTING_BALANCE, bet: 0, wins: 0, player_type: player_type, index: index}; + } + + fn get_name(&self) -> String { + format!("{}{}", self.player_type.to_string(),self.index) + } + + /** + * gets a bet from the player + */ + fn get_bet(&mut self) { + if let PlayerType::Player = self.player_type { + if self.balance < 1 { + println!("{} is out of money :(", self.get_name()); + self.bet = 0; + } + self.bet = get_number_from_user_input(format!("{}\tBet?",self.get_name()).as_str(), 1, self.balance); + } } /** @@ -199,7 +225,7 @@ impl<'a> PLAYER<'a> { * if player is a dealer, returns the first card in the hand followed by *'s for every other card * if player is a player, returns every card and the total */ - fn print_hand(&self, hide_dealer:bool) -> String { + fn hand_as_string(&self, hide_dealer:bool) -> String { if !hide_dealer { return format!( "{}\n\ttotal points = {}", //message @@ -213,34 +239,35 @@ impl<'a> PLAYER<'a> { self.hand.get_total() //total points in hand ); } - - match &self.player_type { - &PlayerType::Dealer => { //if this is a dealer - return format!( - "{}*",//message - { //*'s for other cards - let mut s:String = String::new(); - let mut cards_in_hand = self.hand.cards.iter(); - cards_in_hand.next();//consume first card drawn - for c in cards_in_hand.rev() { - s += format!("{}\t", c.name).as_str(); + else { + match &self.player_type { + &PlayerType::Dealer => { //if this is a dealer + return format!( + "{}*",//message + { //*'s for other cards + let mut s:String = String::new(); + let mut cards_in_hand = self.hand.cards.iter(); + cards_in_hand.next();//consume first card drawn + for c in cards_in_hand.rev() { + s += format!("{}\t", c.name).as_str(); + } + s } - s - } - ); - }, - &PlayerType::Player => { //if this is a player - return format!( - "{}\n\ttotal points = {}", //message - { //cards in hand - let mut s:String = String::new(); - for cards_in_hand in self.hand.cards.iter().rev() { - s += format!("{}\t", cards_in_hand.name).as_str(); - } - s - }, - self.hand.get_total() //total points in hand - ); + ); + }, + &PlayerType::Player => { //if this is a player + return format!( + "{}\n\ttotal points = {}", //message + { //cards in hand + let mut s:String = String::new(); + for cards_in_hand in self.hand.cards.iter().rev() { + s += format!("{}\t", cards_in_hand.name).as_str(); + } + s + }, + self.hand.get_total() //total points in hand + ); + } } } } @@ -277,7 +304,6 @@ impl<'a> PLAYER<'a> { struct GAME<'a> { players: Vec>, //last item in this is the dealer decks: DECKS<'a>, - rounds:usize, games_played:usize, } impl<'a> GAME<'a> { @@ -289,11 +315,11 @@ impl<'a> GAME<'a> { let mut players: Vec = Vec::new(); //add dealer - players.push(PLAYER::new(PlayerType::Dealer)); + players.push(PLAYER::new(PlayerType::Dealer,0)); //create human player(s) (at least one) - players.push(PLAYER::new(PlayerType::Player)); - for _i in 1..num_players { //one less than num_players players - players.push(PLAYER::new(PlayerType::Player)); + players.push(PLAYER::new(PlayerType::Player,1)); + for i in 2..=num_players { //one less than num_players players + players.push(PLAYER::new(PlayerType::Player,i)); } //ask if they want instructions @@ -303,98 +329,102 @@ impl<'a> GAME<'a> { println!(); //return a game - return GAME { players: players, decks: DECKS::new(), rounds: 0, games_played: 0} + return GAME { players: players, decks: DECKS::new(), games_played: 0} } /** * prints the score of every player */ - fn print_wins(&self) { - println!("Scores:"); - for player in self.players.iter().enumerate() { - match player.1.player_type { - PlayerType::Player => println!("Player{} wins:\t{}", player.0, player.1.wins), - PlayerType::Dealer => println!("Dealer wins:\t{}", player.1.wins) - } - } + fn _print_stats(&self) { + println!("{}", self.stats_as_string()); + } + + /** + * returns a string of the wins, balance, and bets of every player + */ + fn stats_as_string(&self) -> String { + format!("Scores:\n{}",{ + let mut s = String::new(); + self.players.iter().for_each(|p| { + //format the presentation of player stats + match p.player_type { + PlayerType::Player => s+= format!("{} Wins:\t{}\t\tBalance:\t{}\t\tBet\t{}\n",p.get_name(),p.wins,p.balance,p.bet).as_str(), + PlayerType::Dealer => s+= format!("{} Wins:\t{}\n",p.get_name(),p.wins).as_str() + } + }); + s + }) } /** * plays a round of blackjack */ fn play_game(&mut self) { - //print score of every user - self.print_wins(); - + //DATA + let scores; + let game = self.games_played; //save it here so we don't have borrowing issues + let mut player_hands_message: String = String::new();//cache it here so we don't have borrowing issues + //deal cards to each player for _i in 0..2 { // do this twice //draw card for each player self.players.iter_mut().for_each(|player| {player.hand.add_card( self.decks.draw_card() );}); } - //keep track of player who haven't busted or stood yet - let mut players_playing: HashSet = HashSet::new(); // the numbers presenting each player still active in the round - for i in 0..self.players.len() { //runs number of players times - players_playing.insert(i); - } - //round loop (each player either hits or stands) - loop { - //increment rounds - self.rounds += 1; + //get everyones bets + self.players.iter_mut().for_each(|player| player.get_bet()); + scores = self.stats_as_string(); //save it here so we don't have borrowing issues later + + //play game for each player + for player in self.players.iter_mut() { + //turn loop, ends when player finishes their turn + loop{ + //clear screen + clear(); + //print welcome + welcome(); + //print game state + println!("\n\t\t\tGame {}", game); + //print scores + println!("{}",scores); + //print hands of all players + print!("{}", player_hands_message); + println!("{} Hand:\t{}", player.get_name(), player.hand_as_string(true)); - //print game state - println!("\n\t\t\tGame {}\tRound {}", self.games_played, self.rounds); - - self.players.iter().enumerate().for_each(|player| { - match player.1.player_type { - PlayerType::Player => println!("Player{} Hand:\t{}", player.0, player.1.print_hand(true)), - PlayerType::Dealer => println!("Dealer Hand:\t{}", player.1.print_hand(true)) - } - }); - - print!("\n"); //empty line - - //get the moves of all remaining players - for player in self.players.iter_mut().enumerate() { - match player.1.player_type {//print the player name - PlayerType::Player => print!("Player{}:", player.0), - PlayerType::Dealer => print!("Dealer:"), + if player.bet == 0 { //player is out of money + break; //exit turn loop } + //play through turn //check their hand value for a blackjack(21) or bust - let score = player.1.hand.get_total(); + let score = player.hand.get_total(); if score >= 21 { if score == 21 { // == 21 - println!("\tBlackjack (21 points)"); + println!("\tBlackjack! (21 points)"); } else { // > 21 println!("\tBust ({} points)", score); } - players_playing.remove(&player.0);//remove player from playing list - continue; + break; //end turn } - //check if player is still playing - if !players_playing.contains(&player.0) {print!("\n");continue;}//print a line and skip to the next iteration of the loop - - //get play - let play = player.1.get_play(); - + //get player move + let play = player.get_play(); //process play match play { Play::Stand => { - println!("\t{}s", play.to_string()); - players_playing.remove(&player.0);//remove player from playing list + println!("\t{}", play.to_string()); + break; //end turn }, Play::Hit => { - println!("\t{}s", play.to_string()); + println!("\t{}", play.to_string()); //give them a card - player.1.hand.add_card( self.decks.draw_card() ); + player.hand.add_card( self.decks.draw_card() ); }, } } - - //loop end condition - if players_playing.is_empty() {break;} + + //add player to score cache thing + player_hands_message += format!("{} Hand:\t{}\n", player.get_name(), player.hand_as_string(true)).as_str(); } //determine winner @@ -410,34 +440,25 @@ impl<'a> GAME<'a> { } } - //print everyones hand - println!(""); - self.players.iter().enumerate().for_each(|player| { - match player.1.player_type { - PlayerType::Player => println!("Player{} Hand:\t{}", player.0, player.1.print_hand(false)), - PlayerType::Dealer => println!("Dealer Hand:\t{}", player.1.print_hand(false)) - } - }); - println!(""); - //for each player with the top score - self.players.iter_mut().enumerate().filter(|x|->bool{x.1.hand.get_total()==top_score}).for_each(|x| { - match x.1.player_type { - PlayerType::Player => print!("Player{}\t", x.0), - PlayerType::Dealer => print!("Dealer\t"), - } - //increment their wins - x.1.wins += 1; + //print winner(s) + self.players.iter_mut().filter(|x|->bool{x.hand.get_total()==top_score}).for_each(|x| {//for each player with the top score + print!("{} ", x.get_name());//print name + x.wins += 1;//increment their wins }); if num_winners > 1 {println!("all tie with {}\n\n\n", top_score);} else {println!("wins with {}!\n\n\n",top_score);} + //handle bets + //remove money from losers + self.players.iter_mut().filter(|p| p.hand.get_total()!=top_score).for_each( |p| p.balance -= p.bet); //for every player who didn't get the winning score, remove their bet from their balance + //add money to winner + self.players.iter_mut().filter(|p| p.hand.get_total()==top_score).for_each(|p| p.balance += p.bet); //for each player who got the winning score, add their bet to their balance + //discard hands self.players.iter_mut().for_each(|player| {player.hand.discard_hand(&mut self.decks);}); //increment games_played self.games_played += 1; - //reset rounds - self.rounds = 0; } @@ -476,10 +497,10 @@ fn main() { */ fn welcome() { //welcome message - println!(" + print!(" BLACK JACK CREATIVE COMPUTING MORRISTOWN, NEW JERSEY - \n\n\n"); + \n\n"); } /** @@ -502,7 +523,10 @@ fn instructions() { NUMBER OF PLAYERS NOTE: Currently only H and S are implemented properly + + PRESS ENTER TO CONTINUE "); + io::stdin().read_line(&mut String::new()).expect("Failed to read line"); } /** @@ -515,7 +539,7 @@ fn get_number_from_user_input(prompt: &str, min:usize, max:usize) -> usize { //print prompt println!("{}", prompt); - + stdout().flush().expect("Failed to flush to stdout."); //read user input from standard input, and store it to raw_input //raw_input.clear(); //clear input io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); @@ -524,11 +548,10 @@ fn get_number_from_user_input(prompt: &str, min:usize, max:usize) -> usize { match raw_input.trim().parse::() { Ok(i) => { if i < min || i > max { //input out of desired range - println!("INPUT OUT OF VALID RANGE. TRY AGAIN."); + println!("INPUT OUT OF VALID RANGE. TRY AGAIN. {}-{}",min,max); continue; // run the loop again } else { - println!(); break i;// this escapes the loop, returning i } }, @@ -551,7 +574,7 @@ fn get_char_from_user_input(prompt: &str, valid_results: &Vec) -> char { //print prompt println!("{}", prompt); - + stdout().flush().expect("Failed to flush to stdout."); //read user input from standard input, and store it to raw_input //raw_input.clear(); //clear input io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); @@ -574,3 +597,10 @@ fn get_char_from_user_input(prompt: &str, valid_results: &Vec) -> char { }; }; } + +/** + * clear std out + */ +fn clear() { + println!("\x1b[2J\x1b[0;0H"); +} From c289c6f4e5cdb48af7f4975f37c89d8a77191eda Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 23:31:43 -0800 Subject: [PATCH 09/13] 10_blackjack implement doubling down --- 10_Blackjack/rust/src/main.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index 0302527f..622756e9 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -27,12 +27,16 @@ impl ToString for PlayerType { enum Play { Stand, Hit, + DoubleDown, + Split, } impl ToString for Play { fn to_string(&self) -> String { match self { &Play::Hit => return String::from("Hit"), &Play::Stand => return String::from("Stand"), + &Play::DoubleDown => return String::from("Double Down"), + &Play::Split => return String::from("Split") } } } @@ -290,10 +294,12 @@ impl<'a> PLAYER<'a> { } }, &PlayerType::Player => { - let play = get_char_from_user_input("\tWhat is your play?", &vec!['s','S','h','H']); + let play = get_char_from_user_input("\tWhat is your play?", &vec!['s','S','h','H','d','D','/']); match play { 's' | 'S' => return Play::Stand, 'h' | 'H' => return Play::Hit, + 'd' | 'D' => return Play::DoubleDown, + '/' => return Play::Split, _ => panic!("get_char_from_user_input() returned invalid character"), } }, @@ -420,6 +426,22 @@ impl<'a> GAME<'a> { //give them a card player.hand.add_card( self.decks.draw_card() ); }, + Play::DoubleDown => { + println!("\t{}", play.to_string()); + + //double their balance if there's enough money, othewise go all-in + if player.bet * 2 < player.balance { + player.bet *= 2; + } + else { + player.bet = player.balance; + } + //give them a card + player.hand.add_card( self.decks.draw_card() ); + }, + Play::Split => { + + }, } } From be2d8eabe7e58eb0ccd4cd6198cced246fc67868 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 23:33:01 -0800 Subject: [PATCH 10/13] update instructions --- 10_Blackjack/rust/src/main.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index 622756e9..7e3c8c37 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -5,8 +5,6 @@ use std::{io, io::{stdout, Write}}; * todo list: * * allow splitting - * - * allow doubling down */ @@ -544,7 +542,7 @@ fn instructions() { BLACKJACK, THE INITIAL RESPONSE SHOULD BE 'S'. NUMBER OF PLAYERS - NOTE: Currently only H and S are implemented properly + NOTE:'/' (splitting) is not currently implemented, and does nothing PRESS ENTER TO CONTINUE "); From 41d5fbf19a8e994f5cff27a5f7ff417488b36b28 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 23:37:48 -0800 Subject: [PATCH 11/13] bug fix --- 10_Blackjack/rust/src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index 7e3c8c37..e1d9a29d 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -292,7 +292,13 @@ impl<'a> PLAYER<'a> { } }, &PlayerType::Player => { - let play = get_char_from_user_input("\tWhat is your play?", &vec!['s','S','h','H','d','D','/']); + let valid_results:Vec; + if self.hand.cards.len() > 2 {//if there are more than 2 cards in the hand, at least one turn has happened, so splitting and doubling down are not allowed + valid_results = vec!['s','S','h','H']; + } else { + valid_results = vec!['s','S','h','H','d','D','/']; + } + let play = get_char_from_user_input("\tWhat is your play?", &valid_results); match play { 's' | 'S' => return Play::Stand, 'h' | 'H' => return Play::Hit, From 4388202a0e60cb1cdc24bd328efd3ca6aad4521e Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 23:44:18 -0800 Subject: [PATCH 12/13] bug fix dealer wasn't playing because of a logic error, fixed --- 10_Blackjack/rust/src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/10_Blackjack/rust/src/main.rs b/10_Blackjack/rust/src/main.rs index e1d9a29d..e3a0a459 100644 --- a/10_Blackjack/rust/src/main.rs +++ b/10_Blackjack/rust/src/main.rs @@ -401,8 +401,10 @@ impl<'a> GAME<'a> { print!("{}", player_hands_message); println!("{} Hand:\t{}", player.get_name(), player.hand_as_string(true)); - if player.bet == 0 { //player is out of money - break; //exit turn loop + if let PlayerType::Player = player.player_type { //player isn't the dealer + if player.bet == 0 {//player is out of money + break;//exit turn loop + } } //play through turn From 0cf0dd217892368a0564e9b44e4b8506b1ea26b9 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Wed, 2 Mar 2022 21:50:35 -0800 Subject: [PATCH 13/13] remove stuff that is in rust-port-amazing branch --- 02_Amazing/rust/Cargo.toml | 9 -- 02_Amazing/rust/README.md | 3 - 02_Amazing/rust/src/main.rs | 197 ------------------------------------ 3 files changed, 209 deletions(-) delete mode 100644 02_Amazing/rust/Cargo.toml delete mode 100644 02_Amazing/rust/README.md delete mode 100644 02_Amazing/rust/src/main.rs diff --git a/02_Amazing/rust/Cargo.toml b/02_Amazing/rust/Cargo.toml deleted file mode 100644 index 276ea543..00000000 --- a/02_Amazing/rust/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "rust" -version = "0.1.0" -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -rand = "0.8.5" \ No newline at end of file diff --git a/02_Amazing/rust/README.md b/02_Amazing/rust/README.md deleted file mode 100644 index f84e546c..00000000 --- a/02_Amazing/rust/README.md +++ /dev/null @@ -1,3 +0,0 @@ -Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) - -Conversion to [Rust](https://www.rust-lang.org/) by Anthony Rubick [AnthonyMichaelTDM](https://github.com/AnthonyMichaelTDM) \ No newline at end of file diff --git a/02_Amazing/rust/src/main.rs b/02_Amazing/rust/src/main.rs deleted file mode 100644 index bc85189f..00000000 --- a/02_Amazing/rust/src/main.rs +++ /dev/null @@ -1,197 +0,0 @@ -use rand::{Rng, thread_rng, prelude::SliceRandom}; -use std::{io, collections::HashSet}; - -fn main() { - //DATA - enum Direction { - LEFT=0, - UP=1, - RIGHT=2, - DOWN=3, - } - impl Direction { - fn val(&self) -> usize { - match self { - Direction::LEFT=>0, - Direction::UP=>1, - Direction::RIGHT=>2, - Direction::DOWN=>3, - } - } - } - const EXIT_DOWN:usize = 1; - const EXIT_RIGHT:usize = 2; - let mut rng = thread_rng(); //rng - /* - vector of: - vectors of: - integers - Initially set to 0, unprocessed cells. - Filled in with consecutive non-zero numbers as cells are processed - */ - let mut used; //2d vector - /* - vector of: - vectors of: - integers - Remains 0 if there is no exit down or right - Set to 1 if there is an exit down - Set to 2 if there is an exit right - Set to 3 if there are exits down and right - */ - let mut walls; //2d vector - let width; - let height; - let entrance_column; //rng, column of entrance - let mut row; - let mut col; - let mut count; - - - - //print welcome message - println!(" - AMAZING PROGRAM - CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n"); - - //prompt for input - width = get_user_input("What is your width?"); - print!("\n"); //one blank line below - height = get_user_input("What is your height?"); - print!("\n\n\n\n");//4 blank lines below - - //generate maze - //initialize used and wall vectors - //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) - used = vec![0; (width * height) as usize]; - let mut used: Vec<_> = used.as_mut_slice().chunks_mut(width as usize).collect(); - let used = used.as_mut_slice(); //accessible as used[][] - //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) - walls = vec![0; (width * height) as usize]; - let mut walls: Vec<_> = walls.as_mut_slice().chunks_mut(width as usize).collect(); - let walls = walls.as_mut_slice(); //accessible as walls[][] - - entrance_column=rng.gen_range(0..width-1); - row = 0; - col = entrance_column; - count = 1; - used[row][col] = count; - count += 1; - - while count != width*height + 1 { - //remove possible directions that are blocked or - //hit cells already processed - let mut possible_directions: HashSet = vec![Direction::LEFT.val(),Direction::UP.val(),Direction::RIGHT.val(),Direction::DOWN.val()].into_iter().collect(); //create it as a vector bc that's easy, then convert it to a hashset - if col==0 || used[row][col-1]!=0 { - possible_directions.remove(&Direction::LEFT.val()); - } - if row==0 || used[row-1][col]!=0 { - possible_directions.remove(&Direction::UP.val()); - } - if col==width-1 || used[row][col+1]!=0 { - possible_directions.remove(&Direction::RIGHT.val()); - } - if row==height-1 || used[row+1][col]!=0 { - possible_directions.remove(&Direction::DOWN.val()); - } - - //If we can move in a direction, move and make opening - if possible_directions.len() != 0 { //all values in possible_directions are not NONE - let pos_dir_vec: Vec<_> = possible_directions.into_iter().collect(); // convert the set to a vector to get access to the choose method - //select a random direction - match pos_dir_vec.choose(&mut rng).expect("error") { - 0=> { - col -= 1; - walls[row][col] = EXIT_RIGHT; - }, - 1=> { - row -= 1; - walls[row][col] = EXIT_DOWN; - }, - 2=>{ - walls[row][col] = walls[row][col] + EXIT_RIGHT; - col += 1; - }, - 3=>{ - walls[row][col] = walls[row][col] + EXIT_DOWN; - row += 1; - }, - _=>{}, - } - used[row][col]=count; - count += 1; - } - //otherwise, move to the next used cell, and try again - else { - loop { - if col != width-1 {col += 1;} - else if row != height-1 {row+=1; col=0;} - else {row=0;col=0;} - - if used[row][col] != 0 {break;} - } - } - - } - // Add a random exit - col=rng.gen_range(0..width); - row=height-1; - walls[row][col]+=1; - - //print maze - //first line - for c in 0..width { - if c == entrance_column { - print!(". "); - } - else { - print!(".--"); - } - } - println!("."); - //rest of maze - for r in 0..height { - print!("I"); - for c in 0..width { - if walls[r][c]<2 {print!(" I");} - else {print!(" ");} - } - println!(); - for c in 0..width { - if walls[r][c] == 0 || walls[r][c]==2 {print!(":--");} - else {print!(": ");} - } - println!("."); - } - - - - - - -} - -fn get_user_input(prompt: &str) -> usize { - //DATA - let mut raw_input = String::new(); // temporary variable for user input that can be parsed later - - //input loop - return loop { - - //print prompt - println!("{}", prompt); - - //read user input from standard input, and store it to raw_input - raw_input.clear(); //clear input - io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); - - //from input, try to read a number - match raw_input.trim().parse::() { - Ok(i) => break i, // this escapes the loop, returning i - Err(e) => { - println!("MEANINGLESS DIMENSION. TRY AGAIN. {}", e.to_string().to_uppercase()); - continue; // run the loop again - } - }; - } -}