-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.java
208 lines (161 loc) · 9.46 KB
/
Action.java
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
import java.util.ArrayList;
public class Action { // the most important class in this project whole game actions are here
public ArrayList<Integer> points = new ArrayList<>(); // to calculate total score
public Integer[] beginPoint(String[][] board){ // beginning coordinates of white ball
Integer coordinates[] = new Integer[2]; // coordinates = (row,column)
for(int i =0;i<board.length;i++){
for(int j=0;j<board[i].length;j++) {
if(board[i][j].equals("*")){ // finding white ball
coordinates[0]=i; // row
coordinates[1]=j; // column
}
}
}
return coordinates;
}
public void switches(String[][] board , int firstRow, int secondRow, int firstColumn, int secondColumn ){ // to switch elements , a basic code
String tmp="";
tmp = board[firstRow][firstColumn];
board[firstRow][firstColumn] = board[secondRow][secondColumn];
board[secondRow][secondColumn] = tmp;
}
// most important method in this project
// there is some printing outputs instead of a lots of return method
public void remove(String[] comment,String[][] board,String fileName){
Action r = new Action();
int totalPoint = 0; // total point
Integer coordinates[] = r.beginPoint(board); // beginner coordinate points (i,j)
int row=coordinates[0]; // beginner row
int column=coordinates[1]; // beginner column
int longEdge = board.length; //satır sayısı
int shortEdge =board[0].length; // sütun sayısı
WriteOutput.write("Game board:\n"); // before everything print the original board
Game.board(fileName);
WriteOutput.write("\n");
//Main part of the project
WriteOutput.write("Your movement is:\n");
for (int k = 0; k < comment.length; k++) { // now, quick trip on the movement list
String element = comment[k];
WriteOutput.write(comment[k] +" "); // to print movements
if (element.equals("L")) { //left
if (r.nextElement(board, row, (column + shortEdge - 1) % shortEdge)) { // important to is next element HOLE
return; // and because of game over, loop should be end.
}
if (board[row][(column + shortEdge - 1) % shortEdge].equals("W")) { // important to next element is WALL
if (r.nextElement(board, row, (column + 1) % shortEdge)) //after wall move we should check again is there any hole
{return; // and because of game over, loop should be end.
}
else{ // there is no hole but there is wall
r.switches(board, row, row, column, (column + 1) % shortEdge); // so switch upto "Right"
column = (column + 1) % shortEdge;
}
} else { // there is no wall
r.switches(board, row, row, column, (column + shortEdge - 1) % shortEdge); // so just switch
column = (column + shortEdge - 1) % shortEdge;
}
} else if (element.equals("R")) { //right
if (r.nextElement(board, row, (column + 1) % shortEdge)) { // important to is next element HOLE
return; // and because of game over, loop should be end.
}
if (board[row][(column + 1) % shortEdge].equals("W")) { // is next element WALL
if (r.nextElement(board, row, (column + 1) % shortEdge)) // important to is next element HOLE after wall movement
{ return; // and because of game over, loop should be end.
}
r.switches(board, row, row, column, (column + shortEdge - 1) % shortEdge); //there is no hole but there is wall so switch up to "LEFT"
column = (column + shortEdge - 1) % shortEdge;
} else { // there is no wall
r.switches(board, row, row, column, (column + 1) % shortEdge); // so just switch
column = (column + 1) % shortEdge;
}
} else if (element.equals("U")) { //up
if (r.nextElement(board, (row + longEdge - 1) % longEdge, column)) { // important to is next element HOLE
return; // and because of game over, loop should be end.
}
if (board[(row + longEdge - 1) % longEdge][column].equals("W")) { // is next element WALL
if (r.nextElement(board, row, (column + 1) % shortEdge)) { // important to is next element HOLE after WALL movement
return; // and because of game over, loop should be end.
}
r.switches(board, row, (row + 1) % longEdge, column, column); // there is no Hole but there is a wall so switch up to "Down"
row = (row + 1) % longEdge;
}
else { // there is no WALL
r.switches(board, row, (row + longEdge - 1) % longEdge, column, column); // so just switch
row = (row + longEdge - 1) % longEdge;
}
} else if (element.equals("D")) { //down
if (r.nextElement(board, (row + 1) % longEdge, column)) { // // important to is next element HOLE
return; // and because of game over, loop should be end.
}
if (board[(row + 1) % longEdge][column].equals("W")) { // is next element WALL
if (r.nextElement(board, row, (column + 1) % shortEdge)) { // important to is next element HOLE after wall movement
return; // and because of game over, loop should be end.
}
r.switches(board, row, (row + longEdge - 1) % longEdge, column, column); // there is wall so switch up to "UP"
row = (row + longEdge - 1) % longEdge;
} else { // there is no wall
r.switches(board, row, (row + 1) % longEdge, column, column); // so just switch elements
row = (row + 1) % longEdge;
}
}
}
//The below part is for condition which ball doesnot drop the hole
WriteOutput.write("\n"); // to be better output.txt
WriteOutput.write("\n");
WriteOutput.write("Your output is:\n");// to print last shape of the board
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
WriteOutput.write(board[i][j]+" ");
}
WriteOutput.write("\n");
}
WriteOutput.write("\n");
for(int i =0;i<r.points.size();i++){ // calculate total score
totalPoint+=r.points.get(i);
}
WriteOutput.write("Score: "+ totalPoint); // print total score
}
// in this game the movements of the ball change up to next element, so we need to check next element;
private boolean nextElement(String[][] board, int row,int column){ // this method also checks "HOLE", if there is a hole, it returns true
String element= board[row][column];
Action r = new Action();
// we have special characters "R", "Y" "B" and "H" we especially have to check them, and also calculate their points
if( element.equals("R")){
board[row][column]="X"; // change "R" to X
points.add(10); // calculate its score
} else if (element.equals("Y")) {
board[row][column]="X"; //change "Y" to X
points.add(5); // calculate its score
} else if (element.equals("B")) {
board[row][column]="X"; //change "B" to X
points.add(-5); // calculate its score
}
else if(element.equals("H")){ // if there is a hole the game must be stopped
r.hole(board); // to show the ball dropped the hole
int totalPoint=0; // initalizing the total point
for(int i =0;i<points.size();i++){ // to calculate the total point
totalPoint+=points.get(i);
}
WriteOutput.write("\n"); // to be better output.txt
WriteOutput.write("\n");
WriteOutput.write("Your output is:\n");// to print last shape of the board
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
WriteOutput.write(board[i][j]+" "); //to print last shape of the board after hole
}
WriteOutput.write("\n");}
WriteOutput.write("\n");
WriteOutput.write("Game Over!\n"); // because of Hole now game is over so just print it
WriteOutput.write("Score: "+ totalPoint); // after game over, print total point
return true; // because ball dropped the hole, this method returns true, and the loop must stop
}
return false;}
public void hole(String[][] board) { // a small method for replacing the ball character "*" to space character " " showing ball dropped the hole.
for(int i = 0;i< board.length;i++){
for(int j=0;j<board[i].length;j++){
if(board[i][j].equals("*")){
board[i][j]=" ";
}
}
}
}
}