9 Replies - 17807 Views - Last Post: 08 July 2010 - 08:45 PM Rate Topic: -----

#1 HanSky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-October 08

Slot Machine

Posted 07 November 2008 - 11:48 PM

Hi i'm trying to make a slot machine game using java.Can someone give me an example of a slot machine game?
Is This A Good Question/Topic? 0
  • +

Replies To: Slot Machine

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Slot Machine

Posted 08 November 2008 - 10:29 AM

http://wizardofodds....lay/slot-fruit/
Was This Post Helpful? 0
  • +
  • -

#3 HanSky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-October 08

Re: Slot Machine

Posted 08 November 2008 - 11:55 PM

Ermm i mean the code.Sorry
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Slot Machine

Posted 09 November 2008 - 12:42 AM

[rules][/rules]

This post has been edited by n8wxs: 09 November 2008 - 12:43 AM

Was This Post Helpful? 1

#5 Guest_LP78*


Reputation:

Re: Slot Machine

Posted 08 July 2010 - 02:07 AM

View Postn8wxs, on 08 November 2008 - 11:42 PM, said:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.


Hello, I also run into the same problem with slotMachine program. I am trying to learn Java through the free video Stanford University posted online. This is what I wrote in my source code:

public void run() {

int reel1 = rgen.nextInt(1, 6);
int reel2 = rgen.nextInt(1, 6);
int reel3 = rgen.nextInt(1, 7);

println( reel1 + " " + reel2 + " " + reel3);
}
private RandomGenerator rgen = RandomGenerator.getInstance();


My question is: is there a way for me to replace the integer random result in each reel with a text string like 1 = CHERRY, 2 = LEMON and so on? That way, I can get the text result to display like this: PLUM LEMON CHERRY.

I tried using switch statement like the following:
switch (reel#)
case 1: println("CHERRY"); break;
case 2: println("LEMON"); break;
and so on....However, the result print vertical on the canvas. I want the result of the three reel to display horizontally in text.

Thanks in advance for your help!
Was This Post Helpful? 0

#6 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Slot Machine

Posted 08 July 2010 - 08:28 AM

Use:

System.out.print()


instead of:

System.out.println()

Was This Post Helpful? 0
  • +
  • -

#7 Guest_LP78*


Reputation:

Re: Slot Machine

Posted 08 July 2010 - 11:51 AM

View PostFib, on 08 July 2010 - 07:28 AM, said:

Use:

System.out.print()


instead of:

System.out.println()

Was This Post Helpful? 0

#8 Guest_LP78*


Reputation:

Re: Slot Machine

Posted 08 July 2010 - 12:06 PM

Fib,

Thanks for your reply. I tried using System.out.print but the canvas doesn't display the text, so i tried print() and it does display the text. I now have a problem with the way the three reel displays on the screen. The print display like this: CHERRYORANGELEMON. I could not get the three text to separate even though i tried to include a blank space " " in my println( reel1 + " " + reel2 + " " reel3). Below is my complete code:

public void run() {

int reel1 = rgen.nextInt(1, 6);
switch (reel1){
case 1: print("CHERRY"); break;
case 2: print("ORANGE"); break;
case 3: print("LEMON"); break;
case 4: print("PLUM"); break;
case 5: print("BELL"); break;
case 6: print("BAR"); break;
}

int reel2 = rgen.nextInt(1, 6);
switch (reel2){
case 1: print("CHERRY"); break;
case 2: print("ORANGE"); break;
case 3: print("LEMON"); break;
case 4: print("PLUM"); break;
case 5: print("BELL"); break;
case 6: print("BAR"); break;
}

int reel3 = rgen.nextInt(1, 7);
switch (reel3){
case 1: print("CHERRY"); break;
case 2: print("ORANGE"); break;
case 3: print("LEMON"); break;
case 4: print("PLUM"); break;
case 5: print("BELL"); break;
case 6: print("BAR"); break;
}
}
private RandomGenerator rgen = RandomGenerator.getInstance();


So, base on the code above, the canvas will just automatically print: CHERRYORANGELEMON with no spacing in between. Is there a way for me to separate the text without actually adding the extra space behind each text like "CHERRY "?

Thank you very much for all your help!
Was This Post Helpful? 0

#9 Guest_LP78*


Reputation:

Re: Slot Machine

Posted 08 July 2010 - 12:45 PM

Does anyone know how I could program this using the toString method? I read the Art & Science of Java chapter 6 and am still confused about how toString method can be applied to this slotMachine program. I suppose it has something to do with replacing the random value with the string CHERRY, PLUM, ORANGE, etc.

I would really appreciate it if someone can demonstrate how i can change my program below to toString. This thing is bugging me for a few days now and I can't solve it. Thanks!
Was This Post Helpful? 0

#10 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Slot Machine

Posted 08 July 2010 - 08:45 PM

View PostLP78, on 08 July 2010 - 02:45 PM, said:

Does anyone know how I could program this using the toString method? I read the Art & Science of Java chapter 6 and am still confused about how toString method can be applied to this slotMachine program. I suppose it has something to do with replacing the random value with the string CHERRY, PLUM, ORANGE, etc.

I would really appreciate it if someone can demonstrate how i can change my program below to toString. This thing is bugging me for a few days now and I can't solve it. Thanks!

Hi, you revived a thread from 2008 to ask a new question. Next time please start a new thread. Also please use code tags to post your code (it makes it a lot easier to read :)).

:code:

For your first problem the simplest way to solve it is indeed just to put a literal space after each print statement, eg. print("CHERRY ");.

As for your second problem, have you looked at using enums? Basically they are constants but they implement toString() so you can just say something like System.out.print(Slot.CHERRY) to print out "CHERRY". You could also override the enum's toString() to print whatever you want.

http://devdaily.com/...string-tutorial

Edit: Also, your code could be cleaned up a bit.

	public void run() 
	{
		for (int i = 0; i < 3; ++i)
		{
			int reel = rgen.nextInt(1, 6);
			
			switch (reel)
			{
			case 1: print("CHERRY"); break;
			case 2: print("ORANGE"); break;
			case 3: print("LEMON"); break;
			case 4: print("PLUM"); break;
			case 5: print("BELL"); break;
			case 6: print("BAR"); break;
			}

			// Print space after each word.
			print(" ");
		}
	}


This post has been edited by Tom9729: 08 July 2010 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1