Question

Assume that we are writing a game like Minecraft in C++. We will have a World...

Assume that we are writing a game like Minecraft in C++. We will have a World class that stores a 3-dimensional array of "Voxels". A voxel is a simple element of volume (think Pixel, but 3D). Your task for this assignment is to create a world, set some Voxels and display them.

You are provided with a simple class for a Voxel that stores a RGB color value, as well as a character code for the type of element.

You are also provided with a simple class for a World that stores a 3-Dimensional array, named boxes, that stores, 100 x 100 x 100 different Voxel objects. Because this large array is stored directly in the World class, you should not create a World object as a normal local stack variable. Depending on your environment this would likely even cause a segmentation fault. Instead you should use a Pointer to a world object, and dynamically allocate it using the "new" command.

As a side note, for all the reasons mentioned, a better design for the world class would be to have it contain a dynamically allocated array, and only store the pointer internally. But this is a little harder for us to practice...

Here is my check07b.cpp file:

#include <iostream>
using namespace std;

#include "voxel.h"
#include "world.h"

/**************************************************************
* Function: main
* Purpose: The driver for the program. Creates a world, sets
* some Voxels and displays them.
**************************************************************/
int main()
{
   cout << "Starting Program.\n";

   // First, we create a voxel for air
   Voxel air(180, 200, 255, 'A');


   // Next, we create a voxel for grass
   Voxel grass(25, 255, 50, 'G');

   cout << "Ending Program.\n";
   return 0;
}

Here is voxel.cpp:

#include "voxel.h"

#include <iostream>
using namespace std;

void Voxel :: display() const
{
   cout << "(" << red << ", " << green << ", " << blue << ") - ";
   cout << typeCode;
}

voxel.h:

#ifndef VOXEL_H
#define VOXEL_H
class Voxel
{
private:
   // stores an integer for Red, Blue, and Green
   // The astute coder will observe the great ineffiency here
   int red;
   int green;
   int blue;

   // Stores a character code for a particular type
   char typeCode;

public:
   Voxel() : red(0), green(0), blue(0), typeCode('_') { }
   Voxel(int red, int green, int blue, char type)
      : red(red), green(green), blue(blue), typeCode(type) { }

   /******************************************************
    * Function: Display
    * Description: Displays the value of a Voxel.
    ******************************************************/
   void display() const;
};

#endif

world.cpp:

#include "voxel.h"
#include "world.h"

#include <iostream>
using namespace std;

World::World()
{
   cout << "Initializing the world and connecting to server...\n";
   // we are not really connecting to a server, just pretending
}

World::~World()
{
   cout << "Disconnecting from the server and saving state...\n";
   // Shh... we're not actually doing anything :-)
}
Voxel World::getBox(int x, int y, int z) const
{
   return boxes[x][y][z];
}

void World::setBox(int x, int y, int z, const Voxel & box)
{
   boxes[x][y][z] = box;
}
void World::display()
{
   cout << "Hello from your world.\n";
}
void World::displayBox(int x, int y, int z)
{
   cout << "The value at (" << x << ", " << y << ", " << z << ") is: ";
   boxes[x][y][z].display();
   cout << endl;
}

world.h:

#ifndef WORLD_H
#define WORLD_H

#include "voxel.h"
class World
{
private:
   Voxel boxes[100][100][100];

public:
   World();
   ~World();

   Voxel getBox(int x, int y, int z) const;
   void setBox(int x, int y, int z, const Voxel & box);
   void display();
   void displayBox(int x, int y, int z);
};

#endif

Here's what I have to do:

  1. Declare a new World object. Make sure to use pointers and dynamic allocation.
  2. Call the "display" method on your World object.
  3. Code is in place to create a Voxel for air. Call the setBox method on your World object to set it at position: (50, 60, 70).
  4. Code is in place to create a Voxel for grass. Call the setBox method on your World object to set it at position: (45, 20, 10).
  5. Call the displayBox method (on your World object) twice to diplay the two Voxels you just set.
  6. Finally, make sure to free up the memory taken by your World object by deleting it.

Sample output:

The following is an example of output for this program:

Starting Program.

Initializing the world and connecting to server...

Hello from your world.

The value at (50, 60, 70) is: (180, 200, 255) - A

The value at (45, 20, 10) is: (25, 255, 50) - G

Disconnecting from the server and saving state...

Ending Program.

Thanks in advance!

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

// voxel.h

#ifndef VOXEL_H
#define VOXEL_H

class Voxel
{
private:
// stores an integer for Red, Blue, and Green
// The astute coder will observe the great ineffiency here
int red;
int green;
int blue;

// Stores a character code for a particular type
char typeCode;

public:
Voxel() : red(0), green(0), blue(0), typeCode('_') { }
Voxel(int red, int green, int blue, char type)
: red(red), green(green), blue(blue), typeCode(type) { }

/******************************************************
* Function: Display
* Description: Displays the value of a Voxel.
******************************************************/
void display() const;
};

#endif
//end of voxel.h

// voxel.cpp

#include "voxel.h"

#include <iostream>
using namespace std;

void Voxel :: display() const
{
cout << "(" << red << ", " << green << ", " << blue << ") - ";
cout << typeCode;
}


//end of voxel.cpp

// world.h

#ifndef WORLD_H
#define WORLD_H

#include "voxel.h"
class World
{
private:
Voxel boxes[100][100][100];

public:
World();
~World();

Voxel getBox(int x, int y, int z) const;
void setBox(int x, int y, int z, const Voxel & box);
void display();
void displayBox(int x, int y, int z);
};

#endif

//end of world.h

// world.cpp

#include "voxel.h"
#include "world.h"

#include <iostream>
using namespace std;

World::World()
{
cout << "Initializing the world and connecting to server...\n";
// we are not really connecting to a server, just pretending
}

World::~World()
{
cout << "Disconnecting from the server and saving state...\n";
// Shh... we're not actually doing anything :-)
}
Voxel World::getBox(int x, int y, int z) const
{
return boxes[x][y][z];
}

void World::setBox(int x, int y, int z, const Voxel & box)
{
boxes[x][y][z] = box;
}
void World::display()
{
cout << "Hello from your world.\n";
}
void World::displayBox(int x, int y, int z)
{
cout << "The value at (" << x << ", " << y << ", " << z << ") is: ";
boxes[x][y][z].display();
cout << endl;
}

//end of world.cpp

// check07b.cpp

#include <iostream>
using namespace std;

#include "voxel.h"
#include "world.h"

/**************************************************************
* Function: main
* Purpose: The driver for the program. Creates a world, sets
* some Voxels and displays them.
**************************************************************/
int main()
{
cout << "Starting Program.\n";

// First, we create a voxel for air
Voxel air(180, 200, 255, 'A');


// Next, we create a voxel for grass
Voxel grass(25, 255, 50, 'G');
// Declare a new World object. Make sure to use pointers and dynamic allocation.
World *world = new World();
// check if world object is created successfully
if(world != NULL)
{
   //Call the "display" method on your World object.
   world->display();
   //Call the setBox method on your World object to set air at position: (50, 60, 70).
   world->setBox(50,60,70,air);
   // Call the setBox method on your World object to set grass at position: (45, 20, 10).
   world->setBox(45,20,10,grass);
   // Call the displayBox method (on your World object) twice to diplay the two Voxels you just set.
   world->displayBox(50,60,70);
   world->displayBox(45,20,10);
   // Finally, make sure to free up the memory taken by your World object by deleting it.
   delete world;
}else
   cout<<"Unable to create object for World"<<endl;

cout << "Ending Program.\n";
return 0;
}

//end of check07b.cpp

Output:

Add a comment
Know the answer?
Add Answer to:
Assume that we are writing a game like Minecraft in C++. We will have a World...
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
  •     class Circle    {    public:        enum Color {UNDEFINED, BLACK, BLUE, GREEN, CYAN,...

        class Circle    {    public:        enum Color {UNDEFINED, BLACK, BLUE, GREEN, CYAN, RED};        Circle(int = 0, int = 0, double = 0.0, Color = UNDEFINED);        void setX(int);        void setY(int);        void setRadius(double);        void setColor(Color);        int getX() const;        int getY() const;        double getRadius() const;        Color getColor() const;           private:        int x;        int y;   ...

  • 1:Fix the problems with this class definition, do not change program functionality #include <iostream> using namespace...

    1:Fix the problems with this class definition, do not change program functionality #include <iostream> using namespace std; class Rainbow{   private:   static int numRainbows;   string colors;   public:   Rainbow() { colors = "Red Orange Yellow Green Blue Violet"; numRainbows++; }   Rainbow(string colors) ; colors(colors) { numRainbows++; }      void setColors(string colors) { this->colors = colors; }   string getColors() { return colors; }   int getCount() { return numRainbows; }      bool operator==(const Rainbow & lhs, const Rainbow& rhs)   {   return lhs.colors == rhs.colors;...

  • --C++-- --spc16-7.cpp-- // Chapter 16, Programming Challenge 7: TestScores class #include <iostream> #include "TestScores.h" #include "NegativeScore.h"...

    --C++-- --spc16-7.cpp-- // Chapter 16, Programming Challenge 7: TestScores class #include <iostream> #include "TestScores.h" #include "NegativeScore.h" using namespace std; int main() { // Constant for the number of test scores const int NUM_SCORES = 5;       try    {        // Create an array of valid scores.        int myScores[NUM_SCORES] = { 88, 90, 93, 87, 99 };        // Create a TestScores object.        //TestScores myTestScores(myScores, NUM_SCORES); // optional constructor        TestScores myTestScores(myScores);...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • 4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...

    4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...

  • 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...

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

    Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works. HINT: Replace the bubbleSort function with the selectionSort. Your test program should perform the following steps: Create an array of random numbers. Display the array in its original order. Pass the array to your sorting function. Display the array again to show that it has been sorted. Provide a screen shot showing that the above steps are working. CODE:...

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