Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lezzio committed Jun 3, 2018
1 parent cbc9fcb commit 979cc1c
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/fr/insalyon/chess/ChessGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public class ChessGame {
public static void main(String ... args) {
Application.launch(GameApplication.class);
}
}
}
26 changes: 12 additions & 14 deletions src/fr/insalyon/chess/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import fr.insalyon.chess.core.pawns.Queen;
import fr.insalyon.chess.core.pawns.Rook;

public class Game implements Cloneable {
public class Game {

private AbstractPawn[][] board;
private int currentPlayer = 0;
Expand Down Expand Up @@ -85,11 +85,16 @@ public void display() {
System.out.println(Arrays.deepToString(board));
}

//The game is over
/**
* Game ending state for a team
* @param team
* @return
*/
public End gameOver(Team team) {

Location[] escapeMoves = new Location[0];

//Possible escape moves for the king
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
AbstractPawn pawn = board[i][j];
Expand All @@ -108,8 +113,11 @@ public End gameOver(Team team) {
}
return End.NONE;
}

//The king is in threat
/**
* The king is in threat ?
* @param team
* @return
*/
public boolean check(Team team) {
King king = getKing(team);

Expand Down Expand Up @@ -137,14 +145,4 @@ public King getKing(Team team) {
}
return king;
}

public Object clone() {
Object clonedGame = null;
try {
clonedGame = super.clone();
} catch(CloneNotSupportedException exception) {
exception.printStackTrace();
}
return clonedGame;
}
}
7 changes: 7 additions & 0 deletions src/fr/insalyon/chess/ai/ChessAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
import fr.insalyon.chess.core.pawns.King;

public class ChessAI {


/*
* ATTENTION
*
* Ancienne version : regarder ChessAI3
*
*/

//Avoid suicide variables
private Location[] allTargetedLocations = new Location[0];
Expand Down
7 changes: 6 additions & 1 deletion src/fr/insalyon/chess/ai/ChessAI2.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public class ChessAI2 {
private int pieces = 0;
private int enemyPieces = 0;

/*
* ATTENTION
*
* Ancienne version : regarder ChessAI3
*
*/

private void setupVariables(Game game, Team team) {
pieces = 0;
Expand Down Expand Up @@ -54,7 +60,6 @@ public int evalState(AbstractPawn[][] board, Team team) {
//Min max tree implementation
public void play(Game game, Team team) {

game = (Game) game.clone();
AbstractPawn[][] board = game.getBoard();

int initialValue = evalState(board, Team.BLACK);
Expand Down
33 changes: 3 additions & 30 deletions src/fr/insalyon/chess/ai/ChessAI3.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@
import fr.insalyon.chess.core.Team;

public class ChessAI3 {

private Location[] allTargetedLocations = new Location[0];
private int enemyPieces = 0;


private void setupVariables(Game game, Team team) {
enemyPieces = 0;
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
AbstractPawn pawn = game.getBoard()[i][j];
if(pawn == null) continue;
if(pawn.getTeam() != team) {
Location[] targets = pawn.getMovement(game, pawn.getLocation(), true);
allTargetedLocations = Location.concat(allTargetedLocations, targets);
enemyPieces++;
}
}
}
}

/*
* Return an integer giving a perspective of the current board for a given team
Expand Down Expand Up @@ -59,22 +40,16 @@ public void play(Game game, Team team) {

long firstTime = System.currentTimeMillis();

game = (Game) game.clone();
AbstractPawn[][] board = game.getBoard();

int initialValue = evalState(game, Team.BLACK);
int bestValue = Integer.MIN_VALUE;
Location bestTo = null;
AbstractPawn bestPawn = null;

int depth = 4;

setupVariables(game, team);
if(enemyPieces == 1) {
depth = 5;
}
int depth = 3;

System.out.println("INITIAL STATE " + initialValue);
System.out.println("INITIAL SCORE " + initialValue);

int a = 0;
for(int i = 0; i < 8; i++) {
Expand All @@ -94,10 +69,8 @@ public void play(Game game, Team team) {
game.movePawn(from, targetedLoc);

game.rotatePlayer();
// System.out.println("Start min max for " + pawn.getName() + " to " + targetedLoc);
//Recursive tree
int value = miniMax(game, game.getCurrentPlayer(), depth, Integer.MIN_VALUE, Integer.MAX_VALUE, false);
// System.out.println("MIN VALUE FOR STRIKE " + (a++) + " is " + value);

//Is it the best one ?
if(value >= bestValue) {
Expand All @@ -119,7 +92,7 @@ public void play(Game game, Team team) {
game.movePawn(bestPawn.getLocation(), bestTo);
System.out.println("Alpha beta triggered " + b);
long timeDif = System.currentTimeMillis() - firstTime;
System.out.println("Time taken = " + timeDif);
System.out.println("Time taken = " + timeDif + "ms");
System.out.println("__________________________");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/fr/insalyon/chess/core/MovementBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void add(MovementType movementType, Location from, Location to) {
row += rowInc;
col += colInc;
Location newLocation = new Location(row, col);
//Check collision and if allowed or not ? (for little pawn purpose)
if(collide) {
noCollision = Game.isEmpty(board, newLocation);
}
Expand Down
5 changes: 3 additions & 2 deletions src/fr/insalyon/chess/gui/GameApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public class GameApplication extends Application {
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Jeu d'échecs");

// Chess board
// Generate the game object
this.game = new Game();
game.init();

// Draw the chess board
this.boardGrid = new GridPane();
this.boardScene = new Scene(boardGrid, WIDTH, HEIGHT);

Expand Down Expand Up @@ -91,7 +92,7 @@ public void drawPieces() {

private ImageView createPawnNode(AbstractPawn pawn) {
// Retrieve the proper pawn image
Image image = new Image(pawn.getTeam().getName() + "_" + pawn.getName() + ".png");
Image image = new Image("resources/" + pawn.getTeam().getName() + "_" + pawn.getName() + ".png");
ImageView pieceImage = new ImageView(image);

// Define drag and drop event
Expand Down
14 changes: 0 additions & 14 deletions src/fr/insalyon/chess/gui/Menu.java

This file was deleted.

1 change: 1 addition & 0 deletions src/fr/insalyon/chess/gui/TakePlaceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void handle(DragEvent event) {
gameApplication.refresh();
event.setDropCompleted(true);
if(!checkOver()) {
//Launch the AI turn in async
gameApplication.setComputerTurn(true);
AsyncComputerPlay asyncComputerPlay = new AsyncComputerPlay(gameApplication);
asyncComputerPlay.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
Expand Down

0 comments on commit 979cc1c

Please sign in to comment.