Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program implements a primitive game in which the player tries to find a pot

ID: 639346 • Letter: T

Question

This program implements a primitive game in which the player tries to find a pot of gold.
Behavior: the game is played on an implicit grid of 2D coordinates. The grid boundaries
are:
Left: 0
Right: 10
Bottom: 0
Top: 10
Initially, the player is placed at coordinate (5,5).
The program places the pot of gold at a random location on the grid that is distinct from
(5,5). (The player has to do at least some work to get rich).
Behavior:
The program prompts the user to enter a direction to move as a single char
n (north)
s (south)
e (east)
w (west)
If any other letter is entered, an error message is printed and the user is
reprompted.
If a valid direction is entered, the following happens:
If the move would result in the player leaving the board, the program
prints

Explanation / Answer

Program Code:
// SpotTheGoldPlaced.cpp : Defines the entry point for the console application.

//

//header files

#include "stdafx.h"

#include <iostream>

#include <time.h>

#include <math.h>

using namespace std;

//global constants

const int LEFT=0;

const int RIGHT=10;

const int TOP=0;

const int BOTTOM=10;

static int x;

static int y;

static int xper=5;

static int yper=5;

//method prototypes

void fillArray(char arr[][10], int size);

void place_the_Gold(char arr[][10], int size);

void displayArray(char arr[][10], int size);

void place_the_Person(char arr[][10], int size);

void getTheTreasuer(char arr[][10], int size);

void setDirection(char arr[][10], int size, char direct);

//main function

int main()

{

     //declare the character array    

     char arrayDimension[10][10];

     //call the fill function

     fillArray(arrayDimension, 10);

     //display the array

     displayArray(arrayDimension, 10);

     //place the person in the array

     place_the_Person(arrayDimension, 10);

     //place the gold in the array

     place_the_Gold(arrayDimension, 10);

     cout<<" After placing the gold in a secrete place: "<<endl;

     displayArray(arrayDimension, 10);

     //getTheTreasuer method will set the directions

     //travelled as per the user

     getTheTreasuer(arrayDimension, 10);  

     system("pause");

     return 0;

}

//displays the treasure map and at the same time

//user travelling

void displayArray(char arr[][10], int size)

{

     for(int i=0;i<10;i++)

     {

          for(int j=0;j<10;j++)

              cout<<arr[i][j]<<" ";

          cout<<endl;

     }

}

//fills the array with initial positions

void fillArray(char arr[][10], int size)

{

     for(int i=0;i<size;i++)

          for(int j=0;j<size;j++)

              arr[i][j]='o';

}

//places the gold treasuer at a random position

void place_the_Gold(char arr[][10], int size)

{

     srand (time(NULL));

     x=rand()%10+1;

     y=rand()%10+1;

     for(int i=0;i<size;i++)

          for(int j=0;j<size;j++)

              if(x==i && y==j)

                   arr[i][j]='X';

}

//places the person at the particular position

void place_the_Person(char arr[][10], int size)

{

    

     for(int i=0;i<size;i++)

          for(int j=0;j<size;j++)

              if(xper==i && xper==j)

                   arr[i][j]='Y';

}

//logic to move towards the treasure

void getTheTreasuer(char arr[][10], int size)

{

     char indirect;

     int count=0;

     int dir;

     cout<<"n-north ";

     cout<<"s-south ";

     cout<<"e-east ";

     cout<<"w-west ";

     //contiuous loop till 10 chances

     do{

          //prompt the user to enter the direction

          cout<<"Enter the direction: ";

          cin>>indirect;

          //check the directions

          if(indirect!='n' && indirect!='s' && indirect!='e' && indirect!='w')

          {

              cout<<"Sorry! no such directions. Please reenter the direction."<<endl;

              cin>>indirect;         

          }

          //if properly entered then move the person

          else

          {

              setDirection(arr, 10, indirect);

              if((xper == x-1 )|| (yper==y-1)||( xper==x+1)||(yper==y+1))

              {

                   cout<<"GETTING WARMER ";

              }

             

              else

              {

                   cout<<"GETTING COLDER ";

              }

          }

          //check whether the person got the gold

          if(xper == x-1 && yper == y)

              {

                   cout<<"Hurray! You got it. ";

                   break;

              }

          count++;

     }while(count<=10);

     //if the user did not reach the treasure in 10 moves

     //display a message

     if(count==10)

     {

          cout<<"Sorry! You lost the game."<<endl;

     }

    

}

//sets the direction of the user

//depending on the user input direction

void setDirection(char arr[][10], int size, char direct)

{

     if(direct=='n')

     {

          if(xper>TOP && xper<BOTTOM)

          {

              arr[xper][yper]='o';

              xper--;

              arr[xper][yper]='Y';

          }

          else

          {

              arr[xper][yper]='Y';

          }

         

     }

     else if(direct=='s')

     {

          if(xper>TOP && xper<BOTTOM)

          {

              arr[xper][yper]='o';

              xper++;

              arr[xper][yper]='Y';

          }

          else

          {

              arr[xper][yper]='Y';

          }

     }

     else if(direct=='e')

     {

          if(yper>LEFT && yper<RIGHT)

          {

              arr[xper][yper]='o';

              yper++;

              arr[xper][yper]='Y';

          }

          else

          {

              arr[xper][yper]='Y';

          }

     }

     else if(direct=='w')

     {

          if(yper>TOP && yper<BOTTOM)

          {

              arr[xper][yper]='o';

              yper--;

              arr[xper][yper]='Y';

          }

          else

          {

              arr[xper][yper]='Y';

          }

     }

     displayArray(arr, 10);

}

---------------------------------------------------------------------------------------------------------------------------------

Sample output:

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

After placing the gold in a secrete place:

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o X

o o o o o Y o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

n-north

s-south

e-east

w-west

Enter the direction: e

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o X

o o o o o o Y o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

GETTING WARMER

Enter the direction: e

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o X

o o o o o o o Y o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

GETTING WARMER

Enter the direction: e

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o X

o o o o o o o o Y o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

GETTING WARMER

Enter the direction: n

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o Y X

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

GETTING WARMER

Enter the direction: n

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o Y o

o o o o o o o o o X

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

GETTING WARMER

Enter the direction: e

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o Y

o o o o o o o o o X

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

o o o o o o o o o o

GETTING WARMER

Hurray! You got it.

Press any key to continue . . .

Note:

The array is displayed for multiple times to track race. If does not want to display then remove the display function in setDirection function.