add hungry ai
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
import chess.ai.DumbAI;
|
import chess.ai.DumbAI;
|
||||||
|
import chess.ai.HungryAI;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
import chess.controller.event.GameAdaptator;
|
import chess.controller.event.GameAdaptator;
|
||||||
@@ -20,7 +21,7 @@ public class SwingMain {
|
|||||||
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
commandExecutor.addListener(ai);
|
commandExecutor.addListener(ai);
|
||||||
|
|
||||||
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
HungryAI ai2 = new HungryAI(commandExecutor, Color.White);
|
||||||
commandExecutor.addListener(ai2);
|
commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
commandExecutor.addListener(new GameAdaptator(){
|
commandExecutor.addListener(new GameAdaptator(){
|
||||||
|
|||||||
61
app/src/main/java/chess/ai/HungryAI.java
Normal file
61
app/src/main/java/chess/ai/HungryAI.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package chess.ai;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
|
public class HungryAI extends AI {
|
||||||
|
|
||||||
|
private final PieceCost pieceCost;
|
||||||
|
private final Random random;
|
||||||
|
|
||||||
|
public HungryAI(CommandExecutor commandExecutor, Color color) {
|
||||||
|
super(commandExecutor, color);
|
||||||
|
this.pieceCost = new PieceCost(color);
|
||||||
|
this.random = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getMoveCost(Move move) {
|
||||||
|
Piece piece = pieceAt(move.getDeadPieceCoords());
|
||||||
|
return pieceCost.getCost(piece);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Move> getBestMoves() {
|
||||||
|
List<Move> moves = getAllowedMoves();
|
||||||
|
List<Move> bestMoves = new ArrayList<>();
|
||||||
|
int bestCost = 0;
|
||||||
|
for (Move move : moves) {
|
||||||
|
int moveCost = getMoveCost(move);
|
||||||
|
if (moveCost == bestCost) {
|
||||||
|
bestMoves.add(move);
|
||||||
|
} else if (moveCost > bestCost) {
|
||||||
|
bestMoves.clear();
|
||||||
|
bestMoves.add(move);
|
||||||
|
bestCost = moveCost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestMoves;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void play() {
|
||||||
|
List<Move> bestMoves = getBestMoves();
|
||||||
|
int randomMove = this.random.nextInt(bestMoves.size());
|
||||||
|
this.commandExecutor.executeCommand(new MoveCommand(bestMoves.get(randomMove)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void promote(Coordinate pawnCoords) {
|
||||||
|
sendCommand(new PromoteCommand(PromoteType.Queen));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
60
app/src/main/java/chess/ai/PieceCost.java
Normal file
60
app/src/main/java/chess/ai/PieceCost.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package chess.ai;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
import chess.model.pieces.Bishop;
|
||||||
|
import chess.model.pieces.King;
|
||||||
|
import chess.model.pieces.Knight;
|
||||||
|
import chess.model.pieces.Pawn;
|
||||||
|
import chess.model.pieces.Queen;
|
||||||
|
import chess.model.pieces.Rook;
|
||||||
|
|
||||||
|
public class PieceCost implements PieceVisitor<Integer> {
|
||||||
|
|
||||||
|
private final Color player;
|
||||||
|
|
||||||
|
public PieceCost(Color color) {
|
||||||
|
this.player = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCost(Piece piece) {
|
||||||
|
if (piece == null)
|
||||||
|
return 0;
|
||||||
|
int cost = visit(piece);
|
||||||
|
if (piece.getColor() == player)
|
||||||
|
cost = -cost;
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer visitPiece(Bishop bishop) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer visitPiece(King king) {
|
||||||
|
return 90;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer visitPiece(Knight knight) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer visitPiece(Pawn pawn) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer visitPiece(Queen queen) {
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer visitPiece(Rook rook) {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user