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...
We started off by building the main since it will take up more of 'my' time...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);
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...
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: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); } }
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.Code:if(Play.AL.get(0).equals(WinStone(Game))) {
To does you might have noticed that I every choice put...
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.Code:Play.al.set(0,"Stone");
Let's dive into the method of our main
That was our rather large method to check our winnersCode: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; } }
To give you guys some info, when we want to check for our winner. I compared the positions and the info inside.
And the user choose stone it will respond with the message...Code:if(Play.Al.get(0).equals("Stone"))
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?Code:showMessageDialog(null,"It ended in a tie");
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...
This small object is what we could call "dumb" Al, well not really but I would consider it that.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,""); } }
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...
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.Code:Collections.shuffle(list<>);
Main
Object.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; } }
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.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,""); } }
If you need some help or "extra" details of what I did and why also functions. I will gladly help and reply.
Cheers everyone !
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"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
cool .. i like it .. +rep![]()
Very nice tutorial. Shouldn't it be "Rock-Paper-Scissors" rather than "Stone"?
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 !
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.)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks