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 !


Sign In
Create Account



Back to top










