+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Game - stone,paper and scissor

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Game - stone,paper and scissor

    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...
    Code:
    //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...
    Code:
    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.
    Code:
    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...
    Code:
    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

    Code:
    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.
    Code:
    if(Play.Al.get(0).equals("Stone"))
    And the user choose stone it will respond with the message...
    Code:
    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...

    Code:
    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...
    Code:
    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
    Code:
    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.
    Code:
    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 !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Game - stone,paper and scissor

    such a nice tutorial!
    why is no one commenting on it, it seems really neat +rep
    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Game - stone,paper and scissor

    Quote Originally Posted by amrosama View Post
    such a nice tutorial!
    why is no one commenting on it, it seems really neat +rep
    Thank you

  5. #4
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: Game - stone,paper and scissor

    cool .. i like it .. +rep

  6. #5
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Game - stone,paper and scissor

    Quote Originally Posted by Egz0N View Post
    cool .. i like it .. +rep
    Thank you

  7. #6
    Jordan Guest

    Re: Game - stone,paper and scissor

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

  8. #7
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Game - stone,paper and scissor

    Quote Originally Posted by Jordan View Post
    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

  9. #8
    Jordan Guest

    Re: Game - stone,paper and scissor

    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.

  10. #9
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Game - stone,paper and scissor

    Quote Originally Posted by Jordan View Post
    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 !

  11. #10
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Re: Game - stone,paper and scissor

    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 (compile it and you'll see what I mean.)

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Rosetta Stone Alternative
    By ki4jgt in forum General Programming
    Replies: 3
    Last Post: 10-18-2010, 01:59 PM
  2. Rock Paper Scissors!!
    By Siten0308 in forum CSharp Tutorials
    Replies: 5
    Last Post: 11-22-2009, 07:17 AM
  3. Stepping Stone!
    By anix in forum Python
    Replies: 2
    Last Post: 02-02-2009, 12:10 PM
  4. Choosing a topic for a research paper
    By gammaman in forum General Programming
    Replies: 3
    Last Post: 01-25-2009, 04:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts