-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayer.java
More file actions
159 lines (143 loc) · 4.24 KB
/
Player.java
File metadata and controls
159 lines (143 loc) · 4.24 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* A class that implements a player for the ConnectN (and possibly other) games.
* <p>
* You do not need to modify this class (except to fix one checkstyle error). But you are encouraged
* to study it since it does some of the things that you need to do to complete ConnectN.
*
* @see <a href="https://cs125.cs.illinois.edu/MP/5/">MP5 Documentation</a>
*/
public class Player {
/** Class variable that we used to generate a unique ID for each newly created player. */
private static int globalID = 0;
/**
* name of the player.
*/
private String name;
/**
* if player is x, first player.
*/
public boolean x;
/**
* if player is o, second player.
*/
public boolean o;
/**
* Get the player's name.
*
* @return the player's name
*/
public String getName() {
return name;
}
/**
* Set the player's name.
*
* @param setName the player's new name
*/
public void setName(final String setName) {
this.name = setName;
}
/** The number of games this player has won. */
private int score;
/**
* Get this player's score.
*
* @return this player's score
*/
public int getScore() {
return score;
}
/**
* Add one to this player's score.
*/
public void addScore() {
score += 1;
}
/** This player's ID. Used internally by {@link #equals(Object) equals()}. */
private int id;
/**
* Get the current player's id.
*
* @return the current player's id
*/
public int getID() {
return id;
}
/**
* Create a new player with the given name.
* <p>
* Each player's score begins at zero, and each receives a monotonically increasing unique ID.
*
* @param setName the name for the new player
*/
public Player(final String setName) {
this.name = setName;
this.score = 0;
this.id = Player.globalID++;
}
/**
* Create a copy of an existing player.
* <p>
* This copy constructor creates a copy of an existing player, so that player information can be
* exposed without allowing the copy to modify the state of the original player.
* <p>
* The id is copied, meaning that the copy will initially be equal to the original. However,
* future modifications to the score field will not change the copy and will result in it being
* considered non-equal to the original.
*
* @param other the other
*/
public Player(final Player other) {
this.name = other.name;
this.score = other.score;
this.id = other.id;
}
/*
* The following methods were auto-generated by Eclipse. We use the id field for
* equals and hash, implementing entity semantics.
*/
/**
* Define the hash code for the Player class.
* <p>
* This method should only use the id field of the instance. This implements what is known as
* <i>entity</i> semantics. Two players with the same name and same score may not be equal if
* they were created at different times. Note that Eclipse can auto-generate this and
* {@link #equals(Object) equals}.
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
/**
* Define equality for the Player class.
* <p>
* This method should only use the id field of the instance. This implements what is known as
* <i>entity</i> semantics. Two players with the same name and same score may not be equal if
* they were created at different times. Note that Eclipse can auto-generate this and
* {@link #hashCode() hashCode()}.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public final boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Player other = (Player) obj;
if (id != other.id) {
return false;
}
return true;
}
}