Repository navigation
Expand file tree
/
Copy pathGame.java
More file actions
116 lines (98 loc) · 3.79 KB
/
Copy pathGame.java
File metadata and controls
116 lines (98 loc) · 3.79 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
import java.util.Scanner;
public class Game {
private Hand dealersHand = new Hand(); //creates a hand for the dealer
private Hand playersHand = new Hand(); //creats a hand for the player
private String tempStop;
private Scanner scanner = new Scanner(System.in); //Makes a scanner
public Game(){
}
//Show's the current Hands
public void showAllHands(){
System.out.print("Dealers Hand: ");
dealersHand.showHand();
System.out.println("---------------------------");
System.out.print("Your Hand: ");
playersHand.showHand();
}
//Checks if hand is bust
public boolean checkBust(Hand hand){
if(hand.value() > 21){
return true;
}
return false;
}
//plays a game
public void playGame(){
//Getting the game ready
boolean stand = false;
Deck playCards = new Deck(); //Makes a new deck called playcards
playCards.fillDeck(); //Fills it with new cards
playCards.shuffleDeck(); //Shuffle's the deck
int userInput; //what the user will say
//Tells the user the game is about to start
System.out.println("You are about to start a game of Black Jack \nPress ENTER To Continue:");
tempStop = scanner.nextLine();
System.out.println();
dealersHand.giveCard(playCards.drawCard()); //gives a card to the dealer
playersHand.giveCard(playCards.drawCard());//gives a card to the player (12, 24)
playersHand.giveCard(playCards.drawCard());
showAllHands(); //shows both hands
System.out.println(); //adds a space line
//prompts the player to hit ot stand until they bust or stand
while(!checkBust(playersHand) && !stand){
System.out.print("Hit(1) or Stand(2): ");
userInput = scanner.nextInt();
if(userInput != 1 && userInput != 2){
System.out.println(userInput + " Is not a valid answer please try again \n(make sure to use the numbers)");
}
if(userInput == 2){
stand = true;
}
else if(userInput == 1){
playersHand.giveCard(playCards.drawCard());
}
System.out.println();
showAllHands(); //shows both hands
System.out.println(); //adds a space line
}
if(checkBust(playersHand)){
System.out.println("Oh no! Your hand busted! You lost...");
playAgain();
}
else{
while(dealersHand.value() < playersHand.value() && !checkBust(dealersHand)){
dealersHand.giveCard(playCards.drawCard());
showAllHands();
System.out.println("Press ENTER To Continue:");
tempStop = scanner.nextLine();
System.out.println();
}
if(checkBust(dealersHand)){
System.out.println("You win! the dealer busted congrats");
playAgain();
}
else if(dealersHand.value() > playersHand.value()){
System.out.println("Oh no! The Dealer has more points. You lost...");
playAgain();
}
else if(dealersHand.value() == playersHand.value()){
System.out.println("You tied");
playAgain();
}
}
}
//starts the game again with empty hands
public void playAgain(){
System.out.print("Would you like to play again? (yes): ");
scanner.nextLine();
tempStop = scanner.nextLine();
if(tempStop.equals("yes")){
playersHand.emptyHand();
dealersHand.emptyHand();
playGame();
}
else{
System.out.println("Ok bye have a good day!");
}
}
}