Question

In: Computer Science

Write fairly complete pseudocode for the Fish Wars card game.   Fish Wars is a card game...

Write fairly complete pseudocode for the Fish Wars card game.   Fish Wars is a card game where two players get 13 cards each. The cards have face values of 1-10 plus one shark card and two peace cards.

The game is played in 13 rounds. Each round, the players each play one card face down and then flip them up at the same time. The player with the higher number (plus any battle bonus) wins. No one wins on a tie. A round winner gets one point added to their score plus one battle bonus point. Play continues until all thirteen rounds are complete and the highest final score wins.

Play exception. If you play a shark card then you always win unless the opponent plays a peace card then it is a tie and no one wins. If you play a point card or a shark card against a peace card then it is a draw plus the player who did not play the peace card has their battle bonus reduced to zero.    If two peace cards are played then it is just a tie.

Your job is to write a program that simulates the Fish Wars game between a human player and an AI opponent. To start, the AI opponent can just randomly select a card. Have the program do all scoring and keep track of the battle bonus. Also make it so that the player can play the game again at the end if they want to do so.

Example of Play:

Round 1: 5 vs. 6 - Player 2 wins!    Score 0 : 1 (BB: 1)
Round 2:   3 vs. 2 (+1 BB) - Draw!    Score 0 : 1 (BB: 1)
Round 3: Shark vs. 7 (+1 BB) - Player 1 wins! Score 1 (BB: 1) - 1 (BB: 1)
Round 4: Peace (+1 BB) vs 5 (+1 BB) - Draw- Player 2 BB reduced to 0; Score 1(BB: 1) - 1 (BB: 0)
Round 5: 2 (+1 BB) vs. Shark - Player 2 wins! Score 1(BB: 1) - 2 (BB: 1)

   For this program, do not use classes and objects plus use standard C++ code and the entire code must be in one file with at most three functions.

Solutions

Expert Solution

 
#include <time.h>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
 
const std::string s = "CDHS", v = "A23456789TJQK";
const int handCards = 9, drawCards = 3;
 
