promote gui
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package chess.io;
|
package chess.io;
|
||||||
|
|
||||||
import chess.io.commands.MoveCommand;
|
import chess.io.commands.MoveCommand;
|
||||||
|
import chess.io.commands.PromoteCommand;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public class CommandExecutor {
|
public class CommandExecutor {
|
||||||
@@ -18,12 +19,24 @@ public class CommandExecutor {
|
|||||||
assert this.outputSystem != null : "No output system specified !";
|
assert this.outputSystem != null : "No output system specified !";
|
||||||
|
|
||||||
CommandResult result = command.execute(this.game, this.outputSystem);
|
CommandResult result = command.execute(this.game, this.outputSystem);
|
||||||
if (result == CommandResult.Moved) {
|
processResult(command, result);
|
||||||
this.game.checkPromotion(((MoveCommand) command).getMove().getFinish());
|
return result;
|
||||||
this.game.checkGameStatus();
|
}
|
||||||
|
|
||||||
|
private void processResult(Command command, CommandResult result) {
|
||||||
|
if (result != CommandResult.Moved)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (command instanceof MoveCommand move) {
|
||||||
|
boolean needsPromote = this.game.checkPromotion(move.getMove().getFinish());
|
||||||
|
if (this.game.checkGameStatus())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!needsPromote)
|
||||||
|
this.game.switchPlayerTurn();
|
||||||
|
} else if (command instanceof PromoteCommand) {
|
||||||
this.game.switchPlayerTurn();
|
this.game.switchPlayerTurn();
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOutputSystem(OutputSystem outputSystem) {
|
public void setOutputSystem(OutputSystem outputSystem) {
|
||||||
@@ -60,6 +73,4 @@ public class CommandExecutor {
|
|||||||
this.game.OnPlayerTurn.connect(outputSystem::playerTurn);
|
this.game.OnPlayerTurn.connect(outputSystem::playerTurn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ public class MoveCommand extends Command {
|
|||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
// we must promote the pending pawn before
|
||||||
|
if (game.pawnShouldBePromoted())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
Piece piece = board.pieceAt(move.getStart());
|
Piece piece = board.pieceAt(move.getStart());
|
||||||
if (piece == null)
|
if (piece == null)
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public class PromoteCommand extends Command {
|
|||||||
board.pieceLeaves(this.pieceCoords);
|
board.pieceLeaves(this.pieceCoords);
|
||||||
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||||
|
|
||||||
return CommandResult.NotMoved;
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Piece createPiece(PromoteType promoteType, Color color) {
|
private Piece createPiece(PromoteType promoteType, Color color) {
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ public class Game {
|
|||||||
public final Signal0 OnMat = new Signal0();
|
public final Signal0 OnMat = new Signal0();
|
||||||
public final Signal0 OnPat = new Signal0();
|
public final Signal0 OnPat = new Signal0();
|
||||||
|
|
||||||
|
|
||||||
public Game(ChessBoard board) {
|
public Game(ChessBoard board) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
}
|
}
|
||||||
@@ -39,23 +38,30 @@ public class Game {
|
|||||||
this.OnPlayerTurn.emit(playerTurn);
|
this.OnPlayerTurn.emit(playerTurn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkPromotion(Coordinate pieceCoords) {
|
public boolean checkPromotion(Coordinate pieceCoords) {
|
||||||
|
|
||||||
Piece piece = getBoard().pieceAt(pieceCoords);
|
Piece piece = getBoard().pieceAt(pieceCoords);
|
||||||
|
|
||||||
if (piece == null || !(piece instanceof Pawn))
|
if (piece == null || !(piece instanceof Pawn))
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
int destY = pieceCoords.getY();
|
int destY = pieceCoords.getY();
|
||||||
|
|
||||||
int enemyLine = piece.getColor() == Color.White ? 0 : 7;
|
int enemyLine = piece.getColor() == Color.White ? 0 : 7;
|
||||||
|
|
||||||
if (destY == enemyLine) {
|
if (destY != enemyLine)
|
||||||
OnPromote.emit(pieceCoords);
|
return false;
|
||||||
}
|
|
||||||
|
OnPromote.emit(pieceCoords);
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkGameStatus() {
|
/**
|
||||||
|
*
|
||||||
|
* @return true if game should end
|
||||||
|
*/
|
||||||
|
public boolean checkGameStatus() {
|
||||||
final ChessBoard board = getBoard();
|
final ChessBoard board = getBoard();
|
||||||
|
|
||||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||||
@@ -66,10 +72,30 @@ public class Game {
|
|||||||
} else {
|
} else {
|
||||||
OnMat.emit();
|
OnMat.emit();
|
||||||
OnWin.emit(getPlayerTurn());
|
OnWin.emit(getPlayerTurn());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else if (!board.hasAllowedMoves(enemy)) {
|
} else if (!board.hasAllowedMoves(enemy)) {
|
||||||
OnPat.emit();
|
OnPat.emit();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean pawnShouldBePromoted() {
|
||||||
|
if (pawnShouldBePromoted(Color.White))
|
||||||
|
return true;
|
||||||
|
return pawnShouldBePromoted(Color.Black);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean pawnShouldBePromoted(Color color) {
|
||||||
|
int enemyLineY = color == Color.White ? 0 : 7;
|
||||||
|
|
||||||
|
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
||||||
|
if(checkPromotion(new Coordinate(x, enemyLineY)))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -196,7 +196,42 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void promotePawn(Coordinate pieceCoords) {
|
public void promotePawn(Coordinate pieceCoords) {
|
||||||
sendCommand(new PromoteCommand(PromoteType.Queen, pieceCoords));
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
// String result = JOptionPane.showInputDialog(this, "Choisissez le type !");
|
||||||
|
// sendCommand(new PromoteCommand(PromoteType.Queen, pieceCoords));
|
||||||
|
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
Object[] possibilities = new Object[PromoteType.values().length];
|
||||||
|
int i = 0;
|
||||||
|
for (PromoteType type : PromoteType.values()) {
|
||||||
|
possibilities[i] = type.name();
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (result == null || result.isEmpty()) {
|
||||||
|
result = (String) JOptionPane.showInputDialog(
|
||||||
|
this,
|
||||||
|
"Choose the type of piece to upgrade the pawn",
|
||||||
|
"Promote Dialog",
|
||||||
|
JOptionPane.PLAIN_MESSAGE,
|
||||||
|
null,
|
||||||
|
possibilities,
|
||||||
|
possibilities[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
PromoteType choosedType = null;
|
||||||
|
|
||||||
|
for (PromoteType type : PromoteType.values()) {
|
||||||
|
if (type.name().equals(result)) {
|
||||||
|
choosedType = type;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (choosedType != null)
|
||||||
|
sendCommand(new PromoteCommand(choosedType, pieceCoords));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user