/* * @(#)Guess.java 1.0 97/09/17 * * Copyright (c) 1997 Erica Schulman. All Rights Reserved. */ import java.util.*; /** * This class defines guesses of arbitrary length * as arrays of integers, along with some useful functions. * Zero represents a blank (unfilled) element. * *

This class was originally an extension of Vector * for greater flexibility, but the type-checking and protection * available by doing so seemed very poor. */ public class Guess { private int [] element; /** * Used to print out one element instead of * the entire array. */ public String toString(int position) { try { return("" + element[position]); } catch(ArrayIndexOutOfBoundsException exc) { return("Invalid position: " + position); } } /** * The constructor, which chooses an initial size and sets * elements to empty. */ Guess(int size) { element = new int[size]; // start guess row as empty (all elements = 0) for(int i=0; i < element.length; i++) { setValue(i, 0); } } /** * Set the value of an element. * * @param position element index * @param num new value */ public void setValue(int position, int num) { try { element[position] = num; } catch(ArrayIndexOutOfBoundsException exc) {} } /** * Shows if two guesses contain the same elements. */ public boolean equals(Guess correct) { // check that both guesses have the same number of elements if(element.length != correct.element.length) { return(false); } for(int i = 0; i < element.length; i++) { if( !(element[i] == correct.element[i]) ) { return(false); } } return(true); } /** * Shows if two guesses have equal elements at a particular * position. * * @param attempt the guess to be compared * @param position position at which comparison is done */ public boolean equals(Guess attempt, int position) { try { return(element[position] == attempt.element[position]); } catch(ArrayIndexOutOfBoundsException exc) { return(false); } } /** * Shows if two guesses have equal elements at particular * positions. * * @param attempt the guess to be compared * @param position1 position of calling object * at which comparison is done * @param position2 position of attempt * at which comparison is done */ public boolean equals(Guess attempt, int position1, int position2) { try { return(element[position1] == attempt.element[position2]); } catch(ArrayIndexOutOfBoundsException exc) { return(false); } } /** * Fill guess with random integer values within 1 to an upper limit. * * @param range upper limit of generated value * @param maxDuplication maximum number of elements with the same * value permitted */ public void makeRandom(int range, int maxDuplication) { Random r = new Random(); int newValue; int index; int duplications; int counter; boolean checked; for(int i = 0; i < element.length; i++) { newValue = 1 + (Math.abs(r.nextInt()) % range); if(maxDuplication > 0) { /* counter is used to avoid possible infinite loops if someone sets a ridiculous value for maxDuplications relative to range */ counter = 0; checked = false; while(!checked && (counter < (range + 1))) { index = 0; duplications = 0; if( newValue > range) { newValue = 1; } try { while((index = indexOf(newValue, index)) != -1) { index++; duplications++; } } catch(ArrayIndexOutOfBoundsException exc) {} if(duplications > (maxDuplication - 1)) { newValue++; counter++;} else { checked = true; } } /* necessary in case we reached this point by exceeding the loop counter can't meet the maxDuplication criteria, just make a random selection */ if( newValue > range) { newValue = 1 + (Math.abs(r.nextInt()) % range); } setValue(i, newValue); } } } /** * Show if all elements have been filled. */ public boolean isFull() { for(int i = 0; i < element.length; i++) { if(element[i] == 0) { return(false); } } return(true); } /** * Returns first position greater than the input * value at which a given value appears. If it is * not found, -1 is returned. * * @param value value to be found * @param start index to start looking at */ public int indexOf(int value, int start) { if(start < 0) { start = 0; } for(int i = start; i < element.length; i++) { if( element[i] == value) { return(i); } } return(-1); } }