-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
425 lines (400 loc) · 13.7 KB
/
Main.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import java.util.*;
public class Main {
// main
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Player player;
String input;
// Character creator
System.out.println("Hello! Welcome to my game!\nThis is the character Creator. You have 50 points to spend\n"
+ "on any skills you want.");
player = creator(in);
Map M = new Map();
System.out.println("For help, type Help. Type 0 to exit");
// actual game loop
while (true) {
System.out.println(M.directions(player));
System.out.println(M.getInfo(player.getX(), player.getY()));
// player heals for 5 health every 'hour'
player.heal(5);
// if player has enough xp to level up
if (player.levelup())
System.out.println("You have " + (player.getXp() / 100) + " level ups available!");
System.out.println();
input = in.nextLine();
if (input.equals("0"))
break;
else if (input.equalsIgnoreCase("help"))
help();
else if (input.equalsIgnoreCase("area"))
area();
else if (input.equalsIgnoreCase("explore"))
explore(player);
else if (input.equalsIgnoreCase("Map") || input.equalsIgnoreCase("look map"))
M.printMap(player);
else if (input.equalsIgnoreCase("Level up") || input.equalsIgnoreCase("Level"))
levelup(player);
else
Action(input, M, player);
System.out.println("------------");
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// action state, allows for interaction by splitting verb and subject
// and putting verb through dictionary class to find out what it is.
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void Action(String input, Map M, Player P) {
char v;
String s;
Dictionary d = new Dictionary();
NPC[] Vil = M.getNPCs(P.getX(), P.getY());
//////////////////////////////
// if there is a space between two words. splits the verb and subject.
//////////////////////////////
if (input.trim().indexOf(" ") != -1) {
v = getVerb(input);
s = getSubject(input);
// talk action//
if (v == 's') {
for (int i = 0; i < Vil.length; i++) {
if (Vil[i] != null) {
if (Vil[i].equals(s)) {
System.out.println(Vil[i].talkTo());
break;
}
} else if (i == Vil.length - 1) {
System.out.println("I dont know who you want to do that to.");
break;
}
}
}
// look action//
else if (v == 'i') {
if (s.equalsIgnoreCase("area") || s.equalsIgnoreCase("around") || s.equalsIgnoreCase("surroundings")) {
area();
} else if (s.equalsIgnoreCase("self") || s.equalsIgnoreCase("me")) {
P.inspect();
} else {
for (int i = 0; i < Vil.length; i++) {
if (Vil[i] != null) {
if (Vil[i].equals(s)) {
System.out.println(Vil[i].inspect());
break;
}
} else if (i == Vil.length - 1) {
System.out.println("I dont know who you want to do that to.");
break;
}
}
}
}
// attack action//
else if (v == 'k') {
for (int i = 0; i < Vil.length; i++) {
if (Vil[i] != null) {
if (Vil[i].equals(s)) {
System.out.println(Vil[i].attack());
break;
}
} else if (i == Vil.length - 1) {
System.out.println("I dont know who you want to do that to.");
}
}
}
// move action//
else if (v == 'g') {
int rn = (int) (Math.random() * 9 + 1);
if (rn == 1)
battle(P);
else {
movement(input, P, M);
}
}
}
//////////////////////////////
// if only one word is detected
//////////////////////////////
else {
// move action//
if (d.getDirection(input) != 'x') {
int rn = (int) (Math.random() * 9 + 1);
if (rn == 1)
battle(P);
else {
movement(input, P, M);
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// get verb, goes from beginning to first space
//////////////////////////////////////////////////////////////////////////////////////////////////
public static char getVerb(String in) {
Dictionary d = new Dictionary();
int spc = in.indexOf(' ');
String verb = in.substring(0, spc);
return d.verb(verb);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// get subject, goes from first space to end.
//////////////////////////////////////////////////////////////////////////////////////////////////
public static String getSubject(String in) {
int spc = in.indexOf(' ');
String Subject = in.substring(spc + 1, in.length());
return Subject;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// prints help
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void help() {
System.out.println(
"You can go north, south, east, or west depending on where you are. Available directions should be printed above where you type.\n"
+ "If there are any NPCs in your area, they should be printed below the directions. You can talk, inspect, taunt, or attack NPCs.\n"
+ "You can explore for items or enemies, and while moving on the map you should find some enemies. Print out the map by saying 'map'."
+ "\nIf you have a levelup available, type 'l' or \"level up\".");
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// prints area//////////will remove this later
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void area() {
System.out.println("You look at a barren wasteland of unflavored text.");
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// chance of finding monster or item
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void explore(Player p) {
int rn = (int) (Math.random() * 9) + 1;
if (rn <= 4) {
System.out.println("you found an Item!");
} else {
System.out.println("A monster appears out of the shadows!");
battle(p);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// battle method, another loop
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void battle(Player P) {
Scanner in = new Scanner(System.in);
Monster S;
// generating the random monster////////
int rn = (int) (Math.random() * 4);
if (rn == 0)
S = new Skeleton();
else if (rn == 1)
S = new MonsterousPlant();
else if (rn == 2)
S = new LoneWolf();
else
S = new CorruptedKnight();
Dictionary d = new Dictionary();
// repeats until someone has no health////////
do {
// intro text//////
System.out.println("You are fighting a " + S.getName());
System.out.println("You have " + P.getHp() + "/" + P.getMHp());
System.out.println("The " + S.getName() + " Has " + S.getHp() + " HP remaining.");
System.out.println("What do you want to do?");
System.out.println();
String a = in.nextLine();
// booleans for player action and attacks.///
boolean playeraction = false;
boolean playerattack = false;
boolean playerblock = false;
if (d.verb(a) == 'r')
break;
if (d.verb(a) == 'a') // attack action//
{
playeraction = true;
playerattack = true;
} else if (d.verb(a) == 'd') // block action//
{
System.out.println("You block the " + S.getName() + "'s attack!");
playerblock = true;
} else if (d.verb(a) == 's') // talk action//
{
System.out.println(S.talkTo());
playeraction = true;
} else if (d.verb(a) == 'i') // inspect action//
{
System.out.println(S.inspect());
} else if (d.verb(a) == 't') // taunt action//
{
System.out.println(S.taunt());
playeraction = true;
} else if (d.verb(a) == 'u') // use action//
{
useItem(P);
playeraction = true;
} else {
System.out.println("I didnt understand that, please only type one word while in a battle.");
}
////////////////////////////////////
// decides if the player did an action that the monster can attack
//////////////////////////////////// after
////////////////////////////////////
if (P.getFullSpd() >= S.getFullSpd())// if player speed greater
{
if (playeraction) {
// decides if the player attacked///
if (playerattack)
System.out.println("You do " + S.damage(P.attack()) + " damage!");
System.out.println(" The " + S.getName() + " does " + P.damage(S.attack()) + " damage!");
}
} else// if monster speed greater
{
if (playeraction) {
System.out.println(" The " + S.getName() + " does " + P.damage(S.attack()) + " damage!");
if (playerattack)
System.out.println("You do " + S.damage(P.attack()) + " damage!");
}
}
System.out.println("Your hp = " + P.getHp());
System.out.println(S.getName() + " hp = " + S.getHp());
System.out.println();
} while (P.getHp() > 0 && S.getHp() > 0);
if (P.getHp() > 0 && S.getHp() <= 0)// player won/////
{
System.out.println("You won!");
int xp = (int) (Math.random() * 25) + 5;// generates random amount
// of experience between 5
// and 25
System.out.println("You got " + xp + " Experience!");
P.addXp(xp);
} else if (P.getHp() > 0 && S.getHp() > 0)// player likely ran
System.out.println("You escaped!");
else // player likely died
System.out.println("You died.");
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Using items
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void useItem(Player P) {
Scanner in = new Scanner(System.in);
Dictionary D = new Dictionary();
///////////////////////////////////////////////////////////////////////////////////////////////
// infinite loop until an output is reached. Possibility to break later
///////////////////////////////////////////////////////////////////////////////////////////////
do {
System.out.println("What item would you like to use? Press 0 to exit.");
P.getPack();
String next = in.nextLine();
if (next.equals("0")) // 0 exit code
break;
if (P.hasItem()) // tests if the player has any items at all, should
// probobly move this up.
{
if (D.getPot(next) == 'h') {
P.useItem(0);
break;
} else if (D.getPot(next) == 's') {
P.useItem(1);
break;
} else if (D.getPot(next) == 'd') {
P.useItem(2);
break;
} else if (D.getPot(next) == 'a') {
P.useItem(3);
break;
} else
System.out.println("I didn't understand that.");
} else {
System.out.println("You have no items to use!");
break;
}
} while (true);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// move commands
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void movement(String input, Player P, Map M) {
Dictionary D = new Dictionary();
char direction = D.getDirection(input);
System.out.println(direction);
if (direction == 'n')
System.out.println(P.goNorth(M));
else if (direction == 's')
System.out.println(P.goSouth(M));
else if (direction == 'e')
System.out.println(P.goEast(M));
else if (direction == 'w')
System.out.println(P.goWest(M));
M.printMap(P);
}
public static void levelup(Player P)// level ups//
// WIP///////////////////////////////////////////////////
{
if (P.levelup()) {
Scanner in = new Scanner(System.in);
Dictionary D = new Dictionary();
System.out.println("What attribute would you like to level up?");
String input = in.nextLine();
if (D.levelup(input) == 'a')
P.levelAtk();
else if (D.levelup(input) == 'd')
P.levelDef();
else if (D.levelup(input) == 's')
P.levelSpd();
else if (D.levelup(input) == 'h')
P.levelHp();
} else
System.out.println("You do not have enough XP to level up!");
}
public static Player creator(Scanner in) {
int pts = 30;
int hp;
int atk;
int def;
int spd;
// name
System.out.println("First off, What is your name?");
String name = in.nextLine();
// health
System.out.println(
"You start with 100 Health, how much would you like to add? \nYou have " + pts + " points left.");
while (true) {
hp = in.nextInt();
if (hp <= pts) {
pts -= hp;
break;
} else
System.out.println("You do not have enough points for that.");
}
// attack
System.out.println(
"You start with 25 attack, how much would you like to add? \nYou have " + pts + " points left.");
while (true) {
atk = in.nextInt();
if (atk <= pts) {
pts -= atk;
break;
} else
System.out.println("You do not have enough points for that.");
}
// defense
System.out.println(
"You start with 25 defense, how much would you like to add? \nYou have " + pts + " points left.");
while (true) {
def = in.nextInt();
if (def <= pts) {
pts -= def;
break;
} else
System.out.println("You do not have enough points for that.");
}
// speed
System.out.println(
"You start with 25 Speed, how much would you like to add? \nYou have " + pts + " points left.");
while (true) {
spd = in.nextInt();
if (spd <= pts) {
pts -= spd;
break;
} else
System.out.println("You do not have enough points for that");
}
System.out.println("So your name is " + name + ", You have " + (hp + 100) + " Health, " + (atk + 25)
+ " attack, " + (def + 25) + " defense, and " + (spd + 25) + " speed.");
return new Player(name, hp + 100, atk + 25, def + 25, spd + 25);
}
}