donkey ollie
donkey ollie

Reputation: 11

How do I count the playerwins for my code

Aye, so I have this assignment to make a rock, paper, scissor game. I did mostly everything right (maybe), but I can't figure out how to count the playerwins at the end when stopping a game of rock, paper, scissors. It is the one thing I am missing when running and stopping the code.

Playerwins are at the very bottom and the very top. The code is long as hell, but im new at coding and don't know how to make it less redundant.

import java.util.*;
import java.io.*;

public class RockPaperScissors {
    public static void main(String[] args) {
        Scanner dodongo = new Scanner(System.in);
        Random hamster = new Random();
        System.out.println("Rock, Paper, Scissors!");
        System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
        while (!dodongo.hasNextInt(4)) {
            System.out.println("Only numbers 1-3!");
            System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
            dodongo.next();
        }
        int player = dodongo.nextInt();
        int rock = 1;
        int paper = 2;
        int scissors = 3;
        int playerwins = 0;
        System.out.println("YOU: Rock, Paper, Scissors! ");
        System.out.println();
        if ((player >= 0) && (player <= 1)) {
            System.out.println("Rock!");
        } else if ((player >= 1) && (player <= 2)) {
            System.out.println("Paper!");
        } else if ((player >= 2) && (player <= 3)) {
            System.out.println("Scissors!");
        }
        System.out.println("Computer is playing...");
        int shoot = -1;
        System.out.println("COM: Rock, Paper, Scissors!");
        do {
            int keyblade = hamster.nextInt(3) + 1;
            if ((keyblade >= 0) && (keyblade <= 1)) {
                System.out.println("Rock!");
            } else if ((keyblade >= 1) && (keyblade <= 2)) {
                System.out.println("Paper!");
            } else if ((keyblade >= 2) && (keyblade <= 3)) {
                System.out.println("Scissors!");
            }
            shoot++;
            if ((player == rock) && (keyblade == paper)) {
                System.out.println("COM Win!");
                System.out.println("You Lose! Paper beats rock, because a piece of paper can cover a  rock!");
            } else if ((player == rock) && (keyblade == scissors)) {
                System.out.println("You Win! Rock beats scissors, because a rock can break a pair of scissors!");
            } else if ((player == paper) && (keyblade == rock)) {
                System.out.println("You Win! Paper beats rock, because a piece of paper can cover a rock!");
            } else if ((player == paper) && (keyblade == scissors)) {
                System.out.println("COM Win!");
                System.out.println("You Lose! Scissors beats paper, because scissors can cut paper!");
            } else if ((player == scissors) && (keyblade == rock)) {
                System.out.println("COM Win!");
                System.out.println("You Lose! Rock beats scissors, because a rock can break a pair of scissors!");
            } else if ((player == scissors) && (keyblade == paper)) {
                System.out.println("You Win! Scissors beats paper, because scissors can cut paper!");
            } else {
                System.out.println("Tie!");
            }
            playerwins++;
        } while (shoot != 0);
        System.out.println("Want to play again?(Press Y or N)");
        String tryagain = dodongo.next();
        if (tryagain.equalsIgnoreCase("Y")) {
            main(null);
        } else {
            System.out.println("Adios...");
            System.out.println("Total Wins: " + playerwins);
        }
        System.out.println();
    }
}

Upvotes: 1

Views: 222

Answers (2)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51515

I broke your code into methods. For the most part, this just organized the code a little better. I was able to use the writeInput method twice, so I also slightly reduced the duplication.

Once I created methods, the code in the main method was reduced enough that I could make a game loop using a do-while loop and remove the recursion.

I also corrected the playerwins count so it increments only when the player wins. The corrected code is in the compareInputs method.

Here's your code after adding methods.

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
    
     public static void main(String[] args) {
            Scanner dodongo = new Scanner(System.in);
            Random hamster = new Random();
            int playerwins = 0;
            String tryagain;
            
            do {
                int player = readPlayerInput(dodongo);;
                System.out.println("YOU: Rock, Paper, Scissors! ");
                System.out.println();
                writeInput(player);
               
                System.out.println("Computer is playing...");
                System.out.println("COM: Rock, Paper, Scissors!");
    
                int keyblade = hamster.nextInt(3) + 1;
                writeInput(keyblade);
                playerwins += compareInputs(player, keyblade);
    
                System.out.println("Want to play again?(Press Y or N)");
                tryagain = dodongo.next();
            } while (tryagain.equalsIgnoreCase("Y")); 
           
            System.out.println("Adios...");
            System.out.println("Total Wins: " + playerwins);
            System.out.println();
        }

    private static int readPlayerInput(Scanner dodongo) {
        System.out.println("Rock, Paper, Scissors!");
        System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
        while (!dodongo.hasNextInt(4)) {
            System.out.println("Only numbers 1-3!");
            System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
            dodongo.next();
        }
        
        return dodongo.nextInt();
    }
    
    private static void writeInput(int input) {
        if ((input >= 0) && (input <= 1)) {
            System.out.println("Rock!");
        } else if ((input >= 1) && (input <= 2)) {
            System.out.println("Paper!");
        } else if ((input >= 2) && (input <= 3)) {
            System.out.println("Scissors!");
        }
    }
    
    private static int compareInputs(int player, int keyblade) {
        int rock = 1;
        int paper = 2;
        int scissors = 3;
        int playerWins = 0;
        
        if ((player == rock) && (keyblade == paper)) {
            System.out.println("COM Win!");
            System.out.println("You Lose! Paper beats rock, because a "
                    + "piece of paper can cover a  rock!");
        } else if ((player == rock) && (keyblade == scissors)) {
            System.out.println("You Win! Rock beats scissors, because a "
                    + "rock can break a pair of scissors!");
            playerWins = 1;
        } else if ((player == paper) && (keyblade == rock)) {
            System.out.println("You Win! Paper beats rock, because a "
                    + "piece of paper can cover a rock!");
            playerWins = 1;
        } else if ((player == paper) && (keyblade == scissors)) {
            System.out.println("COM Win!");
            System.out.println("You Lose! Scissors beats paper, because "
                    + "scissors can cut paper!");
        } else if ((player == scissors) && (keyblade == rock)) {
            System.out.println("COM Win!");
            System.out.println("You Lose! Rock beats scissors, because a "
                    + "rock can break a pair of scissors!");
        } else if ((player == scissors) && (keyblade == paper)) {
            System.out.println("You Win! Scissors beats paper, because "
                    + "scissors can cut paper!");
            playerWins = 1;
        } else {
            System.out.println("Tie!");
        }
        
        return playerWins;
    }

}

Upvotes: 1

Konrad H&#246;ffner
Konrad H&#246;ffner

Reputation: 12207

The first thing you need when you program any game is that you need a game loop, which runs the game until the user exits it. Interestingly, you opt for recursively calling main, which is also possible (although it could theoretically overload stack, which probably won't happen in this simple case but could be a real problem in a game that loops many times per second for a long time).

So the easiest fix would be to move the playerwins variable out of the main function (then you need to make it static as well) but the correct code that builds better habits for later work would be to use another while loop instead of recursively calling main.

Normally, beginners start with iterative code and discover recursion later, so it is nice that you discover it so early, but unfortunately that is not the correct case for it, but keep it in your pocket for other occasions.

Upvotes: 0

Related Questions