MasterMind Lab

MasterMind is a game where four colors are randomly chosen, and after each guess, you are told how many are the correct color in the correct position, and how many are the correct color in the wrong position.

In this applet, you select your guess by clicking the colored circles until you see the color you want to guess, then pressing the "OK" button you will see your guess and a how many are in the right position, and how many are the correct color in the wrong position. You have 10 guesses.

This Lab uses arrays and the ArrayList Class data structures. It also implements the MouseListener interface to get graphic input from the user.


Your browser is ignoring the <APPLET> tag!

8 point version

Implement the makePattern() method of MasterMind.java so that it generates a random pattern or four colors.

9 point version

Use the isCorrect() method in the paint() method of MasterMind.java to show when the guess matches the pattern.

10 point version

Change the classes so that the color possibilities include red, green blue, yellow and magenta

11 point version

Change the classes so that there are not four, but five colors in the pattern to guess.

Other enhancements

You may wish to increase the number of guesses the player has, or a add a reset button to start a new game.

MasterMind.java

        
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 
import java.util.ArrayList;
/**
 * Class MasterMind - the driver for the MasterMind Applet
 * 
 * @author Chris Thiel
 * @version 11  Nov 2008
 */
public class MasterMind extends JApplet implements MouseListener
{
    // instance variables - replace the example below with your own
    private Button guess[]=new Button[4];
    private ArrayList<Clue> history = new ArrayList<Clue>();
    private Rectangle ok;
    private int pattern[];
    boolean gameOver;
    public void init()
    {
        gameOver=false;
        for(int i=0;i<4;i++)
            guess[i]=new Button(50+35*i, 300, 30);
        ok=new Rectangle( 200, 300, 70, 30);
        addMouseListener(this);
        pattern=makePattern();
    }

    
    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {
        // simple text displayed on applet
        g.setColor(Color.black);
        g.fillRect(40, 295, 100+4*35, 45 );
        g.setColor(Color.black);
        g.drawString("Master Mind", 20, 20);
        for (Button b:guess) b.draw(g);
        g.setColor(Color.white);
        g.fillRect(ok.x, ok.y, ok.width, ok.height);
        g.setColor(Color.black);
        g.drawString( "OK", ok.x+25, ok.y+20);
        for (int i=0;i<history.size();i++)
        if (i<10){
            g.drawString(Integer.toString(1+i), 35, 55+i*25);
            history.get(i).draw(g, 50, 40+i*25);
        }
        if (gameOver){
            for(int i=0;i<4;i++)
            {
              g.setColor(Button.colors[pattern[i] ]);
              g.fillOval(50+35*i, 350, 30, 30);
              g.setColor(Color.BLACK);
              g.drawOval(50+35*i, 350, 30, 30);
            }
            g.drawString("was the pattern", 70,400);
        }
    }
/**
 * makePattern returns the pattern of colors
 * that the player needs to guess
 *
 * This needs to be implemented so it will
 * be 4 random integers from 0 to 3
 */    
    private int[] makePattern()
    {
        int[] thePattern={0,1,2,3};
        return thePattern;
    }
/**
 * MOUSE  LISTENER STUFF:
 *
 * Since we are implementing the MouseListener Interface 
 * There are some required methods we need to declare here, even
 * if there is nothing to do if that mouse event occurs:
 */
 
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mousePressed(MouseEvent e) 
    {
      int x = e.getX();
      int y = e.getY();
      for (Button b:guess) if (b.contains(x,y) ) b.changeColor();
      if (ok.contains(x,y) )
      {
          int theGuess[]=new int[4];
          for(int i=0;i<4;i++) theGuess[i]=guess[i].getColor();
          history.add(new Clue( theGuess, pattern));
          if (history.size()>=10) gameOver=true;
       }
      repaint();
    }
}

        

Button.java

import java.awt.Color;
import java.awt.*;

/**
 * Button Class for Master mind.
 * Press to change colors
 * 
 * @author Chris Thiel
 * @version 11 Nov 2008
 */
public class Button 
{
    // instance variables - replace the example below with your own
    public static Color colors[]={Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
    private int currentColor;
    private int x, y, size;
    private Rectangle clickArea;
    /**
     * Constructor for objects of class button
     * requires location and size
     */
    public Button(int xLocation, int yLocation, int s)
    {
        // initialize instance variables
        currentColor=0;
        x=xLocation;
        y=yLocation;
        size=s;
        clickArea=new Rectangle(x,y,size,size);
        
    }
    /**
     * returns the current color selected
     */
    public int getColor()
    {
        return currentColor;
    }
    /**
     * draw will make a circle of the currently selected color
     */
    public void draw(Graphics g)
    {
        g.setColor(colors[currentColor]);
        g.fillOval(x,y,size, size);
    }
    /**
     * changeColor will advance to the next color in the list
     */
    public void changeColor()
    {
        currentColor++;
        currentColor=currentColor%4;
    }
    /**
     * contains returns true if the location is in the click
     * area of the button
     */
    public boolean contains(int xLocation, int yLocation)
    {
        return clickArea.contains(xLocation, yLocation);
    }
}

        

Clue.java

import java.awt.*;
import java.util.ArrayList;
/**
 * Clue records and scores a MasterMind Guess.
 * 
 * @author Chris Thiel
 * @version 1 Jan 2010
 */
public class Clue
{
    
    private int oldGuess[]=new int[4];
    private int rightColor,rightPosition;
    /**
     * Constructor for objects of class Clue
     */
    public Clue(int[] theGuess, int[] theAnswer)
    {
       // Initialize instance variables
        oldGuess=theGuess;
        rightColor=0;
        rightPosition=0;
        // check for correct color in the correct position
        for (int i=0;i<4;i++)
        {
          if (theAnswer[i]==oldGuess[i]) rightPosition++;
        }
        // check for correct color in any position
        
        ArrayList<Integer> guess=new ArrayList<Integer>();
        for (int x:theGuess)guess.add(x);
        ArrayList<Integer> answer=new ArrayList<Integer>();
        for (int x:theAnswer)answer.add(x);
        int j=0;
        while (j<guess.size())
	          if (answer.contains(guess.get(j)))
	          {
	            rightColor++;
	            answer.remove(guess.get(j));
	            guess.remove(j);
	          }else j++;
          
        
        // adjust right color for those in the correct position
        rightColor=rightColor-rightPosition;
    }
    /**
     * draw will draw a picture of the guessed colors
     */
    public void draw(Graphics g, int x, int y)
    {
        for (int i=0;i<4;i++){
            g.setColor(Button.colors[ oldGuess[i] ]);
            g.fillOval(x+i*25,y,20, 20);
            g.setColor(Color.BLACK);
            g.drawOval(x+i*25,y,20, 20);
        }
        g.setColor(Color.black);
        g.drawString (Integer.toString(rightPosition), x+100, y+15);
        g.drawString (Integer.toString(rightColor), x+120, y+15);
        
    }
    
    public boolean isCorrect()
    {
        if (rightPosition==4) return true;
        return false;
    }
    
}