-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoker.java
More file actions
134 lines (114 loc) · 4.18 KB
/
Poker.java
File metadata and controls
134 lines (114 loc) · 4.18 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
import java.util.Scanner;
/**
* A 2-card poker game played between a human and a computer player
*
* @author paw: Phil White
*/
public class Poker {
/**
* a boolean toggle that tells the order of making the stand/fold
* decision. This flips after each hand
*/
private static boolean playerGoesFirst = true;
/**
* Plays a single hand of poker
*
* @param person the human player
* @param comp the computer player
* @param deck the deck
* @return an int telling if the user lost/tied/won (neg/0/pos)
*/
public static int playHand(Human person, Computer comp, Deck deck) {
boolean personStand = true, dealerStand = true;
int playerWon;
System.out.println("== Dealing Cards\n");
//give initial cards
for (int j = 0; j < 2; j++) {
person.addCard(deck.dealCard());
comp.addCard(deck.dealCard());
}
System.out.println("============== Your Cards ========");
person.printHand();
// ask both players if they want to stand
if (playerGoesFirst) {
personStand = person.stand();
}
if (personStand) {
dealerStand = comp.stand();
if (dealerStand) {
System.out.println("Computer Stands");
} else {
System.out.println("Computer Folds");
}
}
if (!playerGoesFirst && dealerStand) {
personStand = person.stand();
}
System.out.println("============== House Cards ========");
comp.printHand();
playerGoesFirst = !playerGoesFirst;
if (personStand && dealerStand) {
//check player win vs. computer
playerWon = person.compareTo(comp);
} else if (personStand) {
playerWon = 1; // player won by default
} else {
playerWon = -1; // House won by default
}
// have everyone throw in their cards
person.newHand();
comp.newHand();
return playerWon;
}
/**
* Main method -- plays multiple hands of poker, after each hand
* ask the user if they want to play again. We also keep trak of
* the number of games played/won by the user and print the results
* at the end.
*
* @param args command line arguments
*/
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String again;
char c;
int playerWon;
int numGames = 0;
int numWon = 0;
int numTie = 0;
Deck theDeck = new Deck();
Computer theComputer = new Computer();
Human theHuman = new Human(in);
do {
numGames = numGames + 1;
System.out.println();
System.out.println("##########################################");
System.out.println("########## NEW HAND ###########");
System.out.println("##########################################");
System.out.println("\n== Shuffling");
theDeck.shuffle();
playerWon = playHand(theHuman, theComputer, theDeck);
if (playerWon > 0) {
System.out.println("\n **** Human Won ****\n");
numWon = numWon + 1;
} else if (playerWon == 0) {
System.out.println(" Tie Game");
numTie = numTie + 1;
} else {
System.out.println("\n ---- House Won ----\n");
}
do {
System.out.print("Do you wish to play " +
"another hand (y/n):");
again = in.nextLine();
again = again.toLowerCase();
c = again.charAt(0);
} while (c != 'y' && c != 'n');
} while (c == 'y');
System.out.println("========== Results ==========");
System.out.println("Games:\t" + numGames);
System.out.println("Wins:\t" + numWon);
System.out.println("Ties:\t" + numTie);
in.close(); // <3 Jim
}
}