public void run() { try { Telnet.writeLine(cs, "<fggreen> >>> Welcome to AdaMUD <<< <reset>"); Telnet.flushInput(cs); while ((player = s.getPlayerDB().login(cs, s)) == null) ; player.look(); while (true) { System.out.println("Waiting for message"); String message = Telnet.readLine(cs).trim(); if (message == null) { // Disconnected break; } if (message.equals("bye")) { Telnet.writeLine(cs, "Goodbye!"); break; } parseCommand(message); } cs.close(); } catch (IOException e) { System.out.println("Client error: " + e); } finally { s.getPlayerDB().remove(player); s.remove(cs); } }
public void checkInput(String input) { if (currentConvo != null) { if (input.equalsIgnoreCase("cancel")) { currentConvo = null; player.currentLoc.look(); } else { currentConvo.talk(input); } return; } System.out.println(input); Pattern lookPattern = Pattern.compile("^look(\\s|$)"); Pattern getPattern = Pattern.compile("^get(\\s|$)"); Pattern dropPattern = Pattern.compile("^drop(\\s|$)"); Pattern usePattern = Pattern.compile("^use(\\s|$)"); Pattern movePattern = Pattern.compile("^move(\\s|$)"); Pattern talkPattern = Pattern.compile("^talk(\\s|$)"); Pattern argumentPattern = Pattern.compile("\\s\\w+($|\\s)"); Pattern twoArgumentPattern = Pattern.compile( "\\s(\\w+|\\w+\\s\\w+)\\son\\s(\\w+|\\w+\\s\\w+)($|\\s)"); // makes UseOn work Pattern savePattern = Pattern.compile("^save(\\s|$)"); Pattern loadPattern = Pattern.compile("^load(\\s|$)"); Matcher argumentMatcher = argumentPattern.matcher(input); Matcher twoArgumentMatcher = twoArgumentPattern.matcher(input); // LOOK if (lookPattern.matcher(input).find()) { if (argumentMatcher.find()) { player.look(input.substring(argumentMatcher.start() + 1, input.length())); } else { player.look(""); } return; } // END LOOK // TALK if (talkPattern.matcher(input).find()) { if (argumentMatcher.find()) { player.talk(input.substring(argumentMatcher.start() + 1, input.length())); } else { Gui.setOutputText("You talk to yourself for a while, it was a rivetting conversation"); } return; } // GET/PICK UP if (getPattern.matcher(input).find()) { if (argumentMatcher.find()) { System.out.println("found argument"); player.pickUp(input.substring(argumentMatcher.start() + 1, input.length())); return; } else { Gui.setOutputText("You can't pick up nothing, f****t."); return; } } // END GET // USE ON if (twoArgumentMatcher .find()) { // checks for two arguments so "use cat picture on joint" should call useOn? input = input.substring(twoArgumentMatcher.start() + 1, input.length()); Pattern findOnPattern = Pattern.compile("\\son\\s"); Matcher findOnMatcher = findOnPattern.matcher(input); findOnMatcher.find(); String firstArg = input.substring(0, findOnMatcher.start()); // item 1 String secondArg = input.substring(findOnMatcher.end(), input.length()); // item 2 player.useOn(firstArg, secondArg); return; } // END USE ON // USE if (usePattern.matcher(input).find()) { if (argumentMatcher.find()) { player.use(input.substring(argumentMatcher.start() + 1, input.length())); return; } else { Gui.setOutputText("There's nothing like that to use."); return; } } // END USE // DROP if (dropPattern.matcher(input).find()) { if (argumentMatcher.find()) { player.drop(input.substring(argumentMatcher.start() + 1, input.length())); return; } else { Gui.setOutputText("You have dropped nothing."); return; } } // END DROP // MOVE if (movePattern.matcher(input).find()) { if (argumentMatcher.find()) { String destination = input.substring(argumentMatcher.start() + 1, input.length()); for (Location l : player.currentLoc.getLinks()) { if (destination.equalsIgnoreCase(l.getName()) && l.isDiscovered()) { player.move(l); return; } } Gui.setOutputText("Invalid location"); return; } else { Gui.setOutputText("You shuffled around a little bit"); return; } } // END MOVE // SAVE if (savePattern.matcher(input).find()) { Gui.setOutputText(TextAdventure.saveWorld()); } // END SAVE // LOAD if (loadPattern.matcher(input).find()) { Gui.setOutputText(TextAdventure.loadWorld()); } // END LOAD // NONE OF THE ABOVE Gui.setOutputText( (String) TextAdventure.pick(TextAdventure.invalidVerb)); // prints out invalid verb return; }
private void parseCommand(String message) throws IOException { if (message.length() == 0) return; // Get the command (first word) String comm; String arguments = null; if (message.indexOf(' ') != -1) { comm = message.substring(0, message.indexOf(' ')); arguments = message.substring(message.indexOf(' ') + 1); } else { comm = message; } switch (comm.toLowerCase()) { case "say": say(arguments); break; case "tell": tell(arguments); break; case "north": case "n": player.move(Direction.N); break; case "northeast": case "ne": player.move(Direction.NE); break; case "east": case "e": player.move(Direction.E); break; case "southeast": case "se": player.move(Direction.SE); break; case "south": case "s": player.move(Direction.S); break; case "southwest": case "sw": player.move(Direction.SW); break; case "west": case "w": player.move(Direction.W); break; case "northwest": case "nw": player.move(Direction.NW); break; case "in": player.move(Direction.IN); break; case "out": player.move(Direction.OUT); break; case "look": case "l": player.look(); break; default: Telnet.writeLine(cs, "Your meaning is unclear\n"); } }
/* * commandInput(String given){...} * This is where the commands input by the player * are handled. You will need to add more if() checks * as you add more commands to the game. This is also * where login and signup are handled. */ public void commandInput(String given) { // No empty strings. if (given.length() == 0) { return; } // Have the user login to their account. if (!login && !signup && username.length() == 0) { username = given; if (find(given, "name", file)) { prompt = true; commandOutput("Log into account " + given + "? [Y/N]\n"); } else { signup = true; commandOutput("User not found. Would you like to make an account? [Y/N]\n"); } return; } // Prompt for accepting login attempt if (prompt) { if (given.toUpperCase().equals("Y")) { newPass = false; commandOutput("Password: "******"N")) { username = ""; commandOutput("Please reenter the account name you wish to log into.\n"); prompt = false; return; } else { return; } } // Prompt for making a new account; if (signup) { if (given.toUpperCase().equals("Y")) { newPass = true; commandOutput("Please enter a password for this account:\n"); signup = false; return; } else if (given.toUpperCase().equals("N")) { username = ""; commandOutput("Please reenter the account name you wish to log into.\n"); signup = false; return; } else { return; } } // Unidentified user with no password. if (newPass) { // Write the new player's information to the file. try { FileWriter writer = new FileWriter("players.txt", true); writer.write(username + " {"); writer.write(System.lineSeparator()); writer.write(username); writer.write(System.lineSeparator()); writer.write("Password: "******"100"); writer.write(System.lineSeparator()); writer.write("100"); writer.write(System.lineSeparator()); writer.write("0"); writer.write(System.lineSeparator()); writer.write("0"); writer.write(System.lineSeparator()); writer.write("0"); writer.write(System.lineSeparator()); writer.write("[]"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("None"); writer.write(System.lineSeparator()); writer.write("}"); writer.write(System.lineSeparator()); writer.close(); try { outgoing = new PrintWriter(socket.getOutputStream(), true); } catch (IOException ex) { } commandOutput( "Thank you! Please log into your new account by reentering this username!\n"); username = ""; newPass = false; } catch (IOException e) { username = ""; newPass = false; commandOutput("Unexpected error occured. Please try again!\n"); } return; } if (!login && username.length() > 0) { if (getPass(username, given, file)) { player = constructPlayer(username, file); login = true; commandOutput("Welcome back " + username + ".\n"); } else { commandOutput("Incorrect password. Please reenter username.\n"); username = ""; } return; } String input = given.toUpperCase(); if (input.equals("QUIT") || input.equals("DISCONNECT")) { try { // #Save_Player; commandOutput("Thank you and goodbye!\n"); savePlayer(player); socket.close(); } catch (IOException ex) { } } else if (input.contains("CON") || input.contains("CONDITION") || input.contains("STATUS")) { player.cond(); } else if ((input.contains("MOVE ") && input.length() > 5) || (input.contains("M ") && input.length() > 2) || (movable.contains(input))) { player.move(input); } else if (input.contains("LOOK") || input.equals("L") || (input.contains("L ") && movable.contains(input.substring(input.indexOf(" ") + 1)))) { player.look(input); } else if (input.contains("GET") || input.contains("PICK UP")) { player.get(input); } else if ((input.contains("EQ ") && input.substring(0, input.indexOf(" ")).length() < 4) || (input.contains("EQUIP ") && input.substring(0, input.indexOf(" ")).length() < 7) || (input.contains("WEAR ") && input.substring(0, input.indexOf(" ")).length() < 5)) { player.equip(input); } else if (input.contains("UNEQ ") || input.contains("UNEQUIP ")) { player.unequip(input); } else if (input.equals("I") || input.equals("INVENTORY") || input.equals("INV")) { player.inventory(); } else if (input.equals("EQ") || input.equals("EQUIPMENT")) { player.equipment(); } else if (input.contains("DROP ")) { player.drop(input); } else if (input.equals("STAT") || input.equals("ST") || input.equals("STATS")) { player.stats(); } else if (input.equals("")) { player.cond(); } else if (input.contains("FLEE") || input.contains("F ") || input.equals("F")) { // player.flee(input); } while (player.drawText.size() > 0) { commandOutput(player.drawText.get(0)); player.drawText.remove(0); } }
static void play(Player player) { String playerInput; boolean gameOn = true; boolean start = true; boolean goblinAlive = true; playerInput = "help"; while (gameOn == true) { Scanner input = new Scanner(System.in); if (!start) { playerInput = input.next(); } start = false; switch (playerInput.toLowerCase()) { case "north": player.moveTo(player.getLocation().north()); break; case "south": player.moveTo(player.getLocation().south()); break; case "east": player.moveTo(player.getLocation().east()); break; case "west": player.moveTo(player.getLocation().west()); break; case "look": player.look(); break; case "drop": player.drop(input.next()); break; case "pickup": player.pickup(input.next()); break; case "inventory": player.inventory(); break; case "quit": gameOn = false; break; case "xyzzy": if (player.getLocation().getName().equals("Room 5")) { System.out.println( "A red portal opens in front of you, and you walk into it without thinking. \nYou are still inside the cave. You see a chest in the center of the room."); player.moveTo(player.getLocation().xyzzy()); } else { System.out.println("Nothing happens."); } break; case "help": printInstructions(); break; case "use": switch (input.next()) { case "man": if (player.getLocation().getName().equals("Room 1")) { player.use(); } case "well": if (player.getLocation().getName().equals("Room 7")) { player.use(); } case "skeleton": if (player.getLocation().getName().equals("Room 2")) { player.use(); } case "writing": if (player.getLocation().getName().equals("Room 8")) { player.use(); } case "ears": if (player.getLocation().getName().equals("Room 3")) { player.use(); } case "chest": if (player.getLocation().getName().equals("Room 6")) { player.use(); } } break; default: System.out.println("What?"); } if (player.getLocation().getName().equals("Room 4") && goblinAlive) { gameOn = goblinChase(player); goblinAlive = false; } } System.out.println("Game Over"); }
static boolean goblinChase(Player player) { String playerInput = ""; System.out.println("As you enter the room a goblin jumps out of the darkness and runs at you."); System.out.println("You feel the urge to either fight or run."); for (int i = 5; i > 0; i--) { Scanner input = new Scanner(System.in); playerInput = input.next(); switch (playerInput.toLowerCase()) { case "north": player.moveTo(player.getLocation().north()); break; case "south": player.moveTo(player.getLocation().south()); break; case "east": player.moveTo(player.getLocation().east()); break; case "west": player.moveTo(player.getLocation().west()); break; case "look": player.look(); break; case "drop": player.drop(input.next()); break; case "pickup": player.pickup(input.next()); break; case "inventory": player.inventory(); break; case "quit": return false; case "xyzzy": if (player.getLocation().getName().equals("Room 5")) { System.out.println( "A red portal opens in front of you, and you walk into it without thinking. \n You are still inside the cave. You see a chest in the center of the room."); player.moveTo(player.getLocation().xyzzy()); } else { System.out.println("Nothing happens."); } break; case "help": printInstructions(); break; case "use": switch (input.next()) { case "man": if (player.getLocation().getName().equals("Room 1")) { player.use(); } case "well": if (player.getLocation().getName().equals("Room 7")) { player.use(); } case "skeleton": if (player.getLocation().getName().equals("Room 2")) { player.use(); } case "writing": if (player.getLocation().getName().equals("Room 8")) { player.use(); } case "ears": if (player.getLocation().getName().equals("Room 3")) { player.use(); } case "chest": if (player.getLocation().getName().equals("Room 6")) { player.use(); } } case "fight": if (player.getInventory().contains("sword")) { System.out.println("You decide to turn and fight."); System.out.println( "You pull out the sword you picked up earlier, the only weapon you have."); System.out.println( "As the goblin charges, you stand your ground and take a fighting stance."); System.out.println( "You plunge your sword into the goblin's chest with one decisive thrust."); System.out.println( "The goblin falls to the ground in front of you as you pull out the bloody sword."); System.out.println("You examine the body. The goblin is dead."); player.getLocation().drop("goblin"); return true; } else { if (player.getLocation().getName().equals("Room 7")) { System.out.println("You decide to turn and fight."); System.out.println("As the goblin charges, you swing at him with all of your might."); System.out.println("The goblin is knocked back slightly and trips over a rock."); System.out.println( "The goblin falls into the well situated in the center of the room."); System.out.println("As he falls you hear him screech, and eventually a loud THUNK!"); System.out.println( "You approach the well and look inside it, but you can't see anything, even with the lamp."); return true; } else { if (1 == 1) { System.out.println("The goblin grabs you and tears your arm off."); System.out.println( "Wounded, you slowly bleed out as you watch the goblin eat your severed arm."); return false; } } } break; default: System.out.println("What?"); } switch (i) { case 5: System.out.println( "The goblin seems dazed by the light of the lantern and trips over a rock."); break; case 4: System.out.println("The goblin gets up and persues you."); break; case 3: System.out.println( "You try to escape the goblin's pursuit, but he is slowly getting closer."); ; break; case 2: System.out.println("The goblin has nearly caught up to you."); break; case 1: System.out.println("The goblin is right behind you, there is no hope for escape."); break; } } System.out.println("The goblin grabs you and tears your arm off."); System.out.println( "Wounded, you slowly bleed out as you watch the goblin eat your severed arm."); return false; }