/** mmind.java: This plays the game mastermind: the user has to guess a code; rules available in HTML at www.students.cs.uu.nl/~smotterl made by smotterl@math.ruu.nl */ import java.applet.*; import java.awt.*; public class mmind extends Applet //the main part { screen s;//a panel that displays guesses and scores TextField t;//the input devive for a guess Button newgame;//press to start a new game int[] code;//the code you're guessing public void init()//preparations { resize(200,420); s=new screen(); add("Center",s); t=new TextField(8);//the input is 4 letters add("South",t); newgame=new Button("New game"); add("North",newgame); randomcode();//fill code with a random code } public boolean action(Event ev, Object obj) /*execute the action belonging to the button that is touched*/ { if(ev.target==t)//the user tried to input a guess {int[] c=getcode();//get it if(c==null) //if it is unreadable return true; //do nothing int[] e=eval(c,code);//get a score for the code if(s.entercode(c)) //try to display it //(fails when to many guesses are allready made) { if(right(e))//the guess whas correct {s.uncloak(code);//show it was correct t.setText("You win"); } else s.entereval(e);//display the score } else {s.uncloak(code); t.setText("You lose"); } } else if(ev.target==newgame) {randomcode(); //create a new code s.wipe(); //empty the screen } return true; } public int[] eval(int[] ttrial,int[] ccode) //return a score for trial, if ccode is the code { int[] trial=new int[4]; int[] thecode=new int[4]; int[] pin=new int[4]; int pins=0; for(int i=0;i<4;i++) {pin[i]=0;//zero means no pin trial[i]=ttrial[i];//copy, because we will write... thecode[i]=ccode[i]; //in these } for(int i=0;i<4;i++) if(trial[i]==thecode[i]) {pin[pins]=2;//black pin :right colour,right pos trial[i]=18;//some weird number, so it can't... thecode[i]=19; //match again pins++; } for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(trial[i]==thecode[j]) {pin[pins]=1;//white pin, right colour,wrong pos trial[i]=20;//this can't match again. thecode[j]=21;//21 will not match pins++; } return pin; } int charToInt(char c) //return the colourno attached to a letter {switch(c) { case 'g':return 0;//the numbers should case 'w':return 1;//match the colours case 'd':return 2;//in screen case 'b':return 3; case 'y':return 4; case 'r':return 5; default: return -1;//unreadable } } public int[] getcode() //get a code from the inputfield { String str=t.getText(); t.setText(""); if(str.length()<4)//not enough input return null; int[] acode=new int[4]; for(int i=0;i<4;i++) if((acode[i]=charToInt(str.charAt(i)))==-1) return null; //if -1, then code is unreadable return acode; } void randomcode() //create a random code { if(code==null) code=new int[4]; for(int i=0;i<4;i++) code[i]=(int)(Math.random()*6); } public boolean right(int[] e) //is e an perfect evaluation (four black pins) { if(e[0]==2&&e[1]==2&&e[2]==2&&e[3]==2) return true; return false; } } class screen extends Canvas {int[][] code; //the guesses done int codefull;//how many positions are used? int[][] eval;//the scores displayed int evalfull;//idem, but evalpos. Color[] color;//the colours used int magfactor;//magnification factor int xlim,ylim;//first offscreen coordinates boolean hidden;//is the to be guessed code hidden? screen() { magfactor=3; xlim=50*magfactor; ylim=110*magfactor; resize(xlim,ylim); code= new int[11][4];//ten guesses + the code eval= new int[10][4]; color= new Color[7]; color[0]=Color.green;//the colours of the code color[1]=Color.white; color[2]=Color.black; color[3]=Color.blue; color[4]=Color.yellow; color[5]=Color.red; color[6]=Color.gray;//background colours //fill in your favourit colours hidden=true; repaint(); } public boolean entercode(int[] acode) //display acode. {if(codefull==10)//screen is full return false; code[codefull]=acode; codefull++; repaint(); return true; } public void entereval(int[] aneval) //display a score {eval[evalfull]=aneval; evalfull++; repaint(); } public void uncloak(int[] acode) {//show the mysterious code to the user code[10]=acode;//put it in the last position hidden=false;//tell paint to paint it repaint(); } public void update(Graphics g) { paint(g);} //avoids flickering,otherwise unnecessary void rect(Color c,Graphics g,int x,int y,int h,int v){ g.setColor(c); g.fillRect(x,y,h,v); g.setColor(Color.black); g.drawRect(x,y,h,v); } void piece(Color c,Graphics g,int x,int y){ rect(c,g,x+2,y+2,9*magfactor-3,9*magfactor-3); } public void paint(Graphics g) //paint it. { Color c; rect(Color.white,g,0,0,xlim-1,ylim-1); //display the guesses for(int i=0;i