Merge branch 'master' of https://gitlab.isima.fr/audorival/gomoku
This commit is contained in:
@@ -5,51 +5,61 @@ import java.util.Map;
|
||||
/**
|
||||
* This class is cell of the board of the Gomoku game.
|
||||
*/
|
||||
public class GomokuCell{
|
||||
/** Enumerate neighbor with key in Cardinal direction and key representing the cell in that direction. */
|
||||
private EnumMap<Cardinal,GomokuCell> neighbour;
|
||||
/** The state of the cell represented by the enumerate Color, Nil is empty, white and black for the color of a token */
|
||||
private Color state;
|
||||
public class GomokuCell {
|
||||
/**
|
||||
* Enumerate neighbor with key in Cardinal direction and key representing the
|
||||
* cell in that direction.
|
||||
*/
|
||||
private EnumMap<Cardinal, GomokuCell> neighbour;
|
||||
/**
|
||||
* The state of the cell represented by the enumerate Color, Nil is empty, white
|
||||
* and black for the color of a token
|
||||
*/
|
||||
private Color state;
|
||||
|
||||
//------------------Constructors--------------------------
|
||||
// ------------------Constructors--------------------------
|
||||
|
||||
/**
|
||||
* The constructor of the cell.
|
||||
*
|
||||
* @param state The state by default of the cell.
|
||||
*/
|
||||
public GomokuCell(Color state){
|
||||
public GomokuCell(Color state) {
|
||||
this.neighbour = new EnumMap<>(Cardinal.class);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
//------------------Gets--------------------------
|
||||
// ------------------Gets--------------------------
|
||||
|
||||
/**
|
||||
* Return one neighbour.
|
||||
*
|
||||
* @param car The Cardinal direction.
|
||||
* @return The GomokuCell at the direction.
|
||||
*/
|
||||
public GomokuCell getNeighbour(Cardinal car){
|
||||
public GomokuCell getNeighbour(Cardinal car) {
|
||||
return this.neighbour.get(car);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return neighbours.
|
||||
*
|
||||
* @return The EnumMap of neighbours.
|
||||
*/
|
||||
public EnumMap<Cardinal, GomokuCell> getAllNeighbour(){
|
||||
public EnumMap<Cardinal, GomokuCell> getAllNeighbour() {
|
||||
return neighbour;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of same colored neighbours in all direction.
|
||||
* @return The Map of neighbours.
|
||||
*/
|
||||
public Map<Cardinal, Integer> getSameColorNeighbour(){
|
||||
* Return the number of same colored neighbours in all direction.
|
||||
*
|
||||
* @return The Map of neighbours.
|
||||
*/
|
||||
public Map<Cardinal, Integer> getSameColorNeighbour() {
|
||||
|
||||
Map<Cardinal, Integer> map = new HashMap<>();
|
||||
map.put(Cardinal.N, 0);
|
||||
map.put(Cardinal.W,0);
|
||||
map.put(Cardinal.W, 0);
|
||||
map.put(Cardinal.NW, 0);
|
||||
map.put(Cardinal.SW, 0);
|
||||
map.put(Cardinal.S, 0);
|
||||
@@ -60,87 +70,94 @@ public class GomokuCell{
|
||||
for (Map.Entry<Cardinal, Integer> entry : map.entrySet()) {
|
||||
|
||||
GomokuCell actualcell = this;
|
||||
while(this.getState() == actualcell.getNeighbour(entry.getKey()).getState())
|
||||
{
|
||||
entry.setValue(entry.getValue()+1);
|
||||
actualcell=actualcell.getNeighbour(entry.getKey());
|
||||
while (this.getState() == actualcell.getNeighbour(entry.getKey()).getState()) {
|
||||
entry.setValue(entry.getValue() + 1);
|
||||
actualcell = actualcell.getNeighbour(entry.getKey());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the state.
|
||||
*
|
||||
* @return The state of the current cell with one Color.
|
||||
*/
|
||||
public Color getState(){
|
||||
public Color getState() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
//------------------Sets--------------------------
|
||||
|
||||
// ------------------Sets--------------------------
|
||||
|
||||
/**
|
||||
* Change state of the current cell.
|
||||
*
|
||||
* @param c The color of the token played.
|
||||
* @throws IllegalStateException If the cell is not playable.
|
||||
*/
|
||||
public void setState(Color c){
|
||||
public void setState(Color c) {
|
||||
this.state = c;
|
||||
}
|
||||
|
||||
|
||||
//------------------Booleans--------------------------
|
||||
// ------------------Booleans--------------------------
|
||||
|
||||
/**
|
||||
* This method returns if the current cell is empty
|
||||
*
|
||||
* @return True if is empty, False if is not.
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
public boolean isEmpty() {
|
||||
return this.state == Color.NIL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method returns if the cell has already played.
|
||||
*
|
||||
* @return True if the cell is already played, False if is not.
|
||||
*/
|
||||
public boolean isPlayed(){
|
||||
public boolean isPlayed() {
|
||||
return !this.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return if the cell is playable.
|
||||
* @return True if the current cell can be played with the condition of the gomoku.
|
||||
*
|
||||
* @return True if the current cell can be played with the condition of the
|
||||
* gomoku.
|
||||
*/
|
||||
public boolean isPlayable(){
|
||||
public boolean isPlayable() {
|
||||
if (this.state != Color.NIL)
|
||||
return false;
|
||||
for (Cardinal c : Cardinal.values()) {
|
||||
GomokuCell current = this.getNeighbour(c);
|
||||
if (current != null && current.isPlayed()) return true;
|
||||
if (current != null && current.isPlayed())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//------------------Methods--------------------------
|
||||
// ------------------Methods--------------------------
|
||||
|
||||
/**
|
||||
* This method link the cell at the Cardinal position on the current cell.
|
||||
* @param car The Cardinal direction of the cell to link.
|
||||
*
|
||||
* @param car The Cardinal direction of the cell to link.
|
||||
* @param cell The GomokuCell to link.
|
||||
*/
|
||||
public void linkCell(Cardinal car, GomokuCell cell){
|
||||
public void linkCell(Cardinal car, GomokuCell cell) {
|
||||
this.neighbour.put(car, cell);
|
||||
}
|
||||
|
||||
|
||||
//------------------Statics--------------------------
|
||||
// ------------------Statics--------------------------
|
||||
|
||||
public static void link(GomokuCell c1, GomokuCell c2, Cardinal c1Toc2){
|
||||
if (c1 == null || c2 == null) return ;
|
||||
public static void link(GomokuCell c1, GomokuCell c2, Cardinal c1Toc2) {
|
||||
if (c1 == null || c2 == null)
|
||||
return;
|
||||
c1.linkCell(c1Toc2, c2);
|
||||
c2.linkCell(c1Toc2.inverse(), c1);
|
||||
}
|
||||
|
||||
//------------------Overides--------------------------
|
||||
// ------------------Overides--------------------------
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (this.getState()) {
|
||||
|
||||
Reference in New Issue
Block a user