-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoRPG.java
More file actions
204 lines (161 loc) · 5.32 KB
/
Copy pathYoRPG.java
File metadata and controls
204 lines (161 loc) · 5.32 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
/*Noah Tang, Tommy Lin, Ibnul Jahan
APCS1 pd5
HW30 Ye Olde Role Playing Game
2016-11-16 */
/*=============================================
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 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() -- gathers info 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 = "";
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 );
try {
difficulty = Integer.parseInt( in.readLine() );
}
catch ( IOException e ) { }
//======================================================================
s = "Intrepid warrior, what doth thy call thyself? (State your name): ";
System.out.print( s );
try {
name = in.readLine();
}
catch ( IOException e ) { }
//======================================================================
s = "Pick your profession please:\n";
s += "\t1: Warrior\n";
s += "\t2: Baker\n";
s += "\t3: Mage\n";
s += "\t4: Summoner\n";
s += "Selection: ";
System.out.print( s );
int choice = 0;
try {
choice = Integer.parseInt( in.readLine() );
}
catch ( IOException e ) { }
//instantiate the player's character
if (choice == 1){pat = new Warrior( name );}
if (choice == 2){pat = new Baker( name);}
if (choice == 3){pat = new Mage(name);}
if (choice == 4){pat = new Summoner(name);}
else{ pat = new Warrior( name);}
}
//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;
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.
try {
System.out.println( "\nDo you feel lucky?" );
System.out.println( "\t1: Nay.\n\t2: Aye!" );
i = Integer.parseInt( in.readLine() );
}
catch ( IOException e ) { }
if ( i == 2 )
pat.specialize();
else
pat.normalize();
d1 = pat.attack( smaug );
d2 = smaug.attack( pat );
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.");
}//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( "Thy game doth be over." );
/*================================================
================================================*/
}//end main
}//end class YoRPG