-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask 3
More file actions
123 lines (104 loc) · 3.46 KB
/
Task 3
File metadata and controls
123 lines (104 loc) · 3.46 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
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class SnakeGame {
private static final int BOARD_WIDTH = 5;
private static final int BOARD_HEIGHT = 5;
private ArrayList<int[]> snake;
private int[] food;
private int direction; // 0: up, 1: right, 2: down, 3: left
private boolean isGameOver;
private int score;
public SnakeGame() {
snake = new ArrayList<>();
snake.add(new int[]{BOARD_HEIGHT / 2, BOARD_WIDTH / 2});
food = new int[]{0, 0};
direction = 1;
isGameOver = false;
score = 0;
generateFood();
}
private void generateFood() {
Random random = new Random();
int x, y;
do {
x = random.nextInt(BOARD_WIDTH);
y = random.nextInt(BOARD_HEIGHT);
} while (isCellOccupied(x, y));
food[0] = x;
food[1] = y;
}
private boolean isCellOccupied(int x, int y) {
for (int[] segment : snake) {
if (segment[0] == x && segment[1] == y) {
return true;
}
}
return false;
}
private void moveSnake() {
int[] head = snake.get(0).clone();
if (direction == 0) head[0]--;
else if (direction == 1) head[1]++;
else if (direction == 2) head[0]++;
else if (direction == 3) head[1]--;
if (head[0] < 0 || head[0] >= BOARD_HEIGHT || head[1] < 0 || head[1] >= BOARD_WIDTH) {
isGameOver = true;
return;
}
if (isCellOccupied(head[0], head[1])) {
isGameOver = true;
return;
}
snake.add(0, head);
if (head[0] == food[0] && head[1] == food[1]) {
score++;
generateFood();
} else {
snake.remove(snake.size() - 1);
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (!isGameOver) {
// Draw the board
for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) {
boolean isSnakeSegment = false;
for (int[] segment : snake) {
if (segment[0] == y && segment[1] == x) {
isSnakeSegment = true;
break;
}
}
if (isSnakeSegment) {
System.out.print("* ");
} else if (food[0] == y && food[1] == x) {
System.out.print("F ");
} else {
System.out.print(". ");
}
}
System.out.println();
}
System.out.println("Score: " + score);
// Read user input
System.out.print("Enter direction (W/A/S/D): ");
String input = scanner.nextLine().toUpperCase();
// Update direction based on user input
if (input.equals("W")) direction = 0;
else if (input.equals("D")) direction = 1;
else if (input.equals("S")) direction = 2;
else if (input.equals("A")) direction = 3;
// Move the snake
moveSnake();
}
System.out.println("Game Over! Final Score: " + score);
scanner.close();
}
public static void main(String[] args) {
SnakeGame game = new SnakeGame();
game.play();
#
}
}