Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
Photo
- - - - -

Game - stone,paper and scissor


  • Please log in to reply
14 replies to this topic

#1 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 26 December 2008 - 01:50 PM

Hey there C.C, today am going to present you guys my simple template of the game called; stone,paper and scissor.


So it won't become something big or so, anyways let's get busy !


First part initialize our code and declaring it...
//Our packages we need to build up the template...
import static javax.swing.JOptionPane.*; 
//-note- importing a package statical will result;
every new line to be exact same "value & w/e", also note it is
for the JOptionPane part... so don't need to type it everytime :)
import java.util.*;

public class template {
   public static void main(String[] arg {

                ArrayList<String> Game = new ArrayList<String>();
		 Rule Play= new Rule(Game,Game);

We started off by building the main since it will take up more of 'my' time...
The ArrayList we are creating is a template to the constructor from our object that we will work on in the end to make the program work...
Afterward are we creating a bridge from object - main class...So we can have a great time with it ;)

Next step will be to make our options...
while(true) {
Play = new Rule(Game,Game); //Side note will tell why I did this...
String Choice = showInputDialog(null,"So what shall it be?\n1.Stone\n2.Paper
\n3.Scissors\n4.End Game");

if(Choice.equals("1")) {
      Play.al.set(0,"Stone");
            showMessageDialog(null,"Your choice was "+Play.al.get(0)+" the
computers choice was "+Play.AL.get(0));
if(Play.AL.get(0).equals(WinStone(Game))) {
    return;
    }
}
else if(Choice.equals("2")) {
      Play.al.set(1,"Paper");
            showMessageDialog(null,"Your choice was "+Play.al.get(1)+" the 
computers choice was "+Play.AL.get(1));
if(Play.AL.get(1).equals(WinPaper(Game))) { 
   return;
   }
}
else if(Choice.equals("3")) {
      Play.al.set(2,"Scissor")) {
            showMessageDialog(null,"Your choice was "+Play.al.get(2)+" the
computers choice was "+Play.AL.get(2));
if(Play.AL.get(2).equals(WinScissor(Game))) {
   return;
   }
}
else if(Choice.equals("4")) {
showMessageDialog(null,"The game has been terminated");
System.exit(0);
    }
}

We did a rather large code stripes, however this part was our game choices. As we do everytime a choice it will check for who wins and looses.
if(Play.AL.get(0).equals(WinStone(Game))) {
This is our method listener, which will check all the time with our methods criteria, we will soon go and have a look in our method.
To does you might have noticed that I every choice put...
Play.al.set(0,"Stone");
That is your choice in the arraylist we have in our object that I will return to...later on. However a small explanation; the 'Play' part is our "link" to the object that we have bridged together with the main and 'al' is our arraylist we have in the object. So by using 'set' we can decide what and where it should be in, this time the position and what it shall contain...side note we are using only ArrayList<String> so every position will contain an empty string, if you didn't declare or what so.

Let's dive into the method of our main :)

public static ArrayList<String> WinStone(ArrayList<String Game) {
         for(int i=0; i<Play.AL.size(); i++) {
          if(Play.AL.get(0).equals("Scissor")) {
  showMessageDialog(null,"You won !");
break;
}
          else if(Play.AL.get(0).equals("Stone")) {
  showMessageDialog(null,"It ended in a tie !");
break;
}
          else if(Play.AL.get(0).equals("Paper")) {
  showMessageDialog(null,"The computer won !");
break;
}
}
return null;
}
public static ArrayList<String> WinPaper(ArrayList<String> Game) {
        for(int j=0; j<Play.AL.size(); j++) {
          if(Play.AL.get(1).equals("Stone")) {
  showMessageDialog(null,"You won !");
break;
}
          else if(Play.AL.get(1).equals("Paper")) {
  showMessageDialog(null,"It ended in a tie !");
break;
}
          else if(Play.AL.get(1).equals("Scissor")) {
  showMessageDIalog(null,"The computer won !);
break;
}
}
return null;
}
public static ArrayList<String> WinScissor(ArrayList Game) {
         for(int k=0; k<Play.AL.size(); k++) {
          if(Play.AL.get(2).equals("Paper")) {
   showMessageDialog(null,"You won !");
break;
}
         else if(Play.AL.get(2).equals("Scissor")) {
  showMessageDialog(null,"It ended in a tie !");
break;
}
         else if(Play.AL.get(2).equals("Stone")) {
  showMessageDialog(null,"The computer won !");
break;
}
}
return null;
}
}

That was our rather large method to check our winners :)
To give you guys some info, when we want to check for our winner. I compared the positions and the info inside.
if(Play.Al.get(0).equals("Stone"))
And the user choose stone it will respond with the message...
showMessageDialog(null,"It ended in a tie");
However this is because we are looping through the whole list and checking with our criteria that in our first position contains 'stone' and the user chooses 'stone' it will end in a tie. Then how do the method knows what 'AL' is?
Simple as pie, we are creating a reference to it. I will tell you guys later on when we start making our object and our object won't be something big or so but something rather simple :)

Now let's begin with our object !
To start off I will explain why I did in this way. I found it simple and neat to have it this way and I will show it now...

Object...

import java.util.*;
class Rule {
static ArrayList<String> AL = new ArrayList<String>();
static ArrayList<String> al = new ArrayList<String>();

//Non given constructor
Rule() {
AL = null;
al = null;
}
//given constructor
public Leken(ArrayList<String> LA, ArrayList<String> la) {
//this part isn't really needed but incase you want to know
where and what your doing and going keep these :)
AL = LA;
al = la;

AL.add(0,"Stone");
AL.add(1,"Scissor");
AL.add(2,"Paper");
Collections.shuffle(AL);


//This part is the users non given anything, we will set the containing later
when we are running the main :)
al.add(0,"");
al.add(1,"");
al.add(2,"");
     }
}
This small object is what we could call "dumb" Al, well not really but I would consider it that.
The computers choice is automated and will be randomized everytime we want to use them.
The randomizer is java's build in component in util library...
Collections.shuffle(list<>);

Also now when we are here at the object, I will tell how we could mix with values in our mains method. By giving our objects variables a static value means following. If we didn't have static and used the variable it would complain, due to the value we have in our main isn't the same as in the object. But if we allow it to be statical the result would mean, every 5 created will be exactly the same. So 5 in main will equal the 5 in the object.

Main
import static javax.swing.JOptionPane.*;
import java.util.*;

public class template {
   public static void main(String[] arg {

                ArrayList<String> Game = new ArrayList<String>();
		 Rule Play= new Rule(Game,Game);

while(true) {
        Play = new Rule(Game,Game); //Side note will tell why I did this...
String Choice = showInputDialog(null,"So what shall it be?\n1.Stone\n2.Paper
\n3.Scissors\n4.End Game");

if(Choice.equals("1")) {
      Play.al.set(0,"Stone");
            showMessageDialog(null,"Your choice was "+Play.al.get(0)+" the
computers choice was "+Play.AL.get(0));
if(Play.AL.get(0).equals(WinStone(Game))) {
    return;
    }
}
else if(Choice.equals("2")) {
      Play.al.set(1,"Paper");
            showMessageDialog(null,"Your choice was "+Play.al.get(1)+" the 
computers choice was "+Play.AL.get(1));
if(Play.AL.get(1).equals(WinPaper(Game))) { 
   return;
   }
}
else if(Choice.equals("3")) {
      Play.al.set(2,"Scissor")) {
            showMessageDialog(null,"Your choice was "+Play.al.get(2)+" the
computers choice was "+Play.AL.get(2));
if(Play.AL.get(2).equals(WinScissor(Game))) {
   return;
   }
}
else if(Choice.equals("4")) {
showMessageDialog(null,"The game has been terminated");
System.exit(0);
    }
}
public static ArrayList<String> WinStone(ArrayList<String Game) {
         for(int i=0; i<Play.AL.size(); i++) {
          if(Play.AL.get(0).equals("Scissor")) {
  showMessageDialog(null,"You won !");
break;
}
          else if(Play.AL.get(0).equals("Stone")) {
  showMessageDialog(null,"It ended in a tie !");
break;
}
          else if(Play.AL.get(0).equals("Paper")) {
  showMessageDialog(null,"The computer won !");
break;
}
}
return null;
}
public static ArrayList<String> WinPaper(ArrayList<String> Game) {
        for(int j=0; j<Play.AL.size(); j++) {
          if(Play.AL.get(1).equals("Stone")) {
  showMessageDialog(null,"You won !");
break;
}
          else if(Play.AL.get(1).equals("Paper")) {
  showMessageDialog(null,"It ended in a tie !");
break;
}
          else if(Play.AL.get(1).equals("Scissor")) {
  showMessageDIalog(null,"The computer won !);
break;
}
}
return null;
}
public static ArrayList<String> WinScissor(ArrayList Game) {
         for(int k=0; k<Play.AL.size(); k++) {
          if(Play.AL.get(2).equals("Paper")) {
   showMessageDialog(null,"You won !");
break;
}
         else if(Play.AL.get(2).equals("Scissor")) {
  showMessageDialog(null,"It ended in a tie !");
break;
}
         else if(Play.AL.get(2).equals("Stone")) {
  showMessageDialog(null,"The computer won !");
break;
}
}
return null;
}
}
Object.
import java.util.*;
class Rule {

static ArrayList<String> AL = new ArrayList<String>();
static ArrayList<String> al = new ArrayList<String>();

Rule() {
AL = null;
al = null;
}
public Leken(ArrayList<String> LA, ArrayList<String> la) {
AL = LA;
al = la;

AL.add(0,"Stone");
AL.add(1,"Scissor");
AL.add(2,"Paper");
Collections.shuffle(AL);

al.add(0,"");
al.add(1,"");
al.add(2,"");
     }
}

This is pretty much the game template, hopefully you guys will understand my explanations of how things work and so here with this program and sorry if it's kinda bloated with text.
If you need some help or "extra" details of what I did and why also functions. I will gladly help and reply.
Cheers everyone !
  • 3

#2 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2,764 posts

Posted 03 January 2009 - 05:18 PM

such a nice tutorial!
why is no one commenting on it, it seems really neat +rep
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 04 January 2009 - 12:33 AM

such a nice tutorial!
why is no one commenting on it, it seems really neat +rep


Thank you :)
  • 0

#4 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,155 posts

Posted 04 January 2009 - 05:17 AM

cool .. i like it .. +rep :)
  • 0

#5 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 04 January 2009 - 06:51 AM

cool .. i like it .. +rep :)


Thank you :D
  • 0

#6 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 04 January 2009 - 08:48 AM

Very nice tutorial. Shouldn't it be "Rock-Paper-Scissors" rather than "Stone"?
  • 0

#7 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 04 January 2009 - 09:10 AM

Very nice tutorial. Shouldn't it be "Rock-Paper-Scissors" rather than "Stone"?


Oh uhm :x, well I did a minor error in giving it a proper name then :)
  • 0

#8 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 04 January 2009 - 10:58 AM

I've been thinking about this game, almost to the point of recreating it myself. If you store the user selections and calculate the percentage of selections you can make a very basic AI. For instance. I am playing your game (I'll just include my selections) and I make these selections:

Paper
Paper
Rock
Scissors
Paper
Scissors
Scissors
Rock
Paper

I selected paper 4 times, rock 2 times and scissors 3 times. This means I selected

Paper 4/9 or 44% of the time
Rock 2/9 or 22% of the time
Scissors 3/9 or 33% of the time

Based on these calculations you can then estimate the chance that I will pick something and select the choice that may beat it. Something to consider adding. :)
  • 0

#9 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 04 January 2009 - 11:47 AM

I've been thinking about this game, almost to the point of recreating it myself. If you store the user selections and calculate the percentage of selections you can make a very basic AI. For instance. I am playing your game (I'll just include my selections) and I make these selections:

Paper
Paper
Rock
Scissors
Paper
Scissors
Scissors
Rock
Paper

I selected paper 4 times, rock 2 times and scissors 3 times. This means I selected

Paper 4/9 or 44% of the time
Rock 2/9 or 22% of the time
Scissors 3/9 or 33% of the time

Based on these calculations you can then estimate the chance that I will pick something and select the choice that may beat it. Something to consider adding. :)


Indeed, I have to tell you a secret am creating already an Al for it :)
Your scores and calculations will be added, I am now gonna make a so called "Java mini games" where I will add black jack game and this and a maze game also a simple tennis game; but those will take time since I have lot to do, so I can enter a nice University :X
Thanks for your nice advice !
  • 0

#10 sourlemon

sourlemon

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 93 posts

Posted 05 January 2009 - 09:27 PM

Wow, this looks so complicated. I'm trying this out, but I'm running into a few problems.

1. For the "Rule" class, what is Leken? Is that a method? If so, is it suppose to return something? If not, shouldn't it be void?

2. I have the following errors:
template.java:40: illegal start of expression
public static ArrayList<String> WinStone(ArrayList<String Game) {
^
template.java:91: ';' expected
}
^
2 errors

-----------------------------------------
Also, I think you forgot to dot your i :P (compile it and you'll see what I mean.)
  • 0

#11 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 05 January 2009 - 11:31 PM

Wow, this looks so complicated. I'm trying this out, but I'm running into a few problems.

1. For the "Rule" class, what is Leken? Is that a method? If so, is it suppose to return something? If not, shouldn't it be void?

2. I have the following errors:
template.java:40: illegal start of expression
public static ArrayList<String> WinStone(ArrayList<String Game) {
^
template.java:91: ';' expected
}
^
2 errors

-----------------------------------------
Also, I think you forgot to dot your i :P (compile it and you'll see what I mean.)



It's the constructor...

The game works out of the box, you might have done it as one main, however the Rule is it own object/class...
  • 0

#12 Megaman163

Megaman163

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 09 January 2009 - 04:55 AM

This one was the code you showed me yesterday lol :X
  • 0