write the function body for each of the functions using R structure and style (R
ID: 3740643 • Letter: W
Question
write the function body for each of the functions using R structure and style (R function)
Player <- function(name, startingPosition, defencePower) {
# Returns a new Player with name 'name', a position of
# 'startingPosition', defence power 'defencePower', and
# 0 gold.
#
# Note: The underlying data of Player objects must be their name.
#
# Precondition: startingPosition, defencePower >= 0
}
Monster <- function(name, attackPower) {
# Returns a new Monster with name 'name' and attack power 'attackPower', and
# a starting position of 0.
#
# Note: The underlying data of Monster objects must be their name.
#
# Precondition: attackPower >= 0
}
### You are allowed to write your own class helper functions (like getters
### and setters) if you wish. Just make sure they work!
GetInfo <- function(self) UseMethod("GetInfo")
GetInfo.Player <- function(self) {
# Returns in a named list the name, position, and amount of gold of
# player 'self'.
}
GetInfo.Monster <- function(self) {
# Returns in a named list the name, position, and attack power
# of Monster 'self'.
}
MovePosition <- function(self, newPosition) {
# Returns a copy of Player or Monster 'self' with its position
# changed to 'newPosition'.
# Note that you don't need separate definitions of this function
# for each class because changing the position is the same for both classes.
#
# Precondition: newPosition >= 0
}
AddGold.player <- function(self, amount) {
# Returns a copy of Player 'self' with 'amount' add to their gold.
#
# Precondition: amount > 0
}
BuyWithGold.player <- function(self, itemCost) {
# Returns player 'self' with their gold depleted by 'itemCost' if they
# have at least 'itemCost' amount of gold. Otherwise returns the player
# without any change to their gold.
#
# Precondition: itemCost > 0
}
Battle <- function(player, monster) {
# Returns the name of the 'monster' or 'player' who has larger
# attack or defense power, only if they are at the same position.
# Returns 0 if no winner can be determined (equal attack/defence).
# Returns -1 if they are not at the same position.
}
Explanation / Answer
#include<iostream>
using namespace std;
class Player{
private:
//Player data members
string name;
string position;
string team;
public:
Player(){}
//constructor with 3 parameters
Player(string n, string p, string t)
{
name = n;
position = p;
team = t;
}
//constructor with 2 parameters
Player(string n, string t)
{
position = "corner"; //default position if not assigned
name = n;
team = t;
}
//member function prototypes
void changeTeam();
Player getPlayerDetails(Player p1);
void showDetails();
};
void Player::changeTeam()
{
cout<<" Which team to change to: ";
cin>>team;
}
Player Player :: getPlayerDetails(Player p1)
{
Player p;
p.name = p1.name;
p.position =p1.position;
p.team = p1.team;
return p;
}
void Player::showDetails()
{
cout<<" Player name: "<<name;
cout<<" Position: "<<position;
cout<<" Team: "<<team;
}
int main()
{
Player p1("Kevin Malone", "forward", "Lakers"); //object passing three parameters
Player p2("Ralf Brody", "Yankees");//object passing two parameters
p1.showDetails();
p2.showDetails();
Player p3;
p3 = p1.getPlayerDetails(p1); //returning player details to p3
p1.changeTeam();
cout<<" New team:";
p1.showDetails();
cout<" Old team: ";
p3.showDetails(); //showing p3 details
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.