package squishgame;

 * The player class allows for a player to be created with:
 *      a name
 *      a location on a grid (0-10, 0-10)
 * The player can move North, South, East, or West but will not move off the
 * grid (must stay within (0,0) to (10,10)).  If a move is called, it will update
 * it's location or not move (if outside the grid).
 * Complete the constructors and methods below

public class Player implements Comparable {

    private String myname;
    private int mylocx,  mylocy;

    public Player(String name) {
        // constucts a new player with a random location
    public Player(String name, int x, int y) {
        // constructs a new player with the given location
    public void moveNorth() {
        // moves the player up 1 location if possible
    public void moveSouth() {
        // moves the player down 1 location if possible
    public void moveEast() {
        // moves the player right 1 location if possible
    public void moveWest() {
        // moves the player left 1 location if possible
    public int getX() {
        // returns the x position
    public int getY() {
        // returns the y position
    public double distance(Player p) {
        // returns the distance between this player and player p
    public boolean isSamelocation(Player p) {
        // returns true if this location is the same a the player p's location
    public String toString() {
        // returns a string representation of the player's name and location

    public int compareTo(Object o) {
        // returns a negative number, 0, or a positive number depending if this
        // object is less than, equal to, or greater than the parameter o
        // Players are ordered by x position, then by y position so (2,5) < (3,1)

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

7 Years
Discussion Span
Last Post by JenniLei

The comments in the methods tell you exactly what you need to do. All is needed is some if statements and reducing or increasing the mylocx or mylocy by one.

Edited by javaAddict: n/a


What you given is just the assignment template and you havent even attempted any of the functions.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.