-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonalityGame.java
More file actions
125 lines (101 loc) · 6.02 KB
/
PersonalityGame.java
File metadata and controls
125 lines (101 loc) · 6.02 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
import java.util.Scanner; // import for Scanner (reads user input).
import java.util.function.Consumer; // import for lambdas (lets you write functional logic in fewer lines).
enum Personality { // enums (named constants that are type-safe, there type is literally Personality, with a fixed set of constants, & the compiler forces type safety!).
EXPLORER,
STRATEGIST,
CREATOR,
}
public class PersonalityGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Scanner is declared and named input.
// int counter variables named explorer, strategist, & creator, to keep track fo the user's stats.
int explorer = 0;
int strategist = 0;
int creator = 0;
System.out.println("Welcome to the Personality Analyzer Game!");
System.out.println("--you are given cool scenarios to choose from--\n");
// scenarios inside a variable type string that makes them intuitive and easier to handle.
String scenario1 =
"Scenario 1:\n\n" +
"You wake up in a mysterious forest. What do you do?\n\n" +
"1 - Walk around and explore\n" +
"2 - Look for a safe place to plan\n" +
"3 - Gather materials and build tools\n" +
"Choice: ";
System.out.print(scenario1); // scenario1 printed out from its type string var.
int choice = input.nextInt(); // intakes an integer from a selected choice.
switch(choice) { // if the integer matches a particular case it increments counter variables to update the user's stats.
case 1 -> explorer++;
case 2 -> strategist++;
case 3 -> creator++;
default -> System.out.println("Invalid Choice!");
}
// scenarios inside a variable type string that makes them intuitive and easier to handle.
String scenario2 =
"Scenario 2:\n\n" +
"You find an abandoned building. What do you do?\n\n" +
"1 - Search every room\n" +
"2 - Check exits and entrances first\n" +
"3 - Try to fix something broken inside\n" +
"Choice: ";
System.out.print(scenario2); // scenario2 printed out from its type string var.
choice = input.nextInt(); // no extra int needed, because it's type is technically already declared when reading from the first scenario.
switch(choice) { // if the integer matches a particular case it increments counter variables to update the user's stats.
case 1 -> explorer++;
case 2 -> strategist++;
case 3 -> creator++;
default -> System.out.println("Invalid Choice!");
}
// scenarios inside a variable type string that makes them intuitive and easier to handle.
String scenario3 =
"Scenario 3:\n\n" +
"You meet a stranger. What do you do?\n\n" +
"1 - Ask them about the world\n" +
"2 - Ask about survival tips\n" +
"3 - Ask what they can build or create\n" +
"Choice: ";
System.out.print(scenario3); // scenario3 printed out from its type string var.
choice = input.nextInt(); // no extra int needed, because it's type is technically already declared when reading from the first scenario.
switch(choice) { // if the integer matches a particular case it increments counter variables to update the user's stats.
case 1 -> explorer++;
case 2 -> strategist++;
case 3 -> creator++;
default -> System.out.println("Invalid Choice!");
}
// scenarios inside a variable type string that makes them intuitive and easier to handle.
String scenario4 =
"Scenario 4:\n\n" +
"You find strange technology. What do you do?\n\n" +
"1 - Test what it does\n" +
"2 - Analyze how it works first\n" +
"3 - Try modifying it\n" +
"Choice: ";
System.out.print(scenario4); // scenario4 printed out from its type string var.
choice = input.nextInt(); // no extra int needed, because it's type is technically already declared when reading from the first scenario.
switch(choice) { // if the integer matches a particular case it increments counter variables to update the user's stats.
case 1 -> explorer++;
case 2 -> strategist++;
case 3 -> creator++;
default -> System.out.println("Invalid Choice!");
}
// determines the final personality type for the trait with the highest incremented score and stores it as result. (uses the special logical operator (AND (&&))
Personality result;
if (explorer >= strategist && explorer >= creator) {
result = Personality.EXPLORER;
} else if (strategist >= explorer && strategist >= creator) {
result = Personality.STRATEGIST;
} else {
result = Personality.CREATOR;
}
// usage of Lambda to display the result
Consumer<Personality> displayResult =
trait -> System.out.println("\nYour personality type is: " + trait);
displayResult.accept(result); // accepts the result so that it can be used in the Lambda expression above.
// shows the total scores and how much they've incremented the personality type that should be displayed is that with the highest score which is what's incremented the most.
System.out.println("\nFinal Scores:");
System.out.println("Explorer: " + explorer);
System.out.println("Strategist: " + strategist);
System.out.println("Creator: " + creator);
input.close();
}
}