-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoRPG.java
More file actions
333 lines (274 loc) · 9.23 KB
/
YoRPG.java
File metadata and controls
333 lines (274 loc) · 9.23 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
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
// Team FileNotFound -- Ryan Siu, Jonathan Quang, Rihui Zheng
// APCS1 pd5
// HW32 -- Ye Olde Role Playing Game, Expanded
// 2016-11-20
/*=============================================
class YoRPG -- Driver file for Ye Olde Role Playing Game.
Simulates monster encounters of a wandering adventurer.
Required classes: Warrior, Monster
=============================================*/
import java.io.*;
import java.util.*;
public class YoRPG
{
// ~~~~~~~~~~~ INSTANCE VARIABLES ~~~~~~~~~~~
//change this constant to set number of encounters in a game
public final static int MAX_ENCOUNTERS = 5;
//each round, a Warrior and a Monster will be instantiated...
private Character pat; //Is it man or woman?
private Monster smaug; //Friendly generic monster name?
private Character bones; //Cuuuuuute!
private int moveCount;
private boolean gameOver;
private int difficulty;
private InputStreamReader isr;
private BufferedReader in;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~ DEFAULT CONSTRUCTOR ~~~~~~~~~~~
public YoRPG() {
moveCount = 0;
gameOver = false;
isr = new InputStreamReader( System.in );
in = new BufferedReader( isr );
newGame();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~ METHODS ~~~~~~~~~~~~~~~~~~~
/*=============================================
void newGame() -- facilitates info gathering to begin a new game
pre:
post: according to user input, modifies instance var for difficulty
and instantiates a Warrior
=============================================*/
public void newGame() {
String s;
String name = "";
String pName = "";
s = "Welcome to Ye Olde RPG!\n";
s += "\nChoose your difficulty: \n";
s += "\t1: Easy\n";
s += "\t2: Not so easy\n";
s += "\t3: Beowulf hath nothing on me. Bring it on.\n";
s += "Selection: ";
System.out.print( s );
while (difficulty == 0) {
int diff = 0;
try {
diff = Integer.parseInt( in.readLine() );
}
catch ( IOException e ) { }
if (diff > 0 && diff < 4) {
difficulty = diff;
}
else {
System.out.println("Invalid Choice. Try Again.");
}
}
s = "Intrepid warrior, what doth thy call thyself? (State your name): ";
System.out.print( s );
try {
name = in.readLine();
}
catch ( IOException e ) { }
s = "Choose a character class from the list:\n";
s += "1) Warrior\n";
s += "2) Mage\n";
s += "3) Rogue\n";
s += "4) Priest\n";
s += "5) Archer\n";
s += "Your input (as a number): ";
System.out.print( s );
while (pat == null) {
int character = 0;
try {
character = Integer.parseInt( in.readLine() );
}
catch ( IOException e ) { }
//instantiate the player's character by checking the character int,
//the return string shows an error choice if the # is not [1,5] or 9000
if ( character == 1 ) {
pat = new Warrior( name );
s = pat.about();
} else if ( character == 2 ) {
pat = new Mage( name );
s = pat.about();
} else if ( character == 3 ) {
pat = new Rogue( name );
s = pat.about();
} else if ( character == 4 ) {
pat = new Priest( name );
s = pat.about();
} else if ( character == 5 ) {
pat = new Archer( name );
s = pat.about();
} else if (character == 9000) {
pat = new God( name );
s = pat.about();
} else {
s = "Invalid choice. Choose a character class from the list: ";
}
System.out.print( s );
}
s = "\nThat is a nice pet you got there. What be it's name?";
System.out.println(s);
try {
pName = in.readLine();
}
catch ( IOException e ) { }
s = "That be a nice name. What kind of pet is it?\n";
s += "1) Dragon\n";
s += "2) Wolf\n";
s += "3) Serpent\n";
s += "Your choice (as a number): ";
System.out.print( s );
while (bones == null) {
int pet = 0;
try {
pet = Integer.parseInt( in.readLine() );
}
catch ( IOException e ) { }
//instantiate the player's pet by checking the pet int,
//the return string shows an error choice if the # is not [1,3] or 9000
if ( pet == 1 ) {
bones = new Dragon( pName );
s = bones.about();
} else if ( pet == 2 ) {
bones = new Wolf( pName );
s = bones.about();
} else if ( pet == 3 ) {
bones = new Serpent( pName );
s = bones.about();
} else if (pet == 9000) {
bones = new doG( pName );
s = bones.about();
} else {
s = "Invalid choice. Choose a pet type from the list: ";
}
System.out.println(s);
}
System.out.println("\n\n~~~~~~ START OF GAME ~~~~~~");
}//end newGame()
/*=============================================
boolean playTurn -- simulates a round of combat
pre: Warrior pat has been initialized
post: Returns true if player wins (monster dies).
Returns false if monster wins (player dies).
=============================================*/
public boolean playTurn() {
int i = 1;
int d1, d2, d3;
if ( Math.random() >= ( difficulty / 3.0 ) )
System.out.println( "\nNothing to see here. Move along!" );
else {
System.out.println( "\nLo, yonder monster approacheth!" );
smaug = new Monster();
while( smaug.isAlive() && pat.isAlive() ) {
// Give user the option of using a special attack:
// If you land a hit, you incur greater damage,
// ...but if you get hit, you take more damage.
System.out.println( "You have " + pat.getHitPts() + " HP." );
System.out.println( "The monster has " + smaug.getHitPts() + " HP." );
System.out.print( "Your inventory: ");
for (InventoryItems item : pat.getInventory()) {
if (item != null) {
System.out.print( item.getName() + "," );
}
}
System.out.println();
try {
System.out.println( "\nDo you feel lucky?" );
System.out.println( "\t1: Nay.\n\t2: Aye!\n\t3: Huh?\n\t4: Time for Hero special?" );
i = Integer.parseInt( in.readLine() );
}
catch ( IOException e ) { }
if (i == 4) {
System.out.println(pat.heroSpecial());
}
// Use item
if ( i == 3 ) {
try {
InventoryItems item = pat.getItem();
if (item.getType().equals("hp")) {
pat.gainHP( item.getEffect() );
System.out.println( "\n" + pat.getName() + " used a " +
item.getName() + " and gained " +
item.getEffect() + " HP.");
} else if (item.getType().equals("att")) {
smaug.lowerHP( item.getEffect() );
System.out.println( "\n" + pat.getName() + " threw a " +
item.getName() + " and dealt " +
item.getEffect() + " points of damage.");
}
pat.removeItem( item );
}
catch (NullPointerException e) {
System.out.println("\nYou don't have any items!");
}
}
else {
// Use special attack
if (i == 2)
pat.specialize();
// Use normal attack
else
pat.normalize();
d1 = pat.attack( smaug );
d2 = smaug.attack( pat );
d3 = bones.attack( smaug );
System.out.println( "\n" + pat.getName() + " dealt " + d1 +
" points of damage.");
System.out.println( "\n" + "Ye Olde Monster smacked " + pat.getName() +
" for " + d2 + " points of damage.");
System.out.println( "\n" + bones.getName() + " bit Ye Olde Monster for " +
+ d3 + " points of damage.");
// Adds item to inventory, if item is not named "none"
InventoryItems item = pat.addItem();
if (!item.equals("none")) {
System.out.println( "\nA " + item.getName() + " was added to " +
pat.getName() + "'s inventory.");
}
}
System.out.println();
}//end while
//option 1: you & the monster perish
if ( !smaug.isAlive() && !pat.isAlive() ) {
System.out.println( "'Twas an epic battle, to be sure... " +
"You cut ye olde monster down, but " +
"with its dying breath ye olde monster. " +
"laid a fatal blow upon thy skull." );
return false;
}
//option 2: you slay the beast
else if ( !smaug.isAlive() ) {
System.out.println( "HuzzaaH! Ye olde monster hath been slain!" );
return true;
}
//option 3: the beast slays you
else if ( !pat.isAlive() ) {
System.out.println( "Ye olde self hath expired. You got dead." );
return false;
}
}//end else
return true;
}//end playTurn()
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static void main( String[] args ) {
//As usual, move the begin-comment bar down as you progressively
//test each new bit of functionality...
//loading...
YoRPG game = new YoRPG();
int encounters = 0;
while( encounters < MAX_ENCOUNTERS ) {
if ( !game.playTurn() )
break;
encounters++;
System.out.println();
}
System.out.println();
System.out.println( "Thy game doth be over." );
/*================================================
================================================*/
}//end main
}//end class YoRPG
/*=============================================
=============================================*/