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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ A basic chess engine written in Java.
It also includes a basic min-max algorithm with alpha-beta pruning and quiescence search.

### How to play:
- Run Game.java
- Run ChessCLI.java to run command line interface
```java -cp src Game.ChessCLI```
- Run ChessGUI.java to run GUI
```java -cp src Game.ChessGUI```
- In- and outputs are text-based
- Whether white and black are player-controlled can be inputted with "True" or "False". The latter results in white or black being controlled by the min-max algorithm.
- Computer difficulty can be set with integers (difficulty refers to default depth in min-max algorithm)
Expand Down
124 changes: 124 additions & 0 deletions src/Game/ChessCLI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package Game;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
* Command-line interface for the chess game.
* Handles all user interaction and delegates game logic to the Game class.
*
*/
public class ChessCLI {

public static void main(String[] args) {
ChessCLI cli = new ChessCLI();
cli.run();
}

private final Scanner scanner = new Scanner(System.in);

/**
* Main entry point for the CLI chess application.
*/
public void run() {
System.out.print(System.lineSeparator());
Game game = createGame();
long startTime = System.nanoTime();
playGame(game);
long duration = System.nanoTime() - startTime;
handleGameEnd(game, duration);
}

/**
* Create a new game with user-specified settings.
*/
private Game createGame() {
System.out.print("White controlled? ");
boolean whiteControlled = scanner.nextBoolean();
System.out.print("Black controlled? ");
boolean blackControlled = scanner.nextBoolean();

int whiteDifficulty = 3;
int blackDifficulty = 3;

if (!whiteControlled) {
System.out.print("White difficulty? ");
whiteDifficulty = scanner.nextInt();
}
if (!blackControlled) {
System.out.print("Black difficulty? ");
blackDifficulty = scanner.nextInt();
}

return new Game(whiteControlled, blackControlled, whiteDifficulty, blackDifficulty);
}

/**
* Play the game until completion.
*/
private void playGame(Game game) {
while (!game.isGameOver()) {
if (game.getPlayerAtPlay().isControlled()) {
handleHumanTurn(game);
} else {
game.playATurn();
}
if (game.getLastPlayedOperation() != null) {
System.out.println(game.getLastPlayedOperation());
}
}
}

private void handleHumanTurn(Game game) {
boolean validMove = false;
while (!validMove) {
System.out.print("Enter starting position: ");
String start = scanner.next().toUpperCase();

if (start.equals("UNDO")) {
if (game.undoMove()) {
validMove = true;
} else {
System.out.println("Cannot undo move.");
}
} else if (start.equals("RESIGN")) {
if (game.resignPlayer()) {
validMove = true;
} else {
System.out.println("Cannot resign.");
}
} else {
System.out.print("Enter ending position: ");
String end = scanner.next();
if (game.makeMove(start, end)) {
validMove = true;
} else {
System.out.println("Wrong input, try again!");
}
}
}
}

/**
* Handle game end - display results and offer to save.
*/
private void handleGameEnd(Game game, long durationNanos) {
long minutes = TimeUnit.NANOSECONDS.toMinutes(durationNanos);
long seconds = TimeUnit.NANOSECONDS.toSeconds(durationNanos) - minutes * 60;
String duration = minutes + "m" + seconds + "s";

System.out.println(duration);
if (game.getGameEndMessage() != null) {
System.out.println(game.getGameEndMessage());
}

System.out.print("Do you want to save this match? (y/n): ");
String response = scanner.next().toLowerCase();
if (response.equals("y") || response.equals("yes")) {
GameSaver.saveGame(game, duration);
}
}



}
Loading