Challenge 73

Description

The purpose of this challenge is to use structs and arrays. This challenge simulates a very simple adventure game

 Requirements

  1. Define a struct called Step as below
    struct Step
    {
      int type;
      int coins;
    };
  2. Copy or type the sample main provided below. Also, include <cstdlib> and <ctime> in the header section
  3. Create a function as below.
    void clear_steps(Step steps[], int size)
    {
      // for all elements of the steps array, 
      // set all the type properties to be 0 (TILE)
      // and all the coins values to be zero
    }
  4. Create a function as below
    void show_path(Step steps[], int size, int pos)
    {
      // this function displays the path on one line
      // each step is displayed as a '-'
      // However, where the player currently is (given by pos),
      // display an 'x'
    }
    
    The basic requirement is to simply show a path (see below)
    -----x--------------
    The x is shown where the player currently is (the value of pos)
    To show the above, you technically don't even need to use the steps array. However, the steps array is in this function if you wanted to access all the properties of the elements to enhance the display of the path
  5. Create a function as below
    void add_coins(Step steps[], int size)
    {
      int coin_values[10] = { 0, 0, 5, 0, 0, 10, 0, 0, 20, 0 };
      // set all the coin values in each of the steps to 
      // a value randomly selected from the coin_values[] array
      // REMEMBER: an expression such as below
      //     rand() % M
      // will return a value between 0 and M-1
      // Example: rand() % 10 will return a random value between 0-9
    }
  6. Create a function as below
    void set_step_type(Step steps[], int size, const int type, int howmany)
    {
      // In howmany locations along the steps array, 
      // place howmany of type on the path
      // Example, if howmany is 3, and type is GLUE
      // then you will randomly set 3 of the steps to be of type GLUE
    }
  7. The gameplay engine is mostly given in main(). Complete the TODO sections in main as given by the comments.

Sample main()

// struct definition here

int main()
{
    const int TILE = 0;
    const int OIL = 1;
    const int GLUE = 2;
    const int BANDIT = 3;

    const int DISTANCE = 20;
    Step path[DISTANCE];
    int playerpos = 0;
    bool alive = true;
    char action;
    int coins = 0;

    srand(time(NULL));

    // set all steps in the path to TILE, and all coins to ZERO
    // TODO - Call clear_steps() function

    // randomly distribute coins along the path
    // TODO - Call add_coins() function

    set_step_type(path, DISTANCE, OIL, 3);      // set 3 steps to OIL
    set_step_type(path, DISTANCE, GLUE, 2);     // set 2 steps to GLUE
    set_step_type(path, DISTANCE, BANDIT, 1);   // set 1 step to BANDIT

    // TODO - Set the first step to be of type TILE

    while (alive && playerpos < DISTANCE)
    {
        show_path(path, 20, playerpos);
        cout << "s-step, h-hop > ";
        cin >> action;

        if (path[playerpos].type == OIL && action == 'h')
        {
            cout << "You're in oil. You can only step" << endl;
            action = 's';
        }
        if (/*TODO - BOOL EXPR to check if player is in GLUE*/)
        {
            cout << "You stepped in glue. That makes you a glue-ser!" << endl;
            alive = false;
        }

        if (action == 's')
            playerpos++;
        if (action == 'h')
            playerpos += 2;

        // TODO Add the coins found on the current step to 
        // player's coins haul
        coins += /* TODO */;

        if (/*BOOL EXPR to check if player has encountered BANDIT*/)
        {
            cout << "Oh no! A bandit took all your coins!" << endl;
            coins = 0;
        }

        cout << "COINS: " << coins << endl << endl; 
     } 
     if (playerpos >= DISTANCE && alive)
        cout << "You WON! You crossed the path unscathed!" << endl;
     else
        cout << "You lose. Womp womp." << endl;

    return 0;
}

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0073

Print Requirements