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 aa9d41f..87d4865 100755 Binary files a/examples/test and b/examples/test differ 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