Skip to content

Instantly share code, notes, and snippets.

Created May 11, 2015 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f07f46acb59d79c3b463 to your computer and use it in GitHub Desktop.
Save anonymous/f07f46acb59d79c3b463 to your computer and use it in GitHub Desktop.
Black Jack
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JLabel;
public class BlackJackgui {
private JFrame frame;
private Deck myDeck;
/**
* @author Haley Jensen
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BlackJackgui window = new BlackJackgui();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public BlackJackgui() {
initialize(); }
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea Phand = new JTextArea();
Phand.setBounds(20, 101, 234, 46);
frame.getContentPane().add(Phand);
JTextArea Pvalue = new JTextArea();
Pvalue.setBounds(21, 194, 203, 38);
frame.getContentPane().add(Pvalue);
JButton btnDealANew = new JButton("Deal a New Hand");
btnDealANew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myDeck = new Deck();
myDeck.shuffle();
Phand.setText(myDeck.toString()); }
// choose top two
// create hand
// put two cards in hand
// add and display value
}
);
btnDealANew.setBounds(145, 20, 164, 29);
frame.getContentPane().add(btnDealANew);
JButton btnHit = new JButton("Hit");
btnHit.setBounds(204, 244, 117, 29);
frame.getContentPane().add(btnHit);
// action for "hit" & "stay" in same way as "deal" action
JButton btnStay = new JButton("Stay");
btnStay.setBounds(333, 244, 117, 29);
frame.getContentPane().add(btnStay);
JLabel lblPlayersHand = new JLabel("Player's Hand");
lblPlayersHand.setBounds(20, 65, 130, 16);
frame.getContentPane().add(lblPlayersHand);
JLabel lblPlayersHandValue = new JLabel("Player's Hand Value");
lblPlayersHandValue.setBounds(20, 166, 200, 16);
frame.getContentPane().add(lblPlayersHandValue);
}
}
public class Card {
private String suit;
private String rank;
public Card(String s, String r) {
this.suit = s;
this.rank = r;
}
/**
* This method should return the BlackJack value of this card.
* @return int value
*/
public int getBlackJackValue() {
if (this.rank.equals("A")) {
return 11;
} else if (this.rank.equals("J") || this.rank.equals("Q") || this.rank.equals("K")) {
return 10;
} else {
return Integer.parseInt(this.rank);
}
}
public String toString() {
return "[" + this.suit + this.rank + "]";
}
}
import java.util.ArrayList;
import java.util.Random;
public class Deck {
public ArrayList<Card> cards = new ArrayList<Card>();
public Deck() {
final String HEART = "♡";
final String SPADE = "♤";
final String CLUB = "♧";
final String DIAMOND = "♢";
addCards(HEART);
addCards(SPADE);
addCards(CLUB);
addCards(DIAMOND);
}
private void addCards(String suit) {
this.cards.add(new Card(suit, "A"));
this.cards.add(new Card(suit, "J"));
this.cards.add(new Card(suit, "Q"));
this.cards.add(new Card(suit, "K"));
for (int i = 2; i <= 10; i++) {
this.cards.add(new Card(suit, Integer.toString(i)));
}
}
public Card GetTop() {
Card first=cards.remove(0);
return first;
// in blackjackGUI call this function // MyDeck.GetTop <-- twice //card first=myDeck.
// then call hand, add card to hand
// Card first=MyDeck.GetTop();
// arrays start at 0
// get the first card from the arrayList
// remove card from the deck
// return card [as value]
}
public String toString() {
String deck = "";
for (Card c : cards) {
deck = deck + c.toString();
}
return deck;
}
public void shuffle() {
Random r = new Random();
for (int i = cards.size() - 1; i > 0; i--) {
int j = r.nextInt(i);
Card x = cards.get(i);
Card y = cards.get(j);
cards.set(i, y);
cards.set(j, x);
}
}
}
import java.util.ArrayList;
public class Hand {
public ArrayList<Card> cards = new ArrayList<Card>();
// data structure (of objects)
public Hand() {
}
public void take(Card card) {
this.cards.add(card);
// . add {adds up hand values}
// first word = type of parameter( Card.java) , 2nd word = name
}
public String toString() {
String hand = "";
for (Card c : cards) {
hand = hand + c.toString();
// call this function to display hand to user
}
return hand;
}
}
// TotalValue - function return: int
// TotalValue : loop through all values in the hand and add up all the values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment