In: Computer Science
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.
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;
}