//*********************** Copy right information ************************* // FAQ's // Q. Is this program copy right protected? // Ans. No. Even if it was you will copy it anyway so what is the point // Q. Can I use it for commercial purposes? // Ans. You are so funny. // Q. How do You feel about my copying this software? // Ans. We think that you are doing us a great honour by copying this // software. We are not worthy of this honour but we will appreciate // your generousity if you will simply e-mail us a thank you note // Q. What do I do if I find a bug? // Ans. Go to KMart. Buy a bug repellent(exterminator preferred) and kill // the beast. We dont like bugs. They are Yuk!!! // Q. Where is the code documentation? // Ans. You should have asked this question before we wrote this program. // you seriously do not expect us to go back and document the code // which we can barely figure out ourselves. We promise to document // our next code. (Cross our hearts. Seriously) // Q. How was the code tested and to what degree? // Ans. Good you asked as we are proud of that part. The code is guaranteed // to work even in a nuclear bomb attack provided (here is the catch) // the computer on which the software is running is able to handle // the extreme temperature and noise and radiation (you got the picture) // We also tested the software for bullet proofing (we will tell you // about that some other time) // // Disclaimer: Although we do not expect you to use this software in any // mission critical application but if you manage to start a // nuclear warhead missile to republic of china or some other // funny place using this piece of code PLEASE do sue us. // You wont get any money out of us but we will really enjoy // the resulting publicity. Kindly break the news to us in a // gentle way otherwise we may die laughing. // If you sue us nice and proper we all may become rich // from TV and film rights and product endorsements. //************************************************************************ // Program: Poker game program (As played on Poker machines in Casinos) // Programmer: H K Gupta Date: 2 July 1997 //************************************************************************ import java.awt.*; import java.applet.Applet; import java.lang.Math; public class Poker extends Applet { Panel pCard; CardControl[] cCard; // 5 cards with control Panel pStatus; // status panel Button deal; // request dealing of score Label winString; // Winning String Display Label results; TextArea historyArea; TextArea winScores; boolean laidOut = false; boolean newHand = true; int score = 0; // cumulative score CardDeck deck; // card deck for playing the game public void init() { setUpDisplay() ; deck = new CardDeck(); // get a new card deck deal.setLabel("New Hand"); newHand = true; } void setUpDisplay() { loadCardDeckImages(getImage(getCodeBase(), "HridayeshCardDeck.gif")); int i; setLayout(new BorderLayout()); pCard = new Panel(); pCard.setLayout(new GridLayout(0, 5)); add("Center", pCard); cCard = new CardControl[5]; for(i = 0; i < 5; i++) { cCard[i] = new CardControl(this); pCard.add(cCard[i]); } pStatus = new Panel(); add("East", pStatus); pStatus.setLayout(new GridLayout(0, 1, 1, 1)); // display Winning scores winScores = new TextArea(4, 30); winScores.setEditable(false); pStatus.add(winScores); winScores.appendText(" *** Winning Combinations ***\n"); winScores.appendText("Pair of Jack or Above 1 point\n"); winScores.appendText("Two Pairs 2 points\n"); winScores.appendText("Three of a kind 3 points\n"); winScores.appendText("Four of a Kind 10 points\n"); winScores.appendText("Flush(same suite) 20 points\n"); winScores.appendText("Straight 50 points\n"); winScores.appendText("Straight flush 100 points\n"); winScores.appendText("Royal flush 500 points\n"); winString = new Label("", Label.CENTER); pStatus.add(winString); deal = new Button(); deal.setFont(new Font("TimesRoman",Font.BOLD,18)); pStatus.add(deal); results = new Label("", Label.CENTER); pStatus.add(results); historyArea = new TextArea(4, 30); historyArea.setEditable(false); pStatus.add(historyArea); validate(); } public boolean action(Event e, Object arg) { Object target = e.target; int i; for(i = 0; i < 5; i++) { if (target == cCard[i].getHoldButton()) { cCard[i].switchHoldStatus(); return true; } } if (target == deal) { if (newHand) { initHand(); } else { dealCards(); // deal cards to the position not on hold } if (!newHand) updateScore(); newHand = !newHand; return true; } return false; } void initHand() { deck.reset(); int i; for(i = 0; i < 5; i++) { cCard[i].newCard(deck.newCard()); cCard[i].holdEnable(); } score = score - 1; // Charge for playing a Hand deal.setLabel("Deal"); findWinners(); } void winDisp(int win, String winStr) { if (!newHand) score += win; if (newHand) { if(win > 0) { winString.setText("You have " + winStr); } else { winString.setText(""); } } else { winString.setText(winStr); historyArea.appendText("\n> " + win + " " + winStr); } dispScore(); } void dispScore() { results.setText("Score " + score); } void findWinners() { int[] nSuite = new int[4]; // no. of cards in a suite int[] nVal = new int[13]; // no of cards for a value int nPairs = 0; // No. of Pairs int higherPair = 0; // Highest pair index boolean nStraight = false; int nHighest = -1; // Highest card int nHighWOAce = -1; // Highest card without ace int nLowest = -1; // Lowest card int flushColor = -1; int i, ns, nv; for(i = 0; i < 4; i++) nSuite[i] = 0; for(i = 0; i < 13; i++) nVal[i] = 0; for(i = 0; i < 5; i++) { // calculate nSuite and nVal ns = cCard[i].getCardValue() / 13; // suite number nSuite[ns]++; nv = cCard[i].getCardValue() - (ns * 13); nVal[nv]++; } for(i = 0; i < 13; i++) { switch (nVal[i]) { case 2: nPairs++; higherPair = i; break; case 3: winDisp(3, "Three of a kind"); return; case 4: winDisp(10, "Four of a kind"); return; } if (nVal[i] > 0) { nHighest = i; if (nLowest == -1) nLowest = i; if (i != 12) nHighWOAce = i; } } if(nPairs == 2) { winDisp(2, "Two Pairs"); return; } if(higherPair > 8) { switch (higherPair) { case 9 : winDisp(1, "Pair of Jacks"); break; case 10 : winDisp(1, "Pair of Queens"); break; case 11 : winDisp(1, "Pair of Kings"); break; case 12 : winDisp(1, "Pair of Aces"); break; } return; } if(nPairs > 0) { // Single pair less than Jacks winDisp(0, "Sorry no win this time"); return; } for(i = 0; i < 4; i++) { // check for flush ( Same colour) if (nSuite[i] == 5) { flushColor = i; } } if ((nHighWOAce == 3) || ((nHighest - nLowest) == 4)) { // Straight if (flushColor > -1) { // straight flush if ((nHighest == 12) && (flushColor == 2)) { winDisp(500, "*** ROYAL FLUSH ***"); return; } winDisp(100, "** Straight Flush **"); return; } winDisp(50, "* Straight *"); return; } if (flushColor > -1) { winDisp(20, "* Flush *"); return; } winDisp(0, "Sorry No win this time"); } void updateScore() { int i; for(i = 0; i < 5; i++) { // Disable hold buttons cCard[i].holdDisable(); } findWinners(); deal.setLabel("New Hand"); } void dealCards() { int i; for(i = 0; i < 5; i++) { if(!cCard[i].getHoldStatus()) cCard[i].newCard(deck.newCard()); } } // Card deck image load area int cardW = 73; int cardH = 97; int noOfCardImages = 53; // 52 + upturned Image index 52 holds upturned Image Image[] cardImage; MediaTracker tracker; Image cardImages; void loadCardDeckImages(Image cardImagesIn) { cardImages = cardImagesIn; tracker = new MediaTracker(this); tracker.addImage(cardImages, 0); tracker.checkAll(true); try { tracker.waitForAll(); } catch (InterruptedException e) { } cardImage = new Image[noOfCardImages]; Graphics cardG; for(int i = 0; i < 4; i++) { for(int j = 0; j < 13; j++) { cardImage[i * 13 + j] = createImage(cardW, cardH); cardG = cardImage[i * 13 + j].getGraphics(); cardG.drawImage(cardImages, - cardW * j, - i * cardH, this); } } cardImage[52] = createImage(cardW, cardH); cardG = cardImage[52].getGraphics(); cardG.drawImage(cardImages, - cardW * 13, 0, this); } } class CardControl extends Panel { private PlayingCard pCard; private Label holdLabel; private Button holdButton; private boolean holdStatus; private Panel pBottom; public CardControl(Poker controller) { super(); setLayout(new BorderLayout()); pCard = new PlayingCard(controller); add("Center", pCard); pBottom = new Panel(); add("South", pBottom); pBottom.setLayout(new GridLayout(0, 1, 1, 1)); holdLabel = new Label("", Label.CENTER); pBottom.add(holdLabel); holdButton = new Button(); holdButton.disable(); pBottom.add(holdButton); holdStatus = false; } public void switchHoldStatus() { holdStatus = !holdStatus; setHoldStatus(holdStatus); } public void setHoldStatus(boolean hStatus) { holdStatus = hStatus; if (holdStatus) { holdLabel.setText("Yes"); } else { holdLabel.setText(""); } } public void newCard(int i) { pCard.changeValue(i); } public void holdEnable() { holdStatus = false; holdButton.setLabel("Hold"); holdLabel.setText(""); holdButton.enable(); } public void holdDisable() { holdStatus = false; holdButton.setLabel(""); holdLabel.setText(""); holdButton.disable(); } public boolean getHoldStatus() { return holdStatus; } public int getCardValue() { return pCard.getCardValue(); } public Button getHoldButton() { return holdButton; } } class PlayingCard extends Canvas { private int cardValue; private Poker controller; public PlayingCard (Poker controller) { // upturned card super(); this.controller = controller; drawCard(-1); } private void drawCard(int cardIndex){ // -ve means upturned card cardValue = cardIndex; repaint(); } public void paint(Graphics g) { update(g); } public void update(Graphics g) { if(cardValue < 0 || cardValue > 51) { g.drawImage(controller.cardImage[52], 0, 0, this); } else { g.drawImage(controller.cardImage[cardValue], 0, 0, this); } } public void changeValue(int cardIndex){ cardValue = cardIndex; controller.deal.setLabel("in playDeal"); repaint(); } public int getCardValue() { return cardValue; } } class CardDeck { private boolean[] deck; // card deck containing 52 cards CardDeck() { deck = new boolean[52]; reset(); } public void reset() { int i; for(i = 0; i < 52; i++) deck[i] = true; // initialize the card deck } public int newCard() { int i, rNum; int cardsLeft; for(i = 0, cardsLeft = 0; i < 52; i++) { if (deck[i]) cardsLeft++; } if(cardsLeft == 0) return -1; // empty deck rNum = (int)(Math.random() * cardsLeft); for(i = 0; i < 52; i++) { if (deck[i]) { rNum--; if (rNum < 0) { deck[i] = false; return i; // This is the rNum'th card on the deck } } } // GOD save us if we ever reach this place return -1; } }