-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGame.java
More file actions
255 lines (192 loc) · 8.48 KB
/
Game.java
File metadata and controls
255 lines (192 loc) · 8.48 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Game {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
public Player CurrentUser;
public static void main(String[] args)
throws IOException{
System.out.println("Welcome to the funfair game,\n"
+ "By Daniel Hyde\n");
Game TheGame = new Game();
TheGame.Startgame();
}
/**This method starts the 'game **/
public void Startgame()throws IOException{
Funrooms Main = new Funrooms();
Room StartRM = Main.CreateFunfair();
Scanner input = new Scanner(System.in);
List<Player> playerlist = new ArrayList<Player>();
System.out.println("Welcome User, please enter a name:");
String username = input.nextLine();
System.out.println("You have been given 100 currency to spend\n");
playerlist.add(new Player());
CurrentUser = playerlist.get(0);
CurrentUser.setname(username);
CurrentUser.setEnt(StartRM);
BufferedReader keyboard
= new BufferedReader(new InputStreamReader(System.in));
String inputString = "prepare";
int direction = 9;
/** User is now offically in the funfair **/
System.out.printf( "Welcome %s", CurrentUser.getname() +
"\nWhich way (North, East, South ,West), \n" +
"Or type HELP for a list of avaliable Commands \n");
while(CurrentUser != null){
inputString = keyboard.readLine();
/** List all commands in the system **/
if (inputString.equalsIgnoreCase("HELP"))
System.out.println("USE THE COMMANDS: NORTH, EAST, SOUTH ,WEST to travel between rooms. \n " +
"VIEW EXITS - List which rooms you can currently travel to. \n" +
"TAKE itemname - Take the specified item and add it to your cart if it is avaliable. \n" +
"LIST CART - View the items currently in your cart. \n" +
"EDIT CART - Delete any items from your cart. \n" +
"LIST PLAYERS - Lists the users in the funfair, and which rooms they are in" +
"ENTER username - Enter the funfair as a new/existing user \n" +
"LIST ITEMS - List the items avaliable in the room\n" +
"ENTER username - Enter as a new user of control another existing one\n" +
"CHECK ROOM - list the users in the current room \n" );
else if (inputString.equalsIgnoreCase("NORTH")){
direction = convertDirection(inputString);
CurrentUser.go(direction, CurrentUser);
if(CurrentUser.getLoc().equals("EXIT")){
}
}
/** Directions for travelling rooms **/
else if (inputString.equalsIgnoreCase("EAST")){
direction = convertDirection(inputString);
CurrentUser.go(direction, CurrentUser);}
else if (inputString.equalsIgnoreCase("SOUTH")){
direction = convertDirection(inputString);
CurrentUser.go(direction, CurrentUser);}
else if (inputString.equalsIgnoreCase("WEST")){
direction = convertDirection(inputString);
CurrentUser.go(direction, CurrentUser);}
/** viewing exits to a room **/
else if (inputString.equals("VIEW EXITS"))
System.out.printf("%s\n", (CurrentUser.getLoc()).getDesc());
else if (inputString.contains("TAKE")){ /**Take an item from a room **/
Item I;
Boolean Itemcheck;
inputString = inputString.replace("TAKE ", "");
Itemcheck = (CurrentUser.getLoc()).checkitem(inputString);
if (Itemcheck == true){
CurrentUser.getLoc().removeitem(inputString);
I = (CurrentUser.getLoc()).getitemname(inputString);
CurrentUser.Additem(I);
System.out.printf("item added \n");
CurrentUser.setSpentmoney(I.getamount());
}
else
System.out.printf("Item not avaliable to purchase\n");
} /** listing current users in the funfair **/
else if (inputString.equalsIgnoreCase("LIST PLAYERS")){
String list = "";
for (int i = 0; i < playerlist.size(); i++){
list = list + "User: " + playerlist.get(i).getname() + " is in " + playerlist.get(i).getLoc().getname() + "\n";
}
System.out.printf("%s\n", list);
}
/** Check for users in the same room as you **/
else if (inputString.equalsIgnoreCase("CHECK ROOM")){
String list = "Users in this room: \n";
for (int i = 0; i < playerlist.size(); i++){
if(playerlist.get(i).getLoc().equals(CurrentUser.getLoc()))
list = list + playerlist.get(i).getname() + "\n";
}
System.out.printf("%s\n", list);
}
/**list items in current room **/
else if (inputString.equalsIgnoreCase("LIST ITEMS")){
String list = "";
list = list + CurrentUser.getLoc().Listitems(list);
System.out.printf("%s\n", list);
}
/** list the items in the current users inventory **/
else if (inputString.equalsIgnoreCase("LIST CART"))
CurrentUser.listitems();
else if (inputString.equalsIgnoreCase("EDIT CART")){
String a = null;
CurrentUser.listitems();
System.out.printf("Enter the item name you want to remove:\n");
a = keyboard.readLine();
CurrentUser.removeitem(a);
System.out.printf("Item Removed\n");
}
else if (inputString.contains("ENTER")){ /*Changes the user of the fun fair */
int num = 0;
Boolean a = false;
inputString = inputString.replace("ENTER ", "");
for (int i = 0; i < playerlist.size(); i++){
if (playerlist.get(i).getname().contains(inputString)){
CurrentUser = playerlist.get(i);
System.out.printf( "Welcome %s\n", CurrentUser.getname() +
"Which way (North, East, South ,West), \n" +
"Or type HELP for a list of avaliable Commands \n");
a = true;}
num = num + i;
}
if (a == false){
num = num + 1;;
playerlist.add(new Player());
playerlist.get(num).setname(inputString);
playerlist.get(num).setmoney(10);
CurrentUser = playerlist.get(num);
CurrentUser.setEnt(StartRM);}
System.out.printf( "Welcome %s\n", CurrentUser.getname() + "\n" +
"Which way (North, East, South ,West), \n" +
"Or type HELP for a list of avaliable Commands \n");
}
/** This code is for displaying previous invoices, but is not fully functioning, so i have commented out the code **/
else if (inputString.equalsIgnoreCase("LIST PREVIOUS")){
/** FileWrite Display = new FileWrite();
Display.getInvoice(CurrentUser); **/
}
if (CurrentUser.getLoc().getname().contains("EXIT")){ /**If the user is in the EXIT room **/
if (CurrentUser.getspent() < CurrentUser.getmoney())
{
FileOutput Output = new FileOutput();
PlayerInvoice Invoice = new PlayerInvoice();
CurrentUser.setLoc(StartRM);
Invoice.setname(CurrentUser.getname());
Invoice.setAmount(CurrentUser.getspent());
Invoice.SetDate(dateFormat.format(date));
Output.openfile();
Output.AddInvoice(Invoice);
Output.closefile();
CurrentUser = null;
}
else{ /**User cannot afford all of the items in cart **/
System.out.printf("You cannot afford the items you are carrying\n");
CurrentUser.go(2, CurrentUser); /** User goes back a room **/
} } }
if (CurrentUser == null){
backtostart();
}
}
private void backtostart() throws IOException {
Startgame();
}
private int convertDirection(String input){ /** This converts the text direction into numbers, so it's easier for the system to read**/
int theDirection = 9999;
if(input.equalsIgnoreCase("NORTH"))
theDirection = 0;
else if(input.equalsIgnoreCase("EAST"))
theDirection = 1;
else if(input.equalsIgnoreCase("SOUTH"))
theDirection = 2;
else if(input.equalsIgnoreCase("WEST"))
theDirection = 3;
return theDirection;
}
public Player getcurrentuser() {
return CurrentUser;
}
}