-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
96 lines (86 loc) · 1.6 KB
/
Player.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Player describes the player/character of the game, including the player's name,
* score, and inventory. It implements the singleton design
* pattern, as there is only ever one instance of Player in any given game.
* @author Alison Mayer
* @date May 6, 2012
* @version 1.1
*/
public class Player
{
private static ItemBag inventory;
private static int score;
private static String name;
private static Player player;
//private constructor; Player implements a singleton design pattern
private Player()
{
name = "";
score = 0;
inventory = new ItemBag();
}
/**
* Returns the singleton Player to user. If there is no instance of Player,
* constructs a new Player.
*
* @return player, the instance of Player
*/
public static Player getPlayer()
{
if (player == null)
player = new Player();
return player;
}
public static void changeScore(int x)
{
score+=x;
}
public static void printScore()
{
System.out.println("Your score is " + score);
}
/**
* Returns the player name.
*
* @return name
*/
public String getName()
{
return name;
}
/**
* Returns the player score.
*
* @return score
*/
public int getScore()
{
return score;
}
/**
* Returns the player inventory.
*
* @return inventory
*/
public static ItemBag getInv()
{
return inventory;
}
/**
* Sets the player name to user-specified String.
*
* @param newName the new name of the player
*/
public void setName(String newName)
{
name = newName;
}
public static void addItem(Item i)
{
inventory.addItem(i);
}
public static void dropItem(Item i)
{
inventory.removeItem(i);
}
}