Advertisement
Guest User

Finished Game 5/22 w/ Comments

a guest
May 22nd, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.19 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Game {
  5.    
  6.     static Scanner input = new Scanner(System.in);
  7.     static String charType; // character type
  8.     static Character player; // player info
  9.     static Random rand = new Random();
  10.  
  11.     public static void main(String[] args) {
  12.         startGame();
  13.         player = new Character(charType);
  14.         //pause();
  15.         intro();
  16.         System.out.println();
  17.         System.out.println();
  18.         System.out.print("Start out by choosing a direction to move: ");
  19.         movePlayer();
  20.        
  21.         while (player.getX() != 5 || player.getY() != 5) {
  22.             if (player.getX() == 1 && player.getY() == 1) {
  23.                 boss();
  24.             }
  25.             if (player.getX() == 2 && player.getY() == 1) {
  26.                 advise("South");
  27.             }
  28.             if (player.getX() == 3 && player.getY() == 1) {
  29.                 weakVil();
  30.             }
  31.             if (player.getX() == 4 && player.getY() == 1) {
  32.                 mislead("East");
  33.             }
  34.             if (player.getX() == 5 && player.getY() == 1) {
  35.                 tornado();
  36.             }
  37.             if (player.getX() == 1 && player.getY() == 2) {
  38.                 mislead("North");
  39.             }
  40.             if (player.getX() == 2 && player.getY() == 2) {
  41.                 playerChoice();
  42.             }
  43.             if (player.getX() == 3 && player.getY() == 2) {
  44.                 quickSand();
  45.             }
  46.             if (player.getX() == 4 && player.getY() == 2) {
  47.                 System.out.println();
  48.                 if (player.getHealth() + 10 > 100) {
  49.                     player.setNumPots(player.getNumPots() + 1);
  50.                     System.out.println("You already have max health. Instead of getting health, you craft it into a potion. You now have" + player.getNumPots() + " potions.");
  51.                 }
  52.                 else {
  53.                 System.out.println("You found health.");
  54.                 plusHP(10);
  55.                 getStats();
  56.                 System.out.println();
  57.                 }
  58.             }
  59.             if (player.getX() == 5 && player.getY() == 2) {
  60.                 weakVil();
  61.             }
  62.             if (player.getX() == 1 && player.getY() == 3) {
  63.                 // start
  64.             }
  65.             if (player.getX() == 2 && player.getY() == 3) {
  66.                 weakVil();
  67.             }
  68.             if (player.getX() == 3 && player.getY() == 3) {
  69.                 System.out.println();
  70.                 System.out.println("You found damage.");
  71.                 plusDamage(5);
  72.                 getStats();
  73.                 System.out.println();
  74.             }
  75.             if (player.getX() == 4 && player.getY() == 3) {
  76.                 weakVil();
  77.             }
  78.             if (player.getX() == 5 && player.getY() == 3) {
  79.                 System.out.println();
  80.                 if (player.getHealth() + 10 > 100) {
  81.                     player.setNumPots(player.getNumPots() + 1);
  82.                     System.out.println("You already have max health. Instead of getting health, you craft it into a potion. You now have" + player.getNumPots() + " potions.");
  83.                 }
  84.                 else {
  85.                 System.out.println("You found health.");
  86.                 plusHP(10);
  87.                 getStats();
  88.                 System.out.println();
  89.                 }
  90.             }
  91.             if (player.getX() == 1 && player.getY() == 4) {
  92.                 weakVil();
  93.             }
  94.             if (player.getX() == 2 && player.getY() == 4) {
  95.                 System.out.println();
  96.                 if (player.getHealth() + 5 > 100) {
  97.                     player.setNumPots(player.getNumPots() + 1);
  98.                     System.out.println("You already have max health. Instead of getting health, you craft it into a potion. You now have" + player.getNumPots() + " potions.");
  99.                 }
  100.                 else {
  101.                 System.out.println("You found health.");
  102.                 plusHP(5);
  103.                 getStats();
  104.                 System.out.println();
  105.                 }
  106.             }
  107.             if (player.getX() == 3 && player.getY() == 4) {
  108.                 weakVil();
  109.             }
  110.             if (player.getX() == 4 && player.getY() == 4) {
  111.                 System.out.println();
  112.                 if (player.getHealth() + 10 > 100) {
  113.                     player.setNumPots(player.getNumPots() + 1);
  114.                     System.out.println("You already have max health. Instead of getting health, you craft it into a potion. You now have" + player.getNumPots() + " potions.");
  115.                 }
  116.                 else {
  117.                 System.out.println("You found health.");
  118.                 plusHP(10);
  119.                 getStats();
  120.                 System.out.println();
  121.                 }
  122.             }
  123.             if (player.getX() == 5 && player.getY() == 4) {
  124.                 megaBoss();
  125.             }
  126.             if (player.getX() == 1 && player.getY() == 5) {
  127.                 System.out.println();
  128.                 if (player.getHealth() + 10 > 100) {
  129.                     player.setNumPots(player.getNumPots() + 1);
  130.                     System.out.println("You already have max health. Instead of getting health, you craft it into a potion. You now have" + player.getNumPots() + " potions.");
  131.                 }
  132.                 else {
  133.                 System.out.println("You found health.");
  134.                 plusHP(10);
  135.                 getStats();
  136.                 System.out.println();
  137.                 }
  138.             }
  139.             if (player.getX() == 2 && player.getY() == 5) {
  140.                 weakVil();
  141.             }
  142.             if (player.getX() == 3 && player.getY() == 5) {
  143.                 System.out.println();
  144.                 if (player.getHealth() + 10 > 100) {
  145.                     player.setNumPots(player.getNumPots() + 1);
  146.                     System.out.println("You already have max health. Instead of getting health, you craft it into a potion. You now have " + player.getNumPots() + " potions.");
  147.                     System.out.println();
  148.                 }
  149.                 else {
  150.                 System.out.println("You found health.");
  151.                 plusHP(10);
  152.                 getStats();
  153.                 System.out.println();
  154.                 }
  155.             }
  156.             if (player.getX() == 4 && player.getY() == 5) {
  157.                 megaBoss();
  158.             }
  159.             if (player.getX() == 5 && player.getY() == 5) {
  160.                 System.out.println("You defeat the Dark Fantasy and become King.");
  161.             }
  162.            
  163.             System.out.print("Enter another direction to move: ");
  164.             movePlayer();
  165.         }
  166.            
  167.  
  168.     }
  169.  
  170.     public static void startGame() {
  171.         System.out.println("Welcome to Dark Fantasy");
  172.         System.out.println("Choose a Character: Warrior, Wizard, or Titan");
  173.         charType = input.nextLine();
  174.         System.out.println();
  175.     }
  176.    
  177.     public static void intro() {
  178.         System.out.println();
  179.         System.out.println("Press enter to continue");
  180.         String dummy = input.nextLine();
  181.         System.out.println();
  182.         System.out.println();
  183.         System.out.println("Welcome to your journey!");
  184.     }
  185.    
  186.     public static void movePlayer() {
  187.         String direction = input.nextLine();
  188.         if (direction.equalsIgnoreCase("north") || direction.equalsIgnoreCase("south") || direction.equalsIgnoreCase("east") || direction.equalsIgnoreCase("west")) {
  189.             player.updatePos(direction.toLowerCase());
  190.         }
  191.         else {
  192.             System.out.println("Invalid direction, please try again.");
  193.             movePlayer();
  194.         }
  195.        
  196.         /* Old Method of Direction Input Validation, broken with v1.6
  197.         switch (direction) {
  198.         case "north": player.updatePos(direction); break;
  199.         case "south": player.updatePos(direction); break;
  200.         case "east": player.updatePos(direction); break;
  201.         case "west": player.updatePos(direction); break;
  202.         default: System.out.println("Invalid direction, please try again."); movePlayer(); break;
  203.         }
  204.         */
  205.     }
  206.    
  207.     public static void pause() {
  208.         String dummy = input.nextLine();
  209.         System.out.println();
  210.     }
  211.    
  212.     //methods for the levels
  213.     public static void megaBoss() {
  214.         System.out.println();
  215.         System.out.println("------------------------");
  216.         System.out.println();
  217.         System.out.println("A Dragon approaches you!");
  218.         System.out.println("Dragons have a total health of 100 and attack power of 100. Fight off the Dragon to survive.");
  219.         monsterAttack(100,100,"Dragon");
  220.     }
  221.     public static void boss() {
  222.         System.out.println();
  223.         System.out.println("------------------------");
  224.         System.out.println();
  225.         System.out.println("A Cyclops approaches you!");
  226.         System.out.println("Cyclops have a total health of 40 and attack power of 60. Fight off the Cyclops to survive.");
  227.         monsterAttack(40,60,"Cyclops");
  228.     }
  229.     public static void weakVil() {
  230.         System.out.println();
  231.         System.out.println("------------------------");
  232.         System.out.println();
  233.         System.out.println("A Troll approaches you!");
  234.         System.out.println("Troll have a total health of 10 and attack power of 5. Fight off the Troll to survive.");
  235.         monsterAttack(10,5,"Troll");
  236.     }
  237.     public static void tornado() {
  238.         /* generates a random number and then
  239.          * selects a random X,Y position via
  240.          * the switch statement. this allows
  241.          * for us to put the player in a
  242.          * random square
  243.          */
  244.         System.out.println();
  245.         System.out.println("You get sucked into a tornado. The wind places you back into a different square.");
  246.         System.out.println("------------------------------------------------------------------------------------");
  247.         try {
  248.             Thread.sleep(2500);
  249.         } catch(InterruptedException ex) {
  250.             Thread.currentThread().interrupt();
  251.         }
  252.         int randInt = rand.nextInt(21) + 1;
  253.         switch (randInt) {
  254.         case 1: player.setX(1); player.setY(1); break;
  255.         case 2: player.setX(1); player.setY(2); break;
  256.         case 3: player.setX(1); player.setY(3); break;
  257.         case 4: player.setX(1); player.setY(4); break;
  258.         case 5: player.setX(1); player.setY(5); break;
  259.         case 6: player.setX(2); player.setY(1); break;
  260.         case 7: player.setX(2); player.setY(2); break;
  261.         case 8: player.setX(2); player.setY(3); break;
  262.         case 9: player.setX(2); player.setY(4); break;
  263.         case 10: player.setX(2); player.setY(5); break;
  264.         case 11: player.setX(3); player.setY(1); break;
  265.         case 12: player.setX(3); player.setY(2); break;
  266.         case 13: player.setX(3); player.setY(3); break;
  267.         case 14: player.setX(3); player.setY(4); break;
  268.         case 15: player.setX(3); player.setY(5); break;
  269.         case 16: player.setX(4); player.setY(1); break;
  270.         case 17: player.setX(4); player.setY(2); break;
  271.         case 18: player.setX(4); player.setY(3); break;
  272.         case 19: player.setX(4); player.setY(4); break;
  273.         case 20: player.setX(5); player.setY(2); break;
  274.         case 21: player.setX(5); player.setY(3); break;
  275.         }
  276.         //System.out.println("The tornado threw you into " + player.getX() + "," + player.getY());
  277.         System.out.println();
  278.     }
  279.     public static void quickSand() { // player chooses to sacrifice -10hp or -10dmg
  280.         System.out.println();
  281.         System.out.println("You have fallen on a patch of quicksand and you must sacrifice either 10HP or 10DMG to escape.");
  282.         Scanner input = new Scanner(System.in);
  283.         System.out.print("Enter 1 to choose HP, Enter 2 to choose DMG: ");
  284.         int choice = input.nextInt();
  285.         if (choice == 1) {
  286.             if (player.getHealth() - 10 <= 0) { // check and see if player died from this sacrifice
  287.                 minusHP(10);
  288.                 checkHealth();
  289.             }
  290.             else {
  291.                 minusHP(10);
  292.             }
  293.         }
  294.         else if (choice == 2)
  295.             minusDamage(10);
  296.         System.out.println();
  297.         getStats();
  298.         System.out.println();
  299.     }
  300.     public static void mislead(String direction) {
  301.         System.out.println();
  302.         System.out.println("Hello sir, Are you on your way to the great land of Finish? Well, I suggest you go " + direction + ". It's quicker!");
  303.         System.out.println();
  304.        
  305.     }
  306.     public static void advise(String direction) {
  307.         System.out.println();
  308.         System.out.println("Hey, I'm Hero. I suggest you to go " + direction + ".");
  309.         System.out.println();
  310.     }
  311.     public static void playerChoice() { // player is given a choice of +5hp or +10 dmg
  312.         System.out.println();
  313.         System.out.println("You have been given a choice, +5 HP or +10DMG.");
  314.         Scanner input = new Scanner(System.in);
  315.         System.out.print("Enter 1 to choose HP, Enter 2 to choose DMG: ");
  316.         int choice = input.nextInt();
  317.         if (choice == 1) {
  318.             System.out.println();
  319.             if (player.getHealth() + 5 > 100) { // if the additional hp added onto the player hp is greater than 100, craft hp into a potion
  320.                 player.setNumPots(player.getNumPots() + 1);
  321.                 System.out.println("You already have max health. Instead of getting health, you craft it into a potion. You now have " + player.getNumPots() + " potions.");
  322.             }
  323.             else {
  324.             plusHP(5);
  325.             }
  326.         }
  327.         else if (choice == 2)
  328.             plusDamage(10);
  329.         System.out.println();
  330.         getStats();
  331.         System.out.println();
  332.  
  333.     }
  334.     public static void plusHP(int ph) {
  335.         int newHealth = player.getHealth() + ph;
  336.         player.setHealth(newHealth);
  337.     }
  338.     public static void minusHP(int mh) {
  339.         int newHealth = player.getHealth() - mh;
  340.         player.setHealth(newHealth);
  341.     }
  342.     public static void plusDamage(int pd) {
  343.         int newDamage = player.getDamage() + pd;
  344.         player.setDamage(newDamage);
  345.     }
  346.     public static void minusDamage(int md) {
  347.         int newDamage = player.getDamage() - md;
  348.         player.setDamage(newDamage);
  349.     }
  350.     public static void getStats() { // displays the players health, dmg, and number of health pots
  351.         System.out.println("----------------------------------------------");
  352.         System.out.print("| HP: " + player.getHealth() + "/100" + "   " + "DMG: " + player.getDamage() + "/100" + "   " + "Health Pots: " + player.getNumPots());
  353.         System.out.println(" |");
  354.         System.out.println("----------------------------------------------");
  355.     }
  356.     public static void monsterAttack(int mh, int md, String name) {
  357.        
  358.         /* Really big and complicated method, don't have to go into
  359.          * so much detail commenting, but basically this method is
  360.          * for the random number crit system. It generates a random
  361.          * number from player and monster damage, determines if it
  362.          * is a critical strike, and runs through the monster battle.
  363.          * It also has the ability to randomly drop health potions
  364.          * and apply them when you choose to.
  365.          */
  366.        
  367.         int randDamageGiven = rand.nextInt(player.getDamage() + 1) + 1;
  368.         int randDamageTaken = rand.nextInt(md + 1) + 1;
  369.         int monsterHP = mh;
  370.         monsterHP-=randDamageGiven;
  371.         System.out.println();
  372.         if (randDamageGiven >= (0.80 * player.getDamage()))
  373.             System.out.println("Critical Strike! You strike the monster for " + randDamageGiven);
  374.         else
  375.              System.out.println("You strike the monster for " + randDamageGiven);
  376.        
  377.         if (monsterHP > 0) {
  378.             minusHP(randDamageTaken);
  379.             if (player.getHealth() <= 0) {
  380.                 System.out.println("The monster strikes you for " + randDamageTaken);
  381.                 checkHealth();
  382.             }
  383.             else {
  384.             System.out.println();
  385.                 if (randDamageTaken >= (0.80 * md)) {
  386.                     System.out.println("The monster crits you for " + randDamageTaken);
  387.                 }
  388.                 else {
  389.                     System.out.println("The monster attacks you for " + randDamageTaken);
  390.                 }
  391.                 System.out.println();
  392.                 System.out.println("---------------------------------------------------");
  393.                 System.out.print("| HP: " + player.getHealth() + "/100" + "   " + "Monster HP: " + monsterHP + "/100" + "   " + "Health Pots: " + player.getNumPots());
  394.                 System.out.println(" |");
  395.                 System.out.println("---------------------------------------------------");
  396.                 System.out.println();
  397.                 System.out.println("Do you choose to attack again or use a health potion?");
  398.                 System.out.print("Enter 1 to drink a potion, Enter 2 to attack: ");
  399.                 Scanner input = new Scanner(System.in);
  400.                 int choice = input.nextInt();
  401.                 System.out.println();
  402.                 if (choice == 1) {
  403.                     if (player.getNumPots() > 0) {
  404.                         player.setHealth(100);
  405.                         player.setNumPots(player.getNumPots() - 1);
  406.                         getStats();
  407.                         try {
  408.                             Thread.sleep(2000);
  409.                         } catch(InterruptedException ex) {
  410.                             Thread.currentThread().interrupt();
  411.                         }
  412.                         monsterAttack(monsterHP,md,name);
  413.                     }
  414.                     else {
  415.                         System.out.println("You are out of health potions! Fight off the monster to survive.");
  416.                         try {
  417.                             Thread.sleep(2000);
  418.                         } catch(InterruptedException ex) {
  419.                             Thread.currentThread().interrupt();
  420.                         }
  421.                         monsterAttack(monsterHP,md,name);
  422.                     }
  423.                 }
  424.                 else if (choice == 2) {
  425.                     try {
  426.                         Thread.sleep(2000);
  427.                     } catch(InterruptedException ex) {
  428.                         Thread.currentThread().interrupt();
  429.                     }
  430.                     monsterAttack(monsterHP,md,name);
  431.                 }
  432.             }
  433.  
  434.         }
  435.         else {
  436.             System.out.println();
  437.             System.out.println("You have defeated the " + name + "!");
  438.             if (name.equals("Dragon")) {
  439.                 System.out.println();
  440.                 System.out.println("You successfully defeated the Dark Fantasy!");
  441.                 System.out.println("Thank you for playing!");
  442.                 System.exit(0);
  443.             }
  444.             else {
  445.             int randChance = rand.nextInt(2);
  446.             try {
  447.                 Thread.sleep(1000);
  448.             } catch(InterruptedException ex) {
  449.                 Thread.currentThread().interrupt();
  450.             }
  451.             switch (randChance) { // 50/50 chance for a potion to drop
  452.             case 0: player.setNumPots(player.getNumPots() + 1); System.out.println("The " + name + " dropped a health potion! You now have " + player.getNumPots() + " potions."); System.out.println(); System.out.println("------------------------"); System.out.println(); break;
  453.             case 1: System.out.println(); System.out.println("------------------------"); System.out.println(); break;
  454.             }
  455.             }
  456.         }
  457.        
  458.        
  459.        
  460.        
  461.     }
  462.     public static void checkHealth() {
  463.         //check if players health is less than 0. if less than 0, game is over and program is terminated
  464.         if (player.getHealth() <= 0) {
  465.         System.out.println();
  466.         System.out.println("You were defeated by the monsters of the Dark Fantasy.");
  467.         System.out.println();
  468.         System.out.println("Thank you for playing!");
  469.         System.exit(0);
  470.         }
  471.     }
  472.     // end of level methods
  473.    
  474. }
  475.  
  476. class Character {
  477.     private int health;
  478.     private int damage;
  479.     private int numPots;
  480.     private String type;
  481.     private int PlayerPosX = 1;
  482.     private int PlayerPosY = 3;
  483.  
  484. public Character(String t) {
  485.     if (t.equalsIgnoreCase("Warrior")) {
  486.         this.type = "Warrior";
  487.         health = 50;
  488.         damage = 4;
  489.         numPots = 1;
  490.     }
  491.     if (t.equalsIgnoreCase("Wizard")) {
  492.         this.type = "Wizard";
  493.         health = 75;
  494.         damage = 75;
  495.         numPots = 2;
  496.     }
  497.     if (t.equalsIgnoreCase("Titan")) {
  498.         this.type = "Titan";
  499.         health = 100;
  500.         damage = 50;
  501.         numPots = 1;
  502.     }
  503.    
  504.    
  505.  
  506. }
  507.  
  508. public int getHealth() {return health;}
  509. public int getDamage() {return damage;}
  510. public int getNumPots() {return numPots;}
  511. public int getX() {return PlayerPosX;}
  512. public int getY() {return PlayerPosY;}
  513. public void setX(int x) {PlayerPosX = x;}
  514. public void setY(int y) {PlayerPosY = y;}
  515.  
  516. public void setHealth(int h) {health = h;}
  517. public void setDamage(int d) {damage = d;}
  518. public void setNumPots(int n) {numPots = n;}
  519.  
  520. public void updatePos(String dir) {
  521.    
  522.     if (dir.equals("west"))
  523.         PlayerPosX--;
  524.     if (dir.equals("east"))
  525.         PlayerPosX++;
  526.     if (dir.equals("north"))
  527.         PlayerPosY--;
  528.     if (dir.equals("south"))
  529.         PlayerPosY++;
  530.    
  531.    
  532.     /* Old Movement System, broken with v1.6
  533.     switch(dir) {
  534.     case "west": PlayerPosX--; break;
  535.     case "east": PlayerPosX++; break;
  536.     case "north": PlayerPosY--; break;
  537.     case "south": PlayerPosY++; break;
  538.     }
  539.     */
  540.    
  541.    
  542.     if (PlayerPosX < 0) {
  543.         System.out.println("Invalid Move");
  544.         PlayerPosX++;}
  545.     if (PlayerPosY < 0) {
  546.         System.out.println("Invalid Move");
  547.         PlayerPosY++;}
  548.     if (PlayerPosX > 5) {
  549.         System.out.println("Invalid Move");
  550.         PlayerPosX--;}
  551.     if (PlayerPosY > 5) {
  552.         System.out.println("Invalid Move");
  553.         PlayerPosY--;}
  554.     //System.out.println(this.getX() + "," + this.getY());
  555.     }
  556.  
  557. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement