Skip to content

Commit 206ea85

Browse files
100RABHpy100RABHpy
100RABHpy
authored and
100RABHpy
committed
Handled Exceptions
1 parent f565d24 commit 206ea85

File tree

2 files changed

+196
-113
lines changed

2 files changed

+196
-113
lines changed

tut-7/Game.java

+173-100
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,56 @@ public static void menu_display() {
2424
}
2525

2626
public void New_User() {
27-
System.out.println("Enter Username");
28-
// s.nextLine();
29-
String name = s.nextLine();
30-
System.out.println("Choose Class");
31-
System.out.println("1) Warrior");
32-
System.out.println("2) Thief");
33-
System.out.println("3) Mage");
34-
System.out.println("4) Healer");
35-
int choice = Integer.parseInt(s.nextLine());
36-
switch (choice) {
37-
case 1:
38-
Hero_list.add(new Warrior(name));
39-
break;
40-
case 2:
41-
Hero_list.add(new Thief(name));
42-
break;
43-
case 3:
44-
Hero_list.add(new Mage(name));
45-
break;
46-
case 4:
47-
Hero_list.add(new Healer(name));
48-
break;
49-
default:
50-
System.out.println("give correct input");
27+
boolean flag = true;
28+
String name = null;
29+
while (flag) {
30+
System.out.println("Enter Username");
31+
name = s.nextLine();
32+
if (name.equals("") || name.equals(" ")) {
33+
System.out.println("Give correct username");
34+
} else {
35+
flag = false;
36+
}
37+
}
38+
flag = true;
39+
while (flag) {
40+
System.out.println("Choose Class");
41+
System.out.println("1) Warrior");
42+
System.out.println("2) Thief");
43+
System.out.println("3) Mage");
44+
System.out.println("4) Healer");
45+
46+
boolean check = true;
47+
int choice = 0;
48+
try {
49+
choice = Integer.parseInt(s.nextLine());
50+
} catch (NumberFormatException e) {
51+
System.out.println("Wrong Input. Give Integer Input.");
52+
check = false;
53+
54+
}
55+
if (check) {
56+
switch (choice) {
57+
case 1:
58+
Hero_list.add(new Warrior(name));
59+
flag = false;
60+
break;
61+
case 2:
62+
Hero_list.add(new Thief(name));
63+
flag = false;
64+
break;
65+
case 3:
66+
Hero_list.add(new Mage(name));
67+
flag = false;
68+
break;
69+
case 4:
70+
Hero_list.add(new Healer(name));
71+
flag = false;
72+
break;
73+
default:
74+
System.out.println("Please choose from available choice.");
75+
}
76+
}
5177
}
5278
System.out.println("User Creation done. Log in to play game . Exiting");
5379
}
@@ -110,30 +136,47 @@ public int choose_path(int start_pos) {
110136
int flag = 1;
111137
int temp_i = 10;
112138
HashMap<Integer, Integer> choice_find = new HashMap<>();
113-
for (Integer i : graph.get(start_pos)) {
114-
if (visited[i] == 1) {
115-
flag = 0;
116-
temp_i = i;
117-
} else {
118-
System.out.println(count + ") Go to Location " + i);
119-
choice_find.put(count, i);
120-
count++;
139+
boolean check = true;
140+
int choice = 0;
141+
while (check) {
142+
for (Integer i : graph.get(start_pos)) {
143+
if (visited[i] == 1) {
144+
flag = 0;
145+
temp_i = i;
146+
} else {
147+
System.out.println(count + ") Go to Location " + i);
148+
choice_find.put(count, i);
149+
count++;
150+
}
151+
152+
}
153+
if (flag == 0) {
154+
System.out.println(count + ") Go back");
155+
choice_find.put(count, temp_i);
121156
}
157+
System.out.println("Enter -1 to exit");
122158

123-
}
124-
if (flag == 0) {
125-
System.out.println(count + ") Go back");
126-
choice_find.put(count, temp_i);
127-
}
128-
System.out.println("Enter -1 to exit");
129-
int choice = Integer.parseInt(s.nextLine());
130-
if (choice != count) {
131-
visited[start_pos] = 1;
159+
try {
160+
choice = Integer.parseInt(s.nextLine());
161+
check = false;
162+
} catch (NumberFormatException e) {
163+
System.out.println("Wrong Input. Give Integer Input.");
164+
return choose_path(start_pos);
165+
166+
}
132167
}
133168
if (choice == -1) {
134169
return -1;
135170
}
136-
return choice_find.get(choice);
171+
if ((!(choice >= count || choice < 1)) || (flag == 0 && choice == count)) {
172+
if (choice != count) {
173+
visited[start_pos] = 1;
174+
}
175+
return choice_find.get(choice);
176+
} else {
177+
System.out.println("Please choose from available locations.");
178+
return choose_path(start_pos);
179+
}
137180
}
138181

139182
public int fight(Hero hero, Monster monster) {
@@ -142,65 +185,91 @@ public int fight(Hero hero, Monster monster) {
142185
int move_count = 1;
143186
int def = 0;
144187
int special_power_count = -10;
188+
145189
while (flag == 1) {
146-
System.out.println("Choose move: ");
147-
System.out.println("1) Attack");
148-
System.out.println("2) Defense");
149-
if (move_count >= 4 && hero.getSpecial_move_count() == move_count) {
150-
System.out.println("3)Special Attack");
151-
}
152-
int choice = Integer.parseInt(s.nextLine());
153-
if (choice != 3 && move_count >= 4 && hero.getSpecial_move_count() == move_count) {
154-
hero.setSpecial_move_count(hero.getSpecial_move_count() + 1);
155-
} else if (choice == 3) {
156-
hero.setSpecial_move_count(hero.getSpecial_move_count() + 4);
157-
}
158-
if (hero.getSpecial_power() == 0) {
159-
if (choice == 1) {
160-
System.out.println("You choose to attack");
161-
monster.setHp(monster.getHp() - hero.getAttack());
162-
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit() + "Monsters Hp :"
163-
+ monster.getHp() + "/" + monster.getHp_limit());
164-
} else if (choice == 2) {
165-
System.out.println("You choose to defend");
166-
def = hero.getDefense();
167-
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit() + "Monsters Hp :"
168-
+ monster.getHp() + "/" + monster.getHp_limit());
169-
} else if (choice == 3) {
170-
special_power_count = move_count;
171-
System.out.println("Special power activated");
172-
hero.setSpecial_power(1);
190+
boolean superCheck = true;
191+
int choice = 0;
192+
while (superCheck) {
193+
boolean check = true;
194+
while (check) {
195+
System.out.println("Choose move: ");
196+
System.out.println("1) Attack");
197+
System.out.println("2) Defense");
198+
if (move_count >= 4 && hero.getSpecial_move_count() == move_count) {
199+
System.out.println("3)Special Attack");
200+
}
201+
202+
try {
203+
choice = Integer.parseInt(s.nextLine());
204+
check = false;
205+
} catch (NumberFormatException e) {
206+
System.out.println("Wrong Input. Give Integer Input.");
207+
}
173208
}
174-
}
175-
if (hero.getSpecial_power() == 1) {
176-
def = hero.special_power_fight(monster, choice);
177-
}
178-
if (monster.getHp() <= 0) {
179-
hero.setXp(hero.getXp() + monster.getLvl() * 20);
180-
hero.level_check();
181-
monster.respawn();
182-
hero.default_game();
183-
return 1;
184-
}
185-
move_count++;
186-
if (move_count - special_power_count == 3) {
187-
special_power_count = -10;
188-
hero.setSpecial_power(0);
189-
System.out.println("Special power deactivated");
190-
}
191-
int damage = new Random().nextInt(monster.getHp() / 4);
192-
if (damage - def >= 0) {
193-
damage = damage - def;
194-
}
195-
System.out.println("Monster attack!");
196-
hero.setHp(hero.getHp() - damage);
197-
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit() + "Monsters Hp :" + monster.getHp()
198-
+ "/" + monster.getHp_limit());
199-
200-
if (hero.getHp() <= 0) {
201-
monster.respawn();
202-
hero.default_game();
203-
return 0;
209+
if (!(choice > 3 || choice < 1)) {
210+
211+
if ((choice == 3 && !(move_count >= 4 && hero.getSpecial_move_count() == move_count))) {
212+
System.out.println("You can't use special power");
213+
214+
} else {
215+
superCheck = false;
216+
if (choice != 3 && move_count >= 4 && hero.getSpecial_move_count() == move_count) {
217+
hero.setSpecial_move_count(hero.getSpecial_move_count() + 1);
218+
} else if (choice == 3) {
219+
hero.setSpecial_move_count(hero.getSpecial_move_count() + 4);
220+
}
221+
if (hero.getSpecial_power() == 0) {
222+
if (choice == 1) {
223+
System.out.println("You choose to attack");
224+
monster.setHp(monster.getHp() - hero.getAttack());
225+
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit()
226+
+ "Monsters Hp :" + monster.getHp() + "/" + monster.getHp_limit());
227+
} else if (choice == 2) {
228+
System.out.println("You choose to defend");
229+
def = hero.getDefense();
230+
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit()
231+
+ "Monsters Hp :" + monster.getHp() + "/" + monster.getHp_limit());
232+
} else if (choice == 3) {
233+
special_power_count = move_count;
234+
System.out.println("Special power activated");
235+
hero.setSpecial_power(1);
236+
}
237+
}
238+
if (hero.getSpecial_power() == 1) {
239+
def = hero.special_power_fight(monster, choice);
240+
}
241+
if (monster.getHp() <= 0) {
242+
hero.setXp(hero.getXp() + monster.getLvl() * 20);
243+
hero.level_check();
244+
monster.respawn();
245+
hero.default_game();
246+
return 1;
247+
}
248+
move_count++;
249+
if (move_count - special_power_count == 3) {
250+
special_power_count = -10;
251+
hero.setSpecial_power(0);
252+
System.out.println("Special power deactivated");
253+
}
254+
int damage = new Random().nextInt(monster.getHp() / 4);
255+
if (damage - def >= 0) {
256+
damage = damage - def;
257+
}
258+
System.out.println("Monster attack!");
259+
hero.setHp(hero.getHp() - damage);
260+
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit() + "Monsters Hp :"
261+
+ monster.getHp() + "/" + monster.getHp_limit());
262+
263+
if (hero.getHp() <= 0) {
264+
monster.respawn();
265+
hero.default_game();
266+
return 0;
267+
}
268+
}
269+
} else {
270+
System.out.println("Wrong option. Please choose from available options.");
271+
}
272+
204273
}
205274
}
206275
return 0;
@@ -232,7 +301,9 @@ public void Load_game(Hero hero) {
232301
}
233302
}
234303

235-
public void Existing_User(String name) {
304+
public void Existing_User() {
305+
System.out.println("Enter user name");
306+
String name = s.nextLine();
236307
for (Hero i : Hero_list) {
237308
if (i.getUsername().equals(name)) {
238309
System.out.println("User Found... logging in");
@@ -241,6 +312,8 @@ public void Existing_User(String name) {
241312
}
242313
}
243314
System.out.println("User Not Found... Enter again");
315+
Existing_User();
316+
return;
244317
}
245318

246319
}

tut-7/Menu.java

+23-13
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@ public static void main(String args[]) {
66
int flag = 1;
77
while (flag == 1) {
88
new_game.menu_display();
9-
int choice = Integer.parseInt(s.nextLine());
10-
switch (choice) {
11-
case 1:
12-
new_game.New_User();
13-
break;
14-
case 2:
15-
System.out.println("Enter user name");
16-
String name = s.nextLine();
17-
new_game.Existing_User(name);
18-
break;
19-
case 3:
20-
System.out.println("Exiting");
21-
flag = 0;
9+
int choice = 0;
10+
boolean check = true;
11+
try {
12+
choice = Integer.parseInt(s.nextLine());
13+
} catch (NumberFormatException e) {
14+
System.out.println("Wrong Input. Give Integer Input.");
15+
check = false;
16+
}
17+
if (check) {
18+
switch (choice) {
19+
case 1:
20+
new_game.New_User();
21+
break;
22+
case 2:
23+
new_game.Existing_User();
24+
break;
25+
case 3:
26+
System.out.println("Exiting");
27+
flag = 0;
28+
break;
29+
default:
30+
System.out.println("Please choose from available choices.");
31+
}
2232
}
2333
}
2434
}

0 commit comments

Comments
 (0)