-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreasureHunt.java
More file actions
100 lines (92 loc) · 2.61 KB
/
TreasureHunt.java
File metadata and controls
100 lines (92 loc) · 2.61 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
private final int GRID_SIZE = 5;
private int treasureX;
private int treasureY;
private int playerX;
private int playerY;
private int moves;
private final String PLAYER_EMOJI = "\uD83D\uDC4B";
public void init() {
Random rand = new Random();
treasureX = rand.nextInt(GRID_SIZE);
treasureY = rand.nextInt(GRID_SIZE);
playerX = GRID_SIZE / 2;
playerY = GRID_SIZE / 2;
moves = 0;
println("\n\n🏝️ Welcome to Treasure Hunt!");
println("Find the hidden treasure on a " + GRID_SIZE + "x" + GRID_SIZE + " grid.");
println("Use commands: N, E, W, S, or north, south, east, west to move.");
displayGrid();
playGame();
}
private void playGame() {
while (true) {
String move = readln("Enter your move: ");
move = move.trim().toLowerCase();
if (makeMove(move)) {
moves++;
displayGrid();
if (playerX == treasureX && playerY == treasureY) {
println("🎉 You found the treasure in " + moves + " moves! Congratulations!");
break;
} else {
giveHint();
}
} else {
println("❌ Invalid move. Try other moves.");
}
}
}
private boolean makeMove(String direction) {
switch (direction) {
case "north", "n":
if (playerY > 0) {
playerY--;
return true;
}
break;
case "south", "s":
if (playerY < GRID_SIZE - 1) {
playerY++;
return true;
}
break;
case "east", "e":
if (playerX < GRID_SIZE - 1) {
playerX++;
return true;
}
break;
case "west", "w":
if (playerX > 0) {
playerX--;
return true;
}
break;
}
return false;
}
private void displayGrid() {
println("\nCurrent Grid:");
for (int y = 0; y < GRID_SIZE; y++) {
for (int x = 0; x < GRID_SIZE; x++) {
if (x == playerX && y == playerY) {
print("[" + PLAYER_EMOJI + "] ");
} else {
print("[⬛] ");
}
}
println();
}
println();
}
private void giveHint() {
int distance = Math.abs(playerX - treasureX) + Math.abs(playerY - treasureY);
switch (distance) {
case 1, 2 -> println("🔥 Great! You're getting closer!");
case 3, 4 -> println("🌤️ Watch out! You're moving farther.");
default -> println("❄️ Damn.. You're farther.");
}
}
void main() {
init();
}