I have a text adventure game where a player can pick up and drop items. For the drop method, the game takes in the command d as well as the item they want to drop. The method must go through the player's inventory to see if they have that item. My problem is that if the item is third in line in the inventory, the game will print out "That is not an item in your inventory" twice before going through the if statement where the item is actually dropped and points subtracted from the score. I know this is happening because it must loop through three times to find the item, so I tried making the else statement only show when i == the size of the inventory, but then there is no result at all printed. All help is great! Thanks!
Here is my drop method:
else if (userCommand.command.equalsIgnoreCase("D") && userCommand.item != null) {
if (player1.inventory.size() != 0) {
String itemToDrop = userCommand.item;
Locale locale = locales[currentLocation];
if (locale.item == null) {
int itemFoundAt = -1;
for (int i = 0; i < player1.inventory.size(); i++) {
Item checkItem;
checkItem = player1.inventory.get(i);
if (checkItem.itemName.equalsIgnoreCase(itemToDrop)) {
itemFoundAt = i;
if (itemFoundAt >= 0) {
//Remove item entered from a player's inventory
System.out.println("\tYou have dropped the " + checkItem.itemName + ".");
//Subtract the item's value from the player's score
player1.score -= checkItem.value;
System.out.println("\n\t" + checkItem.value + " points have been subtracted from your score.");
System.out.println("\n\tThis is your current score: " + player1.score);
player1.inventory.remove(i);
//Place the item at the player's current location so it can be picked up again
locale.item = checkItem;
}
} else if (itemFoundAt < 0) {
System.out.println("\tThat is not an item in your inventory.");
}
}
} else {
System.out.println("\n\tThere is already an item at this location, you can't drop an item.");
}
} else {
System.out.println("\n\tYou have no items in your inventory.");
}
break;
}//End of Drop
Aucun commentaire:
Enregistrer un commentaire