I am doing a slot machine project. It is close to completion and as far as I know runs perfectly, as I took great care in checking for and eliminating bugs (one thing that may arguably be a bit sloppy is the fact it is possible to get a 3 in a row of more than one type of value). However, there are a few requirements I have yet to complete, and these requirements merely are to demonstrate that I know material from previous chapters.
Requirements I would appreciate help with said:
5. At least ONE interface and THREE classes (One of which MUST be abstract), Not including the driver, which brings the total to FIVE.
9. Polymorphism must be implemented with the student designed classes.
10. Class Array List must be used in at least ONE student designed class and it MUST be traversed through AND accessed via an Iterator.
12. Animation OR Graphical Interface are optional for extra points
9. Polymorphism must be implemented with the student designed classes.
10. Class Array List must be used in at least ONE student designed class and it MUST be traversed through AND accessed via an Iterator.
12. Animation OR Graphical Interface are optional for extra points
However, I would also appreciate help understanding how to do graphics. I guess I have one simple fundamental question about Java and graphics. Like, say you have some images saved on your computer that you want to apply (probably as slot icons I'd assume). How do you apply those images onto like a Java applet? On a related note, I believe this is why I can't get this code (not related to the project) to work. Tiled Pictures Java, aka there are supposed to be pictures that are displayed getting progressively smaller from the lower right to the upper left.
Now, I'll show what code I'm using.
public interface InsertCredits
{
public void SetCost (int cost);
public void DepositCredits();
}
import java.util.Random;
public class ClassicSlots implements InsertCredits
{
public int slotvalues;
public int display[][];
public int wheel1[], wheel2[], wheel3[];
public int credits, maxCost, pay;
public boolean win;
public int position1, position2, position3;
Random generator = new Random();
String response = "y";
public ClassicSlots (int num, int cash)
{
slotvalues = num;
credits = cash;
if (slotvalues < 5)
slotvalues = 5;
display = new int[3][3];
for (int index = 0; index < 3; index++)
{
for (int index2 = 0; index2 < 3; index2++)
{
display[index][index2] = index2 + 1;
}
}
wheel1 = new int[slotvalues + 1]; ////the first reel is meant to have values 1,2,3,4,3,5,6 in this order
for (int index1 = 0; index1 < slotvalues; index1++)
{
wheel1[index1] = index1 + 1;
}
for (int index1 = 5; index1 < slotvalues + 1; index1++)
{
wheel1[index1] = index1;
}
wheel1[4] = 3;
wheel2 = new int[slotvalues]; ////the second reel is meant to have values 1,2,3,4,5,6 in this order
for (int index1 = 0; index1 < slotvalues; index1++)
{
wheel2[index1] = index1 + 1;
}
wheel3 = new int[slotvalues + 1]; ////the third reel is meant to have values 3,2,3,4,5,6,1 in this order
for (int index1 = 0; index1 < slotvalues + 1; index1++)
{
wheel3[index1] = index1 + 1;
}
wheel3[0] = 3;
wheel3[slotvalues] = 1;
}
public void SetCost (int cost) //// This should be set to 3 (check the driver). There is no case to handle any larger maximum cost to play.
{
maxCost = cost;
if (maxCost > 3) //// In case some sadistic person has any bright ideas, this ensures things stay simple.
maxCost = 3;
pay = cost + 1;
}
public void DepositCredits()
{
while (pay < 1 || pay > maxCost)
{
System.out.print ("\f");
if (pay < 1)
{
System.out.println("That is not a valid amount.");
}
System.out.println("You have " + credits + " credits.");
System.out.print("Insert 1-"+maxCost+" credits: ");
pay = Keyboard.readInt();
if (pay >= maxCost)
pay = maxCost;
if (pay > credits)
pay = 0;
}
}
public void Play()
{
System.out.print ("\f");
credits -= pay;
System.out.println("You have " + credits + " credits.");
for (int index = position1; index < generator.nextInt(10) + 5; index++) ////this simulates the spinning of the first reel
{
display[0][0] = wheel1[index % (slotvalues+1)];
display[1][0] = wheel1[(index + 1) % (slotvalues+1)];
display[2][0] = wheel1[(index + 2) % (slotvalues+1)];
position1 = index % (slotvalues + 1);
}
for (int index = position2; index < generator.nextInt(10) + 8; index++) ////this simulates the spinning of the second reel
{
display[0][1] = wheel2[index % slotvalues];
display[1][1] = wheel2[(index + 1) % slotvalues];
display[2][1] = wheel2[(index + 2) % slotvalues];
position2 = index % slotvalues;
}
for (int index = position3; index < generator.nextInt(10) + 11; index++) ////this simulates the spinning of the third reel
{
display[0][2] = wheel3[index % (slotvalues+1)];
display[1][2] = wheel3[(index + 1) % (slotvalues+1)];
display[2][2] = wheel3[(index + 2) % (slotvalues+1)];
position3 = index % (slotvalues + 1);
}
System.out.println (display[0][0] + "\t" + display[0][1] + "\t" + display[0][2]);
System.out.println (display[1][0] + "\t" + display[1][1] + "\t" + display[1][2]);
System.out.println (display[2][0] + "\t" + display[2][1] + "\t" + display[2][2]);
if (pay == 1) //only the middle row is checked for wins
{
if (display[1][0] == display[1][1] && display[1][0] == display[1][2])
win = true;
else
win = false;
}
if (pay == 2) //all three rows are only checked horizontally
{
if (display[0][0] == display[0][1] && display[0][0] == display[0][2]
|| display[1][0] == display[1][1] && display[1][0] == display[1][2]
|| display[2][0] == display[2][1] && display[2][0] == display[2][2])
win = true;
else
win = false;
}
if (pay == 3) //all three rows are checked horizontally and diagonally
{
if (display[0][0] == display[0][1] && display[0][0] == display[0][2]
|| display[1][0] == display[1][1] && display[1][0] == display[1][2]
|| display[2][0] == display[2][1] && display[2][0] == display[2][2]
|| display[0][0] == display[1][1] && display[0][0] == display[2][2]
|| display[2][0] == display[1][1] && display[2][0] == display[0][2]
|| display[2][0] == display[1][1] && display[2][0] == display[2][2]
&& display[2][0] == display[0][0] && display[2][0] == display[0][2])
win = true;
else
win = false;
}
if (win == true)
{
if (display[2][0] == display[1][1] && display[2][0] == display[2][2]
&& display[2][0] == display[0][0] && display[2][0] == display[0][2] && display[2][0] == 3)
{
credits += 100; // if the number '3' is found in the formation of an 'X'
System.out.println("Jackpot! You just won 100 credits and you now have " + credits + " credits!");
win = false;
}
}
if (win == true)
{
if (display[0][0] == display[0][1] && display[0][0] == display[0][2] && display[0][0] == 3
|| display[1][0] == display[1][1] && display[1][0] == display[1][2] && display[1][0] == 3
|| display[2][0] == display[2][1] && display[2][0] == display[2][2] && display[2][0] == 3
|| display[0][0] == display[1][1] && display[0][0] == display[2][2] && display[0][0] == 3
|| display[2][0] == display[1][1] && display[2][0] == display[0][2] && display[2][0] == 3)
{
credits += 50; // the number '3' is given priority over others and is worth more credits
System.out.println("Wow! You just won 50 credits and you now have " + credits + " credits!");
}
else
{
credits += 15; // this is when you get 3 in a row of anything other than a '3'
System.out.println("Nice. You won 15 credits and you now have " + credits + " credits.");
}
}
pay = 4;
if (credits >= 1)
{
System.out.print ("Play again? (enter \"y\" or \"n\" or enter your next credit deposit from 1-3) ");
response = Keyboard.readString();
if (response.equals("1") || response.equals("2") || response.equals("3"))
{ //these statements allow you to skip pressing 'y' in order to reinitiate the DepositCredits() method
if (response.equals("1"))
pay = 1;
if (response.equals("2"))
pay = 2;
if (response.equals("3"))
pay = 3;
response = "y";
if (pay > credits)
pay = 0;
}
}
else
{
System.out.print ("Ha ha ha, the house wins. Better luck next time with the one-armed bandit!");
response = "Game Over. Loop terminates here.";
}
}
public String Response() //this enables the driver to check the user's response for its 'while' loop
{
return response;
}
}
public class Driver
{
public static void main (String args[])
{
String answer = "y";
ClassicSlots Slot1 = new ClassicSlots(6, 100);
Slot1.SetCost(3);
while (answer.equalsIgnoreCase("y"))
{
Slot1.DepositCredits();
Slot1.Play();
answer = Slot1.Response();
}
}
}
This post has been edited by A2ZOMG: 15 April 2009 - 09:54 PM

New Topic/Question
Reply




MultiQuote




|