Purpose of this miniproject:



MiniProject: Dog Survival (Classes, Objects, and random numbers)(80 pts, Due Mon, Mar 8 at midnight)Note: you may work with a partner or you may work alone. If you choose to work with a partner, make sure you both turn in the project, and make sure you include both names on the project. Equally, note your partner’s name in canvas. Please be aware that if your partner flakes on you, you are still responsible for completing the mini project and turning it in on time.Time: This project may take approximately 8 hours, and should be worked on in a spread-out manner, as opposed to one sitting. On a scale of 1-5 of difficulty, with 1 being really easy for this class and 5 being really difficult for this class, this should be about a 2.This miniproject is closely tied to week 2 videos and ppts on my web site: (yeah, I’m just gonna keep plugging it until everyone gets used to the idea that that is where I place my course content).Objective: This lab is designed to confirm your ability to work with classes and objects and class composition. Contents TOC \o "1-3" \h \z \u Purpose of this miniproject: PAGEREF _Toc65417378 \h 1Dog Survival Game: Description of Game PAGEREF _Toc65417379 \h 1Your job: PAGEREF _Toc65417380 \h 2Getting Started: PAGEREF _Toc65417381 \h 2Board Class Methods (38 pts): PAGEREF _Toc65417382 \h 2Board.hpp PAGEREF _Toc65417383 \h 3Board.cpp PAGEREF _Toc65417384 \h 5Dog Class (33 pts) : PAGEREF _Toc65417385 \h 7MainGame.cpp File: PAGEREF _Toc65417386 \h 8My approach to writing this lab (for testing purposes): PAGEREF _Toc65417387 \h 9TO TURN IN: PAGEREF _Toc65417388 \h 10EXTRA CREDIT OPPORTUNITY (up to 20 pts total): PAGEREF _Toc65417389 \h 11Purpose of this miniproject:The purpose of this project is to establish a firm foundation in creating and using basic classes and objects. For many of our future data structures we will be using classes and objects. Random number generation is thrown in as well (because you can never go wrong with a bit of randomness).Thus I am requiring the 2 classes: Dog and Board. I am requiring that you largely follow my method outline for the Dog and Board class, although I will allow you to add methods (helper methods, perhaps) and even modify the method parameters. Note: If a pop-up box arises that says “Errors exist, do you wish to continue?” your code DOES NOT COMPILE. Don’t click continue. Fix your code. You will not receive credit for code that does not compile.Dog Survival Game: Description of GameFor this lab you will be using classes and objects to write a program that gets a puppy dog (fluffy) safely through an evil forest. There’s a video here.Your goal is to get fluffy the dog from the random start position on the left side of the board to the random end position on the right side of the board. You must help fluffy navigate around (or through) walls, deal with potentially hidden traps, and gather food to have enough strength to make it to the other side. Every time fluffy moves up, down, left, or right, they lose 2 strength points. In addition, if fluffy lands on a trap, they will lose a random amount of strength. If, however, they land on food, fluffy will gain a random amount of strength. Your goal is to make sure fluffy has enough strength to navigate the board to get to the end on the right side.FLuffy may break through walls if they have enough strength.If, however, fluffy runs out of strength points before making it to the other end of the evil forest, then fluffy dies a sad and horrible death.Your job: For this project, there are 2 classes: the Dog class andthe Board class. I am giving you:The Board.hpp header file for the Board classSome Board methods (for the Board definition file)The mainGame.cpp main file:You must write:The rest of the Board methods (as described below)The Dog.hpp (header) and the Dog.cpp (definition) files for the Dog class (as described below.Getting Started:Start by creating a new project in eclipse (make sure you set the compiler to be mingw on a pc, macosx on the mac). Once you have created a new project, right(control) click on the project you just created, and select file->new->header file to make a .hpp header file, and file->new->source file to make the associated .cpp file. Name them the same thing if the .cpp is where the method definitions associated with the .hpp file will be. (e.g., for the board class, the files should be Board.hpp and Board.cpp, respectively.In C++ we separate the class definitions from the class declarations. The class declaration goes into its own header file (.hpp)The header file should have the fields (aka properties) associated with the class, and the method declarations. It is the header file that is included in other filesInclude the .hpp in the .cpp, and not vice versa!! (Including both in both will cause compiler issues)The associated .cpp file contains the class’ definitions for all the methods. This includes the method constructors (possibly overloaded). Now let’s look at the Board class header file and definitions:Board Class Methods (38 pts):I’m giving you the header file for the board class, and some of the class definitions as well. I am giving you the constructors for this class, as well as the InitAll method and the playGame method. Your job is to write the following methods in the Board.cpp ( the definition) file:The boardConfig (8 pts) method (as defined, below)The printBoard (8 pts) method, (as defined, below)The addFood (5 pts) method (as defined, below)The addTraps (5 pts) method (as defined, below)The moveDog (12 pts) method (as defined, below)Board.hppHere is the Board Class Header file, along with property and method descriptions. It should be in its own file, with a .hpp extension. See above (Getting Started) for how to create Board.hpp/***********************************************************************************************/#ifndef BOARD_HPP_#define BOARD_HPP_#include "Dog.hpp"#include <iostream>#include <string.h>using namespace std;class Board {int size = 20; //the square board's sizechar board[20][20]; //the boardint wallStrength; //the amount of strenght needed to take down part of the wall (6)int startx; //Where the dog enters the board (randomly along the left side)int starty; //again, the dog's y entrance to the board (0)int endx; // THe goal location on the right side (random x value)int endy; // will be size -1char level; //'e' for easy, 'm' for medium, 'h' for hard (user can input in the initAll() method)Dog mydog; //the dog object that's moving throughbool debug;//this is a boolean value that I used for debugging - so if it was true, and//I was in debug mode, I'd include a lot more print statements in each method//so I could see where I was and what was happening. Then I could just switch//this to false and all the print statements would become invisible, but if I//moved on, I could always switch the debug print statemetns back on to see//what was happening in m code.public:Board(char diff, string name, bool d);/* constructor - has as a parameter the level of difficulty, the name I'll give * to my dog object, and whether debug mode is on or off * This method calls the initAll method. */Board(char diff, bool d);/* constructor - sets level of difficulty and the debug mode. With this the * dog constructor that doesn't require a name is called, so the default dog's * name is fluffy (which is set in the dog's constructor) * This method calls the initAll method. */Board(bool d);/* for this constructor the level is set to be 'e' (which is the default), it calls * the default dog constructor, and it calls the initAll method. */void InitAll();/* (I'm giving you this one. ) * It loops for each playing of the game, continuing * as long as the user enters "yes", "Yes","y", or "Y". It asks the user and * sets the level of difficulty. It finds the start and end square that the * dog must traverse, it calls boardConfig to configure the board, it calls * printBoard to see the board, it calls addFood and addTraps. And then it calls * the playGame method to start the game. It loops to continuously reset everything * and play another round of hte game for every time the user says they want to * continue playing another round. */void boardConfig();/* this method and the moveDog method are probably the longest methods. * This method first puts dummy values in every square (I used '+' - I just didn't want * every square to have gibberish, so I chose a character I wasn't using for any other * purpose and initialized every square to have that value). * I then added my random horizontal and vertical walls. * * Here's how I did it the walls: * * I only placed walls in the odd numbered rows and columns (in other words, row one might * have a row, but then row 2 would not, whereas row three could and row 4 could not. * Same with columns (although you can have flexibility. * * I picked an odd row or column that did not already have a wall in it at all. * * I generated a total of 10 walls. * For each wall, I picked randomly for vertical or horizontal (just rand() % 2 to get either * 0 for vertical, 1 for horizontal. * * I set the easy version to have at most 9 squares in a row or column as a wall, the * medium to have at most 13 squares as walls in a row or column, and the hard version to * have at most 16 squares per row or column.(but you can play with this because sometimes * the hard version was seriously hard!!! * * Let's say I randomly decided to place a wall in row 3. I then randomly chose 9 squares in * row 3 to be a wall (if it was the easy version, 13 if it was the medium, and 16 if it was * the hard) * * So that's the walls. Then I added the 'D' to the beginning square nad an 'E' to the end square. * * */void printBoard();/* this method first uses the dog's printDog method to print out info about the dog, and then * it prints out the board as follows: * * This method prints out hte board such that it looks like a board. It prints a blank space * for the dummy values (for squares that do not have food, traps, walls, the dog, and aren't the * beginning or the ending. Otherwise it prints out the character in the square. * Note that I printed a border around the entire board so it was easier to look at, and, equally, * I placed an -> arro (thats a minus and a greater than sign) in the border for where the * beginning of the game was and where the end of the game was. E.g., this is what my starting * printBoard printed: (Note this is after I called addFood and addTraps) */// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _//| F | | T |//->D _ _ _ _ _ _ _ _ _ _ _ _ |//| | |//| F T | | |//| T | | F | |//| _ | _ _ _ _ _ _ _ _ | _ _ _ _ |//| | F | |//| _ _ | _ _ _ _ F _ _ _ _ _ F _ _ |//| | | |//| _ _ _ | _ _ _ _ _ _ _ _ _ _ |//| T T | |//| T _ | _ _ _ _ _ _ _ F _ _ _ _ _ |//| F F F | | |//| T | | | T |//| F | | |//| | T T | | F |//| F | | F E ->//| _ _ | _ _ _ _ _ _ _ _ | _ | _ _ |//| | T F | | F |//| | F F | | T |// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _///* Note: Debug Mode: * The board class has a debug field. For every example I showed you, I had the * debug field set to true. * * When the debug field is set to true, the Traps are printed out. This makes the game easier * to test (and a lot easier to play). However, if the debug mode is set to false, the Traps * are not printed out unless the Dog steps on a trap, at which point they randomly lose points. * So when not in debug mode, the printBoard prints " " for every 'T' character on the board. * When the game is in debug mode, though, the printBoard prints out the 'T'. */void addFood();/* this method randomly adds food around the board. For easy, I added the field size * number of foods randomly around the board. For medium, I added size-2 foods, and for * hard I added size-4 foods. The amount of strength the dog gets when they eat (aka * move onto) the food is determined in the moveDog method. */void addTraps();/* this method randomly adds traps around the board. For easy I added size - 6 traps, * for medium, I added size - 8 traps, and for hard I added size - 10 traps. Note: Traps are * only printed on the board when the game is in debug mode. The amount of strength each trap * saps from the dog is determined in the moveDog method when the dog moves on a Trap. */void playGame();/* (I'm giving you this one) * this method loops for every move the dog makes. It asks whether the user wants to move up, * down, left, or right. It then calls the moveDog method with the character indicating which * way the dog should move, and then it calls the printBoard method. */bool moveDog(char c);/* This is a somewhat long method. * First, it determines the new coordinats of the dog, based on teh direction in which the * dog wants to move (based on what c holds - u is up, d is down, l is left, r is right). * * If the dog is at teh edge of the board, teh dog should not move * * If the dog moves over food, a random number between 2 and 17 is generated, and the * dog's changeStrength method is used to increase the dog's strength by the random amount. * * If the dog moves over the end of the board, the dog's won method is called and false is * returned from this method. * * If the dog moves over a Trap, a random number between 2 and 17 is generated and the dog's * changeStrength method is called with that negative number. (Note that the changeStrength * method returns a boolean indicating whether the dog's strength has gone to 0 or less, and * thus the dog has died and the game is over. That boolean value is returned from this method). * * If the dog moves over a wall, the method checks to see if the dog has enough strength to * knock down the wall (I made it cost 6 strength points to knock down a wall). If the dog * has enough strength, the user is asked, "Do you want to knock down that wall?" If the user * responds "yes", then the wall is knocked down, the dog moves into that square, adn the dog's * strength decreases by 6. If the answer is "no", the dog loses 1 strength point, just because. * * If the dog moves into a blank square, the dog loses 2 strength points using the changeStrength * method (which, again, will return false if the dog has lost all their strength and died. * * NOTE: I am not concerned with the precise rules here. If you want to change the range of * random numbers for food, if you are worried about whether you still lose 2 strength points for * moving even though you gained points for moving over food - I'm leaving all that to your preference. * I am concerned that you are using classes, objects, and object methods and accessing object * fields correctly! * */};#endif /* BOARD_HPP_ *//**************************************************************************************************/Board.cppThis is where the class definitions go! To create it (if you haven’t already), right(control) click on the project, select source file, name it Board.cpp. Here is an outline (and some written methods) for the Board.cpp class definition file:#include "Board.hpp"#include <iostream>using namespace std;Board::Board(char diff, bool d){level = diff;debug = d;wallStrength = 6;InitAll();}Board::Board(bool d){level = 'e';debug = d;wallStrength = 6;InitAll();}Board::Board(char diff, string name, bool d) {level = diff;debug = d;mydog.name = name;wallStrength = 6;InitAll();}void Board::InitAll() {bool keepPlaying = true;while (keepPlaying) {cout << "What level of difficulty do you want (e, m, or h)?" << endl;char c;cin >> c;level = c;startx = rand() % size;starty = 0;endx = rand() % size;endy = size-1;mydog.x = startx;mydog.y = starty;boardConfig();addFood();addTraps();printBoard();playGame();cout << "Play again? " << endl;string s = "no";cin>>s;if (s == "yes" || s == "Yes" || s == "Y" || s == "y") {keepPlaying = true;mydog.reset();}else {cout <<"Thanks for playing!" << endl;keepPlaying = false;}}}void Board::playGame() {bool play = true;while (play) {cout << "Move (u, d, l, r) "<<endl;char c;cin >> c;play = moveDog(c);printBoard();}}void Board::addFood() {/* (5 pts) code for the addFood method goes here*/}void Board::addTraps() {/* (5 pts) code for the addTraps method goes here*/}void Board::boardConfig() {/* (8 pts) code for the boardConfig method goes here*/}void Board::printBoard() {/* (8 pts) code for the printBoard method goes here */}bool Board::moveDog(char c) {/* (12 pts) code for the moveDog method goes here*/}Dog Class (33 pts) :For the dog class, you should create both the header file and the definition file (Dog.hpp and Dog.cpp, respectively).(8 pts for creating a proper Dog header file)The dog class consists of the following fields:string name; // for the dog's nameint strength; //for the dog's current strengthint x; // the x coordinate of where the dog is currently on the boardint y; // the y coordinate of where the dog is currently on the boardAnd the following methods (all are defined in more detail below this list): (4 pts) Dog(string n); //constructor(3 pts) Dog(); //constructor(6 pts) bool changeStrength(int amt); //changes dog’s strength field(3 pts) void die(); //die message when strength at or below 0(3 pts) void printDog(); // prints out info about dog(3 pts) void won(); //won message for when dog is at end of evil forest(3 pts) void reset(); //resets dog for restarting a new gameNOTE: The dog header file (created by right-clicking on the project, then selecting file->new->header file) should contain the following line right below the class Dog { line:friend class Board;//by making the Board a friend of this class, the//Dog class is allowing the board class and objects //to access all of the dog class' private fieldsHere are more in-depth explanations of the constructors and the methods for the dog class:Dog(string n); (/* constructor, sets the name to be whatever name gets passed in, the * strength to be 50 (I just picked 50 - you want another number, go for it!) and I made * the original coordinates be 0 and 0 because the compiler yells at me a tiny little yell * if I don't initialize all my fields in the constructor */Dog();/* constructor, I made the default be fluffy, but you can make it anything you like!! * I set the strength to be 50 (again, I just picked 50), and I made the original * coordinates be 0 and 0 because... */bool changeStrength(int amt);/*changeStrength modifies the strength property by whatever amt is (basically adding here) * It then calls printDog method to print out current information about the dog. * It also checks to make sure that the dog object's strength is >0. * If the dog object's strength is 0 or less, it calls the die method. * It returns true if the strength is still >0 and false otherwise. */void die();/* This method just prints out a sad pathetic death of a dog message, letting everyone * know of the demise of the dog object. My message has ACK!!! and judgmental statements * about how the user is a cruel, heartless person for letting a puppy dog die, but * yours can be whatever you like. */void printDog();/* this method just prints out the basic information about the dog - its *name and its current strength. */void won();/* again, just a message about the user's incredible prowess in successfully navigating the * sweet little puppy dog through the evil forest to the other side. You can make it whatever * you like.*/void reset();/* this method just resets the strength (for me it was 50) and the x and y to 0 and 0. * *//*********************************************************************************/MainGame.cpp File:Finally, here is my mainGame.cpp file:#include "Board.hpp"#include <iostream>#include <time.h>#include <stdlib.h>using namespace std;int main() {srand(time(NULL));Board board('m',"fido", true);return 0;}If you get all this written, tested, and working, you’re done!!! Congratulations!!! Savor this coding moment of victory./****************************************************************************************************/My approach to writing this lab (for testing purposes): NOTE – THIS MAY NOT HAVE ALL THE STEPS!!! I always get questions on how to test class objects, so I’m giving you a general outline of how I would approach writing and testing these class methods. Nothing here is set in stone. I am hoping this outline will give you an idea of how one would go about creating classes and objects and testing as they go. I started with the board class and methods. In the constructor hand-coded the fields, commenting out the mydog field. I commented everything out in the InitAll method, except the call to BoardConfig. I wrote the BoardConfig method to just fill the entire matrix (the board) with a dummy value (something that you can use to tell a square is empty. I used ‘+’, but you can use pretty much anything other than D,T,F,|, and _.I wrote the printBoard method to print out the board.Got both of these working.I added the code that adds walls in the BoardConfig methodTested by printingI added a method for adding Food.Added the call to that method back to the initAll method.Tested by printingAdded a method for adding the trapsAdded the call to that method back to the initAll methodTested by printingNOW I started working on the Dog class I made the dog constructorsI wrote the method for printDogI added the dog object back to my board methodI added a printDog call in my initAll method to make sure I was good.I could work on the won and die methods now, or later – they won’t be tested till laterBack to the Board classAdd back code in the InitAll method that generates the random begin (startx, starty) and end, and set’s the dog’s x and y to the beginning. Modified the boardConfig method so the dog is in the startx, starty position (You just need to put a ‘D’ in that square, not a dog object).Test to make sure the dog shows up in the right position.Modified the boardConfig method to reflect the start and end location Tested.In the Board class I now added in the moveDog methodMade the dog move up, down, left, and rightTestedMade sure the dog would not move off the boardTestedBack to Dog class – wrote the changeStrength methodThen in the board class, in the moveDog method, added code such that every time the dog moves, the dog’s changeStrength method is called to reduce strength by 2TestedIn moveDog method, added check for if the dog is moving over food, and, if so, generated a random amount of food strength and sent that into the changeStrength method. (also changing the square to be empty so you can’t just keep getting that food)TestedIn moveDog method, added check for if the dog is over a Trap, and, if so, generated a random amount for strength-sapping, and sent that into the changeStrength method.TestedIn moveDog, added code for if the dog hits a wall, and if so, checks to see if the dog has enough strength to break down the wall. If so, asks the user if the dog wants to break down the wall, and, if so, breaks down the wall and calls the dog’s changeStrength method to reflect breaking down the wall (-6 strength points) and changes the wall to be an empty space.TestedIn moveDog, added code for when the dog made it to the end, and, if so, called the dog’s won methodTestedAt this point, I was largely done. Maybe played around with spacing, with allowing the user to start over, but the game was pretty much written.That’s the general idea. I hope it helped!!/****************************************************************************************/TO TURN IN:You should turn in:A zip file (named AA_BC_Lab1.zip where AA is your initials and BC is your partner’s initials. If you do not have a partner, skip the second set of initials) of code that compiles, with the following files:Board.cppBoard.hppDog.cppDog.hppmainGame.cppScreenshot of your code running*NOTE that if you worked with a partner, BOTH your names must be at the top comment section of each fileWhile you may both turn in the same zip file (so AA_BC_Lab1.zip – it doesn’t matter whether your initials are AA and your partner’s initials are BC, or vice versa), you BOTH SHOULD TURN IT IN!Why? This helps us if there are ever any issues with things being turned in (e.g., your partner was supposed to turn it in but their computer crashed at 11:59pm, you forgot because there was free pizza in the hallway, you turned in an old version that didn’t work, etc.). This way we have back-up.*(Why a screenshot of your running code? Because if there’s an issue getting your code to run on our computer(s), a screenshot gives us enough info to know whether to meet with you to see it running on your computer. I do not want screenshots of every single part of the code running properly – just one or two shots of the running code that indicates that the code did actually compile and work).EXTRA CREDIT OPPORTUNITY (up to 20 pts total):C++ is a middle level language (as opposed to Java, which is a high-level language). In a low level langage, there’s a 1-1 correspondence between commands and what the processor executes. In a high-level language, the focus is on higher level functioning and usually no ability to manipulate memory and processor executions. C++ bridges the gap between those two levels. It is often used for systems programming (for instance, your operating system) because of this. I told you all of that because I’m explaining why this lab has a pretty klugey interface. Yeah, not pretty. I get that. Wanna make it pretty? Up to 15 points extra credit to make a prettier interface. (You’ll have to look up and import libraries, most likely).Along those lines, this is a pretty basic game. What did you want for a one-week lab? But I can think of many ways to make it way cooler. Up to 15 points extra credit for coming up with some cool improvements on the game(Note that, while each of these opportunities is worth up to 15 points, even if you do both in an astounding, breathtaking way, you can only get 20 pts max extra credit on this lab). ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download