Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication1;
- import java.util.Scanner;
- public class MainClass {
- public static void main(String[] args) {
- //creating initial variables
- //variable for number of encounters.
- int battleNum = 0;
- //score related variables
- int score = 0;
- int scoreModifer = 1;
- // boolean for loop.
- boolean answer;
- //creating exp variable
- int exp = 0;
- //creating variables for the player health and health modifer.
- int playerHealth;
- int playerHealthModifer;
- //creating variables for the player attack and attack modifer.
- int playerAttack;
- int playerAttackModifer;
- //creating variables for the player defense and defense modifer.
- int playerDefense;
- int playerDefenseModifer;
- //asking and recieving username using scanner class
- System.out.println("welcome, please enter an user name.");
- //creating scanner to take input from the keyboard.string.
- Scanner scan = new Scanner(System.in);
- // using scanner to take take next input and saving as userName
- String userName = scan.nextLine();
- //asking user for their choice of class.
- System.out.println("Would you like to be a warrior, fighter, or a thief?");
- //scanner taking next input as string.
- String classChoice = scan.nextLine();
- //switch statement deciding which class user decided, default is a "deprived" class.
- switch (classChoice) {
- case "warrior":
- //changing stat variables for the warrior class
- playerHealth = 150;
- playerHealthModifer = 10;
- playerAttack = 4;
- playerAttackModifer = 1;
- playerDefense = 7;
- playerDefenseModifer = 2;
- break;
- case "fighter":
- //changing stat variables for the fighter class.
- playerHealth = 90;
- playerHealthModifer = 7;
- playerAttack = 6;
- playerAttackModifer = 2;
- playerDefense = 5;
- playerDefenseModifer = 1;
- break;
- case "thief":
- //changing stat variables for the thief class.
- playerHealth = 75;
- playerHealthModifer = 5;
- playerAttack = 7;
- playerAttackModifer = 2;
- playerDefense = 3;
- playerDefenseModifer = 1;
- break;
- default:
- // changing the stat variables for the deprived class.
- playerHealth = 50;
- playerHealthModifer = 1;
- playerAttack = 4;
- playerAttackModifer = 1;
- playerDefense = 3;
- playerDefenseModifer = 1;
- break;
- }
- // stating user's inputed name and stating the purpose.
- System.out.println(userName + ", defeat the slimes.");
- //do loop sending player stats out to a method to do the battle encounter.
- do{
- // sending the stats to the battle method and saving return in playerHealth.
- playerHealth = Battle(userName, playerHealth, playerAttack, playerDefense,battleNum);
- // if statement to allocate score if players health is above 0
- if(playerHealth > 0){
- score = score + 5 * scoreModifer++;
- exp += 25 + score;
- }
- else;
- // asking player a true or false quest, would you like to keep playing.
- System.out.println("Would you like to keep playing? True or false");
- //saving true or false response into answer
- // using scanner to take take next input and saving as of health.
- answer = scan.nextBoolean();
- // if statement, if answer is false then print message and end game.
- if (answer == false) {
- System.out.println("Thanks for playing, your score was " + score);
- System.exit(0);
- }
- else;
- if(exp >= 100) {
- exp = exp - 100;
- // upgrading the players attacks by the modifer each encounter.
- playerAttack = playerAttack + (int) (Math.random() * playerAttackModifer);
- playerHealth = playerHealth + (int) (Math.random() * playerHealthModifer);
- playerDefense = playerDefense + (int) (Math.random() * playerDefenseModifer);
- }
- else;
- // counting up the battle num.
- battleNum += 2;
- }
- while(playerHealth > 0 || answer == true);
- System.out.println("You have died, your score was " + score);
- }
- //method for the battle loop.
- public static int Battle(String userName, int newPlayerHealth, int playerAttack, int playerDefense, int battleNum) {
- //creating enemies stats
- int playerHealth = newPlayerHealth;
- // enemy states are randomized with a floor set by the number of encounters.
- int slimeHealth = (int) (Math.random() * 20 + 1 + battleNum);
- int slimeAttack = (int) (Math.random() * 10 + battleNum);
- int slimeDefense = (int) (Math.random() * 5 + battleNum);
- //battle loop
- System.out.println("Encountered Slime.");
- do {
- // printing out players health.
- System.out.println(userName + "'s Health: " + playerHealth);
- // giving player the option of attacking or running.
- System.out.println("Attack, defend or run?");
- // creating scanner
- Scanner scan = new Scanner(System.in);
- // saving response into response string.
- String response = scan.nextLine();
- // switch for the response
- switch (response) {
- // case run, setting the slimes health to zero and breaking the switch.
- case "run":
- slimeHealth = 0;
- break;
- case "Defend":
- //defending against next attack.
- int playerDamage = (slimeAttack - (playerDefense * 2));
- if(playerDamage > 0){
- playerHealth = playerHealth - playerDamage;
- }
- else;
- break;
- case "Attack":
- //attack case doing some math to decide damage
- int slimeDamage =(playerAttack - slimeDefense);
- if(slimeDamage > 0) {
- slimeHealth = slimeHealth - slimeDamage;
- }
- else;
- // detecting if slime health is still above 0 before allowing it to damage player.
- if(slimeHealth > 0) {
- playerDamage = (slimeAttack - playerDefense);
- if(playerDamage > 0) {
- playerHealth = playerHealth - playerDamage;
- }
- else;
- }
- else;
- break;
- default:
- ;
- break;
- }
- }
- // do loop goes on until either player or slime's health is 0 or lower.
- while(playerHealth > 0 && slimeHealth > 0);
- // returning player's health.
- return playerHealth;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement