-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
185 lines (168 loc) · 4.62 KB
/
Player.java
File metadata and controls
185 lines (168 loc) · 4.62 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Represents a player in the Bluejack game.
* Each player has a hand of cards, a board, and a score.
*/
public class Player {
private String name = "";
private final Card[] hand = new Card[4];
private Card[] board = new Card[9];
private int cardNumber = -1;
private int score = 0;
private boolean isCont = true;
private boolean noAction = false;
private Card LocationOfLastPlayedCard = null;
/**
* Sets the player's name.
*
* @param name The player's name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the player's name.
*
* @return The player's name
*/
public String getName() {
return name;
}
/**
* Gets the player's hand.
*
* @return The player's hand as an array of Cards
*/
public Card[] getHand() {
return hand;
} /**
* Gets the current card number.
*
* @return The current card number
*/
public int getCardNumber() {
return cardNumber;
}
/**
* Increases the card number by one and returns the new value.
*
* @return The increased card number
*/
public int getAndIncreaseCardNumber() {
cardNumber++;
return cardNumber;
}
/**
* Displays the player's hand to the console.
* For computer player, cards are hidden.
*
* @param isComp true if this is the computer's hand, false otherwise
*/
public void writeHand(boolean isComp) {
if (isComp) {
for (int i = 0; i < 4; i++) {
if (i == 0) {
System.out.print(name + "'s hand: ");
}
if (this.hand[i].isUsed()) {
System.out.print((i + 1) + ") " + "X");
} else {
System.out.print((i + 1) + ") " + "( )");
}
if (i != 3) {
System.out.print(" | ");
} else {
System.out.println();
}
}
} else {
for (int i = 0; i < 4; i++) {
if (i == 0) {
System.out.print(name + "'s hand: ");
}
if (this.hand[i].isUsed()) {
System.out.print((i + 1) + ") " + "X");
} else {
System.out.print((i + 1) + ") " + this.hand[i].getCardInfo());
}
if (i != 3) {
System.out.print(" | ");
} else {
System.out.println();
}
}
}
} /**
* Displays the player's board to the console.
*/
public void writeBoard() {
for (int i = 0; i < 9; i++) {
if (i == 0) {
System.out.print(name + "'s board: ");
}
if (this.board[i] != null) {
System.out.print(this.board[i].getCardInfo());
}
if (i != 8 && this.board[i] != null) {
System.out.print(" | ");
}
if (i == 8) {
System.out.println();
}
}
}
/**
* Calculates the sum of all cards on the board.
*
* @return The sum of all card values on the board
*/
public int getSumOfBoard() {
int sum = 0;
for (int i = 0; i < cardNumber + 1; i++) {
sum += board[i].getValue();
}
return sum;
}
/**
* Adds a card to the board at a specific index.
*
* @param card The card to add
* @param index The index on the board where the card will be placed
*/
public void addToBoard(Card card, int index) {
this.board[index] = card;
}
/**
* Resets the player's board to a new empty board.
*/
public void reset() {
board = new Card[9];
cardNumber = -1;
isCont = true;
noAction = true;
LocationOfLastPlayedCard = null;
}
public void set_isCont(boolean isCont) {
this.isCont = isCont;
}
public boolean get_isCont() {
return isCont;
}
public Card getLocationOfLastPlayedCard() {
return LocationOfLastPlayedCard;
}
public void setLocationOfLastPlayedCard(Card locationOfLastPlayedCard) {
LocationOfLastPlayedCard = locationOfLastPlayedCard;
}
public int getScore() {
return score;
}
public void incScore() {
this.score += 1;
}
public void setNoAction(boolean noAction) {
this.noAction = noAction;
}
public boolean isNoAction() {
return noAction;
}
}