Source Code Listing 1.

Component JAVA
Mastermind and Design Paradigms

Vince Huston
Listing 1.


import java.io.*;
import java.util.*;

public class MastermindPlayOO {
   public static void main( String[] args ) throws IOException {
      BufferedReader rdr = new BufferedReader( new InputStreamReader( System.in ));
      String str = null;
      BoardOO board = new BoardOO();
      int[] response = new int[2];
      while (response[0] != BoardOO.NUM_SLOTS) {
         System.out.print( "Enter guess: " );
         str = rdr.readLine();
         board.evaluate( str.toCharArray(), response );
         System.out.println( "                     " + response[0] + ' ' + response[1] );
}  }  }

class BoardOO {
   public static final int NUM_CHOICES = 6;
   public static final int NUM_SLOTS   = 4;

   private Slot[] answer = new Slot[NUM_SLOTS];

   private class Slot {
      private char value;
      public Slot() { value = (char) ('a' + (int) (Math.random()*1000) % NUM_CHOICES); }
      public Slot( Character chObj ) { value = chObj.charValue(); }
      public boolean equals( Object obj ) {
         if (obj instanceof Slot)      return value == ((Slot)obj).value;
         if (obj instanceof Character) return value == ((Character)obj).charValue();
         return false;
      }
      public String toString() { return String.valueOf( value ); }
   }
   public BoardOO() {
      for (int i=0; i < num_slots;="" i++)="" answer[i]="new" slot();="" for="" (int="" i="0;" i="">< num_slots;="" i++)="" system.out.print(="" answer[i]="" );="" system.out.println();="" }="" public="" void="" evaluate(="" char[]="" guess,="" int[]="" response="" )="" {="" response[0]="response[1]" =="" 0;="" linkedlist="" guessll="new" linkedlist();="" linkedlist="" answerll="new" linkedlist();="" for="" (int="" i="0;" i="">< num_slots;="" i++)="" {="" guessll.add(="" new="" character(="" guess[i]="" ));="" answerll.add(="" answer[i]="" );="" }="" compute="" black="" answer="" listiterator="" ait="answerLL.listIterator(0);" listiterator="" git="guessLL.listIterator(0);" while="" (ait.hasnext())="" if="" (((slot)ait.next()).equals(="" (character)="" git.next()="" ))="" {="" response[0]++;="" ait.remove();="" git.remove();="" }="" compute="" white="" answer="" git="guessLL.listIterator(0);" object="" obj;="" while="" (git.hasnext())="" {="" if="" (answerll.contains(="" obj="new" slot(="" (character)="" git.next()="" )))="" {="" response[1]++;="" answerll.remove(="" obj="" );="" }="" }="" }="" }="">