Question

In: Computer Science

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() 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


/*Program.cs*/
/*
C sharp program that demonstrates the Player class
* and then call the methods of the Player class and display
* the results on to console window.
* Create a C sharp console project named as "VideoGameConsole"
* The add a class, "Player" to the project and write the code.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VideoGameConsole
{
class Program
{
static void Main(string[] args)
{
Player player = new Player();
player.setAncestry("Human");
player.moveEast();
/*Print player details*/
Console.WriteLine("Move east...");
Console.WriteLine(player.ToString());
player.moveWest();
/*Print player details*/
Console.WriteLine("Move west...");
Console.WriteLine(player.ToString());
player.moveSouth();
/*Print player details*/
Console.WriteLine("Move south...");
Console.WriteLine(player.ToString());
player.moveNorth();
/*Print player details*/
Console.WriteLine("Move north...");
Console.WriteLine(player.ToString());

Console.WriteLine("Add score 4 ...");
player.addToScore(4);
Console.WriteLine(player.ToString());
Console.ReadLine();
}
}
}

--------------------------------------Player.cs-----------------------------------------------------------------------

//Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VideoGameConsole
{
//Player class
class Player
{
//declare instance variables
private int locX;
private int locY;
private string ancestry;
private int score;
/*Default constructor */
public Player()
{
locX = 0;
locY = 0;
ancestry = "Not Set";
score = 0;
}
/*Set and get method for ancestry*/
public void setAncestry(string ancestry)
{
this.ancestry = ancestry;
}

public string getAncestry()
{
return ancestry;
}

public int getScore()
{
return score;
}
/*Method that increments the locX by 1*/
public void moveEast()
{
locX++;
}
/*Method that decrements the locX by 1*/
public void moveWest()
{
locX--;
}
/*Method that increments the locY by 1*/
public void moveNorth()
{
locY++;
}
/*Method that decrements the locY by 1*/
public void moveSouth()
{
locY--;
}
/*Add points to the score*/
public void addToScore(int points)
{
score = score + points;
}
/*Override the ToString method*/
public override string ToString()
{
return string.Format("Ancestry : {0} Location: ({1}, {2}) Score : {3}", ancestry, locX, locY, score);
}
}
}

----------------------------------------------Sample Run#-------------------------------------------------------------


Related Solutions

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...
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...
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?
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below. Monomial Description Each monomial object has following properties: Each monomial has a coefficient and a power. Monomial power is always positive. Monomial power is always integer. Monomials cannot be added if they have different powers Class Details The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value...
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
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of integers The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows...
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...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given...
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT