-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trade.java
61 lines (61 loc) · 2.07 KB
/
Trade.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Class Trade - a "trade" action in the Room.
* It is a subclass of Command class.
* an NPC to trade its item for the item given to it by a player.
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* @author Bowen Yan
* @version 6th, April 2008
*/
public class Trade extends Command
{
/**
* Constructor for objects of class Trade
*/
public Trade(String firstWord, String secondWord)
{
super(firstWord,secondWord);
}
/**
* An NPC to trade its item for the item given to it by a player.
* @param command
* @param player
* @return
*/
public boolean executeCommand(Command command, Player player, Parser parser)
{
if (!command.hasSecondWord())
{
// if there is no second word, we don't know what should be traded...
System.out.println("Trade what?");
return false;
}
String itemname = command.getSecondWord();
Item playeritem = player.checkItem(itemname);
if (playeritem==null) //estimate whether the item is existent in player's item list or not.
{
System.out.println("There is no item named "+itemname+" in the player's item list!");
return false;
}
if (player.getCurrentRoom().getNpc()==null)//estimate whether there is an NPC in current room or not.
{
System.out.println("There is no NPC in this room!");
return false;
}
Item npcitem = player.getCurrentRoom().getNpc().getItem();
if (npcitem == null) //estimate whether the NPC has an item or not.
{
System.out.println("There is no item belonging to NPC!");
return false;
}
//change the current information of item list for the player and NPC.
player.addItem(npcitem);
player.removeItem(playeritem);
player.getCurrentRoom().getNpc().changeItem(playeritem);
System.out.println("The trade has been done!");
System.out.println("The NPC has gotten "+player.getCurrentRoom().getNpc().getItem().getName()+"!");
System.out.println("The player has gotten a "+npcitem.getName()+"!");
return false;
}
}