Question

Write a program to create a game map. A game map is a 2D array of...

Write a program to create a game map. A game map is a 2D array of regions, or tiles, of a world, as in the example of the figure to the right . For example, a tile with a value of 0 might represent water, 1 might represent land, 2 might represent a mountain and so forth. This map is represented as a class named CMap that has the properties:

• A 2D array, having a height and width of 64, of tiles, each an integer, where a value 0 is water, 1 is land, 2 is a mountain, 3 is a town, 4 is a cave and 5 is a castle.

• The number of non-water tiles of a world.

• The number of populated tiles, namely towns or castles, of a world.

This map has the interface:

• A constructor to set all tiles of a world to contain water.

• A member function GetTile(x,y) to return the value of a tile of a world with

coordinates (x,y). If the the coordinates are outside of the range of a world, then -1 is returned.

  

• A member function SetTile(x,y,nVal) to set the value of a tile of a world with coordinates (x,y) to nVal. If the the coordinates are outside of the range of a world or nVal is outside of the range of tile values, then the world is not changed.

• A member function GetNumNonWaterTiles to return the number of non-water tiles of a world.

• A member function GetNumPopulatedTiles to return the number of populated tiles of a world.

Your submission should include a screenshot of the execution of the program with the following instructions in the main function:

CMap m;

cout << m.GetTile(0,0) << " ";

cout << m.GetTile(63,0) << " ";

cout << m.GetTile(0,63) << " ";

cout << m.GetTile(63,63) << " ";

cout << m.GetTile(64,64) << " ";

cout << m.GetNumNonWaterTiles() << " ";

cout << m.GetNumPopulatedTiles() << endl;

srand(123); // use a specified seed of 123

for(auto i = 0; i < 100; i++)

m.SetTile(rand()%65, rand()%65, rand()%6);

m.SetTile(128,0,4);

m.SetTile(0,128,9);

cout << m.GetTile(16,0) << " ";

cout << m.GetTile(0,32) << " ";

cout << m.GetTile(48,48) << " ";

cout << m.GetNumNonWaterTiles() << " ";

cout << m.GetNumNonWaterTiles() << endl;

m.SetTile(8,8,3);

m.SetTile(4,8,5);

cout << m.GetNumNonWaterTiles() << " ";

cout << m.GetNumNonWaterTiles() << endl;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#define SIZE 64

using namespace std;

//CMap class

class CMap{

                //2d grid

                int grid[SIZE][SIZE];

                //number of non water tiles

                int nonWaterTiles;

                //number of populated tiles

                int populatedTiles;

                public:

                //constructor

                CMap();

                //method to return the tile

                int GetTile(int x, int y) const;

                //method to set a tile

                void SetTile(int x,int y,int nVal);

                //method to get the total number of non water tiles

                int GetNumNonWaterTiles() const;

                //method to get the total number of populated tiles

                int GetNumPopulatedTiles() const;

};

//implementation of all methods

CMap::CMap(){

                //initializing all tiles to water (0)

                for(int i=0;i<SIZE;i++){

                                for(int j=0;j<SIZE;j++){

                                               grid[i][j]=0;

                                }

                }

                //setting number of non water tiles and populated tiles to 0

                nonWaterTiles=0;

                populatedTiles=0;

}

int CMap::GetTile(int x, int y) const{

                //validating x, y

                if(x<0 || y<0 || x>=SIZE || y>=SIZE){

                                return -1; //invalid

                }

                //returning tile value at row x and column y

                return grid[x][y];

}

void CMap::SetTile(int x,int y,int nVal){

                //validating x and y and nVal

                if(x<0 || y<0 || x>=SIZE || y>=SIZE || nVal<0 || nVal>5 ){

                                return; //invalid, returning from function

                }

                //if current location has water and new value is not water, incrementing non

                //water tiles

                if(grid[x][y]==0 && nVal!=0){

                                nonWaterTiles++;

                }

                //if current location has no water and new value is water, decrementing non

                //water tiles

                if(grid[x][y]!=0 && nVal==0){

                                nonWaterTiles--;

                }

                //if current location has no town or castle and new location has town or castle

                //incrementing populated tiles

                if(grid[x][y]!=3 && grid[x][y]!=5 && (nVal==3 || nVal==5)){

                                populatedTiles++;

                }

                //if current location has town or castle and new location has no town or castle

                //decrementing populated tiles

                if((grid[x][y]==3 || grid[x][y]==5) && (nVal!=3 && nVal!=5)){

                                populatedTiles--;

                }

                //assigning new value

                grid[x][y]=nVal;

}

int CMap::GetNumNonWaterTiles() const{

                //returns the number of non water tiles

                return nonWaterTiles;

}

int CMap::GetNumPopulatedTiles() const{

                //returns the number of populated tiles

                return populatedTiles;

}

int main(){

                //using the given test code

                CMap m;

                cout << m.GetTile(0,0) << " ";

                cout << m.GetTile(63,0) << " ";

                cout << m.GetTile(0,63) << " ";

                cout << m.GetTile(63,63) << " ";

                cout << m.GetTile(64,64) << " ";

                cout << m.GetNumNonWaterTiles() << " ";

                cout << m.GetNumPopulatedTiles() << endl;

                srand(123); // use a specified seed of 123

                for(auto i = 0; i < 100; i++)

                m.SetTile(rand()%65, rand()%65, rand()%6);

                m.SetTile(128,0,4);

                m.SetTile(0,128,9);

                cout << m.GetTile(16,0) << " ";

                cout << m.GetTile(0,32) << " ";

                cout << m.GetTile(48,48) << " ";

                cout << m.GetNumNonWaterTiles() << " ";

                cout << m.GetNumNonWaterTiles() << endl;

                m.SetTile(8,8,3);

                m.SetTile(4,8,5);

                cout << m.GetNumNonWaterTiles() << " ";

                cout << m.GetNumNonWaterTiles() << endl;

                return 0;

}

/*OUTPUT (may vary depends on the system you are using)*/

0 0 0 0 -1 0 0

0 0 0 78 78

80 80

Add a comment
Know the answer?
Add Answer to:
Write a program to create a game map. A game map is a 2D array of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

  • In this project, you will write a complete program that allows the user to play a...

    In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps: 1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m. 2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1]. 3. The codebreaker is prompted to enter a guess, an n-digit sequence....

  • In C++ program Fishing Game Simulation   For this assignment, you will write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Eac...

    In C++ program Fishing Game Simulation   For this assignment, you will write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Each possible item is worth a certain number of fishing points. The points will not be displayed until the user has finished fishing, and then a message is displayed congratulating the user depending on the number of fishing points gained.   Here are some suggestions for the...

  • I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up...

    I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up to you, but at a minimum, you should have a Maze class with appropriate constructors and methods. You can add additional classes you may deem necessary. // This program fills in a maze with random positions and then runs the solver to solve it. // The moves are saved in two arrays, which store the X/Y coordinates we are moving to. // They are...

  • C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game...

    C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game we have four different types of Creatures: Humans, Cyberdemons, Balrogs, and elves. To represent one of these Creatures we might define a Creature class as follows: class Creature { private: int type; // 0 Human, 1 Cyberdemon, 2 Balrog, 3 elf int strength; // how much damage this Creature inflicts int hitpoints; // how much damage this Creature can sustain string getSpecies() const; //...

  • Task 1 of 3 Game Rules Presented here, is an outline of the rules of the...

    Task 1 of 3 Game Rules Presented here, is an outline of the rules of the game around which this assignment is centred. This is not meant to be a completely exhaustive explanation, as you should defer to the specics provided in the Task descriptions for more detail, but rather this is to esh out the design and intention behind the game. 2.1.1 Game Board The game is played on a square board made of NxN characters where N is...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • First, you will need to create the Creature class. Creatures have 2 member variables: a name,...

    First, you will need to create the Creature class. Creatures have 2 member variables: a name, and the number of gold coins that they are carrying. Write a constructor, getter functions for each member, and 2 other functions: - void addGoldCoins(int) adds gold coins to the Creature. - void identify() prints the Creature information. The following should run in main: Creature d{"dragon", 50}; d.addGoldCoins(10); d.identify(); // This prints: The dragon is carrying 60 gold coins. Second, you are going to...

  • Hi I need a fix in my program. The program needs to finish after serving the...

    Hi I need a fix in my program. The program needs to finish after serving the customers from the queue list. Requeriments: Headers: DynamicArray.h #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class DynamicArray { V* values; int cap; V dummy; public: DynamicArray(int = 2); DynamicArray(const DynamicArray&); ~DynamicArray() { delete[] values; } int capacity() const { return cap; } void capacity(int); V operator[](int) const; V& operator[](int); DynamicArray& operator=(const DynamicArray&); }; template DynamicArray::DynamicArray(int cap) { this->cap = cap; values =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT