Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-10">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>chess-system</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>
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=10
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=10
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.release=enabled
org.eclipse.jdt.core.compiler.source=10
55 changes: 55 additions & 0 deletions src/application/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package application;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import chess.ChessException;
import chess.ChessMatch;
import chess.ChessPiece;
import chess.ChessPosition;

public class Program {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
ChessMatch chessMatch = new ChessMatch();
List<ChessPiece> captured = new ArrayList<>();

while (!chessMatch.getCheckMate()) {
try {
UI.clearScreen();
UI.printMatch(chessMatch, captured);
System.out.println();
System.out.print("Source: ");
ChessPosition source = UI.readChessPosition(sc);

boolean[][] possibleMoves = chessMatch.possibleMoves(source);
UI.clearScreen();
UI.printBoard(chessMatch.getPieces(), possibleMoves);

System.out.println();
System.out.print("Target: ");
ChessPosition target = UI.readChessPosition(sc);

ChessPiece capturedPiece = chessMatch.performChessMove(source, target);

if (capturedPiece != null) {
captured.add(capturedPiece);
}
} catch (ChessException e) {
System.out.println(e.getMessage());
sc.nextLine();
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
sc.nextLine();
}

}
UI.clearScreen();
UI.printMatch(chessMatch, captured);

}
}
125 changes: 125 additions & 0 deletions src/application/UI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package application;

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

import chess.ChessMatch;
import chess.ChessPiece;
import chess.ChessPosition;
import chess.Color;

public class UI {

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";

public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";
public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";

// https://stackoverflow.com/questions/2979383/java-clear-the-console
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}

public static ChessPosition readChessPosition(Scanner sc) {
try {
String s = sc.nextLine();
char column = s.charAt(0);
int row = Integer.parseInt(s.substring(1));
return new ChessPosition(column, row);
} catch (RuntimeException e) {
throw new InputMismatchException("Error reading ChessPosition. Valid values are from a1 to h8");
}
}

public static void printMatch(ChessMatch chessMatch, List<ChessPiece> captured) {
printBoard(chessMatch.getPieces());
System.out.println();
printCapturedPieces(captured);
System.out.println();
System.out.println("turn : " + chessMatch.getTurn());
if (!chessMatch.getCheckMate()) {
System.out.println("Waiting player: " + chessMatch.getCurrentPlayer());
if (chessMatch.getCheck()) {
System.out.println("CHECK!");
}
} else {
System.out.println("CHECKMATE!");
System.out.println("Winner: " + chessMatch.getCurrentPlayer());
}
}

public static void printBoard(ChessPiece[][] pieces) {
for (int i = 0; i < pieces.length; i++) {
System.out.print((8 - i + " "));
for (int j = 0; j < pieces.length; j++) {
printPiece(pieces[i][j], false);
}
System.out.println();
}
System.out.print(" a b c d e f g h ");
}

public static void printBoard(ChessPiece[][] pieces, boolean[][] possibleMoves) {
for (int i = 0; i < pieces.length; i++) {
System.out.print((8 - i + " "));
for (int j = 0; j < pieces.length; j++) {
printPiece(pieces[i][j], possibleMoves[i][j]);
}
System.out.println();
}
System.out.print(" a b c d e f g h ");
}

private static void printPiece(ChessPiece piece, boolean background) {
if (background) {
System.out.print(ANSI_BLUE_BACKGROUND);

}
if (piece == null) {
System.out.print("-" + ANSI_RESET);
} else {
if (piece.getColor() == Color.WHITE) {
System.out.print(ANSI_WHITE + piece + ANSI_RESET);
} else {
System.out.print(ANSI_YELLOW + piece + ANSI_RESET);
}
}
System.out.print(" ");
}

private static void printCapturedPieces(List<ChessPiece> captured) {
List<ChessPiece> white = captured.stream().filter(x -> x.getColor() == Color.WHITE)
.collect(Collectors.toList());
List<ChessPiece> black = captured.stream().filter(x -> x.getColor() == Color.BLACK)
.collect(Collectors.toList());
System.out.println("Captured piece:");
System.out.print("White: " + white);
System.out.println(ANSI_WHITE);
System.out.println(Arrays.toString(white.toArray()));
System.out.println(ANSI_RESET);
System.out.print("Black:" + black);
System.out.println(ANSI_BLACK);
System.out.println(Arrays.toString(black.toArray()));
System.out.println(ANSI_RESET);

}

}
75 changes: 75 additions & 0 deletions src/boardgame/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package boardgame;

public class Board {
private int rows;
private int columns;
private Piece[][] pieces;

public Board(int rows, int columns) {
if (rows < 1 || columns < 1) {
throw new BoardException("Error creating board there must be at least 1 row and 1 column");
}
this.rows = rows;
this.columns = columns;
pieces = new Piece[rows][columns];
}

public int getRows() {
return rows;
}

public int getColumns() {
return columns;
}

public Piece piece(int row, int column) {
if (!positionExists(row, column)) {
throw new BoardException("Position not on the board");
}
return pieces[row][column];
}

public Piece piece(Position position) {
if (!positionExists(position)) {
throw new BoardException("Position not on the board");
}
return pieces[position.getRow()][position.getColumn()];
}

public void placePiece(Piece piece, Position position) {
if (thereIsAPiece(position)) {
throw new BoardException("there is already a piece on position " + position);
}
pieces[position.getRow()][position.getColumn()] = piece;
piece.position = position;
}

public Piece removePiece (Position position) {
if (!positionExists(position)) {
throw new BoardException("Position not on the board");
}
if (piece(position) == null) {
return null;
}
Piece aux = piece(position);
aux.position = null;
pieces[position.getRow()][position.getColumn()] = null;
return aux;
}

private boolean positionExists(int row, int column) {
return row >= 0 && row < rows && column >= 0 && column < columns;
}

public boolean positionExists(Position position) {
return positionExists(position.getColumn(), position.getColumn());
}

public boolean thereIsAPiece(Position position) {
if (!positionExists(position)) {
throw new BoardException("Position not on the board");
}
return piece(position) != null;
}

}
11 changes: 11 additions & 0 deletions src/boardgame/BoardException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package boardgame;

public class BoardException extends RuntimeException {
private static final long serialVersionUID = 1L;

public BoardException(String msg) {
super(msg);
}


}
33 changes: 33 additions & 0 deletions src/boardgame/Piece.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package boardgame;

public abstract class Piece {
protected Position position;
private Board board;

public Piece(Board board) {
this.board = board;
}

protected Board getBoard() {
return board;
}

public abstract boolean[][] possibleMoves();

public boolean possibleMove(Position position) {
return possibleMoves()[position.getRow()][position.getColumn()];
}

public boolean isThereAnyPossibleMove() {
boolean[][] mat = possibleMoves();
for (int i=0; i<mat.length; i++) {
for (int j=0; j<mat.length; j++) {
if(mat[i][j]) {
return true;
}
}
}
return false;
}

}
45 changes: 45 additions & 0 deletions src/boardgame/Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package boardgame;

public class Position {
private int row;
private int column;

public Position() {

}

public Position(int row, int column) {
this.row = row;
this.column = column;

}

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}

public int getColumn() {
return column;
}

public void setColumn(int column) {
this.column = column;
}

public void setValues(int row, int column) {
this.row = row;
this.column = column;

}

@Override
public String toString() {
return row + ", " + column;

}

}
Loading