/* Name: David Keech (Ladadadada) Student No.: 9906488 Tutorial Time: Well, it's just not relevant anymore... Assignment No.1 for SENG111, 2001, semester 2. This class simulates a poker machine. There is a player name, and three reels. The reels contain combinations of "9JQKA" except the third reel which doesn't have any 9's. Output consists of randomly choosing a part of the reel and displaying the next three characters from it. If the three middle characters match, or if the first two are "9" or if the first one is a "9" then some coins are credited to the player. AAA = 150 KKK = 20 QQQ = 15 JJJ = 10 99- = 5 9-- = 2 Output to the player should be between %80 and %97 of what they put in. To achieve this, I created some checking variables (left in) and edited the reel1, reel2 and reel3 in newPlayer() until the %ouput was correct. To re-check, un-comment the marked lines in the code and comment the line in setRow that looks like: displayPayout(calculatePayout(row2)); */ import java.awt.*; import BreezyGUI.*; import java.lang.*; public class PokerMachine extends GBFrame { private final int MAXREELLENGTH = 30; private final int MINREELLENGTH = 20; //The MAX and MIN lengths for the reels... //each reel can be a different length private String displayString = ""; //The String that is displayed in the TextArea private String reel1 = "9JJQKAA"; private String reel2 = "9JJQKAA"; private String reel3 = "JJQKAA"; //These Strings are to be randomized and extended out to //20+ characters every time we get a new player. private String player = ""; //This holds the name of the current player. private String row1,row2,row3; //The three rows that will be displayed to the player. private int money = 500; //How much money the player has. //private double counter = 0; //A counter for how many spins the player has had. //private double winnings = 0; //The players winnings. //These are for calculating the percentage of payouts. //Uncomment the doubles for checking %payout private Label nameLabel = addLabel("Player :",1,1,1,1); private TextField nameField = addTextField("Type your name here",1,3,2,1); private Label moneyLabel = addLabel("Money :",2,1,1,1); private IntegerField moneyField = addIntegerField(0,2,2,2,1); private TextArea outPutArea = addTextArea("KKK\nAAA\nQQQ",3,3,1,2); private Button spinReels = addButton("Spin Reels",5,1,2,1); private Button collect = addButton("Collect",5,3,2,1); private Button newPlayer = addButton("New Player",5,4,2,1); public PokerMachine() //Constructor sets moneyField so it can't be edited { moneyField.setEditable(false); outPutArea.setEditable(false); } public void newPlayer() { player = nameField.getText(); if(player.equals("")) displayMessage("You must put something\nin the name field"); else if(player.length() < 3) { displayMessage("Names have to be greater than\nthree characters"); player = ""; } else { money = 500; moneyField.setNumber(money); reel1 = randomizeReel("9JJQKAA"); reel2 = randomizeReel("9JJQKAA"); reel3 = randomizeReel("JJQKAA"); displayMessage("Starting a new game for " + player + "\n" + player + " has been given $" + money + "\nto start with."); displayResults(player + "'s three reels are:\n" + reel1 + "\n" + reel2 + "\n" + reel3); } //for(int i = 0;i < 10000;i++) //{ // spinReels(); //} //System.out.println("" + (int)counter + " spins $" // + (int)winnings + " won %" // + (((winnings/counter)/5)*100)); // //These lines for %output checking. //To check %payout, uncomment the above lines. } public String randomizeReel(String reelContents) { String reelString = ""; int reelLength = (int)(Math.random() * (MAXREELLENGTH - MINREELLENGTH) + MINREELLENGTH); for(int i = 0; i < reelLength; i++) { reelString = reelString + reelContents.charAt((int)(Math.random()* reelContents.length())); } return reelString; } public void spinReels() //Starts the spinReel process. Makes three three-char //Strings and passes them to setRow { String visibleReel1 = spinReel(reel1); String visibleReel2 = spinReel(reel2); String visibleReel3 = spinReel(reel3); setRow(visibleReel1,visibleReel2,visibleReel3); } public String spinReel(String theReel) //Takes a String representing a reel and outputs //three consecutive characters from it. { String visibleReel = ""; int startInt = (int)(Math.random()*(theReel.length())); if(startInt == (theReel.length() - 2)) { visibleReel = visibleReel + theReel.charAt(startInt) + theReel.charAt(startInt + 1) + theReel.charAt(0); } else if(startInt == (theReel.length() - 1)) { visibleReel = visibleReel + theReel.charAt(startInt) + theReel.charAt(0) + theReel.charAt(1); } else { visibleReel = visibleReel + theReel.charAt(startInt) + theReel.charAt(startInt + 1) + theReel.charAt(startInt + 2); } return visibleReel; } public void setRow(String reel1,String reel2,String reel3) //Takes three reel Strings (three characters each) and makes //them ready for printing to the screen. ie. turning each one //90 degrees and reading across the rows. //Also sets the relevant fields to their respective values. { row1 = "" + reel1.charAt(0) + reel2.charAt(0) + reel3.charAt(0) + "\n"; row2 = "" + reel1.charAt(1) + reel2.charAt(1) + reel3.charAt(1) + "\n"; row3 = "" + reel1.charAt(2) + reel2.charAt(2) + reel3.charAt(2) + "\n"; displayString = row1 + row2 + row3; displayResults(displayString); money = money - 5; moneyField.setNumber(money); displayPayout(calculatePayout(row2)); //Pops up a MessageBox. Comment this line out when checking %payout. money = money + calculatePayout(row2); moneyField.setNumber(money); //counter++; //winnings = winnings + calculatePayout(row2); //Used for checking %payout, un-comment when checking %payout. } public int calculatePayout(String payoutRow) //Takes the middle row and calculates how many //coins the player should be credited with. { int payout = 0; if(payoutRow.equals("KKK\n")) payout = 20; else if(payoutRow.equals("QQQ\n")) payout = 15; else if(payoutRow.equals("JJJ\n")) payout = 10; else if(payoutRow.equals("AAA\n")) payout = 150; else if(payoutRow.charAt(0) == '9') { if (payoutRow.charAt(1) == '9') payout = 5; else payout = 2; } return payout; } public void displayResults(String resultString) { outPutArea.setText(resultString); } public void displayPayout(int payout) { displayMessage(player + ", you won " + payout + " coins"); } public void displayMessage(String message) //Handles all messageBoxes. Puts the messageBox in the middle of the screen. { if (!message.equals("")) //If message is blank, don't display. { MessageBox myMessage = new MessageBox(this, message); myMessage.setLocation(200,200); myMessage.setVisible(true); } } public void buttonClicked(Button thisButton) //Called every time a button is clicked. //Either "New player" or "Spin reels". { if(thisButton == newPlayer) { newPlayer(); } else if (thisButton == spinReels) { if(player == "") displayMessage("I don't know who you are."); else if (money < 1) displayMessage(player + ", you've run out of money.\nLuckily, it's easy to get more here,\njust hit \"New Player\" again."); else spinReels(); } else if(thisButton == collect) { displayMessage(player + ", you won $" + money + "\nAll of your winnings will\ncome out the disk drive.\nJust hold a cup underneath and wait.\nIf you have any problems,\njust call for an attendant."); money = 0; moneyField.setNumber(0); nameField.setText("Machine vacant"); player = ""; } } public static void main(String[] args) { Frame frm = new PokerMachine(); frm.setLocation(100,100); frm.setTitle("Poker Machine"); frm.setSize(300,300); frm.setVisible(true); } }