Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class JZORK
- {
- public static void main(String[] args)
- {
- final Scanner keyboard = new Scanner(System.in);
- // String to store player input
- String[] inputTable;
- // Get tables for storing data into main function
- String verbs[][] = verbs(), nouns[] = nouns(), rooms[][] = rooms();
- // Variables for recording player status
- int currentRoom = 0;
- int Score;
- boolean dead = false;
- boolean win = false;
- // main game loop, end if player dies or wins
- while (!dead && !win) {
- // print the room's description
- System.out.println(rooms[currentRoom][1]);
- // Get player input and split it into indiviual words separated by spaces and store it into inputTable
- inputTable = keyboard.nextLine().toLowerCase().split(" ", 16);
- // send inputTable to be broken down and understood as commands
- ParseInput(inputTable, verbs, nouns);
- }
- // end game messages
- if (dead) {
- System.out.println("You lose!");
- } else if (win) {
- System.out.println("You win!");
- }
- }
- // read actions performed by the player -- placeholder for now
- public static String DoAction(String input) {
- return "hello";
- }
- // parse player input into machine-understandable grammar
- private static String ParseInput(String[] inputTable, String[][] verbs, String[] nouns) {
- // number for referencing the verbs a noun can be affected by
- int verbID;
- // initiate as false at first, if both are true at the end, game will take an action
- boolean foundVerb = false, foundNoun = false;
- // search dictionaries for the inputted words and see if they exist
- for (String count : inputTable) {
- for (int I_verbs1 = 0; I_verbs1 < verbs.length; I_verbs1++) {
- for (int I_verbs2 = 0; I_verbs2 < verbs[I_verbs1].length; I_verbs2++) {
- if (count.equals(verbs[I_verbs1][I_verbs2])) {
- System.out.print("Verb! ");
- verbID = I_verbs1;
- System.out.println(count + " " + verbID);
- foundVerb = true;
- }
- }
- }
- for (int I_nouns = 0; I_nouns < nouns.length; I_nouns++) {
- if (count.equals(nouns[I_nouns])) {
- System.out.print("Noun! ");
- System.out.println(count);
- foundNoun = true;
- }
- }
- }
- // if both a noun and a verb are found, the game will do an action
- if (foundVerb && foundNoun) {
- System.out.println("Good Sentance!");
- } else {
- System.out.println("I did not understand that!");
- }
- // reset the switches to false
- foundVerb = false;
- foundNoun = false;
- return "waagh";
- }
- // Data
- public static String[] nouns()
- {
- // list of nouns
- String[] nouns = {
- "door",
- "east",
- "entrance",
- "north",
- "south",
- "valley",
- "west"
- };
- return nouns;
- }
- public static String[][] verbs()
- {
- // list of verbs in groups of verbs that are understood as the same thing
- String[][] verbs = {
- {
- "examine",
- "look"
- }, {
- "go",
- "walk"
- }, {
- "open"
- }
- };
- // return the table given here
- return verbs;
- }
- public static String[][] rooms()
- {
- // list of rooms in pairs of names and descriptions
- String[][]rooms = {
- {
- "Valley",
- "You are in a valley"
- }, {
- "Entrance",
- "You stand at the entrance to an ancient temple"
- }, {
- "Foyer",
- " "
- }
- };
- return rooms;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement