View difference between Paste ID: 4nPW6570 and u7wXeNky
SHOW: | | - or go back to the newest paste.
1
public class GameGrid extends JewelPuzzleGridCaller {
2
	private JewelCell[][] jewelGrid;
3
	private Coord         selected;
4
	private int           width;
5
	private int           height;
6
	private ArrayList<Coord> aChangedCells;
7
	private ArrayList<Coord> aToBeDestroyedCells;
8
	private int chainCount = 0;
9
	private JewelGameManager myManager;
10
	
11
	public GameGrid(int width,int height, JewelGameManager myManager) {
12
		jewelGrid = new JewelCell[width][height];
13
		this.myManager = myManager;
14
		this.width  = width;
15
		this.height  = height;
16
		aChangedCells = new ArrayList<Coord>();
17
		aToBeDestroyedCells = new ArrayList<Coord>();
18
		selected  = new Coord(-1, -1);
19
	}
20
	
21
	public void initialize() {
22
		for (int x = 0; x < this.width; x++) {
23
			for (int y = 0; y < this.height; y++) {
24
				jewelGrid[x][y] = new JewelCell(null);
25
			}			
26
		}
27
		checkCellsAndPullDown();
28
		//updateValidMoves();
29
	}
30
31
	public int getWidth() {
32
		return width;
33
	}
34
35
	public int getHeight() {
36
		return height;
37
	}
38
	
39
	public boolean hasSelected(){
40
		return (this.selected != null && this.selected.getX() > -1 && this.selected.getY() > -1);
41
	}
42
	
43
	public Coord getSelected() {
44
		return selected;
45
	}
46
	
47
	public void select(int x, int y){
48
		this.selected.setCoords(x, y);
49
		callShowSelectedMarker(x, y, true);
50
	}
51
52
	public void deSelect(){
53
		this.selected.setCoords(-1, -1);
54
		callShowSelectedMarker(0, 0, false);
55
	}
56
	
57
	public JewelCell getCell(int x, int y){
58
		return jewelGrid[x][y];
59
	}
60
	
61
	public JewelCell getCell(Coord coord){
62
		return jewelGrid[coord.getX()][coord.getY()];
63
	}
64
65
	public int getChainCount() {
66
		return chainCount;
67
	}
68
69
	public void resetChainCount() {
70
		this.chainCount = 0;
71
	}	
72
	
73
	private void addCellToBeChecked(int x, int y){
74
		addCellToBeChecked(new Coord(x, y));
75
	}
76
	
77
	private void addCellToBeChecked(Coord coord){
78
		aChangedCells.add(coord);
79
	}
80
	
81
	private boolean checkChangedCellsForMatches(){
82
		boolean result = false;
83
		if (aChangedCells.size() > 0){
84
			for (Coord coord : aChangedCells) {
85
				if (checkCellForMatches(coord)){
86
					result = true;
87
				}
88
			}
89
		}
90
		return result;
91
	}
92
93
	/**
94
     * Check all cells from the grid for matches.
95
	 * @return <b>true</b> if there is at least one match, <b>false</b> otherwise.
96
	 */
97
	public boolean checkGridForMatches(){
98
		for (int x = 0; x < width; x++) {
99
			for (int y = 2; y < height; y++) {
100
				checkCellForMatches(new Coord(x, y));
101
			}
102
		}
103
		return clearMarkedCells();
104
	}
105
	
106
	private boolean checkCellForMatches(Coord pCoord){
107
		ArrayList<Coord> VLine = new ArrayList<Coord>();
108
		ArrayList<Coord> HLine = new ArrayList<Coord>();
109
		JewelType type;
110
		for (Coord coord : aToBeDestroyedCells) {
111
			// if the cell is already marked for destruction skip the check. It is doomed.
112
			if (coord.match(pCoord)) return false;
113
		}
114
		
115
		
116
		boolean result = false;
117
		final int x = pCoord.getX();
118
		final int y = pCoord.getY();
119
		type = getCell(x, y).getType();
120
		
121
		// @Anila
122
		// this 'for' is because my gems can have more than one type of color
123
		// so I check every type
124
		// regular 1-color gems don't need the 'for'
125
		for (JewelColor color : type.getColors()) {
126
			HLine.clear(); 
127
			VLine.clear(); 
128
			
129
			for (int xt = x; xt >= 0 && JewelType.isMatch(getCell(xt, y).getType(), color); xt--) {
130
				HLine.add(new Coord(xt, y));				
131
			}
132
			for (int xt = x +1; xt < width  && JewelType.isMatch(getCell(xt, y).getType(), color); xt++) {
133
				HLine.add(new Coord(xt, y));				
134
			}
135
			//yt = 2 to ignore the first 2 lines, they will be hidden unless the player has the proper ability
136
			for (int yt = y; yt >= 2 && JewelType.isMatch(getCell(x, yt).getType(), color); yt--) {
137
				VLine.add(new Coord(x, yt));				
138
			}
139
			for (int yt = y +1; yt < height  && JewelType.isMatch(getCell(x, yt).getType(), color); yt++) {
140
				VLine.add(new Coord(x, yt));				
141
			}
142
			
143
			if (HLine.size() > 2){
144
				aToBeDestroyedCells.addAll(HLine);
145
				result = true;
146
			}
147
			
148
			if (VLine.size() > 2){
149
				aToBeDestroyedCells.addAll(VLine);
150
				result = true;
151
			}
152
		}
153
		return result;
154
	}
155
	/*
156
	private void updateValidMoves(){
157
		
158
	}*/
159
	
160
	public boolean clearMarkedCells(){
161
		ArrayList<Coord> list = new ArrayList<Coord>();
162
		if (aToBeDestroyedCells.size() > 0){
163
			for (Coord coord : aToBeDestroyedCells) {
164
				if (!getCell(coord).isEmpty()){
165
					for (JewelColor color : getCell(coord).getType().getColors()) {
166
						myManager.matchedPoints(color, 1, coord);						
167
					}
168
					getCell(coord).setEmpty();
169
					list.add(coord);
170
				}
171
			}
172
			
173
			callClearMatchedCells(list);	
174
			aToBeDestroyedCells.clear();
175
			chainCount++;
176
			return true;
177
		} else {
178
			return false;
179
		}
180
	}
181
	
182
	public void swapCell(int x1, int y1, int x2, int y2){
183
		deSelect();
184
		JewelCell cell1 = jewelGrid[x1][y1];
185
		JewelCell cell2 = jewelGrid[x2][y2];
186
		
187
		jewelGrid[x1][y1] = cell2;
188
		jewelGrid[x2][y2] = cell1;
189
		
190
		aChangedCells.clear();
191
		addCellToBeChecked(x1, y1);
192
		addCellToBeChecked(x2, y2);
193
		
194
		if (checkChangedCellsForMatches()){
195
			callSwapJewels(x1, y1, x2, y2);
196
		} else {
197
			jewelGrid[x1][y1] = cell1;
198
			jewelGrid[x2][y2] = cell2;
199
			callSwapJewelsFailed(x1, y1, x2, y2);
200
		}
201
	}
202
	
203
	/**
204
	 * Pull all gems down from bottom to top creating new gems as needed
205
	 * @return <b>true</b> if pulled at least one gem, <b>false</b> if all lines were filled and no one was pulled.
206
	 */
207
	public boolean checkCellsAndPullDown(){
208
		ArrayList<MoveCoord> list = new ArrayList<MoveCoord>();
209
		for (int x = 0; x < width; x++) {
210
			list.addAll(cellPullDown(x));
211
		}
212
		if (list.size() > 0) callExecutePullDown(list);
213
		return list.size() > 0;
214
	}
215
216
	private ArrayList<MoveCoord> cellPullDown(int x){
217
		ArrayList<MoveCoord> list = new ArrayList<MoveCoord>();
218
		final int BOTTOM = height -1;
219
		JewelCell upcell;
220
		
221
		for (int y = BOTTOM; y >= 0; y--) {
222
			JewelCell cell = getCell(x, y);
223
			if (cell.isEmpty()){
224
				Coord toCoord = new Coord(x, y); 
225
				Coord fromCoord = new Coord(x, -1);
226
				boolean foundCell = false;
227
				for (int curY = y -1; curY >= 0 && !foundCell; curY--){
228
					if (curY >= 0 && !(upcell = getCell(x, curY)).isEmpty()){
229
						cell.setType(upcell.getType());
230
						upcell.setEmpty();
231
						fromCoord.setCoords(x, curY);
232
						foundCell = true;
233
					} 
234
				}
235
				if (foundCell){
236
					list.add(new MoveCoord(fromCoord, toCoord));					
237
				} else {
238
					list.add(new MoveCoord(fromCoord, toCoord, cell.newType()));			
239
				}
240
			}
241
		}
242
		return list;
243
	}
244
}