// Project: Observer // Module: casino // Source code file: SlotMachine.java. // The Manager object is the observer. // The SlotMachine objects are the observables. import it313.util.Observable; import java.util.Random; public class SlotMachine extends Observable { private final int NUM_REELS = 3; private final String WINNING_SYMBOLS = "|7|7|7|"; private String[ ] symbols = {"7", "$", "?", "+", "apple", "bar", "bell", "cash", "cent", "cherries", "diamond", "grapes", "heart", "horseshoe", "lemon", "orange", "plum", "pound", "watermelon", "win"}; private int machineId; private String lastPlay; private int[ ] lastIndex = new int[NUM_REELS]; private Random r = new Random( ); public SlotMachine(int theId) { machineId = theId; play( ); } public void play( ) { String result = "|"; for(int i = 0; i < NUM_REELS; i++) { int numSymbols = symbols.length; int index = r.nextInt(numSymbols); lastIndex[i] = index; result += symbols[index] + "|"; } lastPlay = result; if (isJackpot( )) { setChanged( ); notifyObservers(machineId); } } public boolean isJackpot( ) { return lastPlay.equals(WINNING_SYMBOLS); } @Override public String toString( ) { return String.format("Machine ID: %d Last Play: %s", machineId, lastPlay); } }