private void useItem() { Game game = PemberleyGame.getCurrentGame(); ItemControl itemControl = new ItemControl(); String prompt = "What do you want to try and use?"; // check to see if there is anything to use. if (game.getLocalItemNames().length == 0 && game.getInventoryItemNames().length == 0) { this.console.println("There is nothing to use here."); return; } this.console.println("These things are accessible:\n"); for (String i : game.getLocalItemNames()) { this.console.print(i + "\n"); } for (String i : game.getInventoryItemNames()) { this.console.print(i + "\n"); } String playerSelection; do { playerSelection = this.getStringInput(prompt); String useItemMessage = itemControl.useItem(playerSelection, game); this.console.println(useItemMessage); } while (!playerSelection.equalsIgnoreCase("x")); }
public String playerNavigate(String direction, Player player) throws MapControlException { Game game = PemberleyGame.getCurrentGame(); Room currentRoom = player.getLocation(); Room nextRoom; switch (direction) { case "E": case "EAST": nextRoom = currentRoom.getEast(); break; case "S": case "SOUTH": nextRoom = currentRoom.getSouth(); break; case "N": case "NORTH": nextRoom = currentRoom.getNorth(); break; case "W": case "WEST": nextRoom = currentRoom.getWest(); break; default: return "Not sure where you are going."; } if (nextRoom == null) { throw new MapControlException("You can't go that direction"); } else { player.setLocation(nextRoom); currentRoom = player.getLocation(); String[] roomArray = currentRoom.getRoomDrawing(); if (roomArray == null) { String[] roomDrawing = this.renderRoom(nextRoom); nextRoom.setRoomDrawing(roomDrawing); String mapDrawing = this.renderMap(nextRoom); Map currentMap = nextRoom.getMap(); currentMap.setMapDrawing(mapDrawing); } GameControl gameControl = new GameControl(); gameControl.updateGame(); String playerMessage = nextRoom.getName() + "\n" + this.lookAtRoom(); return playerMessage; } }
private void movePlayer() { Game game = PemberleyGame.getCurrentGame(); String roomDescription = ""; Player player = game.getPlayerOne(); String prompt = "Which direction do you want to go? Type X to exit."; String playerSelection; do { playerSelection = this.getStringInput(prompt); MapControl mapControl = new MapControl(); try { roomDescription = mapControl.playerNavigate(playerSelection, player); } catch (MapControlException me) { ErrorView.display(this.getClass().getName(), me.getMessage()); } this.console.println(roomDescription); } while (!playerSelection.equalsIgnoreCase("x")); }
// author Sheila public String lookAtRoom() { Game game = PemberleyGame.getCurrentGame(); Room currentRoom = game.getCurrentRoom(); String roomDescription = currentRoom.getDescription(); roomDescription = roomDescription + "\nThese are the directions you can move from here: "; int i; if (currentRoom.getNorth() != null) { roomDescription = roomDescription + "NORTH "; } if (currentRoom.getWest() != null) { roomDescription = roomDescription + "WEST "; } if (currentRoom.getSouth() != null) { roomDescription = roomDescription + "SOUTH "; } if (currentRoom.getEast() != null) { roomDescription = roomDescription + "EAST "; } if (game.getLocalItemNames().length != 0) { roomDescription = roomDescription + "\nThese Items are here: "; for (String s : game.getLocalItemNames()) { roomDescription = roomDescription + s + "-"; } } if (game.getLocalActorNames().length != 0) { roomDescription = roomDescription + "\nThese People are here: "; for (String s : game.getLocalActorNames()) { roomDescription = roomDescription + s + "-"; } } String[] roomDrawing = currentRoom.getRoomDrawing(); for (String v : roomDrawing) { roomDescription = roomDescription + "\n"; roomDescription = roomDescription + v; } return roomDescription; }
// author Sheila private void speakToActor() { Game game = PemberleyGame.getCurrentGame(); int itemSelection = 1; // start at one so first item in list isn't zero String playerSelection = " "; Actor[] actorNames = game.getLocalActorArray(); // get list of actors in the current location for (int i = 0; i < actorNames.length; i++) { if (actorNames.length == 0) { this.console.print("NO ONE IS HERE"); return; // no selection possible, return to menu } // actors available to speak - return list else { this.console.println( "These actors are here: \n" + itemSelection + " " + actorNames[i].getName()); itemSelection++; // increments number String prompt = "\nType the number of the actor you want to speak to? Type X to exit"; do { playerSelection = this.getStringInput(prompt); if (playerSelection.equalsIgnoreCase( "x")) { // without this here, the 'x' is seen as an error. break; } try { itemSelection = Integer.parseInt(playerSelection); // converts string input to int this.console.println( actorNames[itemSelection - 1].getCharacterScript()); // -1 returns selection to zero } catch (NumberFormatException nf) { ErrorView.display(this.getClass().getName(), nf.getMessage()); } } while (!playerSelection.equalsIgnoreCase("x")); } } }
private void takeItem() { // declare variables Game game = PemberleyGame.getCurrentGame(); Player player = game.getPlayerOne(); ItemControl itemControl = new ItemControl(); Item[] localItemArray = game.getLocalItemArray(); String gameMessage = " "; int indexOfItem; int quantityOfItem = 1; String playerSelection; InventoryControl inventoryControl = new InventoryControl(); // designate the inventory Inventory inventory = player.getInventory(); // while player's selection is not X loop do { // if there is nothing in the localItemArray say there is nothing to take. if (localItemArray.length == 0) { this.console.println("Nothing to Take"); break; } else { String prompt = "What do you want to take? Type X to exit."; // get input from the player playerSelection = this.getStringInput(prompt); // see if the player's selected item is in the room. return it's index in the array. indexOfItem = itemControl.getItemIndex(playerSelection, player, localItemArray); if (indexOfItem != -1) { // run this code if there was an index match // set the item to whatever item matched the players selection Item selectedItem = localItemArray[indexOfItem]; // if the item's multiple attribute is true run this code if (selectedItem.isMultiple() == true) { prompt = "How Many do you want to take? (1 - 9)"; // get the players selection and make sure it is an integer playerSelection = this.getStringInput(prompt); // gets a string // make the player's selection an integer try { quantityOfItem = Integer.parseInt(playerSelection); // converts string to int } catch (NumberFormatException nf) { ErrorView.display( this.getClass().getName(), "\nYou must enter a valid number." + " Try again or type X to exit."); } // call the takeMultipleItem function. try { gameMessage = inventoryControl.takeMultipleItem(selectedItem, quantityOfItem, inventory); } catch (InventoryControlException ie) { ErrorView.display(this.getClass().getName(), ie.getMessage()); } } else { // run this code if the item's multiple attribue is not true quantityOfItem = 1; // call the takeSingleItem function try { gameMessage = inventoryControl.takeSingleItem(selectedItem, quantityOfItem, inventory); } catch (InventoryControlException ie) { ErrorView.display(this.getClass().getName(), ie.getMessage()); } } } else { // if the player's selection is not in the array send this message gameMessage = "Not sure what you are trying to take."; } } this.console.println(gameMessage); } while (!playerSelection.equalsIgnoreCase("X")); // loop }
private void giveItem() { Game game = PemberleyGame.getCurrentGame(); if (game.getInventoryItemNames().length == 0 || game.getLocalActorNames().length == 0) { this.console.println("You have nothing to give or there is no one to give something to."); return; } ItemControl itemControl = new ItemControl(); ActorControl actorControl = new ActorControl(); InventoryControl inventoryControl = new InventoryControl(); String prompt = "What do you want to give?"; Player player = game.getPlayerOne(); Item[] inventoryItems = game.getInventoryItemArray(); Actor[] actorArray = game.getLocalActorArray(); String gameMessage = " "; int quantityOfItem = 1; Item itemToGive; Actor actorToGive; // check to see if there is anything to use. this.console.println("These things are in your inventory\n"); for (String i : game.getInventoryItemNames()) { this.console.print(i + "\n"); } String playerSelection; do { playerSelection = this.getStringInput(prompt); int indexOfItem = itemControl.getItemIndex(playerSelection, player, inventoryItems); try { itemToGive = inventoryItems[indexOfItem]; } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) { ErrorView.display(this.getClass().getName(), "\nNot Sure what you are trying to give."); return; } if (itemToGive.getQuantity() > 1) { prompt = "How many " + itemToGive.getName() + " do you want to give?"; playerSelection = this.getStringInput(prompt); try { quantityOfItem = Integer.parseInt(playerSelection); // converts string to int } catch (NumberFormatException nf) { ErrorView.display( this.getClass().getName(), "\nYou must enter a valid number." + " Try again or type X to exit."); break; } } else { quantityOfItem = 1; } this.console.print("\nThese people are here:"); prompt = "Who do you want to give the " + itemToGive.getName() + " to?\n"; for (String i : game.getLocalActorNames()) { this.console.print(i + "\n"); } playerSelection = this.getStringInput(prompt); int indexOfActor = actorControl.getActorIndex(playerSelection, player, actorArray); try { actorToGive = actorArray[indexOfActor]; } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) { ErrorView.display( this.getClass().getName(), "\nNot Sure who you are trying to give the " + itemToGive.getName() + " to."); return; } this.console.println(inventoryControl.giveItem(itemToGive, quantityOfItem, actorToGive)); break; } while (!playerSelection.equalsIgnoreCase("x")); }