-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPlayer.java
More file actions
142 lines (134 loc) · 4.33 KB
/
Player.java
File metadata and controls
142 lines (134 loc) · 4.33 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
import java.util.InputMismatchException;
import java.util.Scanner;
class Player {
private Hand playerHand = new Hand();
private static int index;
private int value;
Scanner sc = new Scanner(System.in);
/**
* @para 1 int fine
* Prints card in player's hand
*/
public Player (int fine) {
this.value = fine;
}
/**
* method to recognize cards in player's hand
* evaluate the value of the cards in player's hand
*/
public void initDeal() {
playerHand.initialDeal();
playerHand.printHand();
playerHand.evaluateHand();
}
/**
* returns the integer value of the cards in player's hand
*/
public int getHandValue () {
return playerHand.evaluateHand();
}
/**
* para@1 dealer's hand value
* compare the values of cards in player's and dealer's hand
*/
public int compareHands(int dealerHandValue,int get) {
int playerHandValue = getHandValue();
if (playerHandValue > dealerHandValue && playerHandValue <= 21) {
System.out.println ("*** Player Wins");
return get;
} else if ( playerHandValue < dealerHandValue && dealerHandValue <= 21) {
System.out.println ("*** Dealer Wins");
return -get;
} else if (playerHandValue == dealerHandValue) {
System.out.println ("*** That's a Tie");
return 0;
}
return 0;
}
public boolean playAgain () {
System.out.print ("Would you like to play? [y/n]: ");
String option = sc.next();
boolean out = (option.equals("y"));
if (option.equals("n")) {
return false;
} else {
return true;
}
}
public boolean hasPurse(int purse) {
if (purse > 0) {
return true;
} else {
return false;
}
}
public int bet(int purse) {
int ok = 0,bet=0;
System.out.print("Purse = ");
System.out.println(purse);
System.out.print ("How much would you like to bet? ");
boolean continueInput=true;
do {
try{
bet = sc.nextInt();
continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Enter a valid ammount");
System.out.print("Purse = ");
System.out.println(purse);
System.out.print ("How much would you like to bet? ");
sc.nextLine();
}
}
while (continueInput);
while(bet>purse){
System.out.println ("You don't have enough purse ");
System.out.print("Purse = ");
System.out.println(purse);
System.out.print ("How much would you like to bet? ");
continueInput=true;
do {
try{
bet = sc.nextInt();
continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Enter a valid ammount");
System.out.print("Purse = ");
System.out.println(purse);
System.out.print ("How much would you like to bet? ");
sc.nextLine();
}
}while (continueInput);
}
ok = bet;
return ok;
}
public boolean playHand() {
System.out.print ("Would you like to draw another card? ");
while(sc.next().equals("y")) {
int handCount = 0;
for (int i = 0; i < playerHand.hand.length; i++) {
if (playerHand.hand[i] != null) {
handCount++;
}
}
playerHand.hand[handCount] = playerHand.deal();
System.out.println ("****************");
System.out.println ("Player's Hand");
System.out.println ("****************");
playerHand.printHand();
System.out.println ("Player's Hand Value = " + getHandValue());
if (getHandValue() > 21) {
System.out.println ("The player has busted.");
return true;
}
System.out.print ("Would you like to draw another card? ");
}
return false;
}
public void printPlayerHand() {
playerHand.printHand();
}
}