Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MainClass {
  6.    
  7.     public static void main(String[] args) {
  8.         //creating initial variables
  9.         //variable for number of encounters.
  10.         int battleNum = 0;
  11.         //score related variables
  12.         int score = 0;
  13.         int scoreModifer = 1;
  14.         // boolean for loop.
  15.         boolean answer;
  16.         //creating exp variable
  17.         int exp = 0;
  18.         //creating variables for the player health and health modifer.
  19.         int playerHealth;
  20.         int playerHealthModifer;
  21.         //creating variables for the player attack and attack modifer.
  22.         int playerAttack;
  23.         int playerAttackModifer;
  24.         //creating variables for the player defense and defense modifer.
  25.         int playerDefense;
  26.         int playerDefenseModifer;
  27.         //asking and recieving username using scanner class
  28.         System.out.println("welcome, please enter an user name.");
  29.         //creating scanner to take input from the keyboard.string.
  30.         Scanner scan = new Scanner(System.in);
  31.         // using scanner to take take next input and saving as userName
  32.         String userName = scan.nextLine();
  33.         //asking user for their choice of class.
  34.         System.out.println("Would you like to be a warrior, fighter, or a thief?");
  35.         //scanner taking next input as string.
  36.         String classChoice = scan.nextLine();
  37.         //switch statement deciding which class user decided, default is a "deprived" class.
  38.         switch (classChoice) {
  39.             case "warrior":
  40.                 //changing stat variables for the warrior class
  41.                 playerHealth = 150;
  42.                 playerHealthModifer = 10;
  43.                 playerAttack = 4;
  44.                 playerAttackModifer = 1;
  45.                 playerDefense = 7;
  46.                 playerDefenseModifer = 2;
  47.                
  48.                 break;
  49.             case "fighter":
  50.                 //changing stat variables for the fighter class.
  51.                 playerHealth = 90;
  52.                 playerHealthModifer = 7;
  53.                 playerAttack = 6;
  54.                 playerAttackModifer = 2;
  55.                 playerDefense = 5;
  56.                 playerDefenseModifer = 1;
  57.                 break;
  58.                
  59.             case "thief":
  60.                 //changing stat variables for the thief class.
  61.                 playerHealth = 75;
  62.                 playerHealthModifer = 5;
  63.                 playerAttack = 7;
  64.                 playerAttackModifer = 2;
  65.                 playerDefense = 3;
  66.                 playerDefenseModifer = 1;
  67.                 break;
  68.             default:
  69.                 // changing the stat variables for the deprived class.
  70.                 playerHealth = 50;
  71.                 playerHealthModifer = 1;
  72.                 playerAttack = 4;
  73.                 playerAttackModifer = 1;
  74.                 playerDefense = 3;
  75.                 playerDefenseModifer = 1;
  76.                 break;
  77.         }
  78.         // stating user's inputed name and stating the purpose.
  79.         System.out.println(userName + ", defeat the slimes.");
  80.         //do loop sending player stats out to a method to do the battle encounter.
  81.         do{
  82.         // sending the stats to the battle method and saving return in playerHealth.
  83.         playerHealth = Battle(userName, playerHealth, playerAttack, playerDefense,battleNum);
  84.         // if statement to allocate score if players health is above 0
  85.         if(playerHealth > 0){
  86.             score = score + 5 * scoreModifer++;
  87.             exp += 25 + score;
  88.         }
  89.         else;
  90.         // asking player a true or false quest, would you like to keep playing.
  91.         System.out.println("Would you like to keep playing? True or false");
  92.         //saving true or false response into answer
  93.         // using scanner to take take next input and saving as of health.
  94.         answer = scan.nextBoolean();
  95.         // if statement, if answer is false then print message and end game.
  96.         if (answer == false) {
  97.             System.out.println("Thanks for playing, your score was " + score);
  98.             System.exit(0);
  99.         }    
  100.         else;
  101.         if(exp >= 100) {
  102.             exp = exp - 100;
  103.             // upgrading the players attacks by the modifer each encounter.
  104.             playerAttack = playerAttack + (int) (Math.random() * playerAttackModifer);
  105.             playerHealth = playerHealth + (int) (Math.random() * playerHealthModifer);
  106.             playerDefense = playerDefense + (int) (Math.random() * playerDefenseModifer);
  107.         }
  108.         else;
  109.         // counting up the battle num.
  110.         battleNum += 2;
  111.        
  112.         }
  113.         while(playerHealth > 0 || answer == true);
  114.         System.out.println("You have died, your score was " + score);
  115.        
  116.     }
  117.     //method for the battle loop.
  118.     public static int Battle(String userName, int newPlayerHealth, int playerAttack, int playerDefense, int battleNum) {
  119.     //creating enemies stats
  120.     int playerHealth = newPlayerHealth;
  121.     // enemy states are randomized with a floor set by the number of encounters.
  122.     int slimeHealth = (int) (Math.random() * 20 + 1 + battleNum);
  123.     int slimeAttack = (int) (Math.random() * 10 + battleNum);
  124.     int slimeDefense = (int) (Math.random() * 5 + battleNum);
  125.     //battle loop
  126.     System.out.println("Encountered Slime.");
  127.     do {
  128.         // printing out players health.
  129.         System.out.println(userName + "'s Health: " + playerHealth);
  130.         // giving player the option of attacking or running.
  131.         System.out.println("Attack, defend or run?");
  132.         // creating scanner
  133.         Scanner scan = new Scanner(System.in);
  134.         // saving response into response string.
  135.         String response = scan.nextLine();
  136.         // switch for the response
  137.         switch (response) {
  138.             // case run, setting the slimes health to zero and breaking the switch.
  139.             case "run":
  140.                 slimeHealth = 0;
  141.                 break;
  142.             case "Defend":
  143.                 //defending against next attack.
  144.                 int playerDamage = (slimeAttack - (playerDefense * 2));
  145.                 if(playerDamage > 0){
  146.                     playerHealth = playerHealth - playerDamage;
  147.                 }
  148.                 else;
  149.                 break;
  150.             case "Attack":
  151.                 //attack case doing some math to decide damage
  152.                 int slimeDamage =(playerAttack - slimeDefense);
  153.                 if(slimeDamage > 0) {
  154.                     slimeHealth = slimeHealth - slimeDamage;
  155.                 }
  156.                 else;
  157.                 // detecting if slime health is still above 0 before allowing it to damage player.
  158.                 if(slimeHealth > 0) {
  159.                    playerDamage = (slimeAttack - playerDefense);
  160.                    if(playerDamage > 0) {
  161.                        playerHealth = playerHealth - playerDamage;
  162.                    }
  163.                    else;
  164.                 }
  165.                 else;
  166.                 break;
  167.             default:
  168.                 ;
  169.                 break;
  170.         }
  171.        
  172.        
  173.        
  174.     }
  175.     // do loop goes on until either player or slime's health is 0 or lower.
  176.     while(playerHealth > 0 && slimeHealth > 0);
  177.    
  178.     // returning player's health.
  179.     return playerHealth;
  180.     }
  181.    
  182.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement