Java_Chess

.idea
    .name 5 B
    artifacts
       Chess_jar.xml 371 B
    compiler.xml 734 B
    copyright
    description.html 97 B
    encodings.xml 171 B
    libraries
       jansi_1_11.xml 214 B
    misc.xml 5 KB
    modules.xml 261 B
    project-template.xml 91 B
    scopes
       scope_settings.xml 143 B
    uiDesigner.xml 8 KB
    vcs.xml 176 B
    workspace.xml 61 KB
Chess.iml 1 KB
Doxyfile 101 KB
GUI_Manual_Testplan.pdf 437 KB
Graphics
    blackBerolinaPawn.png 1 KB
    blackBishop.png 5 KB
    blackKing.png 8 KB
    blackKnight.png 6 KB
    blackPawn.png 2 KB
    blackQueen.png 8 KB
    blackRook.png 3 KB
    blackWazir.png 1 KB
    whiteBerolinaPawn.png 2 KB
    whiteBishop.png 7 KB
    whiteKing.png 8 KB
    whiteKnight.png 7 KB
    whitePawn.png 4 KB
    whiteQueen.png 10 KB
    whiteRook.png 4 KB
    whiteWazir.png 2 KB
README 342 B
jansi-1.11.jar 111 KB
src
    CHANGES 1001 B
    GUI
       Controller.java 15 KB
       Main.java 360 B
       View.java 14 KB
       textPanel.java 776 B
    Game
       Board.java 5 KB
       CLI.java 5 KB
       Command.java 1022 B
       GameManager.java 11 KB
       GameType.java 74 B
       MoveHandler.java 1 KB
       Player.java 133 B
    META-INF
       MANIFEST.MF 53 B
    Main.java 154 B
    Pieces
       BerolinaPawn.java 1 KB
       Bishop.java 480 B
       BoardSquare.java 4 KB
       King.java 470 B
       Knight.java 846 B
       MoveType.java 530 B
       Pawn.java 1 KB
       Queen.java 474 B
       Rook.java 471 B
       Wazir.java 627 B
    TODO 176 B
    Tests
       BoardTest.java 2 KB
       GameManagerTest.java 21 KB
CLI.java
package Game;

import Pieces.BoardSquare;
import org.fusesource.jansi.AnsiConsole;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static org.fusesource.jansi.Ansi.*;
import static org.fusesource.jansi.Ansi.Color.*;

/**
Command line interface for chess library
*/
public class CLI {


    private static GameManager game;
    private static boolean firstmove;

    /**
     Start Command line interface.
     */
    public static void startCLI(){

        firstmove = true;
        game = new GameManager(GameType.STANDARD);

        System.out.println("New game started!");

        while(true) {//currently busy waiting.. @todo quit option

            CLI.Print(game.gameBoard);

            //System.out.println("testcheck():" + game.testCheck(false));
            if(game.testCheck(false) != Player.NONE)
                System.out.println("A owner is in check.");

            CLI.GetInput();
        }

    }

    /**
    Get owner input.
     */
    public static void GetInput() {

        /*if(game.playerTurn)
            System.out.println("Player 1's turn.");
        else
            System.out.println("Player 2's turn.");*/

        if(firstmove) {
            System.out.println("Enter your move as: ColumnRow ColumnRow\nExample: a2 a4\n\nType your move:");
            firstmove = false;
        } else {
            System.out.println("Enter your move:");
        }

        String input = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

          input = br.readLine();

           // while((input=br.readLine())!=null){
                System.out.println(input);
           //}

        } catch(IOException io) {
            io.printStackTrace();
        }

        if(input != null && input.length() == 5) {
            char[] ia = input.toCharArray();//


            int sx = ((int)(ia[0] - 97));
            int sy = (Character.getNumericValue(ia[1])-1);

            int ex = ((int)(ia[3] - 97));
            int ey = (Character.getNumericValue(ia[4])-1);


            //System.out.println(" " + ((int)(ia[0] - 97)) + " " + (Character.getNumericValue(ia[1])-1) + " " + ((int)(ia[3] - 97)) + " " + (ia[4]-1));
            if(game.Move(sx, sy, ex, ey)!= GameManager.MoveStatus.ERROR){
                System.out.println("Move from: " + sx + " " + sy + ", to:" + ex + " " + ey);
            } else {
                System.out.println("Move invalid!");
            }

        } else if(input != null && input.length() == 2) {
            char[] ia = input.toCharArray();//


            int sx = ((int)(ia[0] - 97));
            int sy = (Character.getNumericValue(ia[1])-1);


            //System.out.println(" " + ((int)(ia[0] - 97)) + " " + (Character.getNumericValue(ia[1])-1) + " " + ((int)(ia[3] - 97)) + " " + (ia[4]-1));
            PrintMoves(game.gameBoard, sx, sy);
            GetInput();

        } else {
            Print(game.gameBoard);
            GetInput();
        }

    }

    /**
    Print possible moves of the Piece at sx, sy
     */
    public static void PrintMoves(Board board, int sx, int sy){

        AnsiConsole.systemInstall();

        System.out.println("  -------------------");

        boolean check = false;
        for(int y = board.height-1; y >= 0; y--) {
            System.out.print((y+1) + " |");
            for(int x = 0; x < board.width; x++) {
                System.out.print(' ');
                BoardSquare p = board.getPiece(x, y);
                if(game.validMove(sx, sy, x, y) != GameManager.MoveStatus.ERROR) {

                    if(p.owner != Player.NONE) {
                        System.out.print(ansi().fg(DEFAULT).a('x').reset());
                        //if(p.getType() == Pieces.PieceType.KING)
                           // check = true;
                    }
                    else
                        System.out.print(ansi().fg(DEFAULT).a('.').reset());

                } else {
                    //@todo refactor duplicate.

                    if (p.owner == Player.WHITE)
                        System.out.print(ansi().fg(YELLOW).a(p.symbol).reset());
                    else if (p.owner == Player.BLACK)
                        System.out.print(ansi().fg(RED).a(p.symbol).reset());
                    else
                        System.out.print(ansi().fg(DEFAULT).a(p.symbol).reset());

                }

                // System.out.print(p.id);
            }
            System.out.print(" |\n");
        }

        System.out.print("  -------------------\n    a b c d e f g h\n");

        //if(check)
          //  System.out.println("A owner is in check.");

        AnsiConsole.systemUninstall();

    }

    /**
    Print out board for playing and testing.
     */
    public static void Print(Board board) {

        AnsiConsole.systemInstall();

        System.out.println("  -------------------");

        for(int y = board.height-1; y >= 0; y--) {
            System.out.print((y+1) + " |");
            for(int x = 0; x < board.width; x++) {
                System.out.print(' ');
                BoardSquare p = board.getPiece(x, y);
                if(p.owner == Player.WHITE)
                    System.out.print(ansi().fg(YELLOW).a(p.symbol).reset());
                else if(p.owner == Player.BLACK)
                    System.out.print(ansi().fg(RED).a(p.symbol).reset());
                else
                    System.out.print(ansi().fg(DEFAULT).a(p.symbol).reset());


               // System.out.print(p.id);
            }
            System.out.print(" |\n");
        }

        System.out.print("  -------------------\n    a b c d e f g h\n");

        AnsiConsole.systemUninstall();

    }

}