Finish cleanup of Assignment 6, complete of Rust, and most of 396's assignments.
This commit is contained in:
parent
5bdbe09523
commit
70d1cf9b35
8 changed files with 371 additions and 152 deletions
|
|
@ -75,7 +75,6 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if(currentChar == '<' || currentChar == '>')
|
if(currentChar == '<' || currentChar == '>')
|
||||||
{
|
{
|
||||||
//
|
|
||||||
if(currentChar == '<')
|
if(currentChar == '<')
|
||||||
{
|
{
|
||||||
outputFile << "<";
|
outputFile << "<";
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ enum Rank
|
||||||
|
|
||||||
impl fmt::Display for Rank {
|
impl fmt::Display for Rank {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
// write!(f, "{}", self)
|
|
||||||
match self
|
match self
|
||||||
{
|
{
|
||||||
Rank::None => write!(f, "{}", 'X'),
|
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
|
fn number_to_rank(num_rank: i32) -> Rank
|
||||||
{
|
{
|
||||||
match num_rank
|
match num_rank
|
||||||
|
|
@ -179,20 +172,16 @@ impl Deck
|
||||||
let mut card_counter = 0;
|
let mut card_counter = 0;
|
||||||
loop
|
loop
|
||||||
{
|
{
|
||||||
//
|
|
||||||
if suit_counter == 4
|
if suit_counter == 4
|
||||||
{
|
{
|
||||||
//
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
loop
|
loop
|
||||||
{
|
{
|
||||||
//
|
|
||||||
if card_counter == 13
|
if card_counter == 13
|
||||||
{
|
{
|
||||||
//
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -214,33 +203,26 @@ impl Deck
|
||||||
// shuffle deck of cards
|
// shuffle deck of cards
|
||||||
full_deck.card_deck.shuffle(&mut thread_rng());
|
full_deck.card_deck.shuffle(&mut thread_rng());
|
||||||
// return deck struct
|
// return deck struct
|
||||||
// deck
|
|
||||||
return full_deck;
|
return full_deck;
|
||||||
}
|
}
|
||||||
fn shuffle(mut self) -> Deck
|
fn shuffle(mut self) -> Deck
|
||||||
{
|
{
|
||||||
//
|
|
||||||
self.card_deck.shuffle(&mut thread_rng());
|
self.card_deck.shuffle(&mut thread_rng());
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
fn draw(&mut self) -> Card
|
fn draw(&mut self) -> Card
|
||||||
{
|
{
|
||||||
// return Card{cardRank: Rank::TWO, cardSuit: Suit::CLUBS};
|
|
||||||
if !self.card_deck.is_empty()
|
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();
|
return self.card_deck.pop().unwrap();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
panic!("Card ran out somehow");
|
panic!("Card ran out somehow");
|
||||||
}
|
}
|
||||||
// return pull_card;
|
|
||||||
}
|
}
|
||||||
fn is_empty(&self) -> bool
|
fn is_empty(&self) -> bool
|
||||||
{
|
{
|
||||||
//
|
|
||||||
return self.card_deck.is_empty();
|
return self.card_deck.is_empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -261,10 +243,8 @@ struct Player
|
||||||
|
|
||||||
impl Player
|
impl Player
|
||||||
{
|
{
|
||||||
//
|
|
||||||
fn new() -> Player
|
fn new() -> Player
|
||||||
{
|
{
|
||||||
//
|
|
||||||
let mut hand = Player
|
let mut hand = Player
|
||||||
{
|
{
|
||||||
player_hand: vec![]
|
player_hand: vec![]
|
||||||
|
|
@ -285,15 +265,12 @@ impl Player
|
||||||
let mut card_out = vec![];
|
let mut card_out = vec![];
|
||||||
loop
|
loop
|
||||||
{
|
{
|
||||||
//
|
|
||||||
if self.player_hand.is_empty() || self.player_hand.len() - 1 < card_counter
|
if self.player_hand.is_empty() || self.player_hand.len() - 1 < card_counter
|
||||||
{
|
{
|
||||||
//
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if self.player_hand.get(card_counter).unwrap().card_rank == *in_card_rank
|
if self.player_hand.get(card_counter).unwrap().card_rank == *in_card_rank
|
||||||
{
|
{
|
||||||
//
|
|
||||||
let current_card = Card
|
let current_card = Card
|
||||||
{
|
{
|
||||||
card_rank: self.player_hand.get(card_counter).unwrap().card_rank.clone(),
|
card_rank: self.player_hand.get(card_counter).unwrap().card_rank.clone(),
|
||||||
|
|
@ -317,10 +294,8 @@ impl Player
|
||||||
{
|
{
|
||||||
if rank_check > 13
|
if rank_check > 13
|
||||||
{
|
{
|
||||||
//
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
for card in &self.player_hand
|
for card in &self.player_hand
|
||||||
{
|
{
|
||||||
if card.card_rank == number_to_rank(rank_check)
|
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));
|
self.player_hand.retain(|card| card.card_rank != number_to_rank(rank_check));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// println!("{} count is: {}", rank_check, count);
|
|
||||||
rank_check = rank_check + 1;
|
rank_check = rank_check + 1;
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -389,7 +363,6 @@ impl Game
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// full_game.human_player.add_cards(full_game.full_card_deck.draw());
|
|
||||||
human_cards_vec.push(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());
|
computer_cards_vec.push(full_game.full_card_deck.draw());
|
||||||
card_count = card_count + 1;
|
card_count = card_count + 1;
|
||||||
|
|
@ -428,7 +401,6 @@ impl Game
|
||||||
// draw a card.
|
// draw a card.
|
||||||
let mut new_card_vec = vec![];
|
let mut new_card_vec = vec![];
|
||||||
new_card_vec.push(self.full_card_deck.draw());
|
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)
|
self.human_player.add_cards(new_card_vec)
|
||||||
}
|
}
|
||||||
if self.human_player.check_set()
|
if self.human_player.check_set()
|
||||||
|
|
@ -439,35 +411,18 @@ impl Game
|
||||||
fn computer_turn(&mut self)
|
fn computer_turn(&mut self)
|
||||||
{
|
{
|
||||||
print!("Computer's Turn... ");
|
print!("Computer's Turn... ");
|
||||||
// unneeded
|
|
||||||
// for card in &*self.computer_player.player_hand
|
|
||||||
// {
|
|
||||||
// print!("{} ", card);
|
|
||||||
// }
|
|
||||||
println!("");
|
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);
|
let rank_pick = rand::thread_rng().gen_range(1..=13);
|
||||||
// check if the computer has the rank requested.
|
// check if the computer has the rank requested.
|
||||||
let mut temp_vec = self.human_player.has_rank(&number_to_rank(rank_pick));
|
let mut temp_vec = self.human_player.has_rank(&number_to_rank(rank_pick));
|
||||||
if !temp_vec.is_empty()
|
if !temp_vec.is_empty()
|
||||||
{
|
{
|
||||||
//
|
|
||||||
println!("Human has {}s.", number_to_rank(rank_pick));
|
println!("Human has {}s.", number_to_rank(rank_pick));
|
||||||
// add the cards from the temp_vec.
|
// add the cards from the temp_vec.
|
||||||
self.computer_player.add_cards(temp_vec);
|
self.computer_player.add_cards(temp_vec);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//
|
|
||||||
println!("Human does not have {}s. Computer goes fish.", number_to_rank(rank_pick));
|
println!("Human does not have {}s. Computer goes fish.", number_to_rank(rank_pick));
|
||||||
// draw a card.
|
// draw a card.
|
||||||
let mut new_card_vec = vec![];
|
let mut new_card_vec = vec![];
|
||||||
|
|
@ -476,19 +431,15 @@ impl Game
|
||||||
}
|
}
|
||||||
if self.computer_player.check_set()
|
if self.computer_player.check_set()
|
||||||
{
|
{
|
||||||
//
|
|
||||||
self.computer_score = self.computer_score + 1;
|
self.computer_score = self.computer_score + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn play(&mut self)
|
fn play(&mut self)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
loop
|
loop
|
||||||
{
|
{
|
||||||
// CURRENTLY, cards run out somehow without ever actually checking?
|
|
||||||
if self.full_card_deck.card_deck.len() == 0
|
if self.full_card_deck.card_deck.len() == 0
|
||||||
{
|
{
|
||||||
//
|
|
||||||
println!("Game Over.");
|
println!("Game Over.");
|
||||||
self.print_scores();
|
self.print_scores();
|
||||||
self.winner();
|
self.winner();
|
||||||
|
|
@ -500,26 +451,21 @@ impl Game
|
||||||
}
|
}
|
||||||
fn print_scores(&mut self)
|
fn print_scores(&mut self)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
println!("Human Score: {}", self.human_score);
|
println!("Human Score: {}", self.human_score);
|
||||||
println!("Computer Score: {}", self.computer_score);
|
println!("Computer Score: {}", self.computer_score);
|
||||||
}
|
}
|
||||||
fn winner(&mut self)
|
fn winner(&mut self)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
if self.human_score > self.computer_score
|
if self.human_score > self.computer_score
|
||||||
{
|
{
|
||||||
//
|
|
||||||
println!("Human Wins with a score of: {}", self.human_score);
|
println!("Human Wins with a score of: {}", self.human_score);
|
||||||
}
|
}
|
||||||
else if self.human_score < self.computer_score
|
else if self.human_score < self.computer_score
|
||||||
{
|
{
|
||||||
//
|
|
||||||
println!("Computer Wins with a score of: {}", self.computer_score);
|
println!("Computer Wins with a score of: {}", self.computer_score);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//
|
|
||||||
println!("It's a tie!");
|
println!("It's a tie!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -529,76 +475,6 @@ fn main()
|
||||||
{
|
{
|
||||||
println!("Welcome to the Go Fish Game!");
|
println!("Welcome to the Go Fish Game!");
|
||||||
println!("----------------------------");
|
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();
|
let mut game = Game::new();
|
||||||
game.play();
|
game.play();
|
||||||
// game.print_scores();
|
|
||||||
// println!("The winner is: {}", game.winner());
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
0
battleship.rs
Normal file
0
battleship.rs
Normal file
254
examples/go_fish_final.cpp
Normal file
254
examples/go_fish_final.cpp
Normal file
|
|
@ -0,0 +1,254 @@
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
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<int>(card.rank) - 2]
|
||||||
|
<< suits[static_cast<int>(card.suit)];
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Deck {
|
||||||
|
private:
|
||||||
|
vector<Card> cards;
|
||||||
|
|
||||||
|
void shuffle() {
|
||||||
|
random_device rd;
|
||||||
|
mt19937 g(rd());
|
||||||
|
std::shuffle(cards.begin(), cards.end(), g);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Deck() {
|
||||||
|
for (int i = static_cast<int>(Rank::TWO); i <= static_cast<int>(Rank::ACE);
|
||||||
|
i++) {
|
||||||
|
for (int j = static_cast<int>(Suit::HEARTS);
|
||||||
|
j <= static_cast<int>(Suit::SPADES); j++) {
|
||||||
|
cards.push_back(Card{static_cast<Rank>(i), static_cast<Suit>(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<Card> hand;
|
||||||
|
map<Rank, int> 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<Card> remove_rank(Rank rank) {
|
||||||
|
vector<Card> 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<int>(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<Card> &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<int>(requested_rank) << "s"
|
||||||
|
<< endl;
|
||||||
|
vector<Card> 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<int>(requested_rank) << "s" << endl;
|
||||||
|
vector<Card> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Rank get_computer_request() {
|
||||||
|
vector<Rank> 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<Card> &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<unsigned>(time(nullptr)));
|
||||||
|
|
||||||
|
Game game;
|
||||||
|
game.play();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
examples/test
BIN
examples/test
Binary file not shown.
|
|
@ -10,33 +10,21 @@
|
||||||
|
|
||||||
using namespace std;
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
B b(10);
|
int wait_time = 46;
|
||||||
g(b);
|
try
|
||||||
g(20);
|
{
|
||||||
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
74
maze.rs
Normal file
74
maze.rs
Normal file
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
wordcounter.rs
Normal file
28
wordcounter.rs
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue