Advertisement
zharry

Java Game #1

Sep 20th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. /*
  2.  * Author: Harry Zhang
  3.  * Date: 20/09/17
  4.  * For: Java Programming Class #1
  5.  */
  6.  
  7. import java.util.Random;
  8. import java.util.Scanner;
  9.  
  10. public class MainGame {
  11.    
  12.     public static void main(String[] args) {
  13.  
  14.         System.out.println("Welcome to Henry's ManPoke game!");
  15.  
  16.         // Sets computer and player HP
  17.         int yourHP = 100;
  18.         int compHP = 100;
  19.  
  20.         // Create a Scanner
  21.         Scanner userInput = new Scanner(System.in);
  22.  
  23.         // Get User information and start game
  24.         System.out.println("What is your name?");
  25.         String name = userInput.nextLine();
  26.         System.out.println("Hello, " + name + ". Would you like to begin?");
  27.         String start = userInput.nextLine();
  28.  
  29.         // What to do if the user wants to play
  30.         if (start.equals("Yes") || start.equals("yes")) {
  31.            
  32.             // Game runs in this infinite loop
  33.             while (true) {
  34.                 // Prompt user to start their turn
  35.                 System.out.println();
  36.                 System.out.println("Start turn?");
  37.                 String go = userInput.nextLine();
  38.                
  39.                 // If the user plays
  40.                 if (go.equals("Yes")) {
  41.                    
  42.                     // Generate an attack damage for both the player and the AI
  43.                     Random rand = new Random();
  44.                     int yourMove = rand.nextInt(50);
  45.                     int compMove = rand.nextInt(50);
  46.                    
  47.                     // Show the user what both players rolled
  48.                     System.out.println("You did " + yourMove + " damage!");
  49.                     System.out.println("Computer did " + compMove + " you!");
  50.                    
  51.                     // Calculate and Display both players health
  52.                     yourHP = yourHP - compMove;
  53.                     compHP = compHP - yourMove;
  54.                     System.out.println("You have " + yourHP + " health!");
  55.                     System.out.println("Computer has " + compHP + " health!");
  56.                    
  57.                     // If both players died at the same time
  58.                     if (yourHP <= 0 && compHP <= 0) {
  59.                         System.out.println("Tie Game!");
  60.                         break;
  61.                     // If you died before the AI
  62.                     } else if (yourHP <= 0) {
  63.                         System.out.println("You Lose!");
  64.                         break;
  65.                     // If the AI died before you
  66.                     } else if (compHP <= 0) {
  67.                         System.out.println("You Win!");
  68.                         break;
  69.                     }
  70.                 // If the user quits
  71.                 } else {
  72.                     System.out.println("You Lose!");
  73.                     break;
  74.                 }
  75.             }
  76.         // What to do if the user does not want to play
  77.         } else {
  78.             System.out.println("Cya!");
  79.         }
  80.  
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement