1 package de.jos.game.logic;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import de.jos.game.Constants;
8 import de.jos.game.objects.Bullet;
9 import de.jos.game.objects.Position;
10
11 public class NInARowValidator implements Constants {
12
13 public static final int DEFAULT_REQUIRED_ALIGNED_BULLETS = 3;
14 public static final int REQUIRED_ALIGNED_BULLETS_FOUR = 4;
15
16 private int requiredAlignedBullets = DEFAULT_REQUIRED_ALIGNED_BULLETS;
17
18 public List<Bullet> checkNInARow(Bullet[][] board) {
19 int xNInARow = -1;
20 int yNInARow = -1;
21
22 Integer lastColor = null;
23 int count = 0;
24
25
26 for (int x = LEFT_BORDER; x < BOARD_WIDTH_X - RIGHT_BORDER; x++) {
27 for (int y = TOP_BORDER; y < BOARD_WIDTH_Y - BOTTOM_BORDER; y++) {
28 if (lastColor != null) {
29 if (board[x][y] == null) {
30 lastColor = null;
31 count = 0;
32 } else {
33 if (board[x][y].getColor().equals(lastColor)) {
34 count++;
35 } else {
36 lastColor = board[x][y].getColor();
37 count = 1;
38 }
39 }
40 } else {
41 if (board[x][y] != null) {
42 lastColor = board[x][y].getColor();
43 count = 1;
44 }
45 }
46 if (count == requiredAlignedBullets) {
47 xNInARow = x;
48 yNInARow = y;
49 count = 0;
50 }
51
52 }
53
54 lastColor = null;
55 }
56
57 if (xNInARow == -1 && yNInARow == -1) {
58 lastColor = null;
59 count = 0;
60
61
62 for (int y = TOP_BORDER; y < BOARD_WIDTH_Y - BOTTOM_BORDER; y++) {
63 for (int x = LEFT_BORDER; x < BOARD_WIDTH_X - RIGHT_BORDER; x++) {
64 if (lastColor != null) {
65 if (board[x][y] == null) {
66 lastColor = null;
67 count = 0;
68 } else {
69 if (board[x][y].getColor().equals(lastColor)) {
70 count++;
71 } else {
72 lastColor = board[x][y].getColor();
73 count = 1;
74 }
75 }
76 } else {
77 if (board[x][y] != null) {
78 lastColor = board[x][y].getColor();
79 count = 1;
80 }
81 }
82
83 if (count == requiredAlignedBullets) {
84 xNInARow = x;
85 yNInARow = y;
86 count = 0;
87 }
88 }
89 }
90
91 lastColor = null;
92 }
93
94 List<Bullet> dissolveBulletList = null;
95 if (xNInARow != -1 && yNInARow != -1) {
96 List<Position> dissolveBulletPositionList = null;
97 System.out.println("======> 4 in a row + x : " + xNInARow + ", y : " + yNInARow);
98 dissolveBulletPositionList = new ArrayList<Position>();
99 findConnectedBullets(board, dissolveBulletPositionList, new Position(xNInARow, yNInARow));
100
101
102
103 dissolveBulletList = new ArrayList<Bullet>();
104 Iterator iter = dissolveBulletPositionList.iterator();
105 while (iter.hasNext()) {
106 Position pos = (Position) iter.next();
107 dissolveBulletList.add(board[pos.getX()][pos.getY()]);
108 }
109
110
111 setRequiredAlignedBullets(DEFAULT_REQUIRED_ALIGNED_BULLETS);
112 }
113
114 return dissolveBulletList;
115 }
116
117 public void findConnectedBullets(Bullet[][] board, List<Position> list, Position pos) {
118 int x = pos.getX();
119 int y = pos.getY();
120 Integer color = board[x][y].getColor();
121
122
123 if (x < LEFT_BORDER || x >= BOARD_WIDTH_X - RIGHT_BORDER || y < TOP_BORDER || y >= BOARD_WIDTH_Y - BOTTOM_BORDER) {
124
125 list.remove(pos);
126 return;
127 }
128
129
130 if (board[x + 1][y] != null && board[x + 1][y].getColor().equals(color)) {
131 Position p = new Position(x + 1, y);
132 if (!list.contains(p)) {
133 list.add(p);
134 findConnectedBullets(board, list, p);
135 }
136 }
137
138 if (board[x - 1][y] != null && board[x - 1][y].getColor().equals(color)) {
139 Position p = new Position(x - 1, y);
140 if (!list.contains(p)) {
141 list.add(p);
142 findConnectedBullets(board, list, p);
143 }
144 }
145
146 if (board[x][y + 1] != null && board[x][y + 1].getColor().equals(color)) {
147 Position p = new Position(x, y + 1);
148 if (!list.contains(p)) {
149 list.add(p);
150 findConnectedBullets(board, list, p);
151 }
152 }
153
154 if (board[x][y - 1] != null && board[x][y - 1].getColor().equals(color)) {
155 Position p = new Position(x, y - 1);
156 if (!list.contains(p)) {
157 list.add(p);
158 findConnectedBullets(board, list, p);
159 }
160 }
161 }
162
163 public void setRequiredAlignedBullets(int requiredAlignedBullets) {
164 this.requiredAlignedBullets = requiredAlignedBullets;
165 }
166
167 }