-
Notifications
You must be signed in to change notification settings - Fork 4
/
Cell.java
104 lines (90 loc) · 2.15 KB
/
Cell.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
package othello.Othello;
/**
* Created by pmunoz on 01/04/14. Updated by aeap and jgeorge on 14/05/14.
* Updated by zwodnik on 09/05/14.
*/
public class Cell {
public static final char BLACK = 'X'; // displays (X) for the black player.
public static final char WHITE = 'O'; // displays (O) for the white player.
public static final char CANSELECT = '?'; // displays (?) for the possible
// moves available.
public boolean empty; // boolean if cell empty or not
public boolean canselect; // boolean if cell can be selected or not
public int value; // 0 = white, 1 = black, -1 = empty
/**
* Class constructor. Cell empty by default.
*/
public Cell() {
this.empty = true;
this.value = -1;
}
/**
* Checks if cell is empty.
*
* @return true when empty, false when there is already a chip in it
*/
public boolean isEmpty() {
return this.empty;
}
/**
* Returns the player's color.
*
* @return the player's color
*/
public int getPlayer() {
return this.value;
}
/**
* Places a chip in the cell.
*
* @param int player - the player
*/
public void placeChip(int player) {
this.empty = false;
this.value = player;
}
/**
* Changes the color of the chip.
*/
public void changeChip() {
placeChip((value + 1) % 2);
}
/**
* Sets the cell so that it's possible to select it.
*/
public void setSelect() {
this.canselect = true;
}
/**
* @return the cells that can be selected.
*/
public boolean canSelect() {
return this.canselect;
}
/**
* Cancels the selection of the cell.
*/
public void unselect() {
this.canselect = false;
}
/**
* Displays the cells that: can be selected, are white, are black.
*/
public void display() {
if (this.isEmpty()) // if cell empty
{
if (this.canselect) // if can be selected
System.out.print("[ " + CANSELECT + " ]"); // print (?)
else
System.out.print("[ " + " " + " ]"); // otherwise print empty
// space
} else {
char content = BLACK;
if (this.value == 0)
content = WHITE;
System.out.print("[ " + content + " ]"); // or print content. for
// black (X) & for white
// (O)
}
}
}