-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackJack.java
executable file
·132 lines (118 loc) · 2.66 KB
/
BlackJack.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
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
public class BlackJack
{
private Deck deck;
private Hand playerHand;
private Hand bankHand;
public boolean gameFinished = false;
public BlackJack()
{
this.deck = new Deck(4);
this.bankHand = new Hand();
this.playerHand = new Hand();
try
{
this.reset();
}
catch(EmptyDeckException ex)
{
ex = new EmptyDeckException("Error, the deck has insuffisent cards");
System.exit(-1);
}
}
public void reset() throws EmptyDeckException
{
try
{
this.bankHand.clear();
this.playerHand.clear();
this.gameFinished = false;
this.bankHand.add(this.deck.draw());
this.playerHand.add(this.deck.draw());
this.playerHand.add(this.deck.draw());
}
catch(EmptyDeckException ex)
{
System.err.println(ex.getMessage());
System.exit(-1);
}
}
public String getPlayerHandString()
{
return this.playerHand.toString();
}
public String getBankHandString()
{
return this.bankHand.toString();
}
public int getPlayerBest()
{
return this.playerHand.best();
}
public int getBankBest()
{
return this.bankHand.best();
}
public boolean isPlayerWinner()
{
if(this.isGameFinished() && this.getPlayerBest() <= 21 && (this.getPlayerBest() > this.getBankBest() || this.getBankBest() > 21))
return true;
return false;
}
public boolean isBankWinner()
{
if(this.isGameFinished() && this.getBankBest() <= 21 && (this.getBankBest() > this.getPlayerBest() || this.getPlayerBest() > 21))
return true;
return false;
}
public boolean isGameFinished()
{
if(gameFinished)
return true;
return false;
}
public void playerDrawAnotherCard() throws EmptyDeckException
{
try
{
if(!(this.isGameFinished()))
this.playerHand.add(this.deck.draw());
if(this.getPlayerBest() > 21)
gameFinished = true;
}
catch(EmptyDeckException ex)
{
System.err.println(ex.getMessage());
System.exit(-1);
}
}
public void bankLastTurn() throws EmptyDeckException
{
try
{
if(!(this.isGameFinished()) && this.getPlayerBest() <= 21 && this.getBankBest() <= 21)
while(this.getBankBest() < this.getPlayerBest())
this.bankHand.add(this.deck.draw());
gameFinished = true;
}
catch(EmptyDeckException ex)
{
System.err.println(ex.getMessage());
System.exit(-1);
}
}
public List<Card> getPlayerCardList()
{
List<Card> originalList = playerHand.getCardList();
LinkedList<Card> copyList = new LinkedList<Card>(originalList);
return copyList;
}
public List<Card> getBankCardList()
{
List<Card> originalList = bankHand.getCardList();
LinkedList<Card> copyList = new LinkedList<Card>(originalList);
return copyList;
}
}