Source Code Listing 2

Component JAVA
Mastermind and Design Paradigms

Vince Huston
Listing 2.


import java.io.*;

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

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

   private char[]    answer     = new char[NUM_SLOTS];
   private boolean[] answerUsed = new boolean[NUM_SLOTS];
   private boolean[] guessUsed  = new boolean[NUM_SLOTS];

   private int[] answerChars = new int[NUM_CHOICES];
   private int[] guessChars  = new int[NUM_CHOICES];

   public BoardDS1() {
      // populate answer array with random characters
      for (int i=0; i < num_slots;="" i++)="" answer[i]="(char)" ('a'="" +="" (int)="" (math.random()*1000)="" %="" num_choices);="" for="" (int="" i="0;" i="">< num_slots;="" i++)="" answerchars[="" answer[i]="" -="" 'a'="" ]++;="" }="" public="" void="" evaluate(="" char[]="" guess,="" int[]="" response="" )="" {="" response[0]="response[1]" =="" 0;="" initialize="" boolean="" arrays="" for="" (int="" i="0;" i="">< num_slots;="" i++)="" guessused[i]="answerUsed[i]" =="" false;="" compute="" black="" answer="" for="" (int="" i="0;" i="">< num_slots;="" i++)="" if="" (guess[i]="=" answer[i])="" {="" response[0]++;="" guessused[i]="answerUsed[i]" =="" true;="" }="" compute="" white="" answer="" for="" (int="" i="0;" i="">< num_slots;="" i++)="" if="" (="" !="" guessused[i])="" for="" (int="" j="0;" j="">< num_slots;="" j++)="" if="" (="" !="" answerused[j])="" if="" (guess[i]="=" answer[j])="" {="" response[1]++;="" guessused[i]="answerUsed[j]" =="" true;="" break;="" }="" }="" }="">

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.