Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.peter;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Random;
- public class Main {
- static boolean play;
- public static void main(String[] args) throws IOException {
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader in = new BufferedReader(isr);
- System.out.println("This is a game of rock paper scissors");
- System.out.println("");
- int playerscore = 0;
- int compscore = 0;
- String again = "y";
- //boolean play;
- while(play = true){
- if(again.equals("y")){
- Random r = new Random();
- int comp = Math.abs(r.nextInt() % 3);
- int playerchoice = 0;
- int compchoice = 0;
- String comphand = null;
- if(comp == 0){
- compchoice = 1;
- }
- else if(comp == 1){
- compchoice = 2;
- }
- else if(comp == 2){
- compchoice = 3;
- }
- if(compchoice == 1){
- comphand = "scissors";
- }
- else if(compchoice == 2){
- comphand = "rock";
- }
- else if(compchoice == 3){
- comphand = "paper";
- }
- System.out.println("----------NEW GAME----------");
- System.out.println("Type down scissors (1), rock (2), or paper (3) below");
- System.out.println("");
- String player;
- player = in.readLine();
- System.out.println("");
- if(player.equals("2")){
- playerchoice = 2;
- System.out.println("You chose rock");
- System.out.println("Computer chose " + comphand);
- }
- else if(player.equals("3")){
- playerchoice = 3;
- System.out.println("You chose paper");
- System.out.println("Computer chose "+ comphand);
- }
- else if(player.equals("1")){
- playerchoice = 1;
- System.out.println("You chose scissors");
- System.out.println("Computer chose "+ comphand);
- }
- else{
- System.out.println("You can only choose scissors (1), rock (2), or paper (3)");
- System.out.println("");
- playerchoice = 0;
- }
- if(compchoice == playerchoice){
- System.out.println("Draw!");
- System.out.println("");
- }
- else if(compchoice == 1){
- if (playerchoice == 2){ //computer = scissors, player = rock
- System.out.println("You win!");
- System.out.println("");
- playerscore = playerscore + 1;
- }
- else if(playerchoice == 3){ //computer = scissors, player = paper
- System.out.println("You lose!");
- System.out.println("");
- compscore = compscore + 1;
- }
- }
- else if(compchoice == 2){
- if (playerchoice == 3){ //computer = rock, player = paper
- System.out.println("You win!");
- System.out.println("");
- playerscore = playerscore + 1;
- }
- else if(playerchoice == 1){ //computer = rock, player = scissors
- System.out.println("You lose!");
- System.out.println("");
- compscore = compscore + 1;
- }
- }
- else if(compchoice == 3){
- if (playerchoice == 1){ //computer = paper, player = scissors
- System.out.println("You win!");
- System.out.println("");
- playerscore = playerscore + 1;
- }
- else if(playerchoice == 2){ //computer = paper, player = rock
- System.out.println("You lose!");
- System.out.println("");
- compscore = compscore + 1;
- }
- }
- String discompscore = Integer.toString(compscore);
- String displayerscore = Integer.toString(playerscore);
- System.out.println("Computer's score: " + discompscore);
- System.out.println("Player's score: " + displayerscore);
- System.out.println("");
- System.out.println("Play again? y/n");
- again = in.readLine();
- } //again
- else{
- play = false;
- }
- } //boolean
- } //main
- } //class
Advertisement
Add Comment
Please, Sign In to add comment