Program: Treasure hunt game using 2D arrays



STOP PRESS! The full book is now available DIRECTLY from Amazon with lower shipping costs!

Here's an example of a simple Treasure Hunt game. It stores the treasure as characters in a 2D array.


The location of each item of treasure is randomly chosen at the start of the game. The player is then repeatedly asked to guess column and row numbers to locate the treasure.


Making an incorrect guess costs the player one life. Once all lives have been used up, the game ends and the contents of the array are displayed to show all of the discovered and undiscovered treasure.







Note that the program contains several constants that allow you to control the difficulty of the game. You can change the width and height of the array, the number of lives that the player starts with and also the number of items of treasure to bury.


Select and copy the C++ source-code at the bottom of this post.


Paste into a text editor, such as Nano or Geany.

Then save the new file, ending in .cpp

I used treasure.cpp


To compile from the command-line:


    g++ -o treasure treasure.cpp


To run from the command-line:


    ./treasure



Here's the code:


#include <iostream>    // Program will be displaying some text on the screen
#include <cstdlib>    // Program uses the random() and srandom() functions
#include <ctime>    // Program uses the time() function as the random seed


using namespace std;    // Will be using standard identifiers cin, cout and endl

const int NUM_ROWS = 4;
const int NUM_COLS = 6;
const int NUM_LIVES = 5;
const int ITEMS_TO_BURY = 5;


int main()
{
    // 2D array to store possible locations of treasure

    char island[NUM_ROWS][NUM_COLS];

    int row, col;

    // Clear each location on the island so the whole array is "blank"
    for ( row = 0;  row < NUM_ROWS;  row++ )
        for ( col = 0;  col < NUM_COLS;  col++ )
            island[row][col] = '.';


    // Seed random number generator so that does not always select the same numbers
    srandom( time( 0 ) );

    // Hide some treasure in three random places
    int randomRow, randomCol;

    for ( int timesHidden = 0;  timesHidden < ITEMS_TO_BURY;  timesHidden++)
    {
        // Pick a random place on the island to hide some treasure

        randomRow = random() % NUM_ROWS;
        randomCol = random() % NUM_COLS;

        island[randomRow][randomCol] = 'X';
    }  // end of for loop

    cout << "TREASURE HUNT" << endl << endl;

    // Allow user to repeatedly guess a place where the treasure is hidden

    int rowGuessed, colGuessed;
    int guessesLeft = NUM_LIVES;

    while ( guessesLeft > 0 )
    {
      cout << "Which column across? 0-" << NUM_COLS-1 << ": ";
      cin >> colGuessed;

      cout << "Which row down? 0-" << NUM_ROWS-1 << ": ";
      cin >> rowGuessed;


      // Check whether any treasure is buried where the user guessed
      if ( island[rowGuessed][colGuessed] == 'X' )
      {
          // Mark the treasure as found

          cout << "Found some treasure at (";

          cout << colGuessed << ", " << rowGuessed << ")" << endl;

          island[rowGuessed][colGuessed] = 'F';
      }
      else
      {
          // No treasure at that spot - lose a life

          cout << "Nothing there." << endl;
          guessesLeft--;
      }  // end of if-else


      // Display blank line after each guess
      cout << endl;

    }  // end of while loop

    // Game over
    cout << endl << "Out of guesses!" << endl << endl;

    // Display the grid on the screen using loops to access the items
    for ( row = 0;  row < NUM_ROWS;  row++ )
    {

        for ( col = 0;  col < NUM_COLS;  col++ )
            cout << island[row][col];


        // Move on to new line at end of finished row
        cout << endl;

    }  // end of for loop for rows

    return 0;
}



Find out more - buy the book on Amazon...






Further details in Chapter 9: Arrays of data