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();
}
}