-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay27s1.java
More file actions
233 lines (202 loc) · 7.22 KB
/
Day27s1.java
File metadata and controls
233 lines (202 loc) · 7.22 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
public class Day27s1 {
public static void main(String[] args) {
MazeGame game = new MazeGame(15, 15);
game.play();
}
}
class MazeGame {
private static final char WALL = '█';
private static final char PATH = ' ';
private static final char PLAYER = 'P';
private static final char EXIT = 'E';
private static final char VISITED = '.';
private static final char TRAP = 'X';
private static final char KEY = 'K';
private static final char DOOR = 'D';
private char[][] maze;
private int width;
private int height;
private int playerX;
private int playerY;
private int exitX;
private int exitY;
private int keys;
private int steps;
private int health;
private boolean hasKey;
private Random random;
private Scanner scanner;
public MazeGame(int width, int height) {
this.width = width;
this.height = height;
this.maze = new char[height][width];
this.random = new Random();
this.scanner = new Scanner(System.in);
this.health = 100;
this.steps = 0;
this.keys = 0;
this.hasKey = false;
initializeMaze();
}
private void initializeMaze() {
// Labirenti duvarlarla doldur
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
maze[i][j] = WALL;
}
}
// Labirenti oluştur
generateMaze();
// Oyuncuyu yerleştir
playerX = 1;
playerY = 1;
maze[playerY][playerX] = PLAYER;
// Çıkışı yerleştir
exitX = width - 2;
exitY = height - 2;
maze[exitY][exitX] = EXIT;
// Kapıyı yerleştir
maze[height - 3][width - 2] = DOOR;
// Anahtarı yerleştir
int keyX, keyY;
do {
keyX = random.nextInt(width - 2) + 1;
keyY = random.nextInt(height - 2) + 1;
} while (maze[keyY][keyX] != PATH);
maze[keyY][keyX] = KEY;
// Tuzakları yerleştir
for (int i = 0; i < 5; i++) {
int trapX, trapY;
do {
trapX = random.nextInt(width - 2) + 1;
trapY = random.nextInt(height - 2) + 1;
} while (maze[trapY][trapX] != PATH);
maze[trapY][trapX] = TRAP;
}
}
private void generateMaze() {
Stack<int[]> stack = new Stack<>();
int startX = 1;
int startY = 1;
maze[startY][startX] = PATH;
stack.push(new int[]{startX, startY});
while (!stack.isEmpty()) {
int[] current = stack.peek();
int x = current[0];
int y = current[1];
// Komşu hücreleri kontrol et
int[][] directions = {{0, 2}, {2, 0}, {0, -2}, {-2, 0}};
boolean[] visited = new boolean[4];
boolean hasUnvisited = false;
for (int i = 0; i < 4; i++) {
int newX = x + directions[i][0];
int newY = y + directions[i][1];
if (newX > 0 && newX < width - 1 && newY > 0 && newY < height - 1 && maze[newY][newX] == WALL) {
hasUnvisited = true;
break;
}
}
if (hasUnvisited) {
int direction;
do {
direction = random.nextInt(4);
} while (visited[direction]);
int newX = x + directions[direction][0];
int newY = y + directions[direction][1];
if (newX > 0 && newX < width - 1 && newY > 0 && newY < height - 1 && maze[newY][newX] == WALL) {
maze[y + directions[direction][1] / 2][x + directions[direction][0] / 2] = PATH;
maze[newY][newX] = PATH;
stack.push(new int[]{newX, newY});
} else {
visited[direction] = true;
}
} else {
stack.pop();
}
}
}
public void play() {
System.out.println("Labirent Kaçışına Hoş Geldiniz!");
System.out.println("Kontroller: W (Yukarı), S (Aşağı), A (Sol), D (Sağ), Q (Çıkış)");
System.out.println("Anahtarı (K) bulun ve kapıdan (D) geçin!");
System.out.println("Tuzaklardan (X) kaçının!");
while (health > 0) {
displayMaze();
System.out.println("Adım: " + steps + " | Can: " + health + " | Anahtar: " + (hasKey ? "Var" : "Yok"));
System.out.print("Hareket (W/S/A/D/Q): ");
char move = scanner.next().toUpperCase().charAt(0);
if (move == 'Q') {
System.out.println("Oyundan çıkılıyor...");
break;
}
if (makeMove(move)) {
steps++;
checkPosition();
if (playerX == exitX && playerY == exitY && hasKey) {
System.out.println("\nTEBRİKLER! Labirentten kaçtınız!");
System.out.println("Toplam adım: " + steps);
System.out.println("Kalan can: " + health);
return;
}
}
}
System.out.println("\nOyun Bitti! Labirentten kaçamadınız.");
}
private boolean makeMove(char direction) {
int newX = playerX;
int newY = playerY;
switch (direction) {
case 'W': newY--; break;
case 'S': newY++; break;
case 'A': newX--; break;
case 'D': newX++; break;
default:
System.out.println("Geçersiz hareket!");
return false;
}
if (newX >= 0 && newX < width && newY >= 0 && newY < height) {
if (maze[newY][newX] != WALL) {
maze[playerY][playerX] = VISITED;
playerX = newX;
playerY = newY;
maze[playerY][playerX] = PLAYER;
return true;
} else {
System.out.println("Duvara çarptınız!");
}
}
return false;
}
private void checkPosition() {
char current = maze[playerY][playerX];
if (current == TRAP) {
health -= 20;
System.out.println("Tuzağa düştünüz! 20 can kaybettiniz.");
maze[playerY][playerX] = PATH;
} else if (current == KEY) {
hasKey = true;
System.out.println("Anahtarı buldunuz! Artık kapıdan geçebilirsiniz.");
maze[playerY][playerX] = PATH;
} else if (current == DOOR) {
if (hasKey) {
System.out.println("Kapıyı açtınız!");
maze[playerY][playerX] = PATH;
} else {
System.out.println("Kapıyı açmak için anahtar gerekli!");
playerX = playerX > 1 ? playerX - 1 : playerX + 1;
}
}
}
private void displayMaze() {
System.out.println();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
}
}