In: Computer Science
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.
/*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#-------------------------------------------------------------