-1

Since I added the while loop to my little rock,paper and scissors game it keeps giving me the number 1.

package steenpapierschaar1;

import java.util.Scanner;

/**
 *
 * @author T
 */
public class SteenPapierSchaar1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int rock = 1 ;   // waarde van Rock
        int paper = 2;  // waarde van paper
        int scissor = 3;// waarde van scissor
        int AI = 1 + (int) (Math.random() *3);// maakt int AI een random getal nog niet af moet een range in komen
        Scanner KeyBoard = new Scanner (System.in);// naam scanner input
        boolean playing = true;
        String answer, answer2 = null;


        while(playing = true){
            System.out.println("choose 1 for rock, 2 for paper and 3 for scissor"); // string met keuze voor User
            int UserChoice = KeyBoard.nextInt();// waarde van UserChoice

            if (AI == 1 && UserChoice == 2 || AI == 2 && UserChoice == 3 || AI == 3 && UserChoice == 1) { // als de speler elke keer 1x hoger heeft wint hij de ronde
                System.out.println("You WIN");// outprint met dat je gewonnen hebt en wat de computer doet
                if (AI == 1)
                    System.out.println("The Computer did rock");
                if (AI == 2)
                    System.out.println("The Computer did paper");
                if (AI == 3)
                    System.out.println("The Computer did scissors");


            }

            else if (AI == 1 && UserChoice == 1 || AI == 2 && UserChoice == 2 || AI == 3 && UserChoice == 3) {
                System.out.println("Draw");
                if (AI == 1)
                    System.out.println("The Computer did rock");
                if (AI == 2)
                    System.out.println("The Computer did paper");
                if (AI == 3)
                    System.out.println("The Computer did scissors");
            }

            else if (AI == 1 && UserChoice == 3 || AI == 2 && UserChoice == 1 || AI == 3 && UserChoice == 2){
                System.out.println("You Lose");
                if (AI == 1)
                    System.out.println("The Computer did rock");
                if (AI == 2)
                    System.out.println("The Computer did paper");
                if (AI == 3)
                    System.out.println("The Computer did scissors");
            }              

        }        
    }
}
Mekap
  • 2,065
  • 14
  • 26
TomStarter
  • 25
  • 7
  • 2
    Well I'd start off by using `java.util.Random` instead of `Math.random()`. I'd then fix all the variables etc to follow Java naming conventions, and sort out the indentation... – Jon Skeet Apr 22 '15 at 13:59
  • Look at where you're assigning a value to `AI` - you're *only* doing that before the loop. (I'd strongly recommend adding brackets when you have lots of conditions connected with `&&` and `||` by the way, to make the grouping really clear. You should also try to refactor the code... you've got the same lines of code to display the computer's move three times...) – Jon Skeet Apr 22 '15 at 14:02
  • I would suggest using enum instead of int and switch instead of ifs, [check](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) – Selçuk Cihan Apr 22 '15 at 14:06
  • In addition, you while-loop: while(playing = true), you are basically assigning playing instead of comparing, use == to compare boolean, but in this case while(playing) is sufficient. – knordbo Apr 22 '15 at 14:06

5 Answers5

1

There are few problems in your code. You have few unused variables like

int rock = 1 ;  // waarde van Rock
int paper = 2;  // waarde van paper
int scissor = 3;// waarde van scissor

which should probably be

final int ROCK = 1; // waarde van Rock
final int PAPER = 2; // waarde van paper
final int SCISSORS = 3;// waarde van scissor

and used like

if (AI == ROCK) ...

Other problem is while (playing == true) notice that = is assignment operator == is comparing operator. So in your loop you are actually assigning true to playing which is then always evaluated as true.

To avoid such problems where you by mistake write = instead of == simply use

while (playing){

}

BTW you should probably ask after each play if user want to continue/quit game. You can also do it by checking values like -1 can represent "quit".


Another problem is that you never reassign value of AI inside loop so its value can't change and will be same as at start of program. So maybe move your

int AI = 1 + (int) (Math.random() * 3);

at start of your while loop to let AI get new value.

BTW2 instead of Math.random()*3 which is kind of cryptic you can use Random class and its nextInt(n) method which will random any integer in range [0; n)

  • 0 (including)
  • n (excluding).

which can also be interpreted as [0; n-1]. So your code could look like

Random r = new Random();
while(..){
    int AI = r.nextInt(3) + 1; // +1 because we want to move range [0; 2] to [1; 3]
    ...
}

It looks like you are invoking

if (AI == 1)
    System.out.println("The Computer did rock");
if (AI == 2)
    System.out.println("The Computer did paper");
if (AI == 3)
    System.out.println("The Computer did scissors");

in each of your win/draw/lose conditions. In that case you can simply move it out of each of these blocks like

if (win conditions){
    print win
}else if (draw condition){
    print draw
}else if (lose condition){
    print lose
}
<move code drawing computer hand here>

Draw condition looks overly complex. It looks like draw is achieved when AI and UserChoice are equal so simple

if (AI == UserChoice)

could replace

if (AI == 1 && UserChoice == 1 || AI == 2 && UserChoice == 2 || AI == 3 && UserChoice == 3)

You can probably also simplify your other conditions using modulo % operator.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Include java.util.Random and do thoses 2 things.

Use this fucntion :

public static int randAI() {
    Random rand = new Random();

    int randomNum = rand.nextInt((3 - 1) + 1) + 1;

 return randomNum;
}

And add this in your loop :

while(playing = true){
 AI = randAI();
 //REST of your code

This way your ai will random at each iteration.

Mekap
  • 2,065
  • 14
  • 26
0

because Math.random() will give values in between 0.0 to 1.0.

so in most of the cases

 int AI = 1 + (int) (Math.random() *3);

for your sub-instruction (int) (Math.random() *3) in most of the cases value will be 0 so AI will be 1.

as mention in javaDoc

public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Instead of this you can use java.util.Random

Prashant
  • 2,556
  • 2
  • 20
  • 26
0

You never set a new value for AI.

So this value is always reused depending on what you set it to during the start of the program.

Also make sure you initialize the random number generator

Random rand = new Random();

then in your loop, you can call

int randomNumber = rand.nextInt((max - min) + 1) + min;

Where min and max are set to your range.

See How do I generate random integers within a specific range in Java? For a good description.

Community
  • 1
  • 1
Joey
  • 1,349
  • 14
  • 26
0

To answer your question the AI isn't updating in the while loop. Just move the AI variable initialization into the while loop.

int AI = 0;
while(playing = true){
        AI = 1 + (int) (Math.random() *3);
        System.out.println("choose 1 for rock, 2 for paper and 3 for scissor");