Hello, I am rather new to this so bare with me!
I am currenlty creating a BlackJack game in Java and attempting to use the MVC design when creating it. I have so far created the following classes:
- Card: To hold create a card
- Deck: To create a deck filled with all 52 cards
- Hand: To hold a 'Hand' of Cards
- Player: To create a Player for game use
- BlackJackTest: Here I am testing all the functions of the game trying to get something working!
I have currently got to the point where I can show all 52 cards, and also deal 2 cards to a Hand and show this Hand. But I'm having a lot of trouble with the next steps. Ideally within the "BlackJackTest" class I would want to use this as a Game Engine where I would enter X amount of players and their names, and it would create the players, and loop through the system to play the game.
My problem is that I am completely stuck on what I should be doing next, or if I have done something wrong. My next task should be to implement a feature so that I can put up to 3 human players against a dealer, but am unsure of how to do this....and I should also implement hit/stand features which I'm not sure where or how I should be coding these.
Any help is appreciated!
Here is my code:
import java.util.Scanner;
public class BlackJackTest {
public static void main(String[] args) {
/*System.out.println(deck.getCard(0).toString());
System.out.println(deck.getCard(0).getSuit());
System.out.println(deck.getCard(0).getSuitCode());
System.out.println(deck.getCard(0).getValue());
System.out.println(deck.getCard(0).getValueCode());
System.out.println();
System.out.println(deck.cardsLeft());
hand.addCard( deck.dealCard() );
hand.addCard( deck.dealCard() );
hand.showHand();
System.out.println(deck.cardsLeft());
System.out.println(hand.getHandSize());
System.out.println(hand.getCard(0));
System.out.println(hand.getCard(1));
System.out.println(hand.getBlackJackValue());
System.out.println();
*/
Deck deck = new Deck();
Hand p1hand = new Hand();
Player player = new Player();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
player.setName(sc.nextLine());
System.out.println("Welcome " + player.getName() + "!");
player.setHand(p1hand);
p1hand.addCard( deck.dealCard() );
p1hand.addCard( deck.dealCard() );
System.out.println(player.getName() + ": ");
p1hand.showHand();
System.out.println("Score: " + p1hand.getBlackJackValue());
System.out.println();
}
}
************
public class Card {
public enum Value {
public enum Value {
TWO(2),
THREE(3),
FOU TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
SEVEN(7),
EIGHT(8),
NINE(9),
TEN(10),
JACK(10),
QUEEN(10),
KING(10),
ACE(1);
public final int value;
Value(int c) {
value = c;
}
}
public enum Suit {
CLUBS('C'),
SPADES('S'),
DIAMONDS('D'),
HEARTS('H');
public final char symbol;
Suit(char c) {
symbol = c;
}
}
private Suit suit;
private Value value;
public Card(Suit suit, Value value) {
this.suit = suit;
this.value = value;
}
public Suit getSuit() {
return suit;
}
public Value getValue() {
return value;
}
public char getSuitCode() {
return suit.symbol;
}
public int getValueCode() {
return value.value;
}
public String toString() {
return value + " of " + suit;
}
}
**************
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Deck {
private List<Card> deck = new LinkedList<Card>();
public Deck() {
for(Card.Suit suit : Card.Suit.values()) {
for(Card.Value value : Card.Value.values()) {
deck.add(new Card(suit,value));
}
}
shuffle();
}
public void shuffle() {
Collections.shuffle(deck);
}
public int cardsLeft() {
// As cards are dealt from the deck, the number of cards left
// decreases. This function returns the number of cards that
// are still left in the deck.
return deck.size();
}
public Card dealCard() {
// Deals one card from the deck and returns it.
if (deck.size() == 52) {
shuffle();
}
Card temp;
temp = deck.get(0);
remove(0);
return temp;
}
public Card getCard(int i) {
return deck.get(i);
}
public Card remove(int i) {
Card c = deck.get(i);
deck.remove(i);
return c;
}
}
*************
import java.util.Vector;
public class Hand {
private Vector<Card> hand;
public Hand() {
hand = new Vector<Card>();
}
public void addCard(Card c) {
if (c != null)
hand.addElement(c);
}
public void showHand() {
for(int i = 0; i < hand.size(); i++){
System.out.println(hand.elementAt(i));
}
}
public int getHandSize() {
return hand.size();
}
public Card getCard(int position) {
if(position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}
public int getBlackJackValue() {
int val = 0;
boolean ace = false;
for(int i = 0; i < hand.size(); i++) {
Card card;
card = getCard(i);
val += card.getValueCode();
if(card.getValueCode() == 1) {
ace = true;
}
}
if(ace == true && val + 10 <= 21) {
val += 10;
}
return val;
}
}
***************
public class Player {
private String name;
private Hand hand;
public Player() {
this.hand = null;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setHand(Hand h) {
this.hand = h;
}
public void getHand(){
hand.showHand();
}
}