Quantcast
Viewing all articles
Browse latest Browse all 5

Java Rock/Paper/Scissors Game

For my computer science class I am tasked with writing a rock/paper/scissors game. I understand how to write the game, but I was wondering if someone more experienced than I could give me some pointers on how to become a better programmer.

Is there anything I could do to reduce the size of the code, but without diminishing functionality?

public class RockPaperScissors {    static double wins = 0;    static double losses = 0;    static double draws = 0;    static double games = 0;    private static void lose() {        System.out.println("YOU LOSE! :D");        losses++;        games++;    }    private static void win() {        System.out.println("you win :D");        wins++;        games++;    }    // main method    public static void main(String[] args)     {        Scanner scan = new Scanner(System.in);        // variables        Integer answer1 = 0;        boolean playing = true;        // start of game        do         {            // main menu            System.out.println("Input R, P, S, or Q(to quit).");            // player's move            String answer = scan.next();            answer = answer.toLowerCase();            // converts player's move from str to int            switch (answer.charAt(0))            {            case 'r':                answer1 = 0;                break;            case 'p':                answer1 = 1;                break;            case 's':                answer1 = 2;                break;            case 'q':                System.out.println("User exit.");                double winP = wins/games*100;                double loseP = losses/games*100;                double drawP = draws/games*100;                System.out.println("Wins: "+ wins);                System.out.printf("%.2f", winP);                System.out.println(" Percent");                System.out.println("Losses: "+ losses);                System.out.printf("%.2f", loseP);                System.out.println(" Percent");                System.out.println("Draws: "+ draws);                System.out.printf("%.2f", drawP);                System.out.println(" Percent");                playing = false;                break;            default:                 System.out.println("ERROR!!");                playing = false;                break;            }            // checks for conditions            if (playing)             {                // computer's move                Random random = new Random();                Integer choice = random.nextInt(3); // (n-1) 0 = rock, 1 = paper, 2 = scissors                String comp = choice.toString();                switch (comp)                {                case "0":                    System.out.println("computer chose rock");                    break;                case "1":                    System.out.println("computer chose paper");                    break;                case "2":                    System.out.println("computer chose scissors");                    break;                default:                    System.out.println("ERROR!");                    break;                }                // checks results                if (answer1 == choice)                {                    System.out.println("Draw");                    draws++;                    games++;                }                else if ((answer1 + 1) % 3 == choice)                    lose();                else if ((choice + 1) % 3 == answer1)                    win();                else                    System.out.println("ERROR! ERROR!");            }                   } while (playing);    }}

Viewing all articles
Browse latest Browse all 5

Trending Articles