Merge branch 'master' of gitlab.isima.fr:audorival/gomoku
This commit is contained in:
@@ -1,7 +1,28 @@
|
|||||||
|
|
||||||
|
|
||||||
public enum Color {
|
public enum Color {
|
||||||
NIL,
|
NIL(-1),
|
||||||
WHITE,
|
WHITE(0),
|
||||||
BLACK;
|
BLACK(1);
|
||||||
|
|
||||||
|
private final int val;
|
||||||
|
|
||||||
|
Color(int i) {
|
||||||
|
this.val = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color fromInt(int i) {
|
||||||
|
for (Color c : Color.values()) {
|
||||||
|
if (c.val == val) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("Invalid value: " + this);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color inverse() {
|
||||||
|
if (this.val == -1)
|
||||||
|
throw new IllegalArgumentException("No inverse of NIL");
|
||||||
|
return fromInt((this.val + 1) % 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,25 @@
|
|||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class GomokuAI extends Player {
|
public class GomokuAI extends Player {
|
||||||
|
|
||||||
|
// ------------------Constructors--------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is an extends of the class Player, this allows the GomokuAI to choose his move.
|
* The constructor of the Human.
|
||||||
|
*
|
||||||
|
* @param name The name of the player.
|
||||||
|
* @param color The color of the player.
|
||||||
|
*/
|
||||||
|
public GomokuAI(String name, Color color) {
|
||||||
|
super(name, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is an extends of the class Player, this allows the GomokuAI to
|
||||||
|
* choose his move.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** The random initialization */
|
/** The random initialization */
|
||||||
@@ -17,9 +30,11 @@ public class GomokuAI extends Player{
|
|||||||
// ------------------Methods--------------------------
|
// ------------------Methods--------------------------
|
||||||
/**
|
/**
|
||||||
* Return the coordinate of the move played by the Gomoku AI.
|
* Return the coordinate of the move played by the Gomoku AI.
|
||||||
|
*
|
||||||
* @param board The actual Gomoku board.
|
* @param board The actual Gomoku board.
|
||||||
* @return The Cell of the move played.
|
* @return The Cell of the move played.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public GomokuCell chooseMove(GomokuBoard board) {
|
public GomokuCell chooseMove(GomokuBoard board) {
|
||||||
|
|
||||||
List<GomokuCell> playableCell = board.getPlayableCells();
|
List<GomokuCell> playableCell = board.getPlayableCells();
|
||||||
@@ -45,12 +60,12 @@ public class GomokuAI extends Player{
|
|||||||
|
|
||||||
System.out.println("L'IA à choisi : " + x + ", " + y);
|
System.out.println("L'IA à choisi : " + x + ", " + y);
|
||||||
|
|
||||||
|
|
||||||
return playCell;
|
return playCell;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a Map of all Cell playable, and their point.
|
* Return a Map of all Cell playable, and their point.
|
||||||
|
*
|
||||||
* @param board The actual Gomoku board.
|
* @param board The actual Gomoku board.
|
||||||
* @return the Map of all Cell playable, and their point.
|
* @return the Map of all Cell playable, and their point.
|
||||||
*/
|
*/
|
||||||
@@ -81,6 +96,4 @@ public class GomokuAI extends Player{
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import java.nio.file.Path;
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
|
||||||
public class GomokuGame {
|
public class GomokuGame {
|
||||||
|
|
||||||
@@ -32,7 +32,26 @@ public class GomokuGame {
|
|||||||
g.renderer.update();
|
g.renderer.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void newGame() {
|
public void newGame(boolean bot, String name1, String name2) {
|
||||||
|
Color[] possible = { Color.WHITE, Color.BLACK };
|
||||||
|
int rnd = new Random().nextInt(possible.length);
|
||||||
|
Color colorPlayer1 = possible[rnd];
|
||||||
|
Color colorPlayer2 = colorPlayer1.inverse();
|
||||||
|
|
||||||
|
this.player1 = new Human(name1, colorPlayer1);
|
||||||
|
|
||||||
|
if (bot) {
|
||||||
|
this.player2 = new GomokuAI(name2, colorPlayer2);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
this.player2 = new Human(name2, colorPlayer2);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.board = new GomokuBoard(15, 15);
|
||||||
|
this.colorP1 = colorPlayer1;
|
||||||
|
this.currP = this.player1.color == Color.WHITE ? 1: 2;
|
||||||
|
this.nbTokens1 = 60;
|
||||||
|
this.nbTokens2 = 60;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,12 +89,19 @@ public class GomokuGame {
|
|||||||
for (int j = 0; j < this.getBoard().getWidth(); ++j) {
|
for (int j = 0; j < this.getBoard().getWidth(); ++j) {
|
||||||
char c;
|
char c;
|
||||||
switch (board.get(i, j).getState()) {
|
switch (board.get(i, j).getState()) {
|
||||||
case BLACK: c = 'X'; break;
|
case BLACK:
|
||||||
case WHITE: c = 'O'; break;
|
c = 'X';
|
||||||
case NIL: c = '.'; break;
|
break;
|
||||||
default: throw new IllegalStateException(
|
case WHITE:
|
||||||
String.format("Unexpected value at cell (%d, %d): %s", i, j, board.get(i, j).getState())
|
|
||||||
);
|
break;
|
||||||
|
case NIL:
|
||||||
|
c = '.';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException(
|
||||||
|
String.format("Unexpected value at cell (%d, %d): %s", i, j,
|
||||||
|
board.get(i, j).getState()));
|
||||||
}
|
}
|
||||||
writer.write(c);
|
writer.write(c);
|
||||||
}
|
}
|
||||||
@@ -138,9 +164,15 @@ public class GomokuGame {
|
|||||||
|
|
||||||
for (int j = 0; j < w; ++j) {
|
for (int j = 0; j < w; ++j) {
|
||||||
switch (line.charAt(j)) {
|
switch (line.charAt(j)) {
|
||||||
case 'X': colors[i][j] = Color.BLACK; break;
|
case 'X':
|
||||||
case 'O': colors[i][j] = Color.WHITE; break;
|
colors[i][j] = Color.BLACK;
|
||||||
case '.': colors[i][j] = Color.NIL; break;
|
break;
|
||||||
|
case 'O':
|
||||||
|
colors[i][j] = Color.WHITE;
|
||||||
|
break;
|
||||||
|
case '.':
|
||||||
|
colors[i][j] = Color.NIL;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Invalid color: " + line.charAt(j));
|
throw new IllegalArgumentException("Invalid color: " + line.charAt(j));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,31 @@ import java.io.Console;
|
|||||||
|
|
||||||
public class Human extends Player {
|
public class Human extends Player {
|
||||||
|
|
||||||
|
// ------------------Constructors--------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is an extends of the class Player, this allows the player to choose his move.
|
* The constructor of the Human.
|
||||||
|
*
|
||||||
|
* @param name The name of the player.
|
||||||
|
* @param color The color of the player.
|
||||||
|
*/
|
||||||
|
public Human(String name, Color color) {
|
||||||
|
super(name, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is an extends of the class Player, this allows the player to
|
||||||
|
* choose his move.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// ------------------Methods--------------------------
|
// ------------------Methods--------------------------
|
||||||
/**
|
/**
|
||||||
* Return the coordinate of the move played by the player.
|
* Return the coordinate of the move played by the player.
|
||||||
|
*
|
||||||
* @param board The actual Gomoku board.
|
* @param board The actual Gomoku board.
|
||||||
* @return The cell of the move played.
|
* @return The cell of the move played.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public GomokuCell chooseMove(GomokuBoard board) {
|
public GomokuCell chooseMove(GomokuBoard board) {
|
||||||
|
|
||||||
Console cons = System.console();
|
Console cons = System.console();
|
||||||
@@ -32,8 +47,7 @@ public class Human extends Player{
|
|||||||
System.out.println("Ce n'est pas un nombre !");
|
System.out.println("Ce n'est pas un nombre !");
|
||||||
pass = false;
|
pass = false;
|
||||||
}
|
}
|
||||||
}
|
} while (!pass);
|
||||||
while(!pass);
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
@@ -45,21 +59,16 @@ public class Human extends Player{
|
|||||||
System.out.println("Ce n'est pas un nombre !");
|
System.out.println("Ce n'est pas un nombre !");
|
||||||
pass = false;
|
pass = false;
|
||||||
}
|
}
|
||||||
}
|
} while (!pass);
|
||||||
while(!pass);
|
|
||||||
|
|
||||||
|
|
||||||
pass = board.get(x, y).isPlayable();
|
pass = board.get(x, y).isPlayable();
|
||||||
if(!pass)
|
if (!pass) {
|
||||||
{
|
|
||||||
System.out.println("Cette case n'est pas jouable !");
|
System.out.println("Cette case n'est pas jouable !");
|
||||||
}
|
}
|
||||||
}
|
} while (!pass);
|
||||||
while(!pass);
|
|
||||||
|
|
||||||
System.out.println("Vous avez saisi : " + x + ", " + y);
|
System.out.println("Vous avez saisi : " + x + ", " + y);
|
||||||
|
|
||||||
|
|
||||||
return board.get(x, y);
|
return board.get(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ abstract class Player{
|
|||||||
* @param name The name of the player.
|
* @param name The name of the player.
|
||||||
* @param color The color of the player.
|
* @param color The color of the player.
|
||||||
*/
|
*/
|
||||||
public void Player(String name,Color color){
|
public Player(String name,Color color){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user