Listing B. Domain layer for SET game—algorithmic design.
- By Vince Huston
- August 16, 2001
Java Primer
The Game of SET & Software Design
Vince Huston
Listing B. Domain layer for SET gamealgorithmic design.
// Domain layer for SET game - algorithmic design
import java.util.Random;
class Card {
private static boolean[] used = new boolean[27];
private static Random rn = new Random();
private int number; ///// 0..26
private int[] facets = new int[3];
public Card() {
number = rn.nextInt(27);
while ( used[number] )
number = rn.nextInt(27);
used[number] = true;
facets[0] = number % 3;
facets[1] = number % 9 / 3;
facets[2] = number / 9;
}
public int getFacet( int i ) { return facets[i]; }
public int getNumber() { return number; }
public String toString() {
return "" + (number+1) + '-' + facets[0] + facets[1] + facets[2];
} }
class Set {
private Card[] cards = new Card[3];
public Set( Card one, Card two, Card thr ) {
cards[0] = one; cards[1] = two; cards[2] = thr;
}
public boolean isValid() {
int sum;
for (int i=0; i < 3; i++) {
sum = 0;
for (int j=0; j < cards.length; j++)
sum += cards[j].getFacet(i);
if (sum % 3 != 0) return false;
}
return true;
} }