-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.java
More file actions
108 lines (106 loc) · 2.23 KB
/
GameEngine.java
File metadata and controls
108 lines (106 loc) · 2.23 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
public class GameEngine{
Deck deck;
Hand dealer, hand1;
int pot, wager;
int PRIZE_PAYOUT = 2;
public GameEngine(){
pot = 50;
wager = 0;
deck = new Deck();
deck.shuffle();
}
/*
total = 0
for (Card card : hand.showHand()) {
total = total + card.getValue()
}
*/
private int calHand(Hand hand){
int total = 0, ace = 0;
for(Card card : hand.showHand()){
if(card.getValue().equals("2")) total += 2;
else if(card.getValue().equals("3")) total += 3;
else if(card.getValue().equals("4")) total += 4;
else if(card.getValue().equals("5")) total += 5;
else if(card.getValue().equals("6")) total += 6;
else if(card.getValue().equals("7")) total += 7;
else if(card.getValue().equals("8")) total += 8;
else if(card.getValue().equals("9")) total += 9;
else if(card.getValue().equals("A")){
total += 1;
ace ++;
}
else total += 10;
}
while(ace > 0){
if((total + 10) <= 21){
total += 10;
}
ace --;
}
return total;
}
public void dealerPlays(){
while(calHand(dealer) < 17){
dealer.addCard(deck.deal());
}
}
public void startGame(){
if(deck.deckSize() < (4*3)){
deck = new Deck();
deck.shuffle();
}
hand1 = new Hand();
dealer = new Hand();
//NUMBER_OF_CARDS_TO_DEAL_INITIALLY = 2
//for (i=0; i<NUM...; i++) {
hand1.addCard(deck.deal());
dealer.addCard(deck.deal());
hand1.addCard(deck.deal());
dealer.addCard(deck.deal());
}
public void playerDeal(){
hand1.addCard(deck.deal());
}
public int calPlayerHand(){
return calHand(hand1);
}
public int calDealerHand(){
return calHand(dealer);
}
public Card[] getPlayerHand(){
return hand1.showHand();
}
public Card[] getDealerHand(){
return dealer.showHand();
}
public boolean isPlayerWin(){
if(calHand(hand1) <= 21 && calHand(dealer) <= 21){
if(calHand(hand1) > calHand(dealer)){
return true;
} else {
return false;
}
} else if (calHand(hand1) <= 21 && calHand(dealer) > 21){
return true;
} else {
return false;
}
}
public boolean setWager(int bet){
wager = bet;
if(bet > pot){
return false;
} else {
pot -= bet;
return true;
}
}
public int getPot(){
return pot;
}
public boolean payoutPlayer(){
pot = pot + (PRIZE_PAYOUT * wager);
return true;
}
}