import java.awt.*; import java.awt.event.*; import java.applet.*; /** "Bulls & Cows" aka "Mastermind" */ public class vacca extends Applet implements MouseListener { static int[][] guesses =new int[10][4]; //colour guesses static int[] actual =new int[4]; //actual colours static int[] result =new int[4]; //bulls & cows static int[] work1 =new int[4]; //work area static int[] work2 =new int[4]; //work area static int number_of_guesses=0; static int peg=0; //the peg to be chosen next static int width,height,debug; //from html static boolean finished=false; //true after correct solution or 10 tries static Font f_small =new Font("SansSerif",Font.PLAIN,10); static Font f_big =new Font("SansSerif",Font.PLAIN,20); static Font f_huge =new Font("SansSerif",Font.PLAIN,30); public void init() {setBackground(Color.gray); addMouseListener(this); width =Integer.parseInt(getParameter("width")); height =Integer.parseInt(getParameter("height")); String Debug =getParameter("debug"); if (Debug == null) debug=0; else debug =Integer.parseInt(Debug); init_actual(); //make 4 random coloured pegs for guessing } public void mouseClicked(MouseEvent event) { Point click =event.getPoint(); int x = click.x-20; int y = click.y-30; if (((y / 20) == 0) && ((x / 20) >= 0) && ((x / 20) < 6)) if (finished) if ((x / 20) == 0) //green peg clicked {finished=false; number_of_guesses=0; peg=0; init_actual();} else ; else {guesses[number_of_guesses][peg] = (x / 20); peg++;} if (peg == 4) {number_of_guesses++; peg=0;} repaint(); } public void paint(Graphics g) { g.setFont(f_small); g.drawRect(5,5,width-10,height-10); //draw border rectangle g.drawString("Click a coloured peg",20,16); //show coloured pegs to choose from for (int i=0; i < 6; i++) { g.setColor(getColour(i)); g.fillOval(20+20*i,30,16,16); } g.setFont(f_huge); g.setColor(Color.black); g.drawString("Bulls",20,160); g.drawString("and",20,200); g.drawString("Cows",20,240); g.setFont(f_small); finished=showGuesses(g); //show guesses + bulls & cows hidden(finished,g); //show hidden code & status of game if (finished) g.drawString("Click the green peg to restart",20,350); } static Color getColour(int i) {switch (i) {case 0: return Color.green; case 1: return Color.blue; case 2: return Color.red; case 3: return Color.white; case 4: return Color.black; case 5: return Color.yellow; default: return Color.gray; } } static void init_actual() { for (int i=0; i<4; i++){actual[i] = (int) (6 * Math.random());} } static void hidden(boolean show, Graphics g) {Color j; for (int i=0; i<4; i++) {j =Color.black; if (show || (debug == 1)) j=getColour(actual[i]); g.setColor(j); g.fillOval(200+20*i,290,16,16); } g.setColor(Color.black); if ((debug == 1) && !show) g.drawString("debug",300,300); g.drawRect(200-5,290-5,90,25); if (!show) g.drawString("Guess the hidden code",20,300); else {g.setFont(f_big); if (is_correct()) {g.setColor(Color.blue); g.drawString("You WIN !",20,300); } else {g.setColor(Color.red); g.drawString("You LOSE !",20,300); } g.setColor(Color.black); g.setFont(f_small); } } static boolean showGuesses(Graphics g) {int i,j,k,l,m; m=0; //#bulls+cows for (i=0; i= 10)); } static boolean is_correct() {int i=number_of_guesses - 1; int j=0; if (i >= 0) while ((j < 4) && (actual[j] == guesses[i][j])) j++; return (j == 4); } public void mousePressed(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} }