Question

In: Computer Science

FOR C++ A friend wants you to start writing a video game. Write a class called...

FOR C++ A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast() and moveWest() that increment/decrement the x and y position of the player, respectively. Write a method called addToScore() that takes in an integer and adds it to the players score.

Solutions

Expert Solution

Refer to screenshots

Program screenshots

Output

Code to copy

#include <iostream>
#include <string>

using namespace std;

class Player{
   private:
       int x;
       int y;
       string ancestry;
       int score;
      
   public:
  
       Player(){
           x = 0;
           y = 0;
           score = 0;
           ancestry = "Not setted";
       }
  
       int get_x(){
           return x;
       }
      
       void set_x(int x_cord){
           x = x_cord;
       }
      
       int get_y(){
           return y;
       }
      
       void set_y(int y_cord){
           y = y_cord;
       }
      
       string get_ancestry(){
           return ancestry;
       }
      
       void set_ancestry(string ance){
           ancestry = ance;
       }
      
       int get_score(){
           return score;
       }
      
       void moveNorth(){
           y += 1;
       }
      
       void moveSouth(){
           y -= 1;
       }
      
       void moveEast(){
           x += 1;
       }
      
       void moveWest(){
           x -= 1;
       }
      
       void addToScore(int new_score){
           score += new_score;
       }
};

int main(){
   Player p1;
   p1.set_ancestry("Human");
   p1.set_x(2);
   p1.set_y(2);
  
   p1.moveNorth();
   p1.moveSouth();
   p1.moveNorth();
   p1.moveEast();
  
   p1.addToScore(5);
   p1.addToScore(3);
  
   cout<<"Ancestry is "<<p1.get_ancestry()<<'\n';
   cout<<"Geo Location is ("<<p1.get_x()<<","<<p1.get_y()<<")\n";
   cout<<"Score is "<<p1.get_score()<<'\n';
  
   return 0;
}


Related Solutions

C# please! A friend wants you to start writing a video game. Write a class called...
C# please! A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Always. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast()...
Your friend tells you that she wants to start saving for retirement by investing in the...
Your friend tells you that she wants to start saving for retirement by investing in the stock market. Given that you have taken this finance class, she asks you for advice about what stocks she should buy. What would you tell her?
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE,...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE, DON'T ANSWER IT IF YOU'RE NOT SURE WHAT YOU'RE DOING. I APPRECIATE IF YOU WRITE COMMENTS AS WELL. WRONG ANSWER WILL GET A DOWNVOTE Thank in Advance. Must do; The class must have 3 types of constructors; default, overloaded with initializer list, copy constructor You must overload the assignment operator You must declare the overloaded output operator as a friend rather than part of...
Suppose you and your friend want to start a business, and the friend suggests to start...
Suppose you and your friend want to start a business, and the friend suggests to start a movie dvd rental store in the bronx. Is that an attractive market? Discuss using Porter's Five Forces
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
For this programming assignment, you will use your previous code that implemented a video game class...
For this programming assignment, you will use your previous code that implemented a video game class and objects with constructors. Add error checking to all your constructors, except the default constructor which does not require it. Make sure that the high score and number of times played is zero or greater (no negative values permitted). Also modify your set methods to do the same error checking. Finally add error checking to any input requested from the user. #include <iostream> #include...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
A video game manufacturer has recently released a new game. The manufacturer wants to know whether...
A video game manufacturer has recently released a new game. The manufacturer wants to know whether players rate their new game as more or less difficult than the average difficulty rating of all of their games, μ = 6 and σ = 2. A random sample of 36 players yielded a sample mean of 7 and a standard deviation(s) of 1.8.              a. State the null and alternative hypothesis.              b. Conduct a z-test on the data given.              c. What do you...
This is a classic retirement problem. A friend is celebrating her birthday and wants to start...
This is a classic retirement problem. A friend is celebrating her birthday and wants to start saving for her anticipates retirement. She has the following years to retirement and retirement spending goals: Years until retirement: 30 Amount to withdraw each year: 90,000 Years to withdraw in retirement: 20 Interest Rate: 8% Because your friend is planning ahead, the first withdraw will not take place until one year after she retires. She wants to make an equal annual deposit into her...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT