0

Good day guys, So i wanted to create a rock, paper, scissors game between the player and the machine, then score the player and machine based on the results. The problem is that my scores always equates to 0, i don't understand! Please help guys, I'm also a newbie...here is the code

import javax.swing.*;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

public class Game2

{

    //Declaring my random object

    private static Random random = new Random();


    public static void main(String[] args)
    {

        //Declaring my variables
        int playerScore = 0;
        int machineScore = 0;

        int totPlayerScore = 0;
        int totMachineScore = 0;

        //Instantiating my list object
        List<String> dice = new ArrayList<>();

        //We gon rename scissors to cuts for a shorter input
        //Adding values to the list
        dice.add("rock");
        dice.add("paper");
        dice.add("cuts");


        //Paper beats rock, rock beats cuts, cuts beats paper
        //For loop to play the game 3 times
        for(int i = 0; i <3; i++)
        {

            //User input
            String strInput = JOptionPane.showInputDialog("Please enter paper, rock or cuts");


            //Randomizing the list
            int index = random.nextInt(dice.size());

            //If statement to see if the usr or machine won
            if(strInput.toLowerCase() == "paper" && dice.get(index) == "rock")//win
            {

                playerScore += 1;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "rock" && dice.get(index) == "cuts")//win
            {
                playerScore += 1;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "cuts" && dice.get(index) == "paper")//Win
            {
                playerScore += 1;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "paper" && dice.get(index) == "paper")//Draw
            {
                playerScore = playerScore;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "rock" && dice.get(index) == "rock")//Draw
            {
                playerScore = playerScore;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "cuts" && dice.get(index) == "cuts")//Draw
            {
                playerScore = playerScore;

                totPlayerScore += playerScore;

            }//End of player if statement

            //Start of machine if statement
            if(strInput.toLowerCase() == "rock" && dice.get(index) == "paper")//win
            {

                machineScore += 1;

                totMachineScore += machineScore;
            }
            else if(strInput.toLowerCase() == "cuts" && dice.get(index) == "rock")//win
            {
                machineScore += 1;

                totMachineScore += machineScore;
            }
            else if(strInput.toLowerCase() == "paper" && dice.get(index) == "cuts")//Win
            {
                machineScore += 1;

                totMachineScore += machineScore;
            }

             //End of if statement


            //Displaying user Results
            System.out.println("User draw: " + strInput.toLowerCase() + " Machine draw: " + dice.get(index).toLowerCase());
            System.out.println("User Score " + playerScore + " Machine Score " + machineScore +  "\n");


        }//End of for loop


        //Displaying my final results
        System.out.println("User Final score: " + totPlayerScore + " Machine Final score: " + totMachineScore + "\n");


        //If statement to see who won, hte player or machine
        if(totPlayerScore > totMachineScore)
        {
            System.out.println("The Player won!!!");
        }
        else if(totPlayerScore == totMachineScore)
        {
            System.out.println("Its a draw!!!");
        }
        else
        {
            System.out.println("The Machine won!!! \n");
        }

        //Console message to end the program and game
        System.out.println("Game over!!!!!");

    }//End of main

}//End of class
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Tebza
  • 1
  • 1
  • besides the comparison way, try to avoid duplicating code. you are repeating conditions, nested conditions will do the same work, but more efficiënt, and the code is easier to read – Stultuske Oct 08 '18 at 09:29
  • yea, i changed my code and used nested if statements thanx for that, but my code still produces a 0 score... – Tebza Oct 08 '18 at 10:38
  • are you still comparing them using the == operator? – Stultuske Oct 08 '18 at 10:39
  • yes, should i use a different operator? if so which? – Tebza Oct 08 '18 at 10:51
  • Oh ya, i just saw the mistake, I'm supposed to use the .equals() method to compare strings and not the == operator, and my program is running...awe thanx man... – Tebza Oct 08 '18 at 11:00

0 Answers0