forked from Mateful/Twitchess
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="src" path="test"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Twitchess</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#Mon Nov 14 20:14:10 CET 2011 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
258 changes: 258 additions & 0 deletions
258
src/de/fhb/projects/chesstwitterbot/chesslogic/ChessBoard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
package de.fhb.projects.chesstwitterbot.chesslogic; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import de.fhb.projects.chesstwitterbot.chesslogic.figures.Bishop; | ||
import de.fhb.projects.chesstwitterbot.chesslogic.figures.Figure; | ||
import de.fhb.projects.chesstwitterbot.chesslogic.figures.King; | ||
import de.fhb.projects.chesstwitterbot.chesslogic.figures.Knight; | ||
import de.fhb.projects.chesstwitterbot.chesslogic.figures.Pawn; | ||
import de.fhb.projects.chesstwitterbot.chesslogic.figures.Queen; | ||
import de.fhb.projects.chesstwitterbot.chesslogic.figures.Rook; | ||
|
||
public class ChessBoard { | ||
// private Figure[][] board; | ||
private Map<Position, Figure> board; | ||
private Color playerToMove; | ||
private Figure enPassant; | ||
private boolean kingCastling[]; | ||
private boolean queenCastling[]; | ||
|
||
public ChessBoard() { | ||
clearBoard(); | ||
setStartingPosition(); | ||
} | ||
|
||
private void clearBoard() { | ||
setBoard(new HashMap<Position, Figure>()); | ||
playerToMove = Color.WHITE; | ||
setEnPassant(null); | ||
|
||
} | ||
|
||
private void setStartingPosition() { | ||
setKingCastling(new boolean[] { true, true }); | ||
setQueenCastling(new boolean[] { true, true }); | ||
|
||
playerToMove = Color.WHITE; | ||
|
||
setFigure(Rook.class, Color.WHITE, Position.getPosition(0, 0)); | ||
setFigure(Rook.class, Color.WHITE, Position.getPosition(7, 0)); | ||
setFigure(Rook.class, Color.BLACK, Position.getPosition(0, 7)); | ||
setFigure(Rook.class, Color.BLACK, Position.getPosition(7, 7)); | ||
|
||
setFigure(Knight.class, Color.WHITE, Position.getPosition(1, 0)); | ||
setFigure(Knight.class, Color.WHITE, Position.getPosition(6, 0)); | ||
setFigure(Knight.class, Color.BLACK, Position.getPosition(1, 7)); | ||
setFigure(Knight.class, Color.BLACK, Position.getPosition(6, 7)); | ||
|
||
setFigure(Bishop.class, Color.WHITE, Position.getPosition(2, 0)); | ||
setFigure(Bishop.class, Color.WHITE, Position.getPosition(5, 0)); | ||
setFigure(Bishop.class, Color.BLACK, Position.getPosition(2, 7)); | ||
setFigure(Bishop.class, Color.BLACK, Position.getPosition(5, 7)); | ||
|
||
setFigure(Queen.class, Color.WHITE, Position.getPosition(3, 0)); | ||
setFigure(Queen.class, Color.BLACK, Position.getPosition(3, 7)); | ||
|
||
setFigure(King.class, Color.WHITE, Position.getPosition(4, 0)); | ||
setFigure(King.class, Color.BLACK, Position.getPosition(4, 7)); | ||
|
||
for (int i = 0; i < 8; i++) { | ||
setFigure(Pawn.class, Color.WHITE, Position.getPosition(i, 1)); | ||
setFigure(Pawn.class, Color.BLACK, Position.getPosition(i, 6)); | ||
} | ||
|
||
// setEnPassant(getFigureAtPosition(4, 3)); | ||
} | ||
|
||
private void setFigure(Class<? extends Figure> type, Color color, | ||
Position position) { | ||
try { | ||
board.put( | ||
position, | ||
type.getConstructor( | ||
new Class[] { Position.class, Color.class }) | ||
.newInstance(position, color)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public Color getPlayerToMove() { | ||
return playerToMove; | ||
} | ||
|
||
public void setPlayerToMove(Color playerToMove) { | ||
this.playerToMove = playerToMove; | ||
} | ||
|
||
public Map<Position, Figure> getBoard() { | ||
return board; | ||
} | ||
|
||
private void setBoard(Map<Position, Figure> board) { | ||
this.board = board; | ||
} | ||
|
||
public Figure getEnPassant() { | ||
return enPassant; | ||
} | ||
|
||
public void setEnPassant(Figure enPassant) { | ||
this.enPassant = enPassant; | ||
} | ||
|
||
public boolean[] getKingCastling() { | ||
return kingCastling; | ||
} | ||
|
||
public void setKingCastling(boolean kingCastling[]) { | ||
this.kingCastling = kingCastling; | ||
} | ||
|
||
public boolean[] getQueenCastling() { | ||
return queenCastling; | ||
} | ||
|
||
public void setQueenCastling(boolean queenCastling[]) { | ||
this.queenCastling = queenCastling; | ||
} | ||
|
||
public Figure getFigureAtPosition(int x, int y) { | ||
return board.get(Position.getPosition(x, y)); | ||
} | ||
|
||
public Figure getFigureAtPosition(Position position) { | ||
return board.get(position); | ||
} | ||
|
||
public List<Move> generateNaiveMoves() { | ||
List<Move> moves = new ArrayList<Move>(); | ||
|
||
for (Figure f : board.values()) | ||
if (isItFiguresTurn(f)) | ||
moves.addAll(f.generateMoves(this)); | ||
|
||
return moves; | ||
} | ||
|
||
private boolean isItFiguresTurn(Figure f) { | ||
return f != null && f.getColor() == getPlayerToMove(); | ||
} | ||
|
||
public void doMove(Move m) { | ||
Figure f = board.get(m.getStart()); | ||
|
||
if (f != null) { | ||
board.remove(m.getStart()); | ||
board.put(m.getDestination(), f); | ||
f.setPosition(m.getDestination()); | ||
playerToMove = f.getColor().getInverse(); | ||
} | ||
|
||
} | ||
|
||
public boolean isCheckAfterMove(Move m) { | ||
HashMap<Position, Figure> boardCopy = new HashMap<Position, Figure>(); | ||
List<Move> moves = new ArrayList<Move>(); | ||
boolean isCheck = false; | ||
Color playerToMoveCopy = playerToMove; | ||
|
||
for (Figure f : board.values()) | ||
boardCopy.put(f.getPosition(), (Figure) f.clone()); | ||
|
||
doMove(m); | ||
|
||
moves = generateNaiveMoves(); | ||
|
||
for (int i = 0; i < moves.size() && !isCheck; i++) { | ||
Figure temp = getFigureAtPosition(moves.get(i).getDestination()); | ||
if (temp instanceof King && temp.getColor() == playerToMoveCopy) | ||
isCheck = true; | ||
} | ||
|
||
|
||
// undo move | ||
board = boardCopy; | ||
playerToMove = playerToMoveCopy; | ||
|
||
return isCheck; | ||
} | ||
|
||
public String toString() { | ||
char[][] output = new char[8][8]; | ||
|
||
for (Figure f : board.values()) { | ||
|
||
if (f instanceof King) { | ||
output[f.getPosition().getX()][f.getPosition().getY()] = 'k'; | ||
} else if (f instanceof Queen) { | ||
output[f.getPosition().getX()][f.getPosition().getY()] = 'q'; | ||
} else if (f instanceof Knight) { | ||
output[f.getPosition().getX()][f.getPosition().getY()] = 'n'; | ||
} else if (f instanceof Bishop) { | ||
output[f.getPosition().getX()][f.getPosition().getY()] = 'b'; | ||
} else if (f instanceof Pawn) { | ||
output[f.getPosition().getX()][f.getPosition().getY()] = 'p'; | ||
} else if (f instanceof Rook) { | ||
output[f.getPosition().getX()][f.getPosition().getY()] = 'r'; | ||
} | ||
|
||
if (f != null && f.getColor() == Color.WHITE) | ||
output[f.getPosition().getX()][f.getPosition().getY()] = ("" + output[f | ||
.getPosition().getX()][f.getPosition().getY()]) | ||
.toUpperCase().charAt(0); | ||
|
||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (int y = 7; y >= 0; y--) { | ||
for (int x = 0; x < 8; x++) { | ||
if (output[x][y] != 0) { | ||
sb.append(output[x][y]); | ||
sb.append(' '); | ||
} else | ||
sb.append(" "); | ||
} | ||
sb.append("\n"); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
ChessBoard b = new ChessBoard(); | ||
|
||
InputStreamReader inputStreamReader = new InputStreamReader(System.in); | ||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); | ||
String s; | ||
|
||
while (true) { | ||
List<Move> moves = b.generateNaiveMoves(); | ||
|
||
System.out.println(b); | ||
System.out.println("move count: " + moves.size()); | ||
|
||
for (int i = 0; i < moves.size(); i++) | ||
System.out.println((i) + ": " + moves.get(i) + " check: " | ||
+ b.isCheckAfterMove(moves.get(i))); | ||
|
||
|
||
try { | ||
s = bufferedReader.readLine(); | ||
|
||
b.doMove(moves.get(Integer.parseInt(s))); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package de.fhb.projects.chesstwitterbot.chesslogic; | ||
|
||
public enum Color { | ||
WHITE, BLACK; | ||
|
||
public Color getInverse() { | ||
return this == WHITE ? BLACK : WHITE; | ||
} | ||
|
||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/de/fhb/projects/chesstwitterbot/chesslogic/FieldPrototype.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.fhb.projects.chesstwitterbot.chesslogic; | ||
|
||
public enum FieldPrototype { | ||
EMPTY(null, null), | ||
KING_WHITE(Type.KING, Color.WHITE), KING_BLACK(Type.KING, Color.BLACK), | ||
QUEEN_WHITE(Type.QUEEN, Color.WHITE), QUEEN_BLACK(Type.QUEEN, Color.BLACK), | ||
ROOK_WHITE(Type.ROOK, Color.WHITE), ROOK_BLACK(Type.ROOK, Color.BLACK), | ||
BISHOP_WHITE(Type.BISHOP, Color.WHITE), BISHOP_BLACK(Type.BISHOP, Color.BLACK), | ||
KNIGHT_WHITE(Type.KNIGHT, Color.WHITE), KNIGHT_BLACK(Type.KNIGHT, Color.BLACK), | ||
PAWN_WHITE(Type.PAWN, Color.WHITE), PAWN_BLACK(Type.PAWN, Color.BLACK); | ||
|
||
private FieldPrototype(Type t, Color c) { | ||
type = t; | ||
color = c; | ||
} | ||
|
||
public enum Type { | ||
KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN; | ||
} | ||
|
||
private Type type; | ||
private Color color; | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(Color color) { | ||
this.color = color; | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
} | ||
|
||
public void setType(Type type) { | ||
this.type = type; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.fhb.projects.chesstwitterbot.chesslogic; | ||
|
||
public class Move { | ||
private Position start, destination; | ||
private boolean enPassant; | ||
|
||
public Position getStart() { | ||
return start; | ||
} | ||
|
||
public void setStart(Position start) { | ||
this.start = start; | ||
} | ||
|
||
public Position getDestination() { | ||
return destination; | ||
} | ||
|
||
public void setDestination(Position destination) { | ||
this.destination = destination; | ||
} | ||
|
||
public boolean isEnPassant() { | ||
return enPassant; | ||
} | ||
|
||
public void setEnPassant(boolean isEnPassant) { | ||
this.enPassant = isEnPassant; | ||
} | ||
|
||
public Move(Position start, Position destination) { | ||
setStart(start); | ||
setDestination(destination); | ||
setEnPassant(false); | ||
} | ||
|
||
public Move(Position start, Position destination, boolean enPassant) { | ||
setStart(start); | ||
setDestination(destination); | ||
setEnPassant(enPassant); | ||
} | ||
|
||
public String toString() { | ||
return start.toString() + destination.toString(); | ||
} | ||
|
||
} |
Oops, something went wrong.