The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I need help with some code. I need to understand what I need to add to the program and where it needs added. I have attached a document with more specifics.
Need help understanding what needs added to this piece of code that is highlighted in purple, and then need to understand where the code needs inserted into the program.
gameObjectplaceInCave( gameObjectType array[TOTAL_ROWS][TOTAL_COLS])
//function that takes in the total number of rows and cols in the array
{
int r, c;
//Find a random row and column that is empty and place the object there.
do
{
r = rand()%8; //gets a random number between 0 and 7
c = rand()%8;//gets a random number between 0 and 7
} while (array[r][c] != EMPTY);
obj.row = r;
obj.column = c;
obj.isFound = false;
return obj;
}
// </WK2>
Â
Program to insert code into.
------------------------------------------------------------------------------------------------------
Â
#pragma once
#include "GSP115_Course_Project.h"
#include "stdafx.h"
#include "String.h"
#include <array>
#include <iostream>
#include <time.h>
Â
Â
Â
// Global Constants
const int MAX_ROWS = 7;
const int MIN_ROWS = 0;
const int MAX_COLS = 7;
const int MIN_COLS = 0;
const int TOTAL_ROWS = MAX_ROWS + 1;
const int TOTAL_COLS = MAX_COLS + 1;
Â
using namespace std;
#include <string.h>
Â
// <WK2 status=permanent>
// function prototypes
gameObjectplaceInCave(gameObjectType array[TOTAL_ROWS][TOTAL_COLS]);
// </WK2>
Â
int _tmain(int argc, _TCHAR* argv[])
{
   //**Initialize Variables**
   srand(time(NULL));      // Seed the random number function
Â
// <WK2 status=permanent>
   gameObjectType cave[TOTAL_ROWS][TOTAL_COLS];   // the cave--a two dimensional array
// </WK2>
// <WK2 status=permanent>
   playerObject player ={true, false, false, false, false,{-1, -1, false, true}};   // the player
   gameObject treasure = {-1, -1, false, true};      // the treasure
   gameObject monster = {-1, -1, false, true};      // the monster
// </WK2>
   int row, column;                        // temporarily hold the new player position
Â
   string msg;                              // status message variable
   char command;                           // player input
Â
   bool movePlayer = true;      // flag to indicate the player position can be updated
   bool gameOver = false;      // status flag
Â
// <WK2 status=permanent>
   //**Prepare Cave***********
   //...Initialize an empty cave
   for (gameObjectType (&R)[TOTAL_COLS] : cave)
   {
      for (auto &C : R) C = EMPTY;
   }
// </WK2>
Â
   //...Add player in rows 0-2, columns 0-2
// <WK2 status=permanent>
   player.position.row = rand() %3;
   player.position.column = rand()%3;
   cave[player.position.row][player.position.column] = PLAYER;
   //...Add Treasure in rows 4-6, column 1-6
   treasure.row = rand()%3 + 4;
   treasure.column = rand()%6 +1;
   cave[treasure.row][treasure.column] = TREASURE;
   //...Add Monster at treasure row +1 , column -1
   monster.row = treasure.row + 1;
   monster.column = treasure.column -1;
   cave[monster.row][monster.column] = MONSTER;
// </WK2>
// <WK1 status=2>
   cout << "Player will start at row " <<char('A' + player.position.row) << " and column " <<player.position.column + 1 << endl;//WK1&2 only
// </WK1>
Â
   //**Play Game*************
   //...Begin Game Loop
   while (!gameOver)
   {
// <WK1 status=2>
      cout << "You have moved to position " <<char('A'+ player.position.row) <<player.position.column +1 << endl;//WK1&2 only
// </WK1>
      cout <<msg.c_str() << endl;
      //....Get command
      cout << "What is your command? ";
      cin >> command;
      //....Clear display and message
      msg.clear();
      system("cls");
      //....Process player command
// <WK2 status=permanent>
      row = player.position.row;
      column = player.position.column;
// </WK2>
      switch (command)
      {
      case 'a':
// <WK2 status=permanent>
         column = player.position.column - 1;
// </WK2>
         if (column < MIN_COLS)
         {
// <WK2 status=permanent>
            column = player.position.column;
// </WK2>
            msg = "You have hit the west wall!";
         }
         break;
      case 's':
// <WK2 status=permanent>
         row = player.position.row + 1;
// </WK2>
         if (row > MAX_ROWS)
         {
// <WK2 status=permanent>
            row = player.position.row;
// </WK2>
            msg = "You have hit the south wall!";
         }
         break;
      case 'w':
// <WK2 status=permanent>
         column = player.position.column;//Is this really needed?
         row = player.position.row - 1;
// </WK2>
         if (row < MIN_ROWS)
         {
// <WK2 status=permanent>
            row = player.position.row;
// </WK2>
            msg = "You have hit the north wall!";
         }
         break;     Â
      case 'd':
// <WK2 status=permanent>
         row = player.position.row;//Is this really needed?
         column = player.position.column + 1;
// </WK2>
         if (column > MAX_COLS)
         {
// <WK2 status=permanent>
            column = player.position.column;
// </WK2>
            msg = "You have hit the east wall!";
         }
         break;     Â
      case 'q':
         gameOver = true;
         msg = "Quitting?\n";
         break;
      default:
         movePlayer = false;
         break;
      }
      //....Check if the game is over
      if (!gameOver)
      {
// <WK2 status=permanent>
         //..... Check for events
         switch (cave[row][column])
         {
            //......If treasure found, set flag to show player has treasure
         case TREASURE:
            player.hasTreasure = true;
            treasure.isFound = true;
            msg = "You found the treasure!";//-add gameover in week2, remove in week 6
// <WK2 status=5>
            gameOver = true;
// </WK2>
            break;
            //......If monster found
         case MONSTER:
            if (!monster.isFound)
            {
               msg = "You have found the monster";
               //.......Resolve combat
               //gameOver = resolveCombat(player, monster, killedValue, killMonsterValue,msg, movePlayer, monsterPause);//-add in week 6
// <WK2 status=5>
               gameOver = true;
// </WK2>
// <WK2 status=3>
               player.alive = false;
// </WK2>
            }
            break;
         }
// </WK2>
Â
         //.....Move Player
         if (movePlayer)
         {
// <WK2 status=permanent>
            cave[player.position.row][player.position.column] = EMPTY;      //updates position information
            cave[row][column] = PLAYER;
            player.position.row = row;
            player.position.column = column;
// </WK2>
         }
         movePlayer = true;
      }
   }
   //...End Game Loop
   //**End Game**************
Â
// <WK2 status=permanent>
   //...Provide end win/loss message
   cout <<msg.c_str() << endl;
   if (player.alive)
   {
      if (player.hasTreasure) msg = "You are alive and rich!";
      else msg = "You didn't get the treasure, but you live to seek adventure another day.";
   }
   else
   {
      msg = "RIP Adventurer";
   }
   cout <<msg.c_str() << endl;
// </WK2>
Â
   //...Do clean-up
   //...Quit
   return 0;
Â
}
Â
Attachments:
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly