-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.java
More file actions
171 lines (146 loc) · 5.07 KB
/
Copy pathTile.java
File metadata and controls
171 lines (146 loc) · 5.07 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
package edu.neu.madcourse.michellelee.numad18s_michellelee;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageButton;
public class Tile {
public enum Owner {
X, O /* letter O */, NEITHER, BOTH
}
// These levels are defined in the drawable definitions
private static final int LEVEL_X = 0;
private static final int LEVEL_O = 1; // letter O
private static final int LEVEL_BLANK = 2;
private static final int LEVEL_AVAILABLE = 3;
private static final int LEVEL_TIE = 3;
private final GameFragment mGame;
private int mLargeTileNo;
private int mSmallTileNo;
private Owner mOwner = Owner.NEITHER;
private View mView;
private Tile mSubTiles[];
public Tile(GameFragment game) {
this.mGame = game;
}
public int getLargeTileNo() {
return this.mLargeTileNo;
}
public void setLargeTileNo(int largeIndex) {
this.mLargeTileNo = largeIndex;
}
public int getSmallTileNo() {
return this.mSmallTileNo;
}
public void setSmallTileNo(int smallIndex) {
this.mSmallTileNo = smallIndex;
}
public View getView() {
return mView;
}
public void setView(View view) {
this.mView = view;
}
public Owner getOwner() {
return mOwner;
}
public void setOwner(Owner owner) {
this.mOwner = owner;
}
public Tile[] getSubTiles() {
return mSubTiles;
}
public void setSubTiles(Tile[] subTiles) {
this.mSubTiles = subTiles;
}
public void updateDrawableState() {
if (mView == null) return;
int level = getLevel();
if (mView.getBackground() != null) {
mView.getBackground().setLevel(level);
}
if (mView instanceof ImageButton) {
Drawable drawable = ((ImageButton) mView).getDrawable();
drawable.setLevel(level);
}
}
private int getLevel() {
int level = LEVEL_BLANK;
switch (mOwner) {
case X:
level = LEVEL_X;
break;
case O: // letter O
level = LEVEL_O;
break;
case BOTH:
level = LEVEL_TIE;
break;
case NEITHER:
level = mGame.isAvailable(this) ? LEVEL_AVAILABLE : LEVEL_BLANK;
break;
}
return level;
}
private void countCaptures(int totalX[], int totalO[]) {
int capturedX, capturedO;
// Check the horizontal
for (int row = 0; row < 3; row++) {
capturedX = capturedO = 0;
for (int col = 0; col < 3; col++) {
Owner owner = mSubTiles[3 * row + col].getOwner();
if (owner == Owner.X || owner == Owner.BOTH) capturedX++;
if (owner == Owner.O || owner == Owner.BOTH) capturedO++;
}
totalX[capturedX]++;
totalO[capturedO]++;
}
// Check the vertical
for (int col = 0; col < 3; col++) {
capturedX = capturedO = 0;
for (int row = 0; row < 3; row++) {
Owner owner = mSubTiles[3 * row + col].getOwner();
if (owner == Owner.X || owner == Owner.BOTH) capturedX++;
if (owner == Owner.O || owner == Owner.BOTH) capturedO++;
}
totalX[capturedX]++;
totalO[capturedO]++;
}
// Check the diagonals
capturedX = capturedO = 0;
for (int diag = 0; diag < 3; diag++) {
Owner owner = mSubTiles[3 * diag + diag].getOwner();
if (owner == Owner.X || owner == Owner.BOTH) capturedX++;
if (owner == Owner.O || owner == Owner.BOTH) capturedO++;
}
totalX[capturedX]++;
totalO[capturedO]++;
capturedX = capturedO = 0;
for (int diag = 0; diag < 3; diag++) {
Owner owner = mSubTiles[3 * diag + (2 - diag)].getOwner();
if (owner == Owner.X || owner == Owner.BOTH) capturedX++;
if (owner == Owner.O || owner == Owner.BOTH) capturedO++;
}
totalX[capturedX]++;
totalO[capturedO]++;
}
public Owner findWinner() {
// If owner already calculated, return it
if (getOwner() != Owner.NEITHER)
return getOwner();
int totalX[] = new int[4];
int totalO[] = new int[4];
countCaptures(totalX, totalO);
if (totalX[3] > 0) return Owner.X;
if (totalO[3] > 0) return Owner.O;
// Check for a draw
int total = 0;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
Owner owner = mSubTiles[3 * row + col].getOwner();
if (owner != Owner.NEITHER) total++;
}
if (total == 9) return Owner.BOTH;
}
// Neither player has won this tile
return Owner.NEITHER;
}
}