/*This applet plays memory made by Sieuwert van Ottterloo, 1998 updated 2000: a mediatracker and different levels have been added. */ import java.awt.*;//basic import java.applet.*;//because this is an applet import java.net.*;//because we load gif files public class Memory extends Applet //the main of the applet { table t;//the panel with the cards on it Button newgame,surrender;//pressable buttons Label l1;//to show the scores Choice mychoice; String mylevel="easy"; public void init()//preparations { resize(9*table.SIZE+20,8*table.SIZE+70); System.out.println((9*table.SIZE+20)+":"+(8*table.SIZE+70)); setBackground(Color.white); Panel buttons=new Panel();//to keep the button together buttons.setLayout(new GridLayout(1,4,2,2));//3 positions buttons.add(newgame=new Button("New game")); buttons.add(surrender=new Button("Show me all")); mychoice=new Choice(); mychoice.add("easy"); mychoice.add("normal"); mychoice.add("hard"); buttons.add(mychoice); l1=new Label(); l1.setFont(new Font("Arial",Font.PLAIN,20)); l1.setBackground(Color.green); buttons.add(l1); add("Center",t=new table(this,l1)); add("South",buttons); } public boolean action(Event ev, Object obj) /*a button is touched*/ { if(ev.target==newgame) t.randomcards();//shuffle the cards else if(ev.target==surrender) t.showall();//show all cards else if (ev.target==mychoice) {String s=mychoice.getSelectedItem(); if (s.equals("easy")&&!s.equals(mylevel)) t.resizemem(8); else if (s.equals("normal")&&!s.equals(mylevel)) t.resizemem(16); if (s.equals("hard")&&!s.equals(mylevel)) t.resizemem(32); mylevel=s; } return true; } } class table extends Canvas implements Runnable { int[] visbuf;//what can be seen on table int[] card;//where are the cards /*card stores the places of the cards. A card entry is either NONE, if the card that was originally there is removed, or a picturenumber (0 to and including 35). Visbuf can also have the value BACK, if the back of the card is visible. */ Applet applet;//needed to get the documentbase final static int TOTAL=72;//total # of cards in play final static int BACK=36;//a facedown card final static int NONE=-2;//no card at all final static int SIZE=52;//cardsize (in pixels) final static int PICOFFSET=1;//#pixels the pictures are translated. int PCMEMSIZE=8;//starta at level easy int pos1,pos2;/*position of the open cards.*/ final static int USER1=21,USER2=22,USER3=23,PC1=24,PC2=25,PC3=26,WAIT=27; int state; /*The state determines what happens after a mouseclick.*/ int pcchoice1,pcchoice2; int[] pcmem; int pcmemstart; Color gray=new Color(204,204,204); int pointspc,pointsuser; Image[] picture;//the pictures used Label l1;//for messages MediaTracker MT; table(Applet a,Label a_label) { setBackground(Color.gray); l1=a_label;//the label for messages applet=a;//the applet we are running in. visbuf=new int[TOTAL]; card=new int[TOTAL]; pcmem=new int[PCMEMSIZE]; resize(9*SIZE,8*SIZE);//the size needed to display it all picture= new Image[37];//36 cards, plus BACK URL base=applet.getDocumentBase(); MT=new MediaTracker(this); for(int i=0;i<37;i++) {try { picture[i]=applet.getImage(new URL(base,i+".gif")); MT.addImage(picture[i],0); } catch(MalformedURLException e) {System.out.println("loading failed");} } randomcards();//shuffle all showall(); new Thread(this).start(); } int pcmakechoice1() { //seek a pair of cards if possible... for(int i=0;i state==USER1: no card open yet. Store the clicked card in pos1, and turn it around. -> state==USER2.One card allready turned around. Turn the clicked card, and store it in pos2 -> state==USER3: two cards open. if the same, remove, else lay them face down. PC1,PC2,PC3 are the same for the PC to move. */ { if(state==WAIT) return true; if(pointspc+pointsuser==36) return true; if(state==USER3||state==PC3)//already two cards open { if(card[pos2]==card[pos1])//same cards {visbuf[pos2]=card[pos2]=NONE; visbuf[pos1]=card[pos1]=NONE;//remove the cards if(state==USER3) { addpoints(1,0); state=USER1; } else { addpoints(0,1); state=PC1; } } else { visbuf[pos2]=visbuf[pos1]=BACK;//flip them back state=(state==USER3)?PC1:USER1; } repaint(); return true; } //one or no card open: if(state==USER1||state==USER2) { x=x/SIZE; y=y/SIZE;//calcuate coordinates in cards. int pos=x+9*y;//conversion to 1D coordinates if(x<0||x>=9||y<0||y>=8||visbuf[pos]!=BACK) return true; /*The mouseclick must be inside the table, and point at a facedown card.*/ visbuf[pos]=card[pos];//show the card pcremember(pos); repaint(); if(state==USER1) pos1=pos; //just store it else pos2=pos;//also store it state++; return true; } else if(state==PC1) { pos1=pcmakechoice1(); visbuf[pos1]=card[pos1];//show the card pcremember(pos1); repaint(); state++; } else { pos2=pcmakechoice2(); visbuf[pos2]=card[pos2];//show the card pcremember(pos2); repaint(); state++; } return true; } public void paint(Graphics g) /*paint all cards. They are drawen like they are in visbuf: Most of them facedown. */ { for(int i=0;i