Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,460 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,653 people online right now. Registration is fast and FREE... Join Now!




Beginning Games for N00blets: Rock, Paper, Scissors

 
Reply to this topicStart new topic

> Beginning Games for N00blets: Rock, Paper, Scissors, Also introduces Math's Random, a little Wrapper, and simple GUI

NeoTifa
Group Icon



post 14 Mar, 2009 - 08:37 PM
Post #1


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 tongue.gif 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:

CODE

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:

CODE

//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 wink2.gif

JOptionPane.showMessageDialog(null, String s);

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

CODE

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 wink2.gif (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.

CODE

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.

CODE

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 tongue.gif.

~~~~~~~~~~~~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.

CODE
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*
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

spikesanguine
*



post 5 Apr, 2009 - 04:30 PM
Post #2
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. biggrin.gif 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: 5 Apr, 2009 - 04:46 PM
Go to the top of the page
+Quote Post

NeoTifa
Group Icon



post 6 Apr, 2009 - 07:28 AM
Post #3
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 tongue.gif It allows max customization methinks.
Go to the top of the page
+Quote Post

Raynes
Group Icon



post 18 Apr, 2009 - 02:22 AM
Post #4
QUOTE(spikesanguine @ 5 Apr, 2009 - 04: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. biggrin.gif 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.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 02:18AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month