Reputation: 19
I have two questions with my code. I am writing a program that simulates a game of Rock, Paper, Scissors played between two users. The winner is determined by the best of five computer generated rounds.
Three requirements:
I need help with the "allowing no ties" in the first requirement and mapping words to the values in the array to print in the third requirement.
So, I need these arrays:
Player 1 1 0 1 2 2
Player 2 2 1 0 1 0
to look like this:
Player 1 Player 2
paper scissors
rock paper
paper rock
scissors paper
scissors rock
Here is my first loop:
for(index=0; index<5; index++){
player1[index]=random.nextInt(3);
player2[index]=random.nextInt(3);
Second loop:
for(index=0; index<5; index++){
if((player1[index]==0) && (player2[index]==2)){
player1Win++;
}else if((player1[index]==1) && (player2[index]==0)){
player1Win++;
}else if((player1[index]==2) && (player2[index]==1)){
player1Win++;
}else{
player2Win++;
}
}
Third loop:
for(index=0; index<5; index++){
System.out.print("\t\t " + player1[index] + "");
System.out.println("\t " + player2[index] + "");
}
Thank you!
Upvotes: 1
Views: 2979
Reputation: 99
for(int j = 0; j <5; j++){
if(player1[j] > player2[j]){// checks to see if player 1 has the upper hand
play1++;// if so bam hers a point for the win
}
else if(player1[j] < player2[j]){ //if player 2 has the upper hand bam gets a point
play2++;
}
if(player1[j] == player2[j]){ // meh if they are tied.. do it again!
while(player1[j] == player2[j]){ // this is where you are having trouble
//use this a psudo code to get an idea of how to steer your problematic thinking
player1[j] = ran.nextInt(3);
player2[j] = ran.nextInt(3);
}
if you don't want the two values to be the same just over-write the current values to get new numbers until they are not equivalent
Upvotes: 0
Reputation: 3390
For third question, have array of String like:
String [] names = new String[3];
names[0] = "Rock";
names[1] = "Paper";
names[2] = "Scissor";
Then map integer value [0,2] to String array like:
for(index = 0; index < 5; index++){
System.out.println("\t\t" + names[player1[index]] + "\t" + names[player2[index]]);
}
For first question, just remove single quotes, you need to compare value in array to integer type, but you are comparing integer with character type, you wont get compile time error, but logically incorrect.
Upvotes: 0
Reputation: 29265
player1[index]=random.nextInt(3);
is populating you arrays with integers.
if((player1[index]=='0') && (player2[index]=='2')){
is comparing yours arrays as if they contain characters.
Try if((player1[index]==0) && (player2[index]==2)){
etc (just remove the single quotes).
Upvotes: 0