Hey guys, this is my first post here and I need a lot of help. I'd like to start off by saying that I'm VERY new to this so I might have some really obvious errors in here/my code might not make sense(i hope this is not the case) and yes I have added some comments to help you guys help me. :D

I'm in a]Computer Science class at my high school and we are supposed to make a game of Rock Paper Scissors that has some form of computer AI in it. The only rules are that it must:
1) Have some form of AI
2) In 4 turns, you must have used at least one of every option (Rock Paper or Scissors)
If it violates this rule, there is a # of points penalty taken away.

Here's what my thought process is:
1) Have two parallel arrays. One will be the computer's input and the other will be the
User's input. The arrays will be int arrays with 4 slots. With each choice a value
will be given.(Rock=1,Paper=3,Scissors=9)
2) The computer AI will take the sum of the last 3 values played, and because each value
is unique (i.e a value of 21 would be Two Scissors and One Paper) it will know what
it has to play in order to (A)not violate rule #2 and (B) beat the other person's
choices because it will also search their array sum.

So, here's my problem:

1) I'm not very skilled with arrays, and I don't know how to (take the sum) or how to
search through the arrays to check the values.

2) Also, once I have looped through 4 rounds of the game (i.e my array lists are full)
I am not sure how to delete the 1st input(userinput) or (computerinput) in order
for the AI to keep refreshing and basing it's decisions off of new information.

OKAY: So here's my code. If there are undefined variables, most likely it's because it was an idea I was thinking about trying, but couldn't figure out how to make it work.

``````import java.io.*;
import java.util.*;
public class PlayGame
{
static int index=4;
static int y;
static int numGames=0;
static int Userinput[]=new int[index];
static int Compinput[]=new int[index];
static int Rock=1;
static int Paper=3;
static int Scissors=9;
static boolean rock,paper,scissors;

public static void main(String args[])
{
//set up scanner
Scanner RPS = new Scanner(System.in);

//set up strings
System.out.println("Welcome to the Rock Paper Scissors Game\n");
System.out.println("Are you ready to play?   y/n");

//User defines if they are ready to play
//Set input as a char for a switch
String WouldYouPlay = RPS.nextLine();
char YesOrNo = WouldYouPlay.charAt(0);

//Define Switch
switch(YesOrNo)
{
case 'y':
PlayGame.RockPaperScissors();
break;
case 'n':
System.out.println("Goodbye!");
break;
}
}

public static void RockPaperScissors()
{
//set up another scanner
//Computer outputs a random number, either 0,1,or 2
//if the comp outputs a 0 it equals Rock, and puts a 1 in the 1st spot of the comp array
//if the comp outputs a 1 it equals Paper, and puts a 3 in the 1st spot of the comp array
//if the comp outputs a 2 it equals Scissors, and puts a 9 in the 1st spot of the comp array
int CompStart = (int)(Math.random()*3);
if(CompStart==0)
{
Compinput=Rock;
System.out.println("My choice is Rock");
}
else if(CompStart==1)
{
Compinput=Paper;
System.out.println("My choice is Paper");
}
else if(CompStart==2)
{
Compinput=Scissors;
System.out.println("My choice is Scissors");
}

for(int xx=0;xx<4;xx++)
{
char RPSchoice= RockPaperScissors.charAt(0);
if(xx==0)
{
y=0;
}
else if(xx==1)
{
y=1;
}
else if(xx==2)
{
y=2;
}
else if(xx==3)
{
y=3;
System.out.println("Last slot of array, (xx==3) it needs to loop over again from this point");
//when xx==3, its on the fourth slot in the array (y) (I was checking to see if this would work may not be necessary.
}
switch(RPSchoice)
{
case 'R':
case 'r':
Userinput[y]=Rock;
System.out.println("You chose Rock!");
boolean rock=true;//an idea that i was messing with, may not be necessary: SEE COMPUTER AI
ComputerAI();//if you enter an 'r' it will call in the computer AI to find out what to play next
break;
case 'P':
case 'p':
Userinput[y]=Paper;
boolean paper=true;
System.out.println("You chose Paper!");
ComputerAI();
break;
case 'S':
case 's':
Userinput[y]=Scissors;
boolean scissors=true;
System.out.println("You chose Scissors!");
ComputerAI();//if you enter an 's' it will call in the computer AI to find out what to play next
break;
}

}

}
public static void ComputerAI()
{
if(rock=true)//checks to see if the User selected 'r'. If the user did, then it will search it's own array to see if it needs to play
{			//something mandatory, if not it will play paper
if(Compinput.equals())
{
}

}
}
}``````

