Saturday, September 23, 2017

Programming Challenge 13.14 - Fishing Game Simulation

Example Files: ClearsSreen.h
                          FishingGameSimulation.h
                          FishingGameSimulation.cpp
                          FishingGameSimulationDm.cpp


FishingGameSimulation.h


#ifndef FISHING_GAME_SIMULATION_H
#define FISHING_GAME_SIMULATION_H

#include <array>

class Fish
{
    private:
        static constexpr int NUM_PRIZES = 7;        // The number of prizes a player can win
        static constexpr int NUM_ITEMS = 6;            // The number of items to be caught
        static constexpr int DIE_SIDES = 6;            // The number of sides of the die

       static const std::array<std::string, NUM_PRIZES> prizes;                // Holds the prize names
        static const std::array<std::string, NUM_ITEMS> items;               // Holds the items being caught
        static const std::array<int, NUM_ITEMS> scoreList;                        // Holds a score list
        static const std::array <int, Fish::NUM_PRIZES> highScoreList;        // Holds a list of highscores

        int             sides;                     // Number of sides of the die
        int             value;                    // The die's value
        int             score;                    // Holds the score achieved by a player
        std::string itemName;            // Holds the items a player must catch
        std::string prizeName;           // Holds the name of prizes a player can win

    public:
        Fish() : sides(DIE_SIDES), value(0), score(0), itemName(" "), prizeName(" ")
        {
            initRNG();
        }

        void initRNG();
        void roll();
        void setItemName();
        void setScore();
        void prizeList();

        int getNumPrizes() const                     // Returns the number of prizes
        { return NUM_PRIZES; }

        int getNumItems() const                      // Returns the number of items
        { return NUM_ITEMS; }

        int getSides() const                              // Returns the number of sides of a die
        { return sides; }

        int getValue() const                              // Returns the die value
        { return value; }

        std::string getItem() const                    // Returns the item name
        { return itemName; }

        int getScore() const                              // Returns the score achieved by a player
        { return score; }

        std::string getPrizeName() const          // Returns the name of a prize
        { return prizeName; }

        int getScoreList(int i) const                  // Returns a value contained in the scoreList array
        { return scoreList[i]; }

        std::string getPrizeList(int i) const       // Returns a price contained in the prizes array
        { return prizes[i]; }

        std::string getItemList(int i) const        // Returns an item from the items array
        { return items[i]; }
   
        int getHighScore(int i) const                // Returns a value from the highScoreList array
        { return highScoreList[i]; }
};
#endif

FishingGameSimulation.cpp


/* FishingGameSimulation.cpp - Implementation file for the Fish class. */

#include <array>
#include <cstdlib>
#include <ctime>
#include <string>
#include "FishingGameSimulation.h"

const std::array <int, Fish::NUM_ITEMS> Fish::scoreList         { 1, 5, 10, 15, 20, 25 };
const std::array <int, Fish::NUM_PRIZES> Fish::highScoreList { 5, 50, 75, 100, 250, 400, 1000 };

const std::array <std::string, Fish::NUM_ITEMS> Fish::items
{
    "Horseshoe", "Red Catfish", "Black Bass", "Spearfish",
    "Rainbow Trout", "Item Chest"
};

const std::array <std::string, Fish::NUM_PRIZES> Fish::prizes
{
    "Chocolate Bar", "Teru Teru Bozu Doll", "Ramen Super Pack",
    "Miyuki Figurine", "BD-Player Set", "Rome Crossrocket Snowboard",
    "Kimono"
};

/* **********************************************************
            Fish::initRNG()
    This function initializes the RNG.
   ********************************************************** */

void Fish::initRNG()
{
    time_t seed = time(0);

    srand(static_cast<unsigned int>(seed));

    roll();
}

/* **********************************************************
            Fish::roll()

    This function simulates a die-roll. It generates a random
    number, which is copied to the value member variable.
   ********************************************************** */

void Fish::roll()
{
    const int MIN_VALUE = 1;

    value = (rand() % (getSides() - MIN_VALUE + 1)) + MIN_VALUE;

    setItemName();
}

/* **********************************************************
            Fish::setItemName()
    This function sets the itemName variable.
   ********************************************************** */

void Fish::setItemName()
{
    itemName = items[getValue() - 1];
}

/* **********************************************************
            Fish::setScore()
    This function sets and updates the score member variable
    during gameplay.
   ********************************************************** */

void Fish::setScore()
{
    score += scoreList[getValue() - 1];

    prizeList();
}

/* **********************************************************
            Fish::prizeList()
    This function determines the prize a player will receive.
    The getScore() function is called, and compared against
    the highScore array. If the score is equal to or higher
    than one of the highscores, the prizeName member gets the
    prize names from the prize array. The getPrizeList(int)
    function contains a call to the array.
   ********************************************************** */

void Fish::prizeList()
{
    for (int i = 0; i < NUM_PRIZES; i++)
    {
        getScore() >= getHighScore(i) ? prizeName = getPrizeList(i) : prizeName;
    }
}

FishingGameSimulationDm.cpp


/* FishingGameSimulationDm.cpp - Demonstration file for the Fish class. */

#include <iomanip>
#include <iostream>
#include <string>
#include "FishingGameSimulation.h"
#include "ClearScreen.h"

enum Menu_Items { DISPLAY_PRIZES = 'D', START_GAME = 'S', QUIT = 'Q' };

void menu();
char options();
void gameLoop(Fish &);
void displayPrizeList(const Fish);
void displayItemList(const Fish);
void displayScore(Fish);

int main()
{
    menu();

    return 0;
}

/* **********************************************************
    options()
    This function contains the menu options. The player is
    asked to make a choice, which is validated and, if it is
    valid, is returned.
    ********************************************************** */

char options()
{
    char choice = ' ';

    std::cout << "\t\tFISHING GAME SIMULATION\n\n"
               << "\t[D]ISPLAY PRIZES\n"
               << "\t[S]TART GAME\n"
                 << "\t[Q]UIT\n\n"
               << "\tSelect: ";
    std::cin >> choice;

    while (toupper(choice) < DISPLAY_PRIZES && toupper(choice) > QUIT)
    {
        std::cout << "\nSelect: ";
        std::cin >> choice;
    }

    std::cout << "\n";
    return (toupper(choice));
}

/* **********************************************************
    menu()
    This function represents the main menu of the game. The
    player can either start a new game or quit.
   ********************************************************** */

void menu()
{
    Fish fishing;

    char choice = ' ';

    do
    {
        choice = options();

        switch (choice)
        {
            case DISPLAY_PRIZES:
            {
                clearScreen();
                displayPrizeList(fishing);
                clearScreen();
            } break;

            case START_GAME:
            {
                clearScreen();
                gameLoop(fishing);
                clearScreen();
            } break;

            case QUIT:
            {
                if (fishing.getScore() != 0)
                {
                    std::cout << "Congratulations, you earned " << fishing.getScore() << " points!\n"
                               << "You win a " << fishing.getPrizeName() << "!";
                    std::cin.get();
                    std::cin.ignore();
                }
                else
                {
                    std::cout << "ONLY THOSE WHO PLAY WIN!";
                    std::cin.get();
                    std::cin.ignore();
                }
            } break;
        }
    } while (toupper(choice) != QUIT);
}

/* **********************************************************
    displayItemList() accepts a fish object
    This function displays the items a player must catch and
    their point values.
   ********************************************************** */

void displayItemList(const Fish fishing)
{
    std::cout << "\t\tFISHING GAME SIMULATION\n\n";
    std::cout << "ITEM NAME " << std::setw(40) << "ITEM VALUE\n"
               << std::setfill('*') << std::setw(51) << "\n"
               << std::setfill(' ');

    for (int i = 0; i < fishing.getNumItems(); i++)
    {
        std::cout << std::setw(45) << std::left << fishing.getItemList(i)
                   << std::setw(5) << std::right << fishing.getScoreList(i) << "\n";
    }
}

/* **********************************************************
    displayPrizeList()
    This function displays a list of prizes a player can win,
    and the score needed to win it.
   ********************************************************** */

void displayPrizeList(const Fish fishing)
{
    std::cout << "\t\tFISHING GAME SIMULATON\n\n";
    std::cout << "PRIZE LIST " << std::setw(40) << "SCORE NEEDED\n"
              << std::setfill('*') <<std::setw(51) << "\n"
              << std::setfill(' ');

    for (int i = 0; i < fishing.getNumPrizes(); i++)
    {
        std::cout << std::setw(45) << std::left << fishing.getPrizeList(i)
                     << std::setw(5) << std::right << fishing.getHighScore(i) << "\n";
    }

    std::cout << "\nPress a key to return to the menu ...";
    std::cin.get();
    std::cin.ignore();
}

/* **********************************************************
    gameLoop()
    In this function the game is executed, and the score is
    accumulated for as long as the player decides that he or
    she wishes to continue playing.
   ********************************************************** */

void gameLoop(Fish &fishing)
{
    char again = ' ';

    displayItemList(fishing);

    do
    {
        fishing.roll();

        std::cout << "\nCaught Item: " << fishing.getItem() << "\n";

        fishing.setScore();

        std::cout << "Continue [Y/N]? ";
        std::cin >> again;

        if (toupper(again) == 'Y')
        {       
            clearScreen();
            displayItemList(fishing);
        }

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            std::cout << "Play Again? (Y/N) ";
            std::cin >> again;
        }
    } while (toupper(again) != 'N');
   
    std::cout << "\n";
}

Example Output:







No comments:

Post a Comment