From 70d1cf9b35815ebcabeb68f903aa983fb7cd3b65 Mon Sep 17 00:00:00 2001 From: cdez Date: Mon, 25 Nov 2024 01:05:10 -0700 Subject: [PATCH] Finish cleanup of Assignment 6, complete of Rust, and most of 396's assignments. --- assignmentFive/htmlConvert.cpp | 1 - assignmentSix/src/main.rs | 126 +--------------- battleship.rs | 0 examples/go_fish_final.cpp | 254 +++++++++++++++++++++++++++++++++ examples/test | Bin 16560 -> 17160 bytes examples/test.cpp | 40 ++---- maze.rs | 74 ++++++++++ wordcounter.rs | 28 ++++ 8 files changed, 371 insertions(+), 152 deletions(-) create mode 100644 battleship.rs create mode 100644 examples/go_fish_final.cpp create mode 100644 maze.rs create mode 100644 wordcounter.rs diff --git a/assignmentFive/htmlConvert.cpp b/assignmentFive/htmlConvert.cpp index d411626..e7d42e1 100644 --- a/assignmentFive/htmlConvert.cpp +++ b/assignmentFive/htmlConvert.cpp @@ -75,7 +75,6 @@ int main(int argc, char **argv) { if(currentChar == '<' || currentChar == '>') { - // if(currentChar == '<') { outputFile << "<"; diff --git a/assignmentSix/src/main.rs b/assignmentSix/src/main.rs index 31e94a6..d0d43f3 100644 --- a/assignmentSix/src/main.rs +++ b/assignmentSix/src/main.rs @@ -30,7 +30,6 @@ enum Rank impl fmt::Display for Rank { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // write!(f, "{}", self) match self { Rank::None => write!(f, "{}", 'X'), @@ -50,13 +49,7 @@ impl fmt::Display for Rank { } } } -// impl PartialEq for Rank -// { -// fn eq(&self, other: &Self) -> bool -// { -// return rank_to_number(*self) == rank_to_number(*other); -// } -// } + fn number_to_rank(num_rank: i32) -> Rank { match num_rank @@ -179,20 +172,16 @@ impl Deck let mut card_counter = 0; loop { - // if suit_counter == 4 { - // break; } else { loop { - // if card_counter == 13 { - // break; } else @@ -214,33 +203,26 @@ impl Deck // shuffle deck of cards full_deck.card_deck.shuffle(&mut thread_rng()); // return deck struct - // deck return full_deck; } fn shuffle(mut self) -> Deck { - // self.card_deck.shuffle(&mut thread_rng()); return self; } fn draw(&mut self) -> Card { - // return Card{cardRank: Rank::TWO, cardSuit: Suit::CLUBS}; if !self.card_deck.is_empty() { - // let pull_card = self.card_deck.pop().unwrap(); - // println!("Num cards left: {}", self.card_deck.len()); return self.card_deck.pop().unwrap(); } else { panic!("Card ran out somehow"); } - // return pull_card; } fn is_empty(&self) -> bool { - // return self.card_deck.is_empty(); } } @@ -261,10 +243,8 @@ struct Player impl Player { - // fn new() -> Player { - // let mut hand = Player { player_hand: vec![] @@ -285,15 +265,12 @@ impl Player let mut card_out = vec![]; loop { - // if self.player_hand.is_empty() || self.player_hand.len() - 1 < card_counter { - // break; } if self.player_hand.get(card_counter).unwrap().card_rank == *in_card_rank { - // let current_card = Card { card_rank: self.player_hand.get(card_counter).unwrap().card_rank.clone(), @@ -317,10 +294,8 @@ impl Player { if rank_check > 13 { - // break; } - // for card in &self.player_hand { if card.card_rank == number_to_rank(rank_check) @@ -333,7 +308,6 @@ impl Player self.player_hand.retain(|card| card.card_rank != number_to_rank(rank_check)); return true; } - // println!("{} count is: {}", rank_check, count); rank_check = rank_check + 1; count = 0; } @@ -389,7 +363,6 @@ impl Game { break; } - // full_game.human_player.add_cards(full_game.full_card_deck.draw()); human_cards_vec.push(full_game.full_card_deck.draw()); computer_cards_vec.push(full_game.full_card_deck.draw()); card_count = card_count + 1; @@ -428,7 +401,6 @@ impl Game // draw a card. let mut new_card_vec = vec![]; new_card_vec.push(self.full_card_deck.draw()); - // self.human_player.player_hand.push(self.full_card_deck.draw()); self.human_player.add_cards(new_card_vec) } if self.human_player.check_set() @@ -439,35 +411,18 @@ impl Game fn computer_turn(&mut self) { print!("Computer's Turn... "); - // unneeded - // for card in &*self.computer_player.player_hand - // { - // print!("{} ", card); - // } println!(""); - // substitute picking a rank here - // print!("Computer's turn. Enter a rank (2-10, A/1, J/11, Q/12, K/13): "); - // let _=io::stdout().flush(); - // let mut rank_enter = String::new(); - // let mut rank_num: i32 = 0; - // let mut rank_exit: Rank; - // io::stdin().read_line(&mut rank_enter).expect("String wrong"); - // rank_num = rank_enter.trim().parse().unwrap(); - // rank_exit = number_to_rank(rank_num); - // end substitute let rank_pick = rand::thread_rng().gen_range(1..=13); // check if the computer has the rank requested. let mut temp_vec = self.human_player.has_rank(&number_to_rank(rank_pick)); if !temp_vec.is_empty() { - // println!("Human has {}s.", number_to_rank(rank_pick)); // add the cards from the temp_vec. self.computer_player.add_cards(temp_vec); } else { - // println!("Human does not have {}s. Computer goes fish.", number_to_rank(rank_pick)); // draw a card. let mut new_card_vec = vec![]; @@ -476,19 +431,15 @@ impl Game } if self.computer_player.check_set() { - // self.computer_score = self.computer_score + 1; } } fn play(&mut self) { - // loop { - // CURRENTLY, cards run out somehow without ever actually checking? if self.full_card_deck.card_deck.len() == 0 { - // println!("Game Over."); self.print_scores(); self.winner(); @@ -500,26 +451,21 @@ impl Game } fn print_scores(&mut self) { - // println!("Human Score: {}", self.human_score); println!("Computer Score: {}", self.computer_score); } fn winner(&mut self) { - // if self.human_score > self.computer_score { - // println!("Human Wins with a score of: {}", self.human_score); } else if self.human_score < self.computer_score { - // println!("Computer Wins with a score of: {}", self.computer_score); } else { - // println!("It's a tie!"); } } @@ -529,76 +475,6 @@ fn main() { println!("Welcome to the Go Fish Game!"); println!("----------------------------"); - // TEMP START - println!("Working game"); - // let mut card_vec = vec![]; - // card_vec.push(Card{card_rank: Rank::FIVE, card_suit: Suit::HEARTS}); - // card_vec.push(Card{card_rank: Rank::THREE, card_suit: Suit::CLUBS}); - // card_vec.push(Card{card_rank: Rank::FIVE, card_suit: Suit::DIAMONDS}); - // card_vec.push(Card{card_rank: Rank::NINE, card_suit: Suit::HEARTS}); - // card_vec.push(Card{card_rank: Rank::FIVE, card_suit: Suit::SPADES}); - // card_vec.push(Card{card_rank: Rank::NINE, card_suit: Suit::CLUBS}); - // card_vec.push(Card{card_rank: Rank::ACE, card_suit: Suit::HEARTS}); - // card_vec.push(Card{card_rank: Rank::NINE, card_suit: Suit::DIAMONDS}); - // card_vec.push(Card{card_rank: Rank::FIVE, card_suit: Suit::CLUBS}); - // card_vec.push(Card{card_rank: Rank::NINE, card_suit: Suit::SPADES}); - // let mut current_player = Player::new(); - // current_player.player_hand.push(Card{card_rank: Rank::FIVE, card_suit: Suit::HEARTS}); - // current_player.player_hand.push(Card{card_rank: Rank::THREE, card_suit: Suit::CLUBS}); - // current_player.player_hand.push(Card{card_rank: Rank::FIVE, card_suit: Suit::DIAMONDS}); - // current_player.player_hand.push(Card{card_rank: Rank::NINE, card_suit: Suit::HEARTS}); - // current_player.player_hand.push(Card{card_rank: Rank::FIVE, card_suit: Suit::SPADES}); - // current_player.player_hand.push(Card{card_rank: Rank::NINE, card_suit: Suit::CLUBS}); - // current_player.player_hand.push(Card{card_rank: Rank::ACE, card_suit: Suit::HEARTS}); - // current_player.player_hand.push(Card{card_rank: Rank::NINE, card_suit: Suit::DIAMONDS}); - // current_player.player_hand.push(Card{card_rank: Rank::FIVE, card_suit: Suit::CLUBS}); - // current_player.player_hand.push(Card{card_rank: Rank::NINE, card_suit: Suit::SPADES}); - // current_player.add_cards(card_vec); - // for card in ¤t_player.player_hand - // { - // // - // println!("After deal: {}", card); - // } - // current_player.check_set(); - // for card in ¤t_player.player_hand - // { - // // - // println!("After set check: {}", card); - // } - // current_player.check_set(); - // for card in ¤t_player.player_hand - // { - // // - // println!("After 2nd set check: {}", card); - // } - // println!("{}{}", Rank::TWO, Suit::DIAMONDS); - // let tempCard = Card - // { - // cardRank: Rank::ACE, - // cardSuit: Suit::HEARTS - // }; - // println!("{}", tempCard); - // deck creation works. - // let mut tempDeck = Deck::new(); - // let mut cardCount = 52; - // loop - // { - // if cardCount == 0 - // { - // break; - // } - // else - // { - // // - // println!("{}", tempDeck.cardDeck.get(cardCount - 1).unwrap()); - // cardCount = cardCount - 1; - // } - // } - // println!("Drawn Card: {}", tempDeck.draw()); // verified works. - // tempDeck = tempDeck.shuffle(); // verified works - // TEMP END let mut game = Game::new(); game.play(); - // game.print_scores(); - // println!("The winner is: {}", game.winner()); } diff --git a/battleship.rs b/battleship.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/go_fish_final.cpp b/examples/go_fish_final.cpp new file mode 100644 index 0000000..d4d84cc --- /dev/null +++ b/examples/go_fish_final.cpp @@ -0,0 +1,254 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +enum Rank { + TWO = 2, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, + TEN, + JACK, + QUEEN, + KING, + ACE +}; + +enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES }; + +struct Card { + Rank rank; + Suit suit; + + friend ostream &operator<<(ostream &os, const Card &card) { + const string ranks[] = {"2", "3", "4", "5", "6", "7", "8", + "9", "10", "J", "Q", "K", "A"}; + const string suits[] = {"H", "D", "C", "S"}; + + os << ranks[static_cast(card.rank) - 2] + << suits[static_cast(card.suit)]; + return os; + } +}; + +class Deck { +private: + vector cards; + + void shuffle() { + random_device rd; + mt19937 g(rd()); + std::shuffle(cards.begin(), cards.end(), g); + } + +public: + Deck() { + for (int i = static_cast(Rank::TWO); i <= static_cast(Rank::ACE); + i++) { + for (int j = static_cast(Suit::HEARTS); + j <= static_cast(Suit::SPADES); j++) { + cards.push_back(Card{static_cast(i), static_cast(j)}); + } + } + + shuffle(); + } + + Card draw() { + if (cards.empty()) { + throw runtime_error("No more cards in the deck!"); + } + + Card card = cards.back(); + cards.pop_back(); + return card; + } + + bool empty() const { return cards.empty(); } +}; + +class Player { +public: + vector hand; + map rank_count; + + void draw_card(Deck &deck) { + try { + Card card = deck.draw(); + hand.push_back(card); + rank_count[card.rank]++; + } catch (const runtime_error &e) { + cout << e.what() << endl; + } + } + + bool has_rank(Rank rank) const { + return rank_count.count(rank) > 0 && rank_count.at(rank) > 0; + } + + vector remove_rank(Rank rank) { + vector removed_cards; + + for (auto it = hand.begin(); it != hand.end();) { + if (it->rank == rank) { + removed_cards.push_back(*it); + rank_count[it->rank]--; + it = hand.erase(it); + } else { + it++; + } + } + + return removed_cards; + } + + bool check_for_set() { + for (const auto &pair : rank_count) { + if (pair.second == 4) { + cout << "Player has a set of " << static_cast(pair.first) << "s" + << endl; + rank_count[pair.first] = 0; + + hand.erase(remove_if(hand.begin(), hand.end(), + [pair](const Card &card) { + return card.rank == pair.first; + }), + hand.end()); + return true; + } + } + + return false; + } + + void add_cards(const vector &cards) { + for (const Card &card : cards) { + hand.push_back(card); + rank_count[card.rank]++; + } + } +}; + +class Game { +private: + Deck deck; + Player human; + Player computer; + int human_score; + int computer_score; + + void deal_initial_cards() { + for (int i = 0; i < 7; i++) { + human.draw_card(deck); + computer.draw_card(deck); + } + } + + void human_turn() { + Rank requested_rank = get_human_request(); + if (computer.has_rank(requested_rank)) { + cout << "Computer has " << static_cast(requested_rank) << "s" + << endl; + vector cards = computer.remove_rank(requested_rank); + human.add_cards(cards); + } else { + cout << "Computer does not have the rank. Go fish!" << endl; + human.draw_card(deck); + } + + if (human.check_for_set()) { + human_score++; + } + } + + void computer_turn() { + cout << "Computer's turn..." << endl; + Rank requested_rank = get_computer_request(); + if (human.has_rank(requested_rank)) { + cout << "Player has " << static_cast(requested_rank) << "s" << endl; + vector cards = human.remove_rank(requested_rank); + computer.add_cards(cards); + } else { + cout << "Player does not have the rank. Computer goes fishing!" << endl; + computer.draw_card(deck); + } + + if (computer.check_for_set()) { + computer_score++; + } + } + + Rank get_human_request() { + cout << "Your turn. Enter the rank (2-14 where 11=J, 12=Q, 13=K, 14=A): "; + int rank; + cin >> rank; + return static_cast(rank); + } + + Rank get_computer_request() { + vector available_ranks; + for (const auto &pair : computer.rank_count) { + if (pair.second > 0) { + available_ranks.push_back(pair.first); + } + } + + return available_ranks[rand() % available_ranks.size()]; + } + + void show_hand(const vector &hand) const { + for (const Card &card : hand) { + cout << card << " "; + } + cout << endl; + } + +public: + Game() : human_score(0), computer_score(0) { deal_initial_cards(); } + + void play() { + while (true) { + cout << endl << "Your hand: "; + show_hand(human.hand); + + human_turn(); + computer_turn(); + + if (deck.empty() && human.hand.empty() && computer.hand.empty()) { + cout << "Game over!" << endl; + cout << "Your score: " << human_score << endl; + cout << "Computer score: " << computer_score << endl; + + if (human_score > computer_score) { + cout << "You win!" << endl; + } else if (human_score < computer_score) { + cout << "Computer wins!" << endl; + } else { + cout << "It's a tie!" << endl; + } + + break; + } + } + } +}; + +int main(void) { + srand(static_cast(time(nullptr))); + + Game game; + game.play(); + + return 0; +} diff --git a/examples/test b/examples/test index aa9d41f7a1883b59fdafe4fb4042894b882c579c..87d4865bac8276309298260e85365818c9047f6d 100755 GIT binary patch literal 17160 zcmeHOeQX@n5r1dr!;lXvNn_hIh# z5*tyPHYLQQp$1ie3aUa>l~7cgB2)oFjUg?BO3(z_G_?Puv`DrTXeccWRL%7_@4Z>? zEq4_}RnI|PqfQZeiGl@F_K+y)m1>>msu8B5M)9OKmAabu z)13B$DUZAJ8KjJ6$p0>VlmyV2di5wa!Fo9?f~yPZ2_2U@-Wlx|!K8Pq*1J{fF|E;h zOgWxphPIV@Km4=-W7L@X^#-(_pSCGA!Ib8p*8PqXJDd4Bwcem1%?LkjR+a3RJ|G$Ln{-Hq z8rjn>x;)EJJQ~FPQl9shF59E_>&do1jL)upcxJ0qD)R3sVCNOVZz_S0mB15lwwRq6 zCGh)8*mmFrrt<49<|b0 zCu2vF?LAgcgB9vt0SB~^&b3|L>250o2cts~%NfX|x9NT_oJ(2Jh!Y*qNFxHD{RZIBv?Z2PBk zINAeums_1TMAgnl(>X^wgZ*abHm0`4!FAc$ToUmkFQPis$w(Yd%6;pLlM{E?0>fD+ z7G1K0?guM~?T+0^WImzm2|f72$2G@DJ!ysT*5 zw5erHyVYnm(V?%voxK8gnko33h93P*!BsyUTL5;){9YT(H za5(I?;~t#uv)oR2@Rum>kw-1d5KiV?T{9-L!MV+TCA{s|>jp7r23q1-m+!DqQ>xl@in zIRfPflq2x}5`hn@Fa0>Y<78EM_tdwR3K710%&Ev93Ge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3zl;Vf9HxXCfFaw-_PtemVy zGNm7;R07_ZQ$EyFr7XCPQ1*xeXUZI-BMdywBX~-edBgm75dHs%S2?~BRPmo*>R@{jiebci&f5$WBINW~LWBLu)(;9(!UMDae)sZ;I+p^Ybq4j1c5DHUMv4ZT$#&7>HFv8{14Fpv4{Z`TS;S)Uy+&-9efepCyXHnO3nY{&E|pZ&BJFujon#%IrNf9|vA??tA( z{$_oD{(hzHOJTxVQ&V1FYgEn^t`mJ87v-PrtHWSWnBJ<`(%m2b50H56Q(>jbw1XRc=4Oe^vqGwtMYo dTNJm_C*-)SOUp5ziWI%{d8M)5XW(PSe*l!cY?S~2 diff --git a/examples/test.cpp b/examples/test.cpp index 4b51cd3..c4c4da0 100644 --- a/examples/test.cpp +++ b/examples/test.cpp @@ -10,33 +10,21 @@ using namespace std; -class A -{ - int i; - -public: - A(int ii = 0) : i(ii) {} - void show() { cout << i << endl; } -}; - -class B -{ - int x; - -public: - B(int xx) : x(xx) {} - operator A() const { return A(x); } -}; - -void g(A a) -{ - a.show(); -} - int main() { - B b(10); - g(b); - g(20); + int wait_time = 46; + try + { + cout << "Try block entered.\n"; + if (wait_time > 30) + throw wait_time; + cout << "Leaving try block.\n"; + } + catch (int thrown_value) + { + cout << "Exception thrown with\n" + << "wait_time equal to " << thrown_value << endl; + } + cout << "After catch block." << endl; return 0; } \ No newline at end of file diff --git a/maze.rs b/maze.rs new file mode 100644 index 0000000..f52bcc6 --- /dev/null +++ b/maze.rs @@ -0,0 +1,74 @@ +use std::io; + +fn main() +{ + let maze = [[' ', ' ', 'X'], [' ', ' ', ' '], [' ', 'E', ' ']]; + let mut x = 0; + let mut y = 0; + + loop + { + if maze[x][y] == 'E' + { + println!("You found the exit."); + break; + } + + let mut moves = Vec::new(); + if x > 0 && maze[x - 1][y] != 'X' + { + // north + moves.push("North"); + } + if x < 2 && maze[x + 1][y] != 'X' + { + // south + moves.push("South"); + } + if y > 0 && maze[x][y - 1] != 'X' + { + // west + moves.push("West"); + } + if y < 2 && maze[x][y + 1] != 'X' + { + // east + moves.push("East"); + } + println!("There are open doors: {}", moves.join(", ")); + println!("Which door do you want to walk through?"); + + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read input"); + input = input.trim().to_uppercase(); + + match input.as_str() + { + "N" if x > 0 && maze[x - 1][y] != 'X' => + { + x = x - 1; + } + "S" if x < 2 && maze[x + 1][y] != 'X' => + { + x = x + 1; + } + "E" if y < 2 && maze[x][y + 1] != 'X' => + { + y = y + 1; + } + "W" if y > 0 && maze[x][y - 1] != 'X' => + { + y = y - 1; + } + "Q" => + { + println!("You quit the game."); + break; + } + _ => + { + println!("Invalid move."); + } + } + } +} \ No newline at end of file diff --git a/wordcounter.rs b/wordcounter.rs new file mode 100644 index 0000000..db4028b --- /dev/null +++ b/wordcounter.rs @@ -0,0 +1,28 @@ +use std::io; + +fn main() +{ + // input from the user to get the number of sentences they'd like to enter + println!("How many sentences would you like to input?"); + + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + + let mut num_sentences: u32 = input.trim().parse().expect("Please enter a number"); + let mut total_words = 0; + + // loop that would take input for each sentence + // calculates the number of words + while num_sentences > 0 + { + println!("Please enter a sentence:"); + input.clear(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + let num_words = input.split_whitespace().count(); + println!("The sentences has {} words", num_words); + total_words = total_words + num_words; + num_sentences = num_sentences - 1; + } + + println!("Total number of words: {}", total_words); +} \ No newline at end of file