-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
142 lines (120 loc) · 3.46 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
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
import java.util.ArrayList;
import java.util.Scanner;
/** Class Player
* stores player stats:
* money balance, number of wins, number of losses
*
*
* methods:
* hit() - draw card from deck, add to hand
* handValue()
* stand()
* bet()
* addBalance()
*
*/
public class Player {
private int balance;
public int betAmt;
private int wins;
private int losses;
private ArrayList<Card> hand;
public Player() {
// players start with $1,000 balance
balance = 1_000;
betAmt = 0;
wins = 0;
losses = 0;
hand = new ArrayList<>();
}
// pick topmost card from deck, add to hand, returns updated hand
public ArrayList<Card> hit(Deck deck) {
hand.add(deck.get(0));
deck.remove(0);
return hand;
}
// ace has value 1 or 11
// ace default gameValue is 11
public int handValue() {
int sum = 0;
int aceCtr = 0;
for (int i = 0; i < hand.size(); i++) {
if (hand.get(i).gameValue == 11)
aceCtr++;
sum += hand.get(i).gameValue;
}
if (aceCtr > 0) {
while (sum > 21 && aceCtr > 0) {
// System.out.println("THE SUM IS === " + sum);
for (int j = 0; j < hand.size(); j++) {
if (hand.get(j).gameValue == 11) {
sum -= 10;
aceCtr--;
break;
}
}
}
}
return sum;
}
public void clearHand() {
hand.clear();
}
// returns new balance
public int bet(int amount) {
// can't bet more than you have
if (amount <= balance) {
balance -= amount;
betAmt = amount;
return balance;
}
return balance;
}
// returns new balance
public int addBalance(int amount) {
balance += amount;
return balance;
}
public int getBalance() {
return balance;
}
public void display() {
System.out.println("\n======== Your Hand ========");
for (int x = 0; x < 9; x ++) {
String lineToPrint = "";
for(int i = 0; i < hand.size(); i ++) {
lineToPrint+=(CardArt.makeCard(hand.get(i))[x] + " ");
}
System.out.println(lineToPrint);
}
}
public boolean insurance(Dealer badGuy) {
System.out.println("Dealer has an ace. Do you want insurance?");
System.out.println("Yes: Y");
System.out.println("No: N");
Scanner look = new Scanner(System.in);
if (look.nextLine().toLowerCase().equals("y")) {
System.out.println("How much do you want to insure. Remember, you can only insure half your main bet, but if the dealer has BlackJack you get paid back double.");
int insuranceBet = look.nextInt();
this.bet(insuranceBet);
System.out.println("You have insured for $" + insuranceBet + "");
if (badGuy.handValue() == 21) {
System.out.println("Congrats! The Dealer had Blackjack. You have been payed $" + (insuranceBet*2));
this.addBalance(insuranceBet*2);
return true;
} else {
System.out.println("Sorry, the Dealer did not have blackjack and your insurance bet has been collected");
return false;
}
}
return false;
}
public static void split() {
}
public static void doubleDown() {
}
// FOR TESTING PURPOSES
public void addCard(Card card) {
hand.add(card);
}
}