-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.pde
120 lines (98 loc) · 3.1 KB
/
Cell.pde
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
public int cellSize = 30;
public abstract class Cell {
public int col, row;
public int size;
public boolean flagged;
color fillColor;
public Cell(int _col, int _row) {
size=cellSize;
flagged = false;
col=_col;
row=_row;
}
public Cell() {
}
public float posX() {
return col*size;
}
public float posY() {
return row*size;
}
public void show() {
fill(fillColor);
rect(posX(), posY(), size, size);
showFlag();
// print(this instanceof Bomb);
}
public abstract boolean isUncovered();
public void showFlag() {
if (flagged) {
ellipseMode(CORNER);
fill(#FF0000);
ellipse(posX(), posY(), size, size);
}
}
float probabilityToBeBomb(Cell[][] cells) {
float maxProb = 0;
//for (Cell myCell : this.surroundingUncoveredCells(cells)) {
// float newProb =myCell.cantTouchingBombs() / myCell.surroundingCoveredCells(cells).size();
// if (maxProb < newProb) maxProb= newProb;
//}
return maxProb;
}
boolean isFull(Cell[][] cells) {
return this.cantTouchingBombs() == this.surroundingFlaggedCells(cells).size();
}
boolean isFullOfMines(Cell[][] cells) {
return this.cantTouchingBombs() == this.surroundingCoveredCells(cells).size();
}
int cantTouchingBombs() {
int countBombs = 0;
for (Cell myCell : surroundingCells(cells)) {
if (myCell instanceof Bomb) countBombs++;
}
return countBombs;
}
public void flag() {
flagged=true;
}
ArrayList<Cell> surroundingCells(Cell[][] cells) {
ArrayList<Cell> surrCells = new ArrayList();
for (int i = max(0, col-1); i<=min(xCells-1, col+1); i++) {
for (int j = max(0, row -1); j<=min(yCells-1, row+1); j++) {
if (i != col || j != row) surrCells.add(cells[i][j]);
}
}
return surrCells;
}
ArrayList<Cell> surroundingFlaggedCells(Cell[][]cells) {
ArrayList<Cell> surrFlagCells = new ArrayList();
for (Cell mySurrCell : this.surroundingCells(cells)) {
if (mySurrCell.flagged)surrFlagCells.add(mySurrCell);
}
return surrFlagCells;
}
ArrayList<Cell> surroundingUncoveredCells(Cell[][] cells) {
ArrayList<Cell> surrCovCells = new ArrayList();
for (Cell mySurrCell : this.surroundingCells(cells)) {
if (mySurrCell.isUncovered())surrCovCells.add(mySurrCell);
}
return surrCovCells;
}
ArrayList<Cell> surroundingCoveredCells(Cell[][] cells) {
ArrayList<Cell> surrCovCells = new ArrayList();
for (Cell mySurrCell : this.surroundingCells(cells)) {
if (!mySurrCell.isUncovered())surrCovCells.add(mySurrCell);
}
return surrCovCells;
}
ArrayList<Cell> surroundingCoveredAndNotFlaggedCells(Cell[][] cells) {
ArrayList<Cell> surrCovCells = new ArrayList();
for (Cell mySurrCell : this.surroundingCells(cells)) {
if (!mySurrCell.isUncovered() && !mySurrCell.flagged)surrCovCells.add(mySurrCell);
}
return surrCovCells;
}
public abstract void uncover();
public abstract boolean isBomb();
}