aquaballoon

Java - Rock, Paper, Scissors

Jul 14th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package com.peter;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Random;
  7.  
  8. public class Main {
  9.    
  10.     static boolean play;
  11.    
  12.     public static void main(String[] args) throws IOException {
  13.        
  14.         InputStreamReader isr = new InputStreamReader(System.in);
  15.         BufferedReader in = new BufferedReader(isr);
  16.        
  17.         System.out.println("This is a game of rock paper scissors");
  18.         System.out.println("");
  19.        
  20.        
  21.         int playerscore = 0;
  22.         int compscore = 0;
  23.         String again = "y";
  24.        
  25.         //boolean play;
  26.                        
  27.         while(play = true){
  28.            
  29.             if(again.equals("y")){
  30.            
  31.            
  32.             Random r = new Random();
  33.             int comp = Math.abs(r.nextInt() % 3);
  34.            
  35.             int playerchoice = 0;
  36.             int compchoice = 0;
  37.             String comphand = null;
  38.            
  39.             if(comp == 0){
  40.                 compchoice = 1;
  41.             }
  42.             else if(comp == 1){
  43.                 compchoice = 2;
  44.             }
  45.             else if(comp == 2){
  46.                 compchoice = 3;
  47.             }
  48.            
  49.            
  50.             if(compchoice == 1){
  51.                 comphand = "scissors";
  52.             }
  53.             else if(compchoice == 2){
  54.                 comphand = "rock";
  55.             }
  56.             else if(compchoice == 3){
  57.                 comphand = "paper";
  58.             }
  59.            
  60.            
  61.            
  62.             System.out.println("----------NEW GAME----------");
  63.             System.out.println("Type down scissors (1), rock (2), or paper (3) below");
  64.             System.out.println("");
  65.            
  66.            
  67.            
  68.            
  69.             String player;
  70.             player = in.readLine();
  71.            
  72.            
  73.             System.out.println("");
  74.            
  75.            
  76.             if(player.equals("2")){
  77.                 playerchoice = 2;
  78.                 System.out.println("You chose rock");
  79.                 System.out.println("Computer chose " + comphand);
  80.                
  81.             }
  82.             else if(player.equals("3")){
  83.                 playerchoice = 3;
  84.                 System.out.println("You chose paper");
  85.                 System.out.println("Computer chose "+ comphand);
  86.             }
  87.             else if(player.equals("1")){
  88.                 playerchoice = 1;
  89.                 System.out.println("You chose scissors");
  90.                 System.out.println("Computer chose "+ comphand);
  91.             }
  92.             else{
  93.                
  94.                 System.out.println("You can only choose scissors (1), rock (2), or paper (3)");
  95.                 System.out.println("");
  96.                 playerchoice = 0;
  97.             }
  98.            
  99.            
  100.            
  101.            
  102.             if(compchoice == playerchoice){
  103.                 System.out.println("Draw!");
  104.                 System.out.println("");
  105.             }
  106.             else if(compchoice == 1){
  107.                 if (playerchoice == 2){     //computer = scissors, player = rock
  108.                     System.out.println("You win!");
  109.                     System.out.println("");
  110.                     playerscore = playerscore + 1;
  111.                 }
  112.                 else if(playerchoice == 3){ //computer = scissors, player = paper
  113.                     System.out.println("You lose!");
  114.                     System.out.println("");
  115.                     compscore = compscore + 1;
  116.                 }
  117.             }
  118.             else if(compchoice == 2){
  119.                 if (playerchoice == 3){     //computer = rock, player = paper
  120.                     System.out.println("You win!");
  121.                     System.out.println("");
  122.                     playerscore = playerscore + 1;
  123.                 }
  124.                 else if(playerchoice == 1){ //computer = rock, player = scissors
  125.                     System.out.println("You lose!");
  126.                     System.out.println("");
  127.                     compscore = compscore + 1;
  128.                 }
  129.             }
  130.             else if(compchoice == 3){
  131.                 if (playerchoice == 1){     //computer = paper, player = scissors
  132.                     System.out.println("You win!");
  133.                     System.out.println("");
  134.                     playerscore = playerscore + 1;
  135.                 }
  136.                 else if(playerchoice == 2){ //computer = paper, player = rock
  137.                     System.out.println("You lose!");
  138.                     System.out.println("");
  139.                     compscore = compscore + 1;
  140.                 }
  141.             }
  142.            
  143.             String discompscore = Integer.toString(compscore);
  144.             String displayerscore = Integer.toString(playerscore);
  145.            
  146.             System.out.println("Computer's score: " + discompscore);
  147.             System.out.println("Player's score: " + displayerscore);
  148.             System.out.println("");
  149.            
  150.             System.out.println("Play again? y/n");
  151.  
  152.             again = in.readLine();
  153.            
  154.        
  155.         }    //again
  156.            
  157.         else{
  158.         play = false;
  159.         }
  160.            
  161.         }    //boolean
  162.  
  163.        
  164.        
  165.     }        //main
  166.  
  167. }            //class
Advertisement
Add Comment
Please, Sign In to add comment