aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/esieequest/Command.java42
-rw-r--r--src/esieequest/CommandWords.java42
-rw-r--r--src/esieequest/Game.java159
-rwxr-xr-xsrc/esieequest/Main.java3
-rw-r--r--src/esieequest/Parser.java67
-rw-r--r--src/esieequest/Room.java48
6 files changed, 360 insertions, 1 deletions
diff --git a/src/esieequest/Command.java b/src/esieequest/Command.java
new file mode 100644
index 0000000..4ebc02a
--- /dev/null
+++ b/src/esieequest/Command.java
@@ -0,0 +1,42 @@
1package esieequest;
2
3/**
4 * Représente une commande tapée au clavier qui provoque une action dans le jeu.
5 *
6 * @author Pacien TRAN-GIRARD
7 * @version Février 2013
8 */
9public class Command
10{
11 private String aCommandWord;
12 private String aSecondWord;
13
14 /**
15 * Constructeur
16 */
17 public Command(final String pCommandWord, final String pSecondWord)
18 {
19 this.aCommandWord = pCommandWord;
20 this.aSecondWord = pSecondWord;
21 }
22
23 public String getCommandWord()
24 {
25 return this.aCommandWord;
26 }
27
28 public String getSecondWord()
29 {
30 return this.aSecondWord;
31 }
32
33 public boolean isUnknown()
34 {
35 return this.aCommandWord == null;
36 }
37
38 public boolean hasSecondWord()
39 {
40 return this.aSecondWord != null;
41 }
42}
diff --git a/src/esieequest/CommandWords.java b/src/esieequest/CommandWords.java
new file mode 100644
index 0000000..d2dd2f3
--- /dev/null
+++ b/src/esieequest/CommandWords.java
@@ -0,0 +1,42 @@
1package esieequest;
2
3/**
4 * This class is part of the "World of Zuul" application.
5 * "World of Zuul" is a very simple, text based adventure game.
6 *
7 * This class holds an enumeration table of all command words known to the game.
8 * It is used to recognise commands as they are typed in.
9 *
10 * @author Michael Kolling and David J. Barnes + D.Bureau
11 * @version 2008.03.30 + 2013.09.15
12 */
13public class CommandWords
14{
15 // a constant array that holds all valid command words
16 private static final String[] sValidCommands = {
17 "go", "quit", "help"
18 };
19
20 /**
21 * Constructor - initialise the command words.
22 */
23 public CommandWords()
24 {
25 // nothing to do at the moment...
26 } // CommandWords()
27
28 /**
29 * Check whether a given String is a valid command word.
30 * @return true if a given string is a valid command,
31 * false if it isn't.
32 */
33 public boolean isCommand( final String pString )
34 {
35 for ( int i=0; i<sValidCommands.length; i++ ) {
36 if ( sValidCommands[i].equals( pString ) )
37 return true;
38 } // for
39 // if we get here, the string was not found in the commands
40 return false;
41 } // isCommand()
42} // CommandWords
diff --git a/src/esieequest/Game.java b/src/esieequest/Game.java
new file mode 100644
index 0000000..de0b2b4
--- /dev/null
+++ b/src/esieequest/Game.java
@@ -0,0 +1,159 @@
1package esieequest;
2
3/**
4 * Moteur du jeu.
5 *
6 * @author Pacien TRAN-GIRARD
7 * @version Février 2013
8 */
9public class Game
10{
11 private Room aCurrentRoom;
12 private Parser aParser;
13
14 /**
15 * Constructor for objects of class Game
16 */
17 public Game()
18 {
19 this.createRooms();
20 this.printWelcome();
21 this.printRoomInfo();
22 this.play();
23 }
24
25 private void play()
26 {
27 aParser = new Parser();
28 boolean vFinished = false;
29 while (!vFinished) {
30 Command vCommand = this.aParser.getCommand();
31 vFinished = this.processCommand(vCommand);
32 }
33
34 System.out.println("Thank you for playing. Good bye.");
35 }
36
37 private void createRooms()
38 {
39 // création des lieux
40 Room vOutside = new Room("outside the main entrance of the university");
41 Room vTheatre = new Room("in a lecture theatre");
42 Room vPub = new Room("in the campus pub");
43 Room vLab = new Room("in a computing lab");
44 Room vOffice = new Room("in the computing admin office");
45
46 // positionnement des sorties
47 vOutside.setExits(null, vLab, vPub, vTheatre);
48 vTheatre.setExits(null, null, vOutside, null);
49 vPub.setExits(null, null, null, vOutside);
50 vLab.setExits(vOutside, null, null, vOffice);
51 vOffice.setExits(null, null, vLab, null);
52
53 // initialisation du lieu courant
54 this.aCurrentRoom = vOutside;
55 }
56
57 public void goRoom(final Command pCommand)
58 {
59 if (!pCommand.hasSecondWord()) {
60 System.out.println("Go where?");
61 }
62
63 Room vNextRoom;
64 switch (pCommand.getSecondWord()) {
65 case "north":
66 vNextRoom = this.aCurrentRoom.aNorthExit;
67 break;
68 case "south":
69 vNextRoom = this.aCurrentRoom.aSouthExit;
70 break;
71 case "east":
72 vNextRoom = this.aCurrentRoom.aEastExit;
73 break;
74 case "west":
75 vNextRoom = this.aCurrentRoom.aWestExit;
76 break;
77 default:
78 System.out.println("Go where ?");
79 return;
80 }
81
82 if (vNextRoom == null) {
83 System.out.println("There is no door!");
84 return;
85 }
86
87 this.aCurrentRoom = vNextRoom;
88 this.printRoomInfo();
89 }
90
91 private void printRoomInfo()
92 {
93 System.out.println(this.aCurrentRoom.getDescription());
94
95 System.out.print("Available exits:");
96 if (this.aCurrentRoom.aNorthExit != null) { System.out.print(" North"); }
97 if (this.aCurrentRoom.aSouthExit != null) { System.out.print(" South"); }
98 if (this.aCurrentRoom.aEastExit != null) { System.out.print(" East"); }
99 if (this.aCurrentRoom.aWestExit != null) { System.out.print(" West"); }
100 System.out.println(".");
101 }
102
103 private void printWelcome()
104 {
105 System.out.println("Welcome to the World of Zuul!");
106 System.out.println("World of Zuul is a new, incredibly boring adventure game.");
107 System.out.println("Type 'help' if you need help.");
108 System.out.println("");
109 }
110
111 private void printHelp()
112 {
113 System.out.println("You are lost. You are alone. ");
114 System.out.println("You wander around at the university.");
115 System.out.println("");
116 System.out.println("Your command words are:");
117 System.out.println(" go quit help");
118 }
119
120 private boolean quit(final Command pCommand)
121 {
122 if (pCommand.hasSecondWord()) {
123 System.out.println("Quit what?");
124 return false;
125 }
126 return true;
127 }
128
129 private boolean processCommand(final Command pCommand)
130 {
131 if (pCommand.getCommandWord() != null) {
132 switch (pCommand.getCommandWord()) {
133 case "go":
134 this.goRoom(pCommand);
135 return false;
136 case "help":
137 this.printHelp();
138 return false;
139 case "quit":
140 return this.quit(pCommand);
141 }
142 }
143 System.out.println("I don't know what you mean...");
144 return false;
145 }
146}
147
148
149
150
151