import edu.neu.ccs.*; import edu.neu.ccs.gui.*; import edu.neu.ccs.codec.*; import edu.neu.ccs.console.*; import edu.neu.ccs.filter.*; import edu.neu.ccs.jpf.*; import edu.neu.ccs.parser.*; import edu.neu.ccs.pedagogy.*; import edu.neu.ccs.quick.*; import edu.neu.ccs.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.font.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.io.*; import java.util.*; import java.math.*; import java.beans.*; import java.lang.reflect.*; import java.lang.Object.*; import java.net.URL; import java.util.regex.*; import java.text.ParseException; public class BlackJackMP extends DisplayPanel implements WindowConstants { boolean debug = true; //deck Card[] cards = getDeck(); Card[] deck = shuffle(); int place = 0; ShapePaintable infoBox; public Paintable hintPaintable; public TextPaintable win; public TextPaintable loose; public TextPaintable bust; public TextFieldView betAmt = new TextFieldView("15"); private Paintable chip; public String picURL = "http://www.ccs.neu.edu/home/lbryant/BlackJack/"; public String picList = "imagelist.txt"; public ImagePaintableLite[] pics = WebImageTools.readImagesAsPaintableLite( picURL, picList); /** The frame for the animation. */ private JPTFrame frame = null; public PaintableSequence sequence = new PaintableSequence(); /** The main panel. */ private TablePanel mainPanel = null; /** The common panel gap. */ private int gap = 10; private Tile tile = null; private volatile boolean running = false; private Object animationLock = new Object(); private Player dealer; private ArrayList players; int current = 0; //-----Constructos------ public BlackJackMP(int players) { this.dealer = new Player(true,0,0); this.players = new ArrayList(); if (players<=0) players=1; if (players>=1) { this.players.add(new Player(false,1,50)); } if (players>=2) { this.players.add(new Player(false,2,50)); } if (players>=3) { this.players.add(new Player(false,1,50)); } if (players>=4) { this.players.add(new Player(false,1,50)); } for (int i=1; i= dealer.total) { players.get(current).updateBalance( players.get(current).balance+(betAmt.demandInt()*2)); players.get(i).win(sequence); } //If lost else { players.get(i).loose(sequence,chip); } } else if (players.get(i).busted) players.get(i).loose(sequence,chip); else if (dealer.busted) players.get(i).win(sequence); } showBalance(players.get(current)); hitAction.setEnabled(false); standAction.setEnabled(false); bet.setEnabled(false); dealAction.setEnabled(false); JPTUtilities.pauseThread(1000); refreshGame(); } /** * The animation loop method that will * execute in a separate thread. */ private void runAnimation(String which, Player player) { // set state for run setRunState(); // perform animation while (true) { synchronized (animationLock) { if (!running) return; if (which.equals("p")) { player.animate(sequence,deck[place]); stopAnimation(); player.hit(deck[place]); player.total = BJValue(player); if (player.total>21) { players.get(current).busted = true; decideWinner(); } } if (which.equals("d")) { dealer.animate(sequence,deck[place]); stopAnimation(); dealer.hit(deck[place]); dealer.total = BJValue(dealer); if (players.get(current).stood && dealer.total<17) { place+=1; runAnimation("d",dealer); } if (dealer.total>21) { dealer.busted = true; decideWinner(); } } } JPTUtilities.pauseThread(50); } } /** * The method to stop the animation loop. */ public void stopAnimation() { // set state for stop setStopState(); // perform any desired cleanup activities } /** *

Set the enable/disable state of the actions * for the Run state.

* *

Set boolean running to true.

*/ private void setRunState() { synchronized (animationLock) { hitAction.setEnabled(false); stopAction.setEnabled(true); running = true; } } /** *

Set boolean running to false.

* *

Set the enable/disable state of the actions * for the Stop state.

*/ private void setStopState() { synchronized (animationLock) { running = false; stopAction.setEnabled(false); hitAction.setEnabled(true); } } /** *

The window closing method.

* *

This method is required to properly terminate * the animation thread if the window is closed * while the animation is running.

*/ private void closingAction() { synchronized (animationLock) { running = false; if (frame != null) frame.dispose(); } } /** * The method to create the application frame and * install the window closing behavior. */ private void createFrame() { } /** The method to set the window closing action. */ private void setWindowClosingAction() { // remove default closing operation frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // set special closing operation WindowActionAdapter adapter = new WindowActionAdapter(frame); adapter.addWindowClosingAction(closingAction); } /** * The main program to launch the animation. * @param args ignored */ public static void main(String[] args) { new BlackJackMP(1).frame("BlackJack"); } //Shuffles a deck and returns it as an arra of cards. public Card[] shuffle() { Card[] shuffledDeck = new Card[52]; int[] deck; deck = ProbStatTools.randomPermutation(52); for (int i = 0; i < 52; i++) { shuffledDeck[i] = cards[deck[i]]; } return shuffledDeck; } public Card[] getDeck() { Card[] deck = new Card[52]; for (int i=0; i<52; i++) { deck[i] = new Card(i, true); } return deck; } //Returns the value of a card taking into acount Blackjack rules public int BJValue(Player p) { int acc = 0; int hardAces = 0; for (int i=0; i21) { while (acc>21 && hardAces>0) { acc-=10; hardAces--; } } if (acc==21 && p.hand.size()==2) { System.out.println("BlackJack!"); players.get(current).win(sequence); return 21; } return acc; } public String getHint(Player player, Card dealer) { String[][] hints = { {"h","h","h","h","h","h","h","h","h","h"}, {"h","h","d","d","d","d","h","h","h","h"}, {"h","d","d","d","d","d","d","d","d","h"}, {"h","d","d","d","d","d","d","d","d","d"}, {"h","h","h","s","s","s","h","h","h","h"}, {"h","s","s","s","s","s","h","h","h","h"}, {"h","s","s","s","s","s","h","h","h","h"}, {"h","s","s","s","s","s","h","h","h","h"}, {"h","s","s","s","s","s","h","h","h","h"}, {"s","s","s","s","s","s","s","s","s","s"}, {"h","h","h","h","d","d","h","h","h","h"}, {"h","h","h","h","d","d","h","h","h","h"}, {"h","h","h","d","d","d","h","h","h","h"}, {"h","h","h","d","d","d","h","h","h","h"}, {"h","h","d","d","d","d","h","h","h","h"}, {"h","h","d","d","d","d","h","h","h","h"}, {"s","s","s","s","s","s","s","s","s","s"}, {"s","s","s","s","s","s","s","s","s","s"}, {"p","p","p","p","p","p","p","p","p","p"}, {"h","h","h","p","p","p","p","h","h","h"}, {"h","h","h","p","p","p","p","h","h","h"}, {"h","h","h","h","h","h","h","h","h","h"}, {"h","h","p","p","p","p","h","h","h","h"}, {"h","p","p","p","p","p","p","h","h","h"}, {"p","p","p","p","p","p","p","p","p","p"}, {"s","s","s","s","s","s","s","p","p","s"}, {"s","s","s","s","s","s","s","s","s","s"}}; if (player.hand.get(0).value() == 1) { Card temp = player.hand.get(0); player.hand.set(0, player.hand.get(1)); player.hand.set(1, temp); } if (isAce(player.hand.get(0))) { return print(hints[BJValue(player)-2][dealer.value()-1]); } if (BJValue(player)<8) return "Hit"; else if (BJValue(player) >=8 || BJValue(player)<=17) return print(hints[BJValue(player)-8][dealer.value()-1]); else if (player.hand.get(0).value() == 10 && player.hand.get(1).value() == 10) return "Stand"; else return print(hints[BJValue(player)][dealer.value()-1]); } private boolean isAce(Card card) { return (card.value() == 1); } public String print(String which) { if (which.equals("h")) return "Hit"; if (which.equals("d")) return "Double"; if (which.equals("s")) return "Stand"; if (which.equals("p")) return "Split"; else return ""; } private void refreshGame() { stopAnimation(); deck=shuffle(); place=0; dealAction.setEnabled(true); bet.setEnabled(true); doubleAction.setEnabled(false); sequence.clear(); sequence.addPaintable(pics[0]); sequence.addPaintable(infoBox); //System.out.println(BJValue(players.get(current).hand)); //System.out.println(BJValue(dealer.hand)); bet.setEnabled(true); dealer = new Player(true,0,0); for (int i=0; i(); players.get(i).total = 0; players.get(i).stood = false; players.get(i).busted = false; } showBalance(players.get(current)); } }