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
~~~~~~~~~~~~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: ");
Not too bad, eh?
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
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*









MultiQuote









|