patternjavaModerate
First attempt at a Blackjack game
Viewed 0 times
blackjackfirstattemptgame
Problem
I am not happy with this code as I am sure there are better ways to do what I'm trying to achieve. I'm a beginner and I've used what I know to date to complete this.
```
import java.util.Random;
import java.util.Scanner;
public class SeventySix {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//52 Cards, Aces = 11, Picture cards = 10, Ace's cannot be reduced to 1.
int[] newCard = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11};
//Shuffle. Once per game.
shuffleArray(newCard);
//Start BlackJack.
System.out.println("Welcome to BlackJack!");
System.out.println();
System.out.println("You get a " + newCard[0] + " and a " + newCard[1] + ".");
int playerTotal = newCard[0] + newCard[1];
System.out.println("Your total is " + playerTotal + ".");
System.out.println();
//Player can get blackjack/bust in the 1st deal. - awaiting betting system (enhanced bets for blackjack in first round)
if (playerTotal == 21){
System.out.println("Blackjack, you win.");
System.exit(0);
}
if (playerTotal > 21){
System.out.println("Bust, You lose.");
System.exit(0);
}
// Dealer cards
System.out.println("The dealer has a " + newCard[2] + " showing, and a hidden card.");
int dealerTotal = newCard[2] + newCard[3];
if (dealerTotal > 21){ //Dealer bust check.
System.out.println();
System.out.println("Dealers total is " + dealerTotal + ".");
System.out.println("Dealer is bust, you win!");
System.exit(0);
}
if (dealerTotal == 21){ //Dealer blackjack check.
System.out.println();
System.out.println("Dealer reveals his second card: " + newCard[3] + ".");
System.out.
```
import java.util.Random;
import java.util.Scanner;
public class SeventySix {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//52 Cards, Aces = 11, Picture cards = 10, Ace's cannot be reduced to 1.
int[] newCard = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11};
//Shuffle. Once per game.
shuffleArray(newCard);
//Start BlackJack.
System.out.println("Welcome to BlackJack!");
System.out.println();
System.out.println("You get a " + newCard[0] + " and a " + newCard[1] + ".");
int playerTotal = newCard[0] + newCard[1];
System.out.println("Your total is " + playerTotal + ".");
System.out.println();
//Player can get blackjack/bust in the 1st deal. - awaiting betting system (enhanced bets for blackjack in first round)
if (playerTotal == 21){
System.out.println("Blackjack, you win.");
System.exit(0);
}
if (playerTotal > 21){
System.out.println("Bust, You lose.");
System.exit(0);
}
// Dealer cards
System.out.println("The dealer has a " + newCard[2] + " showing, and a hidden card.");
int dealerTotal = newCard[2] + newCard[3];
if (dealerTotal > 21){ //Dealer bust check.
System.out.println();
System.out.println("Dealers total is " + dealerTotal + ".");
System.out.println("Dealer is bust, you win!");
System.exit(0);
}
if (dealerTotal == 21){ //Dealer blackjack check.
System.out.println();
System.out.println("Dealer reveals his second card: " + newCard[3] + ".");
System.out.
Solution
You should make your entire application more Object Oriented.
A good place to start would be to move the deck of cards and associated functions into other classes.
Here's an implementation of a deck stolen from a StackOverflow Question. Notice there's also a
Also think about using
You have a lot of multiline print statements that could probably be joined together;
Could be;
To save on complexity, these should be
Finally, your code is not very DRY. You need more of your functionality in separate methods to prevent you repeating yourself. A good example of this is that you have multiple win sequences. Your win/lose conditions should result in a win/lose method being called which executes the appropriate sequence.
Also Blackjack is a repetitious game, turn two is identical to turn one, your code just repeats the same operations a second time. Again this should be moved into methods.
A good place to start would be to move the deck of cards and associated functions into other classes.
Here's an implementation of a deck stolen from a StackOverflow Question. Notice there's also a
Card object.public class DeckOfCards {
private Card cards[];
public DeckOfCards() {
this.cards = new Card[52];
for (int i = 0; i < 52; i++) {
Card card = new Card(...); //Instantiate a Card
this.cards[i] = card; //Adding card to the Deck
}
}
}Also think about using
enums for suit and a method of tracking the card value. Searching Google for Cark rank patterns will help with this.You have a lot of multiline print statements that could probably be joined together;
System.out.println();
System.out.println("Dealer total is " + dealerTotal);
System.out.println("Your total is " + playerTotal);
System.out.println();Could be;
System.out.println(
String.format("\nDealer total is %s \nYour total is %s \n", dealerTotal, playerTotal)
);To save on complexity, these should be
else ifs instead;if (dealerTotal > playerTotal){
System.out.println("Dealer wins.");
}
else if (dealerTotal == playerTotal){
System.out.println("You both draw.");
}
else {
System.out.println("You win.");
}Finally, your code is not very DRY. You need more of your functionality in separate methods to prevent you repeating yourself. A good example of this is that you have multiple win sequences. Your win/lose conditions should result in a win/lose method being called which executes the appropriate sequence.
Also Blackjack is a repetitious game, turn two is identical to turn one, your code just repeats the same operations a second time. Again this should be moved into methods.
Code Snippets
public class DeckOfCards {
private Card cards[];
public DeckOfCards() {
this.cards = new Card[52];
for (int i = 0; i < 52; i++) {
Card card = new Card(...); //Instantiate a Card
this.cards[i] = card; //Adding card to the Deck
}
}
}System.out.println();
System.out.println("Dealer total is " + dealerTotal);
System.out.println("Your total is " + playerTotal);
System.out.println();System.out.println(
String.format("\nDealer total is %s \nYour total is %s \n", dealerTotal, playerTotal)
);if (dealerTotal > playerTotal){
System.out.println("Dealer wins.");
}
else if (dealerTotal == playerTotal){
System.out.println("You both draw.");
}
else {
System.out.println("You win.");
}Context
StackExchange Code Review Q#46439, answer score: 15
Revisions (0)
No revisions yet.