why it formatted weird.

Could be your tabs are set differently than the forums. Can change your tabs to spaces?

I don't know how to (take the sum) or how to
search through the arrays to check the values.

Write a simple program that works with a loop and an …

Have you tried writing the array and loops as I suggested?
Change the array I posted from String to an int array and search through it.
int[] anArray = {22,3,11,44}; // an int array

Write a loop to search …

My question was HOW to search through an array... I wanted you to tell me so I could apply that knowledge in my game.

simple... just use a loop
pseudo:

``````for(int i=0;i<array.length();i++){
if(array[i]==11){
//do something
}
}``````
``Compinput[y``

What i don't know is the loop structure i need to keep constantly updating my array . I know the basic idea but i don't know the code for it.

what do you mean by update? change the content? shouldn't you just assign another value to it

## All 30 Replies

Sorry, I don't know why it formatted weird.
Bump

why it formatted weird.

Could be your tabs are set differently than the forums. Can change your tabs to spaces?

I don't know how to (take the sum) or how to
search through the arrays to check the values.

Write a simple program that works with a loop and an array and learn how you can use them.
Define a String array something like this:
String[] anArray = {"this", "is", "an", "array"};
and then write a loop to go through the array and print out each String in the array.
Then another loop to look for the String that is equal to "an" and print out the index of that String in the array.

What is an example of searching through an array for something. For example: if i wanted to search the array for a value of 1(rock) 3(paper) 9(scissors)?

Have you tried writing the array and loops as I suggested?
Change the array I posted from String to an int array and search through it.
int[] anArray = {22,3,11,44}; // an int array

Write a loop to search this array for 11 and print its index.

My question was HOW to search through an array... I wanted you to tell me so I could apply that knowledge in my game.

My question was HOW to search through an array... I wanted you to tell me so I could apply that knowledge in my game.

simple... just use a loop
pseudo:

``````for(int i=0;i<array.length();i++){
if(array[i]==11){
//do something
}
}``````

ok wow that is really simple. thanks :D

now for part 2: If i wanted to sum up integers in the array what would be the most efficient way to do that?

Still simple... use a loop again

``````int sum;
for(int i=0;i<array.length();i++){
sum=sum+array[i];
}``````
commented: Spoonfeeding - doing the OPs work -3

OMG I'M SO GLAD YOUR ONLINE :) okay i've got updated code if you'd care to take a look at it. I already figured that part out but i need help with this bit...

I've got it so that whenever I check the sum of the array, the AI knows it's options to take(i.e if the value is 21 it chooses rock)

What i don't know is the loop structure i need to keep constantly updating my array . I know the basic idea but i don't know the code for it.

I need the array to constantly delete the  spot and replace it somehow so that when my AI checks for the values it doesn't keep checking old ones...

Sorry if this is hard to read it's 2 a.m here and i'm very tired. This is due today :D lol

``````import java.io.*;
import java.util.*;
public class RockPaperScissors
{
static int Compinput[]=new int;
static int Userinput[]=new int;
static int rockCounter=0,paperCounter=0,scissorCounter=0;

public static void main(String args[])
{
System.out.print("Are You Ready To Play? y/n\n");
char answryn=answr.charAt(0);

switch(answryn)
{
case 'Y':
case 'y':
PlayGame();
break;
case 'N':
case 'n':
System.out.println("Goodbye");
}
}

public static void randomStartValue()
{
int randomStartVal=(int)(Math.random()*3);
if(randomStartVal==0)
{
Compinput=1;
System.out.println("My choice is rock");
}
else if(randomStartVal==1)
{
Compinput=3;
System.out.println("My choice is paper");
}
else if(randomStartVal==2)
{
Compinput=9;
System.out.println("My choice is scissors");
}
}

public static void randomValue()
{
int randomVal=(int)(Math.random()*3);
if(randomVal==0)
{
System.out.println("My choice is rock");
}
else if(randomVal==1)
{
System.out.println("My choice is paper");
}
else if(randomVal==2)
{
System.out.println("My choice is scissors");
}
}

public static void ComputerAI()
{
for(int i=0;i<4;i++)
{
if(Compinput[i]==1)
{
rockCounter++;
}
if(Compinput[i]==3)
{
paperCounter+=3;
}
if(Compinput[i]==9)
{
scissorCounter+=9;
}

int totalCompValue=(rockCounter+paperCounter+scissorCounter);

if(totalCompValue==13)
{
randomValue();
}
else if(totalCompValue==5)
{
System.out.println("My selection is Scissors");

}
else if(totalCompValue==11)
{
System.out.println("My selection is Paper");
}
else if(totalCompValue==15)
{
System.out.println("My selection is Rock");
}
else if(totalCompValue==7)
{
System.out.println("My selection is Scissors");
}
else if(totalCompValue==19)
{
System.out.println("My selection is Paper");
}
else if(totalCompValue==21)
{
System.out.println("My selection is Rock");
}
else
{
randomValue();
}
}
}

{
for(int y=0;y<4;y++)
{
Compinput[y
}
}

public static void PlayGame()
{

}
}``````
``Compinput[y``

What i don't know is the loop structure i need to keep constantly updating my array . I know the basic idea but i don't know the code for it.

what do you mean by update? change the content? shouldn't you just assign another value to it

haha i saw my post got answered when i was changing songs and i dropped everything and started typing! That was a method I was just starting to add the values (looping) to my arrays.

My question(simplified) is what is the best way to have an array be moved/deleted in order to make room for a value?

for instance let's say that array, array, array, array are all full, and but i need a new value to go to array each time i enter Rock Paper or Scissors.
What is the best way to do that?

// it needs to be in a loop so that the game continues until the person tells it to stop//
//also i need to keep the previous 3 so that my AI for the game can check it. (essentially i need to move (assuming array is filled last) array,to array, array to array, and array to array..then array will get replace by my next choice rock paper or scissors

You can't exactly delete an array since its static and not dynamic
maybe just assign a new value to it...

// it needs to be in a loop so that the game continues until the person tells it to stop//

use a boolean and change the value on the boolean once the user makes the choice to stop

Sorry, i updated my last post I don't know if you saw it. Basically what i need it to do is not DELETE the arrays but simply move them. For instance when I put in a new value of rock and all of the four array slots are full then it should move array to array...ect therefore, array will lose it's old value, and array will receive an entirely new value.

Sorry, i updated my last post I don't know if you saw it. Basically what i need it to do is not DELETE the arrays but simply move them. For instance when I put in a new value of rock and all of the four array slots are full then it should move array to array...ect therefore, array will lose it's old value, and array will receive an entirely new value.

make another loop that assigns the current arrays value and the array after it

``array[i]=array[i+1];``

make another loop that assigns the current arrays value and the array after it

``array[i]=array[i+1];``

You're my hero :)

So i was going to do a while loop, and as long as you let the game continue (boolean=true) then it will keep running. I keep getting error messages when I set it up. Help? :)

``````public static void PlayGame()
{
boolean stay=true;
randomStartValue();
while(boolean stay=true)
{
}

}``````

post the errors

also you have no option or statement to change the value of stay

I know, i was going to add the exit from the loop in a second.
All of the errors point towards the While() line#
error:'.class'expected
error: illegal start of expression
error:';' expected
error: not a statement
error:';'expected

while(boolean stay=true)
1. This creates a new boolean when you should be using the one you already declared
2. = is assignment, == is the test for equals
3. (someBoolean == true) is a long version of (someBoolean)

I can't guess the location of the errors in your code

while(boolean stay=true)
1. This creates a new boolean when you should be using the one you already declared
2. = is assignment, == is the test for equals
3. (someBoolean == true) is a long version of (someBoolean)

:yawn: Oh wow thanks. You guys are both life savers. :) I've learned more by your examples of code today then I have in a week from my teacher. How do you know so much!

On another note, any ideas on how i should set up the point system? One point for winning minus points for breaking the rule.. That's pretty much the only thing I don't have SOME idea on how to approach

How do you know so much!

It's easy - 42 years programming experience, of which the last 13 in Java. All it needs is patience.

Hey, how do you compare two arrays? It's something like .equals right? I need to check who wins the rock paper scissors battle by comparing the arrays.

Here's an update of my code btw.

``````import java.io.*;
import java.util.*;
public class RockPaperScissors
{
static int Compinput[]=new int;
static int Userinput[]=new int;
static int rockCounter=0,paperCounter=0,scissorCounter=0;
static int updateArray,updateUserArray;
static boolean stay;

public static void main(String args[])
{
System.out.print("Are You Ready To Play? (y/n) ");
char answryn=answr.charAt(0);

switch(answryn)
{
case 'Y':
case 'y':
PlayGame();
break;
case 'N':
case 'n':
System.out.println("Goodbye");
}
}

public static void randomStartValue()
{
int randomStartVal=(int)(Math.random()*3);
if(randomStartVal==0)
{
Compinput=1;
System.out.println("My choice is rock");
}
else if(randomStartVal==1)
{
Compinput=3;
System.out.println("My choice is paper");
}
else if(randomStartVal==2)
{
Compinput=9;
System.out.println("My choice is scissors");
}
}

public static void randomValue()
{
int randomVal=(int)(Math.random()*3);
if(randomVal==0)
{
System.out.println("My choice is rock");
int updateArray=1;
}
else if(randomVal==1)
{
System.out.println("My choice is paper");
int updateArray=3;
}
else if(randomVal==2)
{
System.out.println("My choice is scissors");
int updateArray=9;
}
}

public static void ComputerAI()
{
for(int i=0;i<4;i++)
{
if(Compinput[i]==1)
{
rockCounter++;
}
if(Compinput[i]==3)
{
paperCounter+=3;
}
if(Compinput[i]==9)
{
scissorCounter+=9;
}

int totalCompValue=(rockCounter+paperCounter+scissorCounter);

if(totalCompValue==13)
{
randomValue();
}
else if(totalCompValue==5)
{
System.out.println("My selection is Scissors");
int updateArray=9;
}
else if(totalCompValue==11)
{
System.out.println("My selection is Paper");
int updateArray=3;
}
else if(totalCompValue==15)
{
System.out.println("My selection is Rock");
int updateArray=1;
}
else if(totalCompValue==7)
{
System.out.println("My selection is Scissors");
int updateArray=9;
}
else if(totalCompValue==19)
{
System.out.println("My selection is Paper");
int updateArray=3;
}
else if(totalCompValue==21)
{
System.out.println("My selection is Rock");
int updateArray=1;
}
else
{
randomValue();
}
}
}

{
for(int y=3;y>0;y--)
{
Compinput[y]=Compinput[y-1];
Compinput=updateArray;
}
}

{
for(int y=3;y>0;y--)
{
Userinput[y]=Userinput[y-1];
Userinput=updateUserArray;
}
}

public static void Score()
{

}

public static void PlayGame()
{
randomStartValue();
while(stay==true)
{

switch(rpsAnswr)
{
case 'R':
case 'r':
{
int updateUserArray=1;
}
case 'P':
case 'p':
{
int updateUserArray=3;
}
case 'S':
case 's':
{
int updateUserArray=9;
}
}
}

}
}``````

Hey, how do you compare two arrays? It's something like .equals right? I need to check who wins the rock paper scissors battle by comparing the arrays.

Again use a loop to compare the indexes then check

``if(array[i]==array2[i]){}``

On another note, any ideas on how i should set up the point system? One point for winning minus points for breaking the rule.. That's pretty much the only thing I don't have SOME idea on how to approach

``````boolean won;