HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Blackjack on console

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
consoleblackjackstackoverflow

Problem

I have decided to create a console Blackjack game as my final Java project. Please take a look at my code and give me some feedback.

I have tried to make the game as object-oriented as possible. I have five different classes.

Player class:

public class Person extends Dealer{

Random ran = new Random ();
Scanner scan = new Scanner (System.in);
public static int personcard, persontotal;
static ArrayList  name = new ArrayList  ();

public void shufflePerson () throws InterruptedException {

    name.add("Bert");

    personcard = ran.nextInt(14);

    while (personcard  21) {
        System.out.println(name +" busted.");
        playAgain p = new playAgain ();
        p.again();
    }
    else {

    System.out.println("Hit or stay?");

    String hitorstay = scan.nextLine();

    if (hitorstay.equals("hit")) {
        shufflePerson();
    }
    else {
        System.out.println("Okay, dealers turn.");
        Dealer d = new Dealer ();
        d.shuffleDealer();
    }
    }
}


Dealer class:

public class Dealer {
Random ran = new Random ();
public static int dealercard, dealertotal;

public void shuffleDealer () throws InterruptedException {

    dealercard = ran.nextInt(14);

    while (dealercard  21) {
        System.out.println("Dealer busted.");
        playAgain p = new playAgain ();
        p.again();
    }

    else if (dealertotal < 15) { 
        System.out.println("Dealer chooses to hit.");
        shuffleDealer ();
    }
    else {
        System.out.println("Dealer chooses to stay.");
        displayWinner w = new displayWinner ();
        w.showWinner();
    }
}


displayWinner class:

```
public class displayWinner extends Person {

public void showWinner () throws InterruptedException {

if (persontotal > dealertotal) {
System.out.println(name +" won!");
playAgain p = new playAgain ();
p.again();
}
else if (dealertotal > persontotal){
System.out.println("Dealer won!");
playAgai

Solution

-
Classes in Java should start with a capital letter, ex: PlayAgain rather than playAgain.

-
If the class name is a verb like play again this means that this shouldn't be a class. Class names should usually be nouns like Person. If you feel that your class name should be a verb then it is a probably a function and not a class, ex:

void playAgain(){
}


-
Your inheritance hierarchy does not make sense. If A extends B, this means A is B all the time, and this condition does not hold in you hierarchy. Is every Person a Dealer? I don't think it is. It is actually the other way around; every Dealer is a Person, inheritance meaning IS. And if you want to avoid the hassle of inheritance, use composition instead. It is usually a safer option

-
Always try to restrict access to your fields by declaring them private rather than package access. It's Java's fault that everything is not private by default.

-
Scanner is a field of the Person class, which doesn't make sense. Think of fields as properties: what does this person have? For you to qualify as a person you shouldn't need a Scanner. Maybe you need a name or a date of birth, but not a Scanner.

-
When you need string equality with a constant, start with the constant. For example, use:

if ("y".equals(yn)) {
////
}


instead of

if (yn.equals("y")) {
///
}


because the second version will throw a NullPointerException if yn is null, whereas the first version gives you null checking for free.

Code Snippets

void playAgain(){
}
if ("y".equals(yn)) {
////
}
if (yn.equals("y")) {
///
}

Context

StackExchange Code Review Q#66528, answer score: 13

Revisions (0)

No revisions yet.