-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGameA.java
More file actions
174 lines (163 loc) · 7.05 KB
/
Copy pathSnakeGameA.java
File metadata and controls
174 lines (163 loc) · 7.05 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
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedList;
public class SnakeGameA {
public static void main(String[] args){
// load the area into 2D char array
char[][] area = loadAndGetArea();
//check the arg if its just 2
if(args.length !=2){
System.out.println("command used: java SnakeGameA.java <direction> <Step> ");
return;
}
//define the arg
String direction = args[0];
int step = Integer.parseInt(args[1]);
//for the movement of directions in the area
int directionRow = 0;//up/down
int directionCol = 0;//left/right
//what user enter in the arg
if( direction.equals("up") ){
directionRow = -1;//move one row up
} else if(direction.equals("down")){
System.out.println("Only open direction up,left.right");
return;
}else if(direction.equals("left")){
directionCol =-1;
}else if(direction.equals("right")){
directionCol =1;
}else {
System.out.println("Invalid direction");
return;
}
//used linkedList for snack body
LinkedList<int[]> snake = findSnake(area);
if(snake.isEmpty()){
System.out.println("No snake find!");
return;
}
//before move
System.out.println("Game before move ");
displayArea(area);
//current head(rightmost) positions
int[] CurrRightmost = snake.getLast();
//movement for steps:
for(int i = 0; i<step; i++){
//1.calculate next positions:
int nextRow = CurrRightmost[0] + directionRow;
int nextCol = CurrRightmost[1] + directionCol;
//this CurrRightmost functions return new head positions
CurrRightmost = moveSnake(area, snake, CurrRightmost, new int[]{nextRow,nextCol});
}
//after move
System.out.println("Game after move ");
displayArea(area);
//save updated area to file
saveArea(area);
//print lines for spices
printEmptyLines();
}
//move snake from CurrRightmost to nextRightmost
public static int[] moveSnake(char[][]area, LinkedList<int[]> snake, int[] CurrRightmost,int[] nextRightmost){
int row = nextRightmost[0];
int col = nextRightmost[1];
//2.wall collision
if(row<0 || row>=15 || col<0 || col>=15){
System.out.println("wall collision ");
return CurrRightmost;
}
//3.self collision
//"i" here like counter of index go through parts of the snake
for(int i = 0; i<snake.size(); i++){
int[] part = snake.get(i);
if(part[0] == row && part[1] == col ){
System.out.println("self collision ");
return CurrRightmost;
}
}
//4.remove tail(leftmost)
int[] leftmost = snake.removeFirst();
area[leftmost[0]][leftmost[1]]= '-';//update grid area, after remove tail replace with "-"
//5.add new head(rightmost)
snake.addLast(nextRightmost);
area[row][col]= 'o';
return nextRightmost;//return new head positions
}
/*
in this method will scan area(grid)+ collect
cell that contain 'o'
*/
public static LinkedList<int[]> findSnake(char[][] area){
//create list to store snake body positions each p stores in array int[]
LinkedList<int[]> snake = new LinkedList<>();
//use this loop to check every cell in the area
for (int row = 0; row < 15; row++) {
for (int col = 0; col < 15; col++) {
if (area[row][col] == 'o') {//check if cell contain part of snake
snake.add(new int[]{row, col});//store positions into list
}
}
}
return snake;//return snake body as list
}
//this print char[][]area
public static void displayArea(char[][] area) {
for (int row = 0; row < 15; row++) {//go through rows starts from 0-14 (15)
for (int col = 0; col < 15; col++) {//go through cols starts from 0-14
System.out.print(area[row][col]);//will print char in that cell
if (col < 15 - 1) System.out.print(" ");//print space between cells
//(col < 15 - 1) avoid printing space after last col
}
System.out.println();
}
}
//loadAndGetArea---> read & load file & convert content to char[15][15]
public static char[][] loadAndGetArea() {
char[][] area = new char[15][15];//2D
try {
//read the file as one string, (relativePath)--> location path
String fileContent = Files.readString(Path.of(relativePath));
//spilt it to lines
String[] lines = fileContent.split("\n");
//read each line of the file
for (int row = 0; row < 15; row++) {
//split each row into cells
String[] cells = lines[row].trim().split(" ");
for (int col = 0; col < 15; col++) {
//cells it's an array of string not char, after split each
// element is string even contain but area stores char so convert
area[row][col] = cells[col].charAt(0);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return area;
}
//FileWriter--> open the file to writing+ auto close the file
public static void saveArea(char[][] area) {
try (FileWriter fw = new FileWriter(relativePath)) {
//scan every r+c
for (int row = 0; row< 15; row++) {
for (int col = 0; col< 15; col++) {
//write char into file
fw.write(area[row][col]);
//add space between col
if (col < 15 - 1) fw.write(' ');
}
fw.write('\n');//new line for row
}
//if writes fails throw Runtime,stop program show an error message
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void printEmptyLines() {
for (int count = 0; count < 18; count++) {
System.out.println();
}
}
public static final String relativePath = "./area.txt";
}