• (2 Pages)
• 1
• 2

## Beginning Games for N00blets: Rock, Paper, Scissors Also introduces Math's Random, a little Wrapper, and simple GUI Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=92907&amp;s=3ddad39131caec583e23f0705ae2ac71&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 NeoTifa

• Whorediot

Reputation: 3399
• Posts: 17,039
• Joined: 24-September 08

Posted 14 March 2009 - 09:37 PM

Beginning Games for N00blets by NeoTifa

Index:
Part 1: Introduction
Part 2: JOptionPane
Part 3: Basic Wrappers
Part 4: Random class
Part 5: Actual full source code

~~~~~~~~~~~~~Part 1~~~~~~~~~~~~~
Introduction:

Hello, again! I'm writing this tutorial as a sort of first step for all the beginners looking to start making games. Most beginners start out with this idea in their mind of some epic rpg or mmorpg or fps. Sorry to burst your bubbles, but that's quite an enormous feat. You need the basics. I mean, you can't really do calculus without knowing 1+1=2, right? If you look at a games code, you are going to be blown away. Take it one step at a time. This tutorial is based on my r, p, s program I wrote eons ago so it goes through all the stuff you probably should know.

~~~~~~~~~~~~Part 2~~~~~~~~~~~~~~
JOptionPane:

So, as you know, java is really good for its GUI, or Graphical User Interface. Without it, everything would be on the console, and that's just boring! So, everyone uses GUI to make their programs fancy. This window itself is considered GUI. Meh.

The first thing you need to know is... your IDE does not automatically put it into your package. You have to import it. There are actually 2 packages for GUI, but for simplicity's sake, and the fact that I only used the 1 in this program, we are only going to stick with the one (the other one is totally outdated, anyways).

Before your class decloration, you should have a statement that looks like this:

```import javax.swing.*;

```

This statement is called the import. I used the wildcard (*) because then you don't have to do a seperate import statement for every class you're tying to call. That would be super lame. It includes everything.

Next, we learn how this GUI works. For this program, you are only going to use the input dialog boxes and message dialog boxes, and the most simplest forms at that. You can look more in depth GUI concepts in the tutorial section. Now, I should state that the input dialog reads strings, so you're going to have to parse them.

Input dialogs will always be in the format:

String input = JOptionPane.showInputDialog(String s);

where input is a String variable that catches what the user inputs through the input dialog, and String s is a string (can be predefined). In my program I declared it like this:

```//prompts for user input and then parses it to uchoice
String input = JOptionPane.showInputDialog("What'll it be? Rock, paper, or scissors?\n" +
"1 for rock, 2 for paper, and 3 for scissors: ");

```

Now, it's time for just simple message dialogs. You'd think they'd be easier, but since when did java ever make sense? Tee hee hee. Where there is a null, just go with it. I'll tell you about it when you get older

JOptionPane.showMessageDialog(null, String s);

Same deal, now just with a null, and no input string.

```JOptionPane.showMessagePane(null, "Hello, World!!!");

```

Pretty lame by itself, ya? Yes, it is....

~~~~~~~~~~~~Part 3~~~~~~~~~~~~~~~
Basic Wrappers:

I'm only going to give a brief description. If you put an interger, say ... 7... into the input dialog, its going to return a String "7" instead of an int. Why, you ask? Just because. You'll find out when you're older (see a methods tutorial for further info... I don't get paid, so I'm not explaining more than I have to... it's 12:15 am....)

In order to get that "7" to be a 7, you have to parse it. How, you ask? Follow me into the land of discovery to find out!

int num = Integer.parseInt(String input);

int num is just an integer that declare to catch the parsed String input. Easy, huh? They have the same thing for double, float, long, and maybe some others. I cba to look. You'll only need the int one for now.

~~~~~~~~~~~~Part 4~~~~~~~~~~~~~~~
Random class:

No video game can go without this! This, or the random class itsself, is the most import part of interesting and compelling games. Without them, you'd have the same thing everytime you play it. No random battles, no random attack values, no random anything! Boring! So, I'm a big fan of the Math class because I'm a nerd like that. Math is part of java.lang methinks, so you don't have to inport it (Yea it is, I just checked). Unfortunatally for you, I used the Random class, which is in util.

```import java.util.*;

```

You probably have already seen that one before. It's super duper handy. Random is a class, so like all classes, you have to instantiate it. "Instantiate, you say? Wtf is that?!" To instantiate means to make an instance of, which you need to do to access that class.

```Random rnd = new Random();

```

This creates an instance of the Random class under the variable name "rnd". Pretty sweet, yea? Yea.

Alright, so want to use your new instance. But how?

int x = rnd.nextInt(int n);

int x is just a catcher for the new randomized number. You can use rnd however many times you feel like, by the way. int n is the number after the number you wish to stop at. Normally, in this format, the randomizer starts at 0, and goes to int n - 1. So, you want a random number from 0 - 2, like in this program, you would have to put a 3 in the parenthesis. Easy enough, right? Now should be able to do all kinds of sweet random numbers. Why? I don't care, but go for it .

~~~~~~~~~~~~Part 5~~~~~~~~~~~~~~~
Actual full source code:

I am sure by the time you've reached this you are probably curled up into the fetal position and sucking your thumb, so I'll reward you with the full thing for reference. ^____^ Enjoy!

This is just the basic skeleton of the program. You need to probably add stuff, and it couldn't hurt to clean it up a little bit, but it's pretty straight forward. It shows every step that every beginner should understand. Nothing special ^__^

My code might be a little shabby, but... you get the idea. All it really is is a bunch of if-else loops. Easy stuff.

```package rockpaperscissors;

/**
*
* @author Erica
* @date 9-25-08
*
* A simple program to utilize the Random class
*/

//imports
import javax.swing.*;
import java.util.*;

public class Main {

public static void main(String[] args) {

String input;					   //stores user input from JOP
int uchoice;						//parse of input; users choice
int compchoice;					 //random choice by computer

//prompts for user input and then parses it to uchoice
input = JOptionPane.showInputDialog("What'll it be? Rock, paper, or scissors?\n" +
"1 for rock, 2 for paper, and 3 for scissors: ");
uchoice = Integer.parseInt(input);

//generates random number and sets a limit
Random randomnum = new Random ();

compchoice = randomnum.nextInt(3);

//determines whether the user or computer wins
if (uchoice == 1 && compchoice == 0)
JOptionPane.showMessageDialog(null, "Tie!");

else if (uchoice == 1 && compchoice == 1)
JOptionPane.showMessageDialog(null, "Paper beats rock. You lose!");

else if (uchoice == 1 && compchoice == 2)
JOptionPane.showMessageDialog(null, "Rock beats scissors. You win!");

else if (uchoice == 2 && compchoice == 0)
JOptionPane.showMessageDialog(null, "Paper beats rock. You win!");

else if (uchoice == 2 && compchoice == 1)
JOptionPane.showMessageDialog(null, "Tie!");

else if (uchoice == 2 && compchoice == 2)
JOptionPane.showMessageDialog(null, "Scissors beats paper. You lose!");

else if (uchoice == 3 && compchoice == 0)
JOptionPane.showMessageDialog(null, "Rock beats Scissors. You lose!");

else if (uchoice == 3 && compchoice == 1)
JOptionPane.showMessageDialog(null, "Scissors beats paper. You win!");

else if (uchoice == 3 && compchoice == 2)
JOptionPane.showMessageDialog(null, "Tie!");

}

}
```

See ya around, neighbor! *gets shot*

Is This A Good Question/Topic? 4

## Replies To: Beginning Games for N00blets: Rock, Paper, Scissors

### #2 spikesanguine

Reputation: 1
• Posts: 16
• Joined: 10-October 08

Posted 05 April 2009 - 05:30 PM

You might consider using a case/switch structure instead of a million else ifs. Nice humor, might be hard for those with a basic knowledge. None the less, thanks. They're also not loops, they're a decision structure, not a iterative structure. Not to be critical, other than that, thanks for the tutorial!

This post has been edited by spikesanguine: 05 April 2009 - 05:46 PM

### #3 NeoTifa

• Whorediot

Reputation: 3399
• Posts: 17,039
• Joined: 24-September 08

Posted 06 April 2009 - 08:28 AM

Your welcome. I had it this way on purpose. Most n00bs know if-else statements before switch. Besides, like I said, this is a very basic skeleton. If they're gonna use my code, they're going to have to work for it It allows max customization methinks.

### #4 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

Posted 18 April 2009 - 03:22 AM

spikesanguine, on 5 Apr, 2009 - 04:30 PM, said:

You might consider using a case/switch structure instead of a million else ifs. Nice humor, might be hard for those with a basic knowledge. None the less, thanks. They're also not loops, they're a decision structure, not a iterative structure. Not to be critical, other than that, thanks for the tutorial!

Or she could use Scala and have Pattern Matching.

Yeah, I went there.

### #5 luigiplumbersinc

Reputation: 4
• Posts: 82
• Joined: 16-February 09

Posted 20 May 2010 - 06:09 PM

i disagree with the whole thing... the structure of rock paper scissors is a circular inequality you should use a data type that matches this... not ints that cant even be equated because 1 == 0, 2 == 1 and 3 == 2

### #6 Javaeagle

Reputation: 2
• Posts: 14
• Joined: 11-April 10

Posted 01 June 2010 - 03:14 PM

As was already said, it was meant to be a basic topic. Yes, the "if-then" parts get a bit lengthy, but the program is still reasonable. By using "if-then" statements, the code is easier to understand, if longer, in order to work at the general concept.

### #7 NeoTifa

• Whorediot

Reputation: 3399
• Posts: 17,039
• Joined: 24-September 08

Posted 01 June 2010 - 05:33 PM

It's more for logic than efficiency. -__- You don't want to go into some serious stuff in a n00b thread.

### #8 Zekorov

Reputation: 22
• Posts: 226
• Joined: 16-May 10

Posted 23 June 2010 - 03:09 PM

Thanks for the post NeoTifa. cause you're a ninja XD

Although, I have a question here. it's quick. Why, in your skeleton code, do you put "package rockpaperscissors;"? is it because there is more than just one class inside the whole program for all the GUI and then the actual program function, or "package"? what exactly is a package in this context if not?
If anyone can help with that, thanks in advance. XD

### #9 NeoTifa

• Whorediot

Reputation: 3399
• Posts: 17,039
• Joined: 24-September 08

Posted 24 June 2010 - 10:06 AM

It was a quick and dirty program I made in Netbeans. I opened a new project for it, and it auto-filled that in there. It's useless, you can take it out if you'd like.

### #10 Zekorov

Reputation: 22
• Posts: 226
• Joined: 16-May 10

Posted 26 June 2010 - 03:43 PM

oh i see.... lol i was wondering because i've never seen that before in a program. like in any of the examples i saw during my little 4 month programming class. so yeah, thanks for the reply.

### #11 Zekorov

Reputation: 22
• Posts: 226
• Joined: 16-May 10

Posted 26 June 2010 - 04:41 PM

ok, i've got two questions. first, is there a way to encode animations of scissors, rock and paper and them doing their respective things when you lose, win or tie? if so what would you need to use?
second, i have a game of my own. it's very simple and it was an assignment for my computer programming class during the school year. however, i'd like to know how to make it to where everything will appear in a GUI like the rock, paper, scissors game does. Here is the code to my game, which is called the HighLow game, because the computer generates a number that's totally random between 1 and 100 and then every time you guess a number, it tells you if the number you got was the number the computer generated. if it wasn't, the computer tells you if you entered a number too high or too low. at the end, when you finally get the number right, it tells you how many times it took you to guess the right number.
```//**********************************
//**********************************
import java.util.*;

public class HighLow
{
public static void main(String[] args)
{
int number, guess, Guesscount = 0;

Random generator = new Random( );
Scanner scan = new Scanner(System.in);

number = generator.nextInt(100) + 1;

System.out.println("Guess a number between 1 and 100: ");
guess = scan.nextInt( );

Guesscount = Guesscount + 1;

while (guess != 0)
{
if (guess <= number)
{
if (guess == number)
{
System.out.println("You guessed right! Congratulations");
System.out.println("It took you " + Guesscount + " guesses to figure out the number.");
number = generator.nextInt(100) + 1;
System.out.println( );
System.out.println("Guess a number between 1 and 100 (0 to quit): ");
guess = scan.nextInt( );
Guesscount = Guesscount + 1;
}
else
{
System.out.println("Your guess was too low. Try again? (0 to quit) ");
guess = scan.nextInt( );
Guesscount = Guesscount + 1;
}
}
else
{
System.out.println("Your guess was too high. Try again? (0 to quit) ");
guess = scan.nextInt( );
Guesscount = Guesscount + 1;
}
}
}
}

```

Now, my question is, what would i need to add to this to make it in a little panel or window, or GUI, of its own?

### #12 NeoTifa

• Whorediot

Reputation: 3399
• Posts: 17,039
• Joined: 24-September 08

Posted 27 June 2010 - 09:55 AM

Javax.Swing and Graphics. I am about half done with my Swing tutorial, and I had planned on making a Graphics tutorial. Check out some of Sun's tutorials. They're really good.

### #13 Zekorov

Reputation: 22
• Posts: 226
• Joined: 16-May 10

Posted 27 June 2010 - 10:23 AM

alright thanks

### #14 Zekorov

Reputation: 22
• Posts: 226
• Joined: 16-May 10

Posted 27 June 2010 - 11:14 AM

just a simple question..... is JOptionPane like a predefined java GUI that you can use to just do text and stuff without having to make a frame and everything? because i've been looking at a GUI tutorial and you're supposed to make a frame and all this stuff. yet all you did was use JOptionPane. so i guessed that it was the basic of basic GUI. frames and everything else would only be used if you were to include anything more complicated, like animations and such correct?

### #15 NeoTifa

• Whorediot

Reputation: 3399
• Posts: 17,039
• Joined: 24-September 08

Posted 28 June 2010 - 06:22 AM

Yea, I think it's a child of MessageBox or something. They're already put together for you and are super easy to use. They're also super customizable too, as a tutorial somewhere on here explains. Check it out for more JOptionPane questions, it's really good. Helped me out a lot.

http://www.dreaminco...ialogs-in-java/ <-- found it

This post has been edited by NeoTifa: 28 June 2010 - 06:25 AM