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?
Main
Class
public 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.equalsIgnoreCase("hit")) {
System.out.println("You drew an ");
Deck.getCardfromDeck("Player");
System.out.println("\n Your total is now " + playerTotal);
Thread.sleep(800);
if (playerTotal > 21) {
Thread.sleep(800);
System.out
.println("You busted, total is over 21, you lost, and you got "
+ playerTotal);
System.exit(1);
}
Thread.sleep(800);
System.out.println("Would you like to 'hit' or 'stay?'");
hitorstay = scan.nextLine();
}
else if (hitorstay.equals("stay")) {
System.out.println("Okay, dealers turn");
Thread.sleep(800);
Dealer.displayDealer();
}
else{
Scanner errorScan = new Scanner(System.in);
System.out.println("You typed wrong. Would you like to play again? Y/N");
String playagain = errorScan.nextLine();
if(playagain.equalsIgnoreCase("Y")){
System.out.println("Would you like to 'hit' or 'stay'?");
hitorstay = scan.nextLine();}
else
System.out.println("Good bye. Have a nice day!");
System.exit(0);
}
}
}
Dealer
Class
public class Dealer {
static int dealerTotal;
public static void displayDealer() throws InterruptedException {
System.out.println("The secretcard that the dealer had was: ");
Deck.getCardfromDeck("Dealer");
System.out.println("\nTheir total is now: " + dealerTotal);
Thread.sleep(800);
// The dealer should hit when his score is <17
while (dealerTotal < 17) {
Thread.sleep(800);
System.out.println("The dealer gets a new card: ");
Deck.getCardfromDeck("Dealer");
System.out.println("\n Their total is now " + dealerTotal);
}
if (dealerTotal > 21) {
Thread.sleep(800);
System.out
.println("They busted, total is over 21, and they lost, you won! Their total was "
+ dealerTotal);
System.exit(1);
}
else if (dealerTotal >= 17) {
Thread.sleep(800);
System.out.println("Okay, dealer stopped at " + dealerTotal);
WhoWon.displayWhoWon();
}
}
}
WhoWon Class
public class WhoWon extends Player{
public static void displayWhoWon(){
if(playerTotal > dealerTotal){
System.out.println("The Player won! The players score was " + playerTotal + ", and the dealers score was " + dealerTotal);
}
else if(playerTotal < dealerTotal){
System.out.println("The Dealer won! The players score was " + playerTotal + ", and the dealers score was " + dealerTotal);
}
}
}
Deck
Class
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Deck extends Player {
//Get the card from the Deck
public static void getCardfromDeck(String name) {
newValor(name);
}
//Parameter 'name' for whom the card should go to.
public static void newValor(String name) {
Random rng = new Random();
int currentcard = rng.nextInt(13);
currentcard++;
Map<Integer, String> nameMap = new HashMap<Integer, String>();
nameMap.put(1, "A");
nameMap.put(10, "T");
nameMap.put(11, "J");
nameMap.put(12, "Q");
nameMap.put(13, "K");
String getValor;
if (currentcard >= 10 || currentcard == 1) {
getValor = nameMap.get(currentcard);
if (name.equals("Player")){
playerTotal = playerTotal + 10;}
else if (name.equals("Dealer")){
dealerTotal = dealerTotal + 10;
}
System.out.println(" ______");
System.out.println("|"+getValor+" |");
newSuit();
System.out.println("|_____"+getValor+"|");
}
else{
if(name.equals("Player"))
playerTotal = currentcard + playerTotal;
else if(name.equals("Dealer"))
dealerTotal = currentcard + dealerTotal;
System.out.println(" ______");
System.out.println("|"+currentcard+" |");
newSuit();
System.out.println("|_____"+currentcard+"|");
}
}
public static void newSuit() {
Random rng = new Random();
int generateSuit = rng.nextInt(4);
generateSuit++;
Map<Integer, String> suitMap = new HashMap<Integer, String>();
suitMap.put(1, "Spades");
suitMap.put(2, "Diamonds");
suitMap.put(3, "Clubs");
suitMap.put(4, "Heart");
if (generateSuit == 1) {
System.out.println("| /\\ |");
System.out.println("| (__) |");
System.out.println("| /\\ |");
}
else if (generateSuit == 2) {
System.out.println("| /\\ |");
System.out.println("| \\/ |");
System.out.println("| |");
} else if (generateSuit == 3) {
System.out.println("| & |");
System.out.println("| &|& |");
System.out.println("| | |");
} else if (generateSuit == 4) {
System.out.println("| (\\/) |");
System.out.println("| \\/ |");
System.out.println("| |");
}
}
}