import java.awt.*; import java.applet.*; import java.net.*; import java.lang.*; public class Poker extends Applet { boolean deck[]; String v[]={"2","3","4","5","6","7","8","9","t","j","q","k","a"}; String s[]={"c","d","h","s"}; String mes[]={"","Pair","Two Pair","Three of a Kind","Four of a Kind","Full House", "Flush","Straight","Straight Flush","Royal Flush","You Lose"}; int curMes=0; int mesLoc[]={0,225,200,160,168,188,215,202,167,182,197}; int payOut[]={0,1,2,3,25,8,5,4,50,250,0}; Color bg; Color surface; int Height=300; int Width=500; int SurfWidth=300; int SurfHeight=106; Image Cards[]; int hand[]; int value[]; int suit[]; int scratch[]; Button reDeal; Button Quitit; Button Card1; Button Card2; Button Card3; Button Card4; Button Card5; Choice money; boolean cardState[]; boolean firstDeal=false; boolean drawable=false; boolean youLose=false; Font boldFont; Font normalFont; long score=100; long highScore=10; URL link=null; String backDoc="http://www.access.digex.net/~kkessler/index.html"; public void init() { int i,r,g,b; String param; param=getParameter("height"); if (param != null) Height=Integer.valueOf(param).intValue(); param=getParameter("width"); if (param!=null) Width=Integer.valueOf(param).intValue(); r=g=0; b=255; param=getParameter("bgcolor"); if (param!=null) if(param.charAt(0)=='#') { r=readColorValue(param,1,3); g=readColorValue(param,3,5); b=readColorValue(param,5,7); } bg = new Color(r,g,b); r=b=0; g=255; param=getParameter("bgcolor"); if (param!=null) if(param.charAt(0)=='#') { r=readColorValue(param,1,3); g=readColorValue(param,3,5); b=readColorValue(param,5,7); } surface = new Color(r,g,b); param=getParameter("highscore"); if(param!=null) { highScore=Long.valueOf(param).longValue(); } param=getParameter("backlink"); if(param!=null) { backDoc=param; } deck=new boolean[52]; for (i=0;i<52;i++) deck[i]=false; hand=new int[5]; cardState=new boolean[5]; value=new int[5]; suit=new int[5]; scratch=new int[5]; Cards=new Image[52]; for (i=0;i<5;i++) { hand[i]=0; cardState[i]=false; } setLayout(null); boldFont=new Font("Helvetica",Font.BOLD,24); normalFont=new Font("Helvetica",Font.PLAIN,12); setFont(normalFont); add(Card1=new Button()); add(Card2=new Button()); add(Card3=new Button()); add(Card4=new Button()); add(Card5=new Button()); Card1.reshape((Height-SurfHeight)/2+12,(Width-SurfWidth)/2 + 54,50,20); Card2.reshape((Height-SurfHeight)/2+70,(Width-SurfWidth)/2 + 54,50,20); Card3.reshape((Height-SurfHeight)/2+128,(Width-SurfWidth)/2 + 54,50,20); Card4.reshape((Height-SurfHeight)/2+186,(Width-SurfWidth)/2 + 54,50,20); Card5.reshape((Height-SurfHeight)/2+244,(Width-SurfWidth)/2 + 54,50,20); add(money=new Choice()); money.addItem("$ 1"); money.addItem("$ 2"); money.addItem("$ 3"); money.addItem("$ 4"); money.addItem("$ 5"); money.reshape(385,230,50,120); setFont(boldFont); add(reDeal=new Button()); add(Quitit=new Button()); reDeal.setLabel("DEAL"); reDeal.reshape((Height-SurfHeight)/2+75,(Width-SurfWidth)/2 + 90,150,40); Quitit.setLabel("QUIT"); Quitit.reshape((Height-SurfHeight)/2+75,(Width-SurfWidth)/2 + 145,150,40); } public void checkResults() { int i,tempory,j,threeVal,bet; boolean flush,pair,three,straight,high,royal,four,marker,two; flush=pair=three=two=straight=high=royal=four=marker=false; threeVal=15; //Break cards into values and suits for (i=0;i<5;i++) { suit[i]=(int)(hand[i]/13); value[i]=hand[i]-suit[i]*13; } //Sort Cards for(i=0;i<4;i++) for(j=1;j<5-i;j++) { if(value[j-1]>value[j]) { tempory=value[j-1]; value[j-1]=value[j]; value[j]=tempory; } } //Check for Flush if((suit[0]==suit[1])&&(suit[0]==suit[2])&&(suit[0]==suit[3])&&(suit[0]==suit[4])) flush=true; //Check for straight straight=true; for(i=1;i<5;i++) if(value[i]!=value[i-1]+1) { straight=false; break; } if(straight) if(value[0]>7) royal=true; //Check for 4 of a kind if(((value[0]==value[1])&&(value[0]==value[2])&&(value[0]==value[3]))|| ((value[1]==value[2])&&(value[1]==value[3])&&(value[1]==value[4]))) four=true; if(!four) { //Check for 3 of a kind for(i=2;i<5;i++) { if((value[i-2]==value[i-1])&&(value[i-1]==value[i])) { three=true; threeVal=value[i]; } } //Check for pairs for(i=1;i<5;i++) { if((value[i-1]==value[i])&&(value[i]!=threeVal)) { if(value[i]>8) high=true; if(pair) two=true; else pair=true; } } } if(four) curMes=4; else if((pair)&&(three)) curMes=5; else if(two) curMes=2; else if(three) curMes=3; else if((pair)&&(high)) curMes=1; else if((straight)&&(flush)&&(royal)) curMes=9; else if((straight)&&(flush)) curMes=8; else if(straight) curMes=7; else if(flush) curMes=6; else curMes=10; bet=money.getSelectedIndex()+1; if(curMes!=10) score+=bet*payOut[curMes]; } public void deal() { int i,card; for (i=0;i<5;i++) { if((firstDeal)||(!cardState[i])) { while(true) { card=(int)(Math.random() * 52); if(!deck[card]) break; } hand[i]=card; deck[card]=true; } } } public int readColorValue(String param,int start,int stop) { String temp; try { temp=param.substring(start,stop); } catch(StringIndexOutOfBoundsException e){return 0;} try { return Integer.valueOf(temp,16).intValue(); } catch(NumberFormatException e){return 0;} } public void paint(Graphics g) { if(drawable) { g.setColor(bg); g.fillRect(0,0,Width,Height); g.setColor(Color.black); g.drawString("Video Poker",180,35); g.drawString(mes[curMes],mesLoc[curMes],60); g.drawString("Your Bet",360,220); g.drawString("Your Score",20,220); g.drawString(Long.toString(score),60,250); g.setColor(surface); g.fillRect((Height-SurfHeight)/2,(Width-SurfWidth)/2-29,SurfWidth,SurfHeight); if(youLose) { g.setColor(Color.black); g.drawString("You lost all your money!",107,120); } else { g.drawImage(Cards[hand[0]],13+(Height-SurfHeight)/2,76,this); g.drawImage(Cards[hand[1]],71+(Height-SurfHeight)/2,76,this); g.drawImage(Cards[hand[2]],129+(Height-SurfHeight)/2,76,this); g.drawImage(Cards[hand[3]],187+(Height-SurfHeight)/2,76,this); g.drawImage(Cards[hand[4]],245+(Height-SurfHeight)/2,76,this); } } } public synchronized void loadImages() { int i,j; for(i=0;i<4;i++) for(j=0;j<13;j++) Cards[i*13+j]=getImage(getCodeBase(),"images/"+v[j]+s[i]+".gif"); drawable=true; } public boolean handleEvent(Event e) { int i; if((e.id!=java.awt.Event.MOUSE_DOWN)||(e.modifiers!=0)) { return false; } if(e.target==reDeal) { if(youLose) { youLose=false; score=100; } Card1.setLabel(""); Card2.setLabel(""); Card3.setLabel(""); Card4.setLabel(""); Card5.setLabel(""); if(firstDeal) { firstDeal=false; money.enable(); } else { curMes=0; for(i=0;i<52;i++) deck[i]=false; firstDeal=true; score-=money.getSelectedIndex()+1; money.disable(); } deal(); if(!firstDeal) { checkResults(); if(score<=0) youLose=true; } repaint(); for(i=0;i<5;i++) cardState[i]=false; return true; } if((e.target==Card1)&&(firstDeal)) { if(cardState[0]) { Card1.setLabel(""); cardState[0]=false; } else { Card1.setLabel("Keep"); cardState[0]=true; } repaint(); return true; } if((e.target==Card2)&&(firstDeal)) { if(cardState[1]) { Card2.setLabel(""); cardState[1]=false; } else { Card2.setLabel("Keep"); cardState[1]=true; } repaint(); return true; } if((e.target==Card3)&&(firstDeal)) { if(cardState[2]) { Card3.setLabel(""); cardState[2]=false; } else { Card3.setLabel("Keep"); cardState[2]=true; } repaint(); return true; } if((e.target==Card4)&&(firstDeal)) { if(cardState[3]) { Card4.setLabel(""); cardState[3]=false; } else { Card4.setLabel("Keep"); cardState[3]=true; } repaint(); return true; } if((e.target==Card5)&&(firstDeal)) { if(cardState[4]) { Card5.setLabel(""); cardState[4]=false; } else { Card5.setLabel("Keep"); cardState[4]=true; } repaint(); return true; } if(e.target==Quitit) { try { link=getDocumentBase(); link=new URL(backDoc); } catch(MalformedURLException er2){} getAppletContext().showDocument(link); } return false; } public void start() { int i; loadImages(); repaint(); } public void stop() { score=100; firstDeal=false; } public void destroy() { } }