patternjavaMinor
Object-oriented Blackjack game
Viewed 0 times
orientedobjectgameblackjack
Problem
I know some things are a bit over the top (such as the hashmap use, but it was needed to use some sort of collection class) - but are there anything else that sticks out as a sore thumb in my code? I'll add comments to all my code eventually.
I was also told to make it as object oriented as I could. Have I achieved it?
```
import java.util.Scanner;
public class Player extends Dealer {
static int playerTotal;
@SuppressWarnings("resource")
public void displayPerson() throws InterruptedException {
Thread.sleep(2000);
System.out.println("\nShuffling cards... Will deal in 2 seconds.");
Thread.sleep(1000);
System.out.println("\n-------------------------\n");
Thread.sleep(1000);
System.out.println("You drew the cards: ");
Thread.sleep(800);
Deck.getCardfromDeck("Player");
Thread.sleep(800);
System.out.println("\n & ");
Thread.sleep(800);
Deck.getCardfromDeck("Player");
Thread.sleep(1000);
System.out.println("\nYour total is: " + playerTotal + "!");
Thread.sleep(1000);
System.out.println("\nThe dealer drew the card");
Thread.sleep(500);
Deck.getCardfromDeck("Dealer");
Thread.sleep(800);
System.out.println("And a secret card he wont show yet");
Thread.sleep(1200);
System.out.println(" ______");
System.out.println("|? |");
System.out.println("| ? |");
System.out.println("| ? ? |");
System.out.println("| |");
System.out.println("|_____?|");
Thread.sleep(1000);
// asking them if they want to stay or hit again
System.out.println("Would you like to 'hit' or 'stay'?");
Scanner scan = new Scanner(System.in);
String hitorstay = scan.nextLine();
Thread.sleep(800);
// if they want to hit, do this
if (hitorstay.equalsIgnoreC
I was also told to make it as object oriented as I could. Have I achieved it?
Main Classpublic class Main {
public static void main (String[] args) throws InterruptedException{
Player deck = new Player();
deck.displayPerson();
}
}Player Class```
import java.util.Scanner;
public class Player extends Dealer {
static int playerTotal;
@SuppressWarnings("resource")
public void displayPerson() throws InterruptedException {
Thread.sleep(2000);
System.out.println("\nShuffling cards... Will deal in 2 seconds.");
Thread.sleep(1000);
System.out.println("\n-------------------------\n");
Thread.sleep(1000);
System.out.println("You drew the cards: ");
Thread.sleep(800);
Deck.getCardfromDeck("Player");
Thread.sleep(800);
System.out.println("\n & ");
Thread.sleep(800);
Deck.getCardfromDeck("Player");
Thread.sleep(1000);
System.out.println("\nYour total is: " + playerTotal + "!");
Thread.sleep(1000);
System.out.println("\nThe dealer drew the card");
Thread.sleep(500);
Deck.getCardfromDeck("Dealer");
Thread.sleep(800);
System.out.println("And a secret card he wont show yet");
Thread.sleep(1200);
System.out.println(" ______");
System.out.println("|? |");
System.out.println("| ? |");
System.out.println("| ? ? |");
System.out.println("| |");
System.out.println("|_____?|");
Thread.sleep(1000);
// asking them if they want to stay or hit again
System.out.println("Would you like to 'hit' or 'stay'?");
Scanner scan = new Scanner(System.in);
String hitorstay = scan.nextLine();
Thread.sleep(800);
// if they want to hit, do this
if (hitorstay.equalsIgnoreC
Solution
OOP
The code isn't really object oriented, but procedural.
First of all, you don't have any objects. You have classes, but you never create instances of them.
Also, all your methods are
Your class inheritance also doesn't make all that much sense; you can think of
Also, there really isn't such a thing as a
OOP - a better approach
When creating classes, try to think of the things like they exist in the real world. What do you have at a blackjack table? You have cards, you have hands, you have player, you have dealer, maybe you also have a table and a game. So you can create classes for all these things.
Then, think about what those things have and what they do, and create appropriate fields and methods. For example, a
A
Here is some code to give you an idea:
And a game might look like this (very abstract, just to give you an idea):
You might want to check out this post about OOP in Poker and some of the questions in Playing Cards, and also read about Object-Oriented Programming Concepts in general.
The code isn't really object oriented, but procedural.
First of all, you don't have any objects. You have classes, but you never create instances of them.
Also, all your methods are
static. Sometimes, static functions make sense, but if you have too many, it's a sign of bad design.Your class inheritance also doesn't make all that much sense; you can think of
extends as is-a to check if your inheritance is correct. For example, is a Deck a kind of Player? Or is a Player a kind of Dealer? They are not, so those extends shouldn't exist.Also, there really isn't such a thing as a
WhoWon, this shouldn't be a class, but a function inside for example a Game class.OOP - a better approach
When creating classes, try to think of the things like they exist in the real world. What do you have at a blackjack table? You have cards, you have hands, you have player, you have dealer, maybe you also have a table and a game. So you can create classes for all these things.
Then, think about what those things have and what they do, and create appropriate fields and methods. For example, a
Card has a suit and a value, so add those fields. A
Hand has Cards, it has a hand-value, and it can take more cards.Here is some code to give you an idea:
public class Card {
private Rank rank;
private Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
@Override
public Suit toString() {
// make asci art here
}
public enum Rank {
TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
SEVEN(7),
EIGHT(8),
NINE(9),
TEN(10),
JACK(11),
QUEEN(12),
KING(13),
ACE(14);
private int value;
private Rank(final int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
public enum Suit {
SPADES, HEARTS, DIAMONS, CLUBS
}
}
public class Deck {
List cards;
public Deck() {
cards = new ArrayList<>();
Card.Rank[] ranks = Card.Rank.values();
Card.Suit[] suits = Card.Suit.values();
for (Card.Rank rank : ranks) {
for (Card.Suit suit : suits) {
cards.add(new Card(rank, suit)); // create a new instance of the Card class
}
}
}
public void shuffle() {
// TODO shuffle
}
public List getCards(int amount) {
// return amount of cards
}
}And a game might look like this (very abstract, just to give you an idea):
public class Game {
private Dealer dealer; // dealer has a Deck
private List player; // player has a hand, which has a list of cards, and methods such as getTotalValue, etc
public void playOneRound() {
while (!quit) {
dealer.deal(player);
applyUserInput(getUserInput());
dealer.play();
checkWin(dealer, player);
}
outputResults(dealer, player);
}
}You might want to check out this post about OOP in Poker and some of the questions in Playing Cards, and also read about Object-Oriented Programming Concepts in general.
Code Snippets
public class Card {
private Rank rank;
private Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
@Override
public Suit toString() {
// make asci art here
}
public enum Rank {
TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
SEVEN(7),
EIGHT(8),
NINE(9),
TEN(10),
JACK(11),
QUEEN(12),
KING(13),
ACE(14);
private int value;
private Rank(final int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
public enum Suit {
SPADES, HEARTS, DIAMONS, CLUBS
}
}
public class Deck {
List<Card> cards;
public Deck() {
cards = new ArrayList<>();
Card.Rank[] ranks = Card.Rank.values();
Card.Suit[] suits = Card.Suit.values();
for (Card.Rank rank : ranks) {
for (Card.Suit suit : suits) {
cards.add(new Card(rank, suit)); // create a new instance of the Card class
}
}
}
public void shuffle() {
// TODO shuffle
}
public List<Card> getCards(int amount) {
// return amount of cards
}
}public class Game {
private Dealer dealer; // dealer has a Deck
private List<Player> player; // player has a hand, which has a list of cards, and methods such as getTotalValue, etc
public void playOneRound() {
while (!quit) {
dealer.deal(player);
applyUserInput(getUserInput());
dealer.play();
checkWin(dealer, player);
}
outputResults(dealer, player);
}
}Context
StackExchange Code Review Q#66765, answer score: 7
Revisions (0)
No revisions yet.