Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fluidcoding;
- // Brian Sonnie
- import java.util.Random;
- import java.util.Scanner;
- public class Main {
- private static final String ROCK = "rock";
- private static final String PAPER = "paper";
- private static final String SCISSORS = "scissors";
- private static final String DONE = "done";
- private static final String WIN_NONE = "0";
- private static final String WIN_PLAYER = "1";
- private static final String WIN_CPU = "2";
- private static String firstName;
- public static void main(String[] args) {
- String playerMove;
- String cpuMove;
- String winner;
- int playerWins = 0;
- int cpuWins = 0;
- int ties = 0;
- int gamesPlayed = 0;
- firstName = getPlayerName();
- welcomeMessage(firstName);
- Scanner in = new Scanner(System.in);
- playerMove = getPlayerMove();
- while(!playerMove.equals(DONE)){
- gamesPlayed++;
- cpuMove = getCpuMove();
- System.out.println("Computer chooses: " + cpuMove);
- winner = getWinner(cpuMove, playerMove);
- switch(winner){
- case WIN_PLAYER:
- System.out.println("You Win!!! :)");
- playerWins++;
- break;
- case WIN_CPU:
- System.out.println("You Lose!!! :(");
- cpuWins++;
- break;
- default:
- System.out.println("Tie...");
- ties++;
- break;
- }
- playerMove = getPlayerMove();
- }
- showScores(playerWins, cpuWins, gamesPlayed, ties);
- }
- public static void welcomeMessage(String firstName){
- System.out.println("Hello " + firstName + " welcome to the Rock, Paper, Scissors game.");
- }
- public static void showScores(int playerWins, int cpuWins, int gamesPlayed, int ties){
- System.out.println("Game Over!!!\n"
- + "Games Played: " + gamesPlayed
- + "\n" + firstName + ": " + playerWins
- + "\nComputer: " + cpuWins
- + "\nTies: " + ties
- );
- if(playerWins==cpuWins)
- System.out.println("TIE...");
- else if(playerWins>cpuWins)
- System.out.println("YOU WIN!!!");
- else
- System.out.println("YOU LOSE!!!");
- }
- private static String getWinner(String cpuMove, String playerMove){
- if(cpuMove.equals(playerMove))
- return WIN_NONE;
- if((cpuMove.equals(ROCK) && playerMove.equals(PAPER))
- || (cpuMove.equals(SCISSORS) && playerMove.equals(PAPER))
- || (cpuMove.equals(PAPER) && playerMove.equals(ROCK)))
- return WIN_CPU;
- return WIN_PLAYER;
- }
- public static String getPlayerMove(){
- String move = "";
- Scanner in = new Scanner(System.in);
- while(move.isEmpty() || !(move.equals(ROCK)
- || move.equals(PAPER)
- || move.equals(SCISSORS)
- || move.equals(DONE))){
- System.out.println("What's your move? ("+ROCK+"|"+PAPER+"|"+SCISSORS+") -- ("+DONE+") to quit");
- move = in.nextLine().toLowerCase();
- }
- return move;
- }
- public static String getCpuMove(){
- Random rnd = new Random();
- int m = rnd.nextInt(3);
- switch(m){
- case 0: return PAPER;
- case 1: return ROCK;
- default: return SCISSORS;
- }
- }
- public static String getPlayerName(){
- System.out.println("Hello what is your name?");
- Scanner in = new Scanner(System.in);
- String name = "";
- while (name.equals(""))
- name = in.nextLine();
- return name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement