001 package jagafa.object;
002
003 import jagafa.JassRound;
004 import jagafa.ai.JassAI;
005 import jagafa.ai.ProbabilityAI;
006
007 /**
008 * The Player class holds the hand and the AI of a player. It also knows wether
009 * a he is a computer player or human.
010 * TODO: Needs refactoring: Add "partner" and "opponent" recognition to the Player class
011 */
012 public class Player {
013
014 /**
015 * Wenn der Spieler ein Mensch ist --> false
016 */
017 private boolean isComputer_;
018
019 /**
020 * Karten auf der Hand des Spielers
021 */
022 private Hand hand_;
023
024 /**
025 * Name des Spielers
026 */
027 private String name_;
028
029 /**
030 * Erreichte Punktzahl
031 */
032 private int score_;
033
034 private int numberOfCards_;
035
036 private JassAI jassAI_;
037
038 private static int players_ = 0;
039
040 /**
041 * Erstellt neues Spielerobjekt
042 * @param isHuman True if this player is a human player
043 * @param numberOfCards The number of cards in the hand of the player
044 */
045 public Player(int numberOfCards, boolean isHuman) {
046 this.jassAI_ = new ProbabilityAI();
047 isComputer_ = !isHuman;
048 if (isHuman) {
049 this.jassAI_ = null;
050 }
051 hand_ = new Hand();
052 this.name_ = String.valueOf(players_);
053 score_ = 0;
054 this.numberOfCards_ = numberOfCards;
055
056 players_++;
057 }
058
059 public Player(int numberOfCards, boolean isHuman, JassAI ai) {
060 this(numberOfCards, isHuman);
061 this.jassAI_ = ai;
062
063 }
064
065 /**
066 * Zählt points zum aktuellen Punktestand dazu
067 * @param points The score to add
068 */
069 public void addScore(int points) {
070 score_ = score_ + points;
071 }
072
073 /**
074 * Get the players score
075 * @return Gibt Anzahl erreichter Punkte zurück
076 */
077 public int getScore() {
078 return score_;
079 }
080
081 /**
082 * Setzt Punktzahl auf Null zurück
083 */
084 public void resetScore() {
085 score_ = 0;
086
087 }
088
089 /**
090 *
091 * @return Liefert einen String mit dem Namen des Spielers
092 */
093 public String toString() {
094 return this.name_;
095 }
096
097 /**
098 * Get the players hand object
099 * @return The Players hand object
100 */
101 public Hand getHand() {
102 return hand_;
103 }
104
105 /**
106 * Gibt die Handkarten auf dem Bildschirm aus
107 */
108 public void printHand() {
109 System.out.println(this.hand_);
110
111 }
112
113 /**
114 * Spielt die ite Karte aus der hand des Spielers auf das Board von game
115 *
116 * @param i The card number in the players hand
117 * @param round The Game object to which the player plays the card
118 */
119 public void playCard(int i, JassRound round) {
120
121 Card toPlay = this.hand_.get(i);
122 if (i >= this.hand_.size()) {
123 return;
124 }
125
126 if (round.isValidPlay(toPlay)) {
127
128 round.playCard(toPlay, this);
129 this.hand_.remove(i);
130
131 } else {
132 System.out.println("Invalid Card!");
133 }
134 }
135
136 /**
137 * Checks wether the player is AI-driven or not
138 * @return True, if the player is driven by AI
139 */
140 public boolean isComputer() {
141 return this.isComputer_;
142 }
143
144 /**
145 * Sets the name of the player
146 * @param newName The new name
147 */
148 private void setName(String newName) {
149 this.name_ = newName;
150 }
151
152 /**
153 * Get the player name
154 * @return A string with the name of the player
155 */
156 public String getName() {
157 return this.name_;
158 }
159
160 /**
161 * Get the players AI instance
162 * @return The AI of the Player
163 */
164 public JassAI getAI() {
165 return this.jassAI_;
166 }
167
168 /**
169 * Spielt eine Karte aus der hand des Spielers auf das Board von game
170 *
171 * @param card The card
172 * @param round The Game object to which the player plays the card
173 */
174 public boolean playCard(Card card, JassRound round) {
175 if (card == null) {
176 return false;
177 }
178
179 if (round.isValidPlay(card)) {
180 round.playCard(card, this);
181 this.hand_.remove(card);
182
183 } else {
184 System.out.println("Invalid Card!");
185 return false;
186
187 }
188 return true;
189 }
190
191 public boolean equals(Object sec) {
192 if (!(sec instanceof Player)) {
193 return false;
194 }
195 Player other = (Player) sec;
196 if (other.getName().equals(this.getName())) {
197 return true;
198 }
199 return false;
200
201 }
202
203 public int hashCode() {
204 int sum = 0;
205 char carray[] = this.getName().toCharArray();
206 for (int i = 0; i < carray.length; i++) {
207 sum += (int) carray[i];
208 }
209
210 return sum;
211 }
212
213 public void setAI(JassAI simpleAI) {
214 this.jassAI_ = simpleAI;
215
216 }
217
218 public static void resetPlayerCount() {
219 players_ = 0;
220 }
221
222 public int getID() {
223 return Integer.valueOf(this.getName()).intValue();
224 }
225 }