Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1.  import java.util.Scanner;
  2.  
  3.  public class JZORK
  4.  {
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         final Scanner keyboard = new Scanner(System.in);
  9.  
  10.         // String to store player input
  11.         String[] inputTable;
  12.  
  13.         // Get tables for storing data into main function
  14.         String verbs[][] = verbs(), nouns[] = nouns(), rooms[][] = rooms();
  15.  
  16.         // Variables for recording player status
  17.         int currentRoom = 0;
  18.         int Score;
  19.         boolean dead = false;
  20.         boolean win = false;
  21.  
  22.         // main game loop, end if player dies or wins
  23.         while (!dead && !win) {
  24.  
  25.             // print the room's description
  26.             System.out.println(rooms[currentRoom][1]);
  27.  
  28.             // Get player input and split it into indiviual words separated by spaces and store it into inputTable
  29.             inputTable = keyboard.nextLine().toLowerCase().split(" ", 16);
  30.  
  31.             // send inputTable to be broken down and understood as commands
  32.             ParseInput(inputTable, verbs, nouns);
  33.  
  34.         }
  35.  
  36.         // end game messages
  37.         if (dead) {
  38.             System.out.println("You lose!");
  39.         } else if (win) {
  40.             System.out.println("You win!");
  41.         }
  42.  
  43.     }
  44.  
  45.     // read actions performed by the player -- placeholder for now
  46.     public static String DoAction(String input) {
  47.         return "hello";
  48.     }
  49.  
  50.     // parse player input into machine-understandable grammar
  51.     private static String ParseInput(String[] inputTable, String[][] verbs, String[] nouns) {
  52.         // number for referencing the verbs a noun can be affected by
  53.         int verbID;
  54.  
  55.         // initiate as false at first, if both are true at the end, game will take an action
  56.         boolean foundVerb = false, foundNoun = false;
  57.  
  58.         // search dictionaries for the inputted words and see if they exist
  59.         for (String count : inputTable) {
  60.             for (int I_verbs1 = 0; I_verbs1 < verbs.length; I_verbs1++) {
  61.                 for (int I_verbs2 = 0; I_verbs2 < verbs[I_verbs1].length; I_verbs2++) {
  62.                     if (count.equals(verbs[I_verbs1][I_verbs2])) {
  63.                         System.out.print("Verb! ");
  64.                         verbID = I_verbs1;
  65.                         System.out.println(count + " " + verbID);
  66.                         foundVerb = true;
  67.                     }
  68.                 }
  69.             }
  70.             for (int I_nouns = 0; I_nouns < nouns.length; I_nouns++) {
  71.                 if (count.equals(nouns[I_nouns])) {
  72.                         System.out.print("Noun! ");
  73.                         System.out.println(count);
  74.                         foundNoun = true;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         // if both a noun and a verb are found, the game will do an action
  80.         if (foundVerb && foundNoun) {
  81.             System.out.println("Good Sentance!");
  82.         } else {
  83.             System.out.println("I did not understand that!");
  84.         }
  85.  
  86.         // reset the switches to false
  87.         foundVerb = false;
  88.         foundNoun = false;
  89.  
  90.         return "waagh";
  91.     }
  92.  
  93.     // Data
  94.  
  95.     public static String[] nouns()
  96.     {
  97.         // list of nouns
  98.         String[] nouns = {
  99.             "door",
  100.             "east",
  101.             "entrance",
  102.             "north",
  103.             "south",
  104.             "valley",
  105.             "west"
  106.         };
  107.  
  108.         return nouns;
  109.     }
  110.  
  111.     public static String[][] verbs()
  112.     {
  113.         // list of verbs in groups of verbs that are understood as the same thing
  114.         String[][] verbs = {
  115.             {
  116.                 "examine",
  117.                 "look"
  118.             }, {
  119.                 "go",
  120.                 "walk"
  121.             }, {
  122.                 "open"
  123.             }
  124.         };
  125.  
  126.         // return the table given here
  127.         return verbs;
  128.     }
  129.  
  130.     public static String[][] rooms()
  131.     {
  132.         // list of rooms in pairs of names and descriptions
  133.         String[][]rooms = {
  134.             {
  135.                 "Valley",
  136.                 "You are in a valley"
  137.             }, {
  138.                 "Entrance",
  139.                 "You stand at the entrance to an ancient temple"
  140.             }, {
  141.                 "Foyer",
  142.                 " "
  143.             }
  144.         };
  145.  
  146.         return rooms;
  147.     }
  148.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement