-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
160 lines (145 loc) · 4.51 KB
/
Copy pathPlayer.java
File metadata and controls
160 lines (145 loc) · 4.51 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
/*
Joseph Calise
ID#: 2380565
calise@chapman.edu
CPSC-231 Section 03
MP3B_Black_Jack
*/
import java.util.LinkedList;
public class Player {
private String name;
// cash will serve as the players bank account.
private int cash;
public LinkedList<Card> hand;
public Player() {
this.name = "Player";
this.cash = 100;
this.hand = new LinkedList<Card>();
}
public Player(String name, int cash) {
this.name = name;
this.cash = cash;
this.hand = new LinkedList<Card>();
}
/**
* totalCash is a more formal getCash method which will System.out the cash
* amount
* as well as the player's name.
*
* @return String of player's name and the cash amount left they have to play
* with.
*/
public String totalCash() {
return this.name + " now has $" + this.cash + " left in their account.";
}
/**
* getCash will return the cash remaining in the player's balance.
*
* @return int that represents the cash the player has left to play with.
*/
public int getCash() {
return this.cash;
}
/**
* lostCash is a method called when the player loses the round.
* This will subtract the bet amount and System.out the remaining balance.
*
* @param bet is the value the player bet on the hand.
* @return String, this string will contain the amount the player has left,
* formatted in text.
*/
public String lostCash(int bet) {
this.cash -= bet;
return this.totalCash();
}
/**
* winHand is a method called when the player wins the round.
* This will add the bet amount and System.out the remaining balance.
*
* @param bet is the value the player bet on the hand.
* @return String, this string will contain the amount the player has left,
* formatted in text.
*/
public String winHand(int bet) {
this.cash += bet;
return this.totalCash();
}
/**
* dealHand is a method that will take the cards from the Dealer class' deal()
* LinkedList
* and add those cards into our player's LinkedList to play the round
*
* @param hand represents the LinkedList from the Dealer's method deal()
* @return LinkedList which will be our players hand after the Dealer's deal
* method.
*/
public LinkedList<Card> dealHand(LinkedList<Card> hand) {
for (Card card : hand) {
this.hand.add(card);
}
return this.hand;
}
/**
* aces is a method that will return whether the player's hand contains an ace.
* This logic is used in the handTotal method to determine what value to assign
* the ace.
*
* @return boolean, TRUE is the player has an ace in their hand, FALSE is it
* does not.
*/
public boolean aces() {
int i;
for (i = 0; i < this.hand.size(); i++) {
if (this.hand.get(i).value == 14) {
return true;
}
}
return false;
}
public int countAces() {
int counter = 0;
for (Card card : this.hand) {
if (card.value == 14) {
counter++;
}
}
return counter;
}
/**
* discardHand is run after every round is concluded.
* it clears the players hand of any cards from the last round.
*/
public void discardHand() {
this.hand.clear();
}
/**
* handTotal is a method written to return the value of the players hand.
* based on card values, the method will access 10 for face cards and 11 or 1
* for aces.
* The end of the method is the logic of whether an ace should be counted as an
* 11 or a 1.
* If the player's hand is greater than 21, then the ace will revert to the
* value of 1.
*
* @return int, which will represent the total of the player's hand.
*/
public int handTotal() {
int handTotal = 0;
int i;
for (i = 0; i < this.hand.size(); i++) {
if (this.hand.get(i).value == 14) {
handTotal += 11;
} else if (this.hand.get(i).value > 10) {
handTotal += 10;
} else {
handTotal += this.hand.get(i).value;
}
}
if (handTotal > 21 && this.aces() == true) {
int count = this.countAces();
return handTotal - 10 * count;
} else {
return handTotal;
}
}
}