class card {
public:
    friend std::ostream& operator<< (std::ostream& os, const card& c ) { 
        os << v[c.val] << s[c.suit]; 
        return os;
    }
    bool isValid()                       { return val > -1; }
    void set( char s, char v )           { suit = s; val = v; }
    char getRank()                       { return v[val]; }
    bool operator == ( const char o )    { return v[val] == o; }
    bool operator < ( const card& a )    { if( val == a.val ) return suit < a.suit; return val < a.val; }
private:
    char                                 suit, val;
};
class deck {
public:
    static deck* instance() {
        if( !inst ) inst = new deck();
        return inst;
    }
    void destroy() {
        delete inst;
        inst = 0;
    }
    card draw() {
        card c;
        if( cards.size() > 0 ) { 
            c = cards.back();
            cards.pop_back();
            return c; 
        }
        c.set( -1, -1 );
        return c;
    }
private:
    deck() { 
        newDeck(); 
    }
    void newDeck() {
        card c; 
        for( char s = 0; s < 4; s++ ) {
            for( char v = 0; v < 13; v++ ) {
                c.set( s, v ); 
                cards.push_back( c ); 
            }
        }
        random_shuffle( cards.begin(), cards.end() );
        random_shuffle( cards.begin(), cards.end() );
    }
    static deck* inst;
    std::vector<card> cards;
};
class player {
public:
    player( std::string n ) : nm( n ) { 
        for( int x = 0; x < handCards; x++ )
            hand.push_back( deck::instance()->draw() );
        sort( hand.begin(), hand.end() );  
    }
    void outputHand() { 
        for( std::vector<card>::iterator x = hand.begin(); x != hand.end(); x++ ) 
            std::cout << ( *x ) << " ";
        std::cout << "\n"; 
    }
    bool addCard( card c ) { 
        hand.push_back( c );
        return checkForBook();
    }
    std::string name() { 
        return nm; 
    }
    bool holds( char c ) { 
        return( hand.end() != find( hand.begin(), hand.end(), c ) ); 
    }
    card takeCard( char c ) {
        std::vector<card>::iterator it = find( hand.begin(), hand.end(), c );
        std::swap( ( *it ), hand.back() );
        card d = hand.back();
        hand.pop_back();
        hasCards();
        sort( hand.begin(), hand.end() ); 
        return d;
    }
    size_t getBooksCount() {
        return books.size();
    }
    void listBooks() {
        for( std::vector<char>::iterator it = books.begin(); it != books.end(); it++ )
            std::cout << ( *it ) << "'s ";
        std::cout << "\n";
    }
    bool checkForBook() {
        bool ret = false;
        std::map<char, int> countMap;
        for( std::vector<card>::iterator it = hand.begin(); it != hand.end(); it++ )
            countMap[( *it ).getRank()]++;
        for( std::map<char, int>::iterator it = countMap.begin(); it != countMap.end(); it++ ) {
            if( ( *it ).second == 4 ) {
                do {
                    takeCard( ( *it ).first );
                } while( holds( ( *it ).first ) );
                books.push_back( ( *it ).first );
                ( *it ).second = 0;
                ret = true;
            }
        }
        sort( hand.begin(), hand.end() );
        return ret;
    }
    bool hasCards() {
        if( hand.size() < 1 ) {
            card c;
            for( int x = 0; x < drawCards; x++ ) {
                c = deck::instance()->draw();
                if( c.isValid() ) addCard( c );
                else break;
            }
        }
        return( hand.size() > 0 );
    }
protected:
    std::string nm; 
    std::vector<card> hand;
    std::vector<char> books;
};
class aiPlayer : public player {
public:
    aiPlayer( std::string n ) : player( n ), askedIdx( -1 ), lastAsked( 0 ), nextToAsk( -1 ) { }
    void rememberCard( char c ) {
        if( asked.end() != find( asked.begin(), asked.end(), c ) || !asked.size() )
            asked.push_back( c );  
    }
    char makeMove() {
        if( askedIdx < 0 || askedIdx >= static_cast<int>( hand.size() ) ) {
            askedIdx = rand() % static_cast<int>( hand.size() );
        }
 
        char c;
        if( nextToAsk > -1 ) {
            c = nextToAsk;
            nextToAsk = -1;
        } else {
            while( hand[askedIdx].getRank() == lastAsked ) {
                if( ++askedIdx == hand.size() ) {
                    askedIdx = 0;
                    break;
                }
            }
            c = hand[askedIdx].getRank();
            if( rand() % 100 > 25 && asked.size() ) {
                for( std::vector<char>::iterator it = asked.begin(); it != asked.end(); it++ ) {
                    if( holds( *it ) ) {
                        c = ( *it );
                        break;
                    }
                }
            }
        }
        lastAsked = c;
        return c;
    }
    void clearMemory( char c ) {
        std::vector<char>::iterator it = find( asked.begin(), asked.end(), c );
        if( asked.end() != it ) {
            std::swap( ( *it ), asked.back() );
            asked.pop_back();
        }
    }
    bool addCard( card c ) {
        if( !holds( c.getRank() ) )
            nextToAsk = c.getRank();
        return player::addCard( c );
    }
private:
    std::vector<char> asked;
    char nextToAsk, lastAsked;
    int askedIdx;
};
class goFish {
public:
    goFish() {
        plr = true; 
        std::string n; 
        std::cout << "Hi there, enter your name: "; std::cin >> n; 
        p1 = new player( n ); 
        p2 = new aiPlayer( "JJ" );
    }
    ~goFish() { 
        if( p1 ) delete p1; 
        if( p2 ) delete p2;
        deck::instance()->destroy();
    }
    void play() {
        while( true ) {
            if( process( getInput() ) ) break;
        }
        std::cout << "\n\n";
        showBooks();
        if( p1->getBooksCount() > p2->getBooksCount() ) {
            std::cout << "\n\n\t*** !!! CONGRATULATIONS !!! ***\n\n\n";
        } else {
            std::cout << "\n\n\t*** !!! YOU LOSE - HA HA HA !!! ***\n\n\n";
        }
    }
private:
    void showBooks() {
        if( p1->getBooksCount() > 0 ) {
            std::cout << "\nYour Book(s): ";
            p1->listBooks();
        }
        if( p2->getBooksCount() > 0 ) {
            std::cout << "\nMy Book(s): ";
            p2->listBooks();
        }
    }
    void showPlayerCards() {
        std::cout << "\n\n" << p1->name() << ", these are your cards:\n";
        p1->outputHand();
        showBooks();
    }
    char getInput() {
        char c;
        if( plr ) {
            if( !p1->hasCards() ) return -1;
            showPlayerCards();
            std::string w;
            while( true ) {
                std::cout << "\nWhat card(rank) do you want? "; std::cin >> w;
                c = toupper( w[0] );
                if( p1->holds( c ) ) break; 
                std::cout << p1->name() << ", you can't ask for a card you don't have!\n\n"; 
            }
        } else {
            if( !p2->hasCards() ) return -1;
            c = p2->makeMove();
            showPlayerCards();
            std::string r;
            std::cout << "\nDo you have any " << c << "'s? (Y)es / (G)o Fish ";
            do {
                std::getline( std::cin, r );
                r = toupper( r[0] );
            }
            while( r[0] != 'Y' && r[0] != 'G' );
            bool hasIt = p1->holds( c );
            if( hasIt && r[0] == 'G' )
                std::cout << "Are you trying to cheat me?! I know you do...\n";
            if( !hasIt && r[0] == 'Y' )
                std::cout << "Nooooo, you don't have it!!!\n";
        }
        return c;
    }
    bool process( char c ) {
        if( c < 0 ) return true;
        if( plr ) p2->rememberCard( c );
 
        player *a, *b;
        a = plr ? p2 : p1;
        b = plr ? p1 : p2;
        bool r;
        if( a->holds( c ) ) {
            while( a->holds( c ) ) {
                r = b->addCard( a->takeCard( c ) );
            }
            if( plr && r )p2->clearMemory( c );
        } else {
            fish();
            plr = !plr;
        }
        return false;
    }
    void fish() {
        std::cout << "\n\n\t  *** GO FISH! ***\n\n";
        card c = deck::instance()->draw();
        if( plr ) {
            std::cout << "Your new card: " << c << ".\n\n******** Your turn is over! ********\n" << std::string( 36, '-' ) << "\n\n";
            if( p1->addCard( c ) ) p2->clearMemory( c.getRank() );
        } else {
            std::cout << "\n********* My turn is over! *********\n" << std::string( 36, '-' ) << "\n\n";
            p2->addCard( c );
        }
    }
 
    player        *p1;
    aiPlayer    *p2;
    bool        plr;
};
deck* deck::inst = 0;
int main( int argc, char* argv[] ) {
    srand( static_cast<unsigned>( time( NULL ) ) ); 
    goFish f;  f.play(); 
    return 0;
}
 

Related Solutions

using matlab Write a script that simulates a card game that works as follows: A dealer...
using matlab Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card. The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess. In terms of coding, your script should...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
IN C++ LANGUAGE PLEASE::: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source...
IN C++ LANGUAGE PLEASE::: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2]
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
The Fish and Game Department stocked a lake with fish in the following proportions: 30% catfish,...
The Fish and Game Department stocked a lake with fish in the following proportions: 30% catfish, 15% bass, 40% bluegill, and 15% pike. Five years later it sampled the lake to see if the distribution of fish had changed. It found that the 500 fish in the sample were distributed as follows. Catfish Bass Bluegill Pike 121 76 237 66 In the 5-year interval, did the distribution of fish change at the 0.05 level? (a) What is the level of...
Fibonacci Write pseudocode to calculate F(n) Write pseudocode to construct a vector of the first n...
Fibonacci Write pseudocode to calculate F(n) Write pseudocode to construct a vector of the first n numbers in the Fibonacci sequence. Write a function: fibo(n = 1){ # Your code here } which takes one parameter ​n​ and returns the ​nth​ fibonacci number. Use the function you wrote to calculate the 58th Fibonacci number. Include your R code in your report The Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...) is defined as F(1) =...
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source...
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then...
PYTHON: Write the code to play a card game called Battle. Two players each have a...
PYTHON: Write the code to play a card game called Battle. Two players each have a card deck consisting of the following cards: two, three, four, … jack, queen, king, ace, in increasing order. One card deck could be represented by a list such as: cardsPlayer1 = ["two", "three", "four"..."jack", "queen", "king", "ace"] Both players have a card randomly selected. When a card is selected, remove it from the player’s deck. The player that plays the higher of the two...
A card game at a casino costs $10 to play. One card is selected at random...
A card game at a casino costs $10 to play. One card is selected at random from a standard deck. If a heart is selected the player wins $19. If a spade is selected, the player wins $14. If a club or diamond is selected, the player wins nothing. What is the expected value of this game to the player? Round your answer to the nearest cent. Do not include the $ sign in your answer. A homeowner purchases an...
Design a program using a flowchart or pseudocode that accepts a person’s loyalty card number and...
Design a program using a flowchart or pseudocode that accepts a person’s loyalty card number and amount spent in the store last month. Display the data only if the customer is a high spender – a person who spent more than $1000 in the store. A program that accepts customer info continuously until a sentinel value is entered and displays a list of high spenders.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT