Skip to content

Commit 6cec09c

Browse files
authored
Merge pull request #17 from 100RABHpy/main
resolvedBugInTut7
2 parents 8f585b9 + f565d24 commit 6cec09c

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

tut-7/Game.java

+25-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ public class Game {
66
private ArrayList<ArrayList<Integer>> graph;
77
private HashMap<Integer, Monster> Monster_map;
88
private int visited[];
9-
private static java.util.Scanner s = new java.util.Scanner(System.in);
10-
;
9+
private static java.util.Scanner s = new java.util.Scanner(System.in);;
1110

1211
public Game() {
1312
Hero_list = new ArrayList<>();
@@ -26,14 +25,14 @@ public static void menu_display() {
2625

2726
public void New_User() {
2827
System.out.println("Enter Username");
29-
//s.nextLine();
28+
// s.nextLine();
3029
String name = s.nextLine();
3130
System.out.println("Choose Class");
3231
System.out.println("1) Warrior");
3332
System.out.println("2) Thief");
3433
System.out.println("3) Mage");
3534
System.out.println("4) Healer");
36-
int choice = s.nextInt();
35+
int choice = Integer.parseInt(s.nextLine());
3736
switch (choice) {
3837
case 1:
3938
Hero_list.add(new Warrior(name));
@@ -47,6 +46,8 @@ public void New_User() {
4746
case 4:
4847
Hero_list.add(new Healer(name));
4948
break;
49+
default:
50+
System.out.println("give correct input");
5051
}
5152
System.out.println("User Creation done. Log in to play game . Exiting");
5253
}
@@ -104,7 +105,7 @@ public void load_graph() {
104105
}
105106

106107
public int choose_path(int start_pos) {
107-
System.out.println("You are at location "+start_pos+" Choose path: ");
108+
System.out.println("You are at location " + start_pos + " Choose path: ");
108109
int count = 1;
109110
int flag = 1;
110111
int temp_i = 10;
@@ -125,11 +126,11 @@ public int choose_path(int start_pos) {
125126
choice_find.put(count, temp_i);
126127
}
127128
System.out.println("Enter -1 to exit");
128-
int choice = s.nextInt();
129+
int choice = Integer.parseInt(s.nextLine());
129130
if (choice != count) {
130131
visited[start_pos] = 1;
131132
}
132-
if(choice==-1){
133+
if (choice == -1) {
133134
return -1;
134135
}
135136
return choice_find.get(choice);
@@ -148,21 +149,23 @@ public int fight(Hero hero, Monster monster) {
148149
if (move_count >= 4 && hero.getSpecial_move_count() == move_count) {
149150
System.out.println("3)Special Attack");
150151
}
151-
int choice = s.nextInt();
152+
int choice = Integer.parseInt(s.nextLine());
152153
if (choice != 3 && move_count >= 4 && hero.getSpecial_move_count() == move_count) {
153154
hero.setSpecial_move_count(hero.getSpecial_move_count() + 1);
154-
} else if(choice==3){
155+
} else if (choice == 3) {
155156
hero.setSpecial_move_count(hero.getSpecial_move_count() + 4);
156157
}
157158
if (hero.getSpecial_power() == 0) {
158159
if (choice == 1) {
159160
System.out.println("You choose to attack");
160161
monster.setHp(monster.getHp() - hero.getAttack());
161-
System.out.println("Your Hp: "+hero.getHp()+"/"+hero.getHp_limit()+"Monsters Hp :"+monster.getHp()+"/"+monster.getHp_limit());
162+
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit() + "Monsters Hp :"
163+
+ monster.getHp() + "/" + monster.getHp_limit());
162164
} else if (choice == 2) {
163165
System.out.println("You choose to defend");
164166
def = hero.getDefense();
165-
System.out.println("Your Hp: "+hero.getHp()+"/"+hero.getHp_limit()+"Monsters Hp :"+monster.getHp()+"/"+monster.getHp_limit());
167+
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit() + "Monsters Hp :"
168+
+ monster.getHp() + "/" + monster.getHp_limit());
166169
} else if (choice == 3) {
167170
special_power_count = move_count;
168171
System.out.println("Special power activated");
@@ -191,7 +194,8 @@ public int fight(Hero hero, Monster monster) {
191194
}
192195
System.out.println("Monster attack!");
193196
hero.setHp(hero.getHp() - damage);
194-
System.out.println("Your Hp: "+hero.getHp()+"/"+hero.getHp_limit()+"Monsters Hp :"+monster.getHp()+"/"+monster.getHp_limit());
197+
System.out.println("Your Hp: " + hero.getHp() + "/" + hero.getHp_limit() + "Monsters Hp :" + monster.getHp()
198+
+ "/" + monster.getHp_limit());
195199

196200
if (hero.getHp() <= 0) {
197201
monster.respawn();
@@ -202,30 +206,32 @@ public int fight(Hero hero, Monster monster) {
202206
return 0;
203207
}
204208

205-
public void Load_game(Hero hero){
209+
public void Load_game(Hero hero) {
206210
load_graph();
207-
int flag=1;
208-
int init_pos=10;
209-
while(init_pos!=-1) {
210-
flag=1;init_pos=10;
211+
int flag = 1;
212+
int init_pos = 10;
213+
while (init_pos != -1) {
214+
flag = 1;
215+
init_pos = 10;
211216
while (flag == 1) {
212217
init_pos = choose_path(init_pos);
213-
if(init_pos==-1){
218+
if (init_pos == -1) {
214219
break;
215220
}
216221
visited[init_pos] = 0;
217222
if (init_pos != 10) {
218223
int result = fight(hero, Monster_map.get(init_pos));
219224
if (result == 0) {
220225
System.out.println("You have died start over");
221-
flag=0;
226+
flag = 0;
222227
} else {
223228
System.out.println("Fight won proceed to next location");
224229
}
225230
}
226231
}
227232
}
228233
}
234+
229235
public void Existing_User(String name) {
230236
for (Hero i : Hero_list) {
231237
if (i.getUsername().equals(name)) {

tut-7/Menu.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
public class Menu {
22
public static java.util.Scanner s = new java.util.Scanner(System.in);
3+
34
public static void main(String args[]) {
4-
Game new_game=new Game();
5+
Game new_game = new Game();
56
int flag = 1;
67
while (flag == 1) {
78
new_game.menu_display();
8-
int choice = s.nextInt();
9+
int choice = Integer.parseInt(s.nextLine());
910
switch (choice) {
1011
case 1:
1112
new_game.New_User();
1213
break;
1314
case 2:
1415
System.out.println("Enter user name");
15-
s.nextLine();
1616
String name = s.nextLine();
1717
new_game.Existing_User(name);
1818
break;

0 commit comments

Comments
 (0)