I am attempting to make a very basic Black Jack game I have it broke into three simple classes, but I am extremely new to Java and I am a terrible coder but I need to learn this for work. I am having some major difficulty understanding how I am suppose to call methods and classes. I figured out the basic structure of the game like how to create the cards and how to enter the game and exit the game. I just can't figure out the game play itself. This is what I have created so far. Please any advice and direction so I can understand this would be great I am desperate.
BlackJack.java
import java.util.*;
public class BlackJack4 {
public static void main(String[] args) {
// write your code here
Scanner keyboard = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
String playGame = "";
System.out.print("Wanna play some BlackJack? \n");
System.out.print("Type yes or no \n");
playGame = keyboard.nextLine();
if (playGame.equals ("yes"))
{
/*
This is the area I need help in figuring out.
*/
}
else if (playGame.equals("no")) //Player decided to no play the game
{
System.out.print("Thank you for playing with us today.\n");
System.out.print("To exit the game please press enter.");
scan.nextLine();
System.exit(0);
}
else
{
System.out.print("Sorry you did something wrong. Please try again.\n");
System.out.print("To exit the game please press enter.");
scan.nextLine();
System.exit(0);
}
}
}
Deck.java
import java.util.*;
public class Deck {
ArrayList<Cards> cards = new ArrayList<Cards>();
String[] Suits = { "Clubs", "Diamonds", "Hearts", "Spades"};
String[] Ranks = {null, "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
public Deck() {
int d = Suits.length * Ranks.length;
String[] deck = new String[d];
for (int i = 0; i < Ranks.length; i++) {
for (int j = 0; j < Suits.length; j++) {
deck[Suits.length * i + j] = Ranks[i] + " of " + Suits[j];
}
}
}
public void shuffle(){//shuffle the deck when its created
Collections.shuffle(this.cards);
}
}
Cards.java
public class Cards {
private String suit;
private String rank;
public Cards(){}
public Cards(String suit, String rank){
this.suit = suit;
this.rank = rank;
}
public String getSuit(){
return suit;
}
/* public void setSuit(String suit){
this.suit = suit;
}*/
public String getRank(){
return rank;
}
/*
public void setRank(String value){
this.rank = rank;
}*/
@Override
public String toString() {
return String.format("%s of %s", rank, suit);
}
}
This kind of task is not very easy level, there is a lot of things to do here, perhaps you have to start from some more basic stuff. But if you sure you want to get through, here some advice and code:
You have to add score
field to the Card class(single name is better than plural for class, as well as lower case first letter for variables, it's some code conventions). Not easy because Ace can have multivalue.
Use LinkedList instead of ArrayList as Deck.cards. It's more similar with real game when dealer polls a card each time.
put this.cards.add(cards);
intead of 'deck[Suits.length * i + j] = Ranks[i] + " of " + Suits[j];' you dont need this String array at all. And the most important part is the sceleton you can put to the place you pointed, where help is most expected:
private static void playGame(Scanner scan) {
Deck deck = new Deck();
String playGame;
Integer yourScore = 0;
Random random = new Random();
Boolean playerEnough = false;
Boolean dealerEnough = false;
Integer dealerScore = 0;
deck.shuffle();
while ((yourScore <= 21 && dealerScore <= 21) && (!dealerEnough || !playerEnough)) {
if (yourScore == 0) {
yourScore += getCardScore(deck, "Player");
yourScore += getCardScore(deck, "Player");
}
if (dealerScore == 0) {
dealerScore += getCardScore(deck, "Dealer");
dealerScore += getCardScore(deck, "Dealer");
}
if (!playerEnough) {
System.out.println("Want a card?");
playGame = scan.nextLine();
if (playGame.equals("yes")) {
yourScore += getCardScore(deck, "Player");
} else {
System.out.println("PlayerDone");
playerEnough = true;
}
}
if (!dealerEnough) {
if (random.nextBoolean()) {
dealerScore += getCardScore(deck, "Dealer");
} else {
System.out.println("DealerDone");
dealerEnough = true;
}
}
System.out.println(yourScore + " [p] : [d] " + dealerScore);
}
// decide who is a winner
}
private static Integer getCardScore(Deck deck, String who) {
System.out.print(who + " given: ");
Cards cards = deck.cards.pollFirst();
System.out.println(cards);
return cards.getScore();
}
this can help you to make a step further, or not, then I suggest you to solve some smaller exercises.