Skip to content

pulkowski-jan/ChessAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChessAPI

Simple api for managing chess games.

Usage

Firstly you need to create instance of GameManager.
Generic type parameter Key is used as a key for accessing games managed by the object.
Note: Type given as key must override both equals, and hashCode methods. It's required, due to internal use of HashMaps
Then you need an implementation of ChessEventListener. One game per one instance is the recommended technique, although if the client deals with synchronisation it's not required. There are also other optional features.

Very Basic Implementation

class Listener implements ChessEventListener {
    private final GamesManager<String> manager;

    Listener(GamesManager<String> manager) {
        this.manager = manager;
    }

    @Override public void onMate(Color winner) {
        System.out.println(winner + " won!");
    }

    @Override public void onDraw(DrawReason reason) {
        System.out.println("That's a draw!");
    }

    @Override public void onMoveHandled() {
        var read = manager.read("Coffee");
        for (ChessPiece[] chessPieces : read) {
            for (ChessPiece piece : chessPieces) 
                if (piece == null) System.out.print(" ".repeat(3));
                else System.out.print(piece.color().name().charAt(0) + piece.toString() + " ");
            System.out.println();
        }
    }

    @Override public void onIllegalMove() {
        System.out.println("You can't move like that");
    }

    @Override public boolean onDrawRequest(Color requestor) {
        return false;
    }
    
    //i know i know, but it ain't hard to write just wordy.
    @Override public Class<? extends Piece> onPawnExchange() {
        return Queen.class;
    }
}    

public class Main {
    public static void main(String[] args) {
        var manager = new GamesManager<String>();
        manager.newGame("Coffee", new Listener());
        var in = new Scanner(System.in);
        for (String input = in.nextLine(); !input.equals("quit"); input = in.nextLine())
            manager.move("Coffee", input);
    }
}

Useful features

Class GameInfo lets you save info about players in the game.

Example

manager.attachInfo(new GameInfo<>("Coffee", 21, 37));
  • First parameter - gameKey
  • Second and third parameter - white and black players
  • First Type parameter - gameKey's type
  • First Type parameter - the players' type

About

Simple api for building chess games

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages