Advertisement
Guest User

Game.java

a guest
Apr 1st, 2015
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.92 KB | None | 0 0
  1. package me.joejoe.rpg;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Game {
  6.  
  7.     public static String command, commandAttack, commandShop;
  8.  
  9.     public static int monsterRat = 100;
  10.     public static int monsterWeasel = 110;
  11.     public static int monsterBear = 120;
  12.    
  13.     public static int hp = 100;
  14.     public static int level = 0;
  15.     public static int mana = 10;
  16.     public static int expTotal = 0;
  17.     public static int goldTotal = 0;
  18.     public static int arrow, shuriken, bomb, hpPotion,
  19.             mpPotion, potion, items;
  20.    
  21.     public static int commandItem, commandBuy;
  22.  
  23.     public static boolean running = false;
  24.  
  25.     public static Client frame;
  26.     public static String[] choices;
  27.  
  28.     public static void main(String[] args) {
  29.         new Save();
  30.         frame = new Client();
  31.         frame.setVisible(true);
  32.         sendMessage("start");
  33.     }
  34.  
  35.     public static String getStringInput() {
  36.         return JOptionPane.showInputDialog("");
  37.     }
  38.  
  39.     public static int getIntegerInput() {
  40.         String userInput = JOptionPane.showInputDialog("");
  41.         char[] chars = userInput.toCharArray();
  42.         for (char c : chars) {
  43.             if (c > 47 && c < 58) {
  44.             } else {
  45.                 return -1;
  46.             }
  47.         }
  48.         return Integer.valueOf(userInput);
  49.     }
  50.  
  51.     // ******************************************
  52.     public static void append(String text) {
  53.         Client.textArea.append(text);
  54.     }
  55.  
  56.     public static void appendLine(String text) {
  57.         Client.textArea.append(text + "\n");
  58.     }
  59.  
  60.     public static void appendLine() {
  61.         Client.textArea.append("\n");
  62.     }
  63.  
  64.     // ******************************************
  65.     static String gameState = "started";
  66.  
  67.     public static void setState(String state) {
  68.         gameState = state;
  69.     }
  70.  
  71.     public static String getState() {
  72.         return gameState;
  73.     }
  74.  
  75.     public static void userInput(String input) {
  76.  
  77.         switch (gameState) {
  78.  
  79.         /**
  80.          * **** GAME JUST STARTED *****
  81.          */
  82.         case "started":
  83.             choices = getPossibleUserInput(gameState);
  84.             for (String choice : choices) {
  85.                 if (input.equals(choice)) {
  86.                     startOfTheGame(choice);
  87.                     break;
  88.                 }
  89.             }
  90.             break;
  91.         /**
  92.          * *************************************************************
  93.          */
  94.  
  95.         case "huntRat":
  96.             choices = getPossibleUserInput(gameState);
  97.             for (String choise : choices) {
  98.                 if (input.equals(choise)) {
  99.                     Monsters.huntRat(choise);
  100.                     break;
  101.                 }
  102.             }
  103.             break;
  104.         case "huntWeasel":
  105.             choices = getPossibleUserInput(gameState);
  106.             for (String choise : choices) {
  107.                 if (input.equals(choise)) {
  108.                     Monsters.huntWeasel(choise);
  109.                 }
  110.             }
  111.  
  112.             break;
  113.  
  114.         case "huntBear":
  115.             choices = getPossibleUserInput(gameState);
  116.             for (String choise : choices) {
  117.                 if (input.equals(choise)) {
  118.                     Monsters.huntBear(choise);
  119.                 }
  120.             }
  121.             break;
  122.  
  123.         case "gameOver":
  124.             choices = getPossibleUserInput(gameState);
  125.  
  126.             for (String choise : choices) {
  127.                 if (input.equals(choise)) {
  128.                     gameOver(choise);
  129.                 }
  130.             }
  131.             break;
  132.  
  133.         default:
  134.             break;
  135.         }
  136.     }
  137.  
  138.     public static String[] getPossibleUserInput(String gameState) {
  139.         switch (gameState) {
  140.         /**
  141.          * ************************************************************** THESE
  142.          * ARE POSSIBLE INPUTS FOR START POSITION OF THE GAME
  143.          * **************************************************************
  144.          */
  145.         case "started":
  146.             return new String[] { "Hunt", "Shop", "Sleep", "Stats", "LevelUp",
  147.                     "Inventory","Save", "Exit", "Admin", "hunt", "shop", "sleep",
  148.                     "stats", "levelup", "inventory","save", "exit" };
  149.             /**
  150.              * *************************************************************
  151.              */
  152.         case "huntRat":
  153.             return new String[] { "1", "2", "4" };
  154.             /**
  155.              * *************************************************************
  156.              */
  157.         case "huntWeasel":
  158.             return new String[] { "1", "2", "4" };
  159.             /**
  160.              * *************************************************************
  161.              */
  162.         case "huntBear":
  163.             return new String[] { "1", "2", "4" };
  164.             /**
  165.              * *************************************************************
  166.              */
  167.         case "gameOver":
  168.             return new String[] { "Yes", "No" };
  169.             /**
  170.              * *************************************************************
  171.              */
  172.         default:
  173.             return null;
  174.         }
  175.     }
  176.  
  177.     private static void startOfTheGame(String choice) {
  178.         switch (choice.toLowerCase()) {
  179.         case "hunt":
  180.             hunt();
  181.             break;
  182.         case "shop":
  183.             shop();
  184.             break;
  185.         case "sleep":
  186.             sleep();
  187.             break;
  188.         case "stats":
  189.             stats();
  190.             break;
  191.         case "levelup":
  192.             levelUp();
  193.             break;
  194.         case "inventory":
  195.             inventory();
  196.             break;
  197.         case "save":
  198.             new Save();
  199.             sendMessage("save");
  200.             sendMessage("menu");
  201.             break;
  202.         case "exit":
  203.             new Save();
  204.             System.exit(0);
  205.             break;
  206.         case "admin":
  207.             admin();
  208.             break;
  209.         }
  210.     }
  211.  
  212.     private static void hunt() {
  213.         int monsterChance = (int) (1 + Math.random() * 100 - 1 + 1);
  214.         if (monsterChance <= 50) {
  215.             gameState = "huntRat";
  216.  
  217.             sendMessage("rat");
  218.             sendMessage("combatChoose");
  219.         }
  220.  
  221.         if (monsterChance <= 90 && monsterChance > 50) {
  222.             gameState = "huntWeasel";
  223.             sendMessage("weasel");
  224.             sendMessage("combatChoose");
  225.         }
  226.         if (monsterChance <= 100 && monsterChance > 90) {
  227.             gameState = "huntBear";
  228.             sendMessage("bear");
  229.             sendMessage("combatChoose");
  230.         }
  231.  
  232.     }
  233.  
  234.     public static void sendMessage(String message) {
  235.         switch (message) {
  236.         case "start":
  237.             Client.textArea.setText("");
  238.             append("Welcome To The Game\n-----------------------\n");
  239.         case "menu":
  240.             appendLine("What do you want to do?\n" + "Hunt | Shop | Sleep "
  241.                     + "| Stats | \nLevelUp" + " | Inventory | Save | Exit |\n");
  242.             break;
  243.         case "rat":
  244.             Client.textArea.setText("");
  245.             appendLine("A wild Rat has appeared");
  246.             break;
  247.         case "combatChoose":
  248.             appendLine("What Skill do you want to use?"
  249.                     + "\n1.Attack, 2.Heal, 3.Item, 4.Run: ");
  250.             break;
  251.         case "weasel":
  252.             Client.textArea.setText("");
  253.             appendLine("A wild Weasel has appeared");
  254.             break;
  255.         case "bear":
  256.             Client.textArea.setText("");
  257.             appendLine("A wild Bear has appeared be careful!");
  258.             break;
  259.         case "save":
  260.             Client.textArea.setText("");
  261.             appendLine("Your game has been saved!\n");
  262.         default:
  263.             break;
  264.         }
  265.     }
  266.  
  267.     private static void gameOver(String choice) {
  268.         switch (choice) {
  269.         case "Yes":
  270.             gameState = "started";
  271.             sendMessage("start");
  272.             resetStats();
  273.             break;
  274.         case "No":
  275.             System.exit(0);
  276.             break;
  277.  
  278.         default:
  279.             break;
  280.         }
  281.     }
  282.  
  283.     public static void admin() {
  284.         hp = 100;
  285.         level = 1;
  286.         mana = 10;
  287.         expTotal += 2000;
  288.         goldTotal += 3000;
  289.         appendLine("You have admin EXP/LEVEL now!\n");
  290.     }
  291.  
  292.     public static void resetStats() {
  293.         hp = 100;
  294.         level = 0;
  295.         mana = 10;
  296.         expTotal = 0;
  297.         goldTotal = 0;
  298.         arrow = 0;
  299.         shuriken = 0;
  300.         bomb = 0;
  301.         hpPotion = 0;
  302.         mpPotion = 0;
  303.         potion = 0;
  304.         items = 0;
  305.         running = false;
  306.     }
  307.  
  308.     private static void sleep() {
  309.         hp = 100;
  310.         mana = 10;
  311.         Client.textArea.setText("");
  312.         appendLine("-You now have full hp and mana!\n");
  313.         sendMessage("menu");
  314.     }
  315.  
  316.     private static void stats() {
  317.         Client.textArea.setText("");
  318.         appendLine("-You have: " + expTotal + " exp!\n" + "-You have: "
  319.                 + goldTotal + " gold!\n" + "-Your level is: " + level + "\n");
  320.         sendMessage("menu");
  321.     }
  322.  
  323.     private static void inventory() {
  324.         Client.textArea.setText("");
  325.         appendLine("-You have: " + arrow + " arrow(s)\n" + "-You have: "
  326.                 + shuriken + " shuriken(s)\n" + "-You have: " + bomb
  327.                 + " bomb(s)\n" + "-You have: " + hpPotion
  328.                 + " Small Hp-Potion(s)\n" + "-You have: " + mpPotion
  329.                 + " Mana-Potion(s)\n" + "-You have: " + potion
  330.                 + " Large Hp-Potion(s)\n");
  331.         sendMessage("menu");
  332.     }
  333.  
  334.     private static void levelUp() {
  335.         Client.textArea.setText("");
  336.         if (expTotal >= 100) {
  337.             level++;
  338.             appendLine("CONGRATS! You have leveled up!" + "\nYour level is: "
  339.                     + level);
  340.             expTotal -= 100;
  341.         } else {
  342.             appendLine("You don't have enough exp! Hunt more!\n");
  343.         }
  344.         sendMessage("menu");
  345.     }
  346.  
  347.     public static void shop() {
  348.         Client.textArea.setText("");
  349.         appendLine("SHOPS:\nNOT IMPLEMENTED YET!\n");
  350.         sendMessage("menu");
  351.     }
  352.  
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement