-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava-code.txt
More file actions
108 lines (77 loc) · 3.95 KB
/
Copy pathJava-code.txt
File metadata and controls
108 lines (77 loc) · 3.95 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
import java.util.Scanner;
public class TextAdventureGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String playerName;
System.out.println("Welcome to the Text Adventure Game!");
System.out.print("Enter your name: ");
playerName = scanner.nextLine();
System.out.println("\nHello, " + playerName + "! Let's begin the adventure.\n");
startGame(scanner);
scanner.close();
}
public static void startGame(Scanner scanner) {
System.out.println("You wake up in a mysterious forest with two paths ahead of you.");
System.out.println("What do you do?");
System.out.println("1. Take the path to the left.");
System.out.println("2. Take the path to the right.");
int choice = getUserChoice(scanner, 1, 2);
if (choice == 1) {
pathLeft(scanner);
} else if (choice == 2) {
pathRight(scanner);
}
}
public static void pathLeft(Scanner scanner) {
System.out.println("\nYou chose the path to the left.");
System.out.println("You encounter a friendly wizard who offers you a magical potion.");
System.out.println("What do you do?");
System.out.println("1. Accept the potion.");
System.out.println("2. Decline the potion.");
int choice = getUserChoice(scanner, 1, 2);
if (choice == 1) {
System.out.println("\nThe wizard smiles and gives you the potion. You feel a surge of energy.");
System.out.println("You continue on your journey.");
} else if (choice == 2) {
System.out.println("\nThe wizard nods and you continue on your journey without the potion.");
System.out.println("You continue exploring the forest.");
}
}
public static void pathRight(Scanner scanner) {
System.out.println("\nYou chose the path to the right.");
System.out.println("You come across a dark cave entrance with a sign that says 'Enter at your own risk'.");
System.out.println("What do you do?");
System.out.println("1. Enter the cave.");
System.out.println("2. Avoid the cave and continue through the forest.");
int choice = getUserChoice(scanner, 1, 2);
if (choice == 1) {
System.out.println("\nYou cautiously enter the cave, your heart pounding.");
System.out.println("Inside, you find hidden treasures and a mysterious artifact.");
System.out.println("You emerge from the cave richer and with a powerful artifact.");
} else if (choice == 2) {
System.out.println("\nYou decide to avoid the cave and explore more of the forest.");
System.out.println("You discover a beautiful waterfall and a peaceful clearing.");
System.out.println("You spend some time relaxing before continuing your journey.");
}
}
public static int getUserChoice(Scanner scanner, int minChoice, int maxChoice) {
int choice = 0;
boolean isValidInput = false;
while (!isValidInput) {
System.out.print("Enter your choice (" + minChoice + "-" + maxChoice + "): ");
if (scanner.hasNextInt()) {
choice = scanner.nextInt();
if (choice >= minChoice && choice <= maxChoice) {
isValidInput = true;
} else {
System.out.println("Invalid input! Please enter a number between " + minChoice + " and " + maxChoice + ".");
}
} else {
System.out.println("Invalid input! Please enter a number.");
scanner.next();
}
}
scanner.nextLine();
return choice;
}
}