Merge branch '6-colorize-output-with-jansi' into 'master'

Resolve "Colorize output with jansi"

Closes #6

See merge request !21
This commit is contained in:
Malte Tammena 2017-10-21 20:36:22 +02:00
commit 4d3b14c3cc
3 changed files with 35 additions and 12 deletions

View file

@ -9,7 +9,8 @@ JAVADOC_OPTIONS=-Xdoclint:all -private -encoding UTF-8 -charset UTF-8 -docencodi
BUILDS=builds BUILDS=builds
DOC=doc DOC=doc
CLASSPATH=src CLASSPATH=src
LIBARIES= LIBARIES=\
lib/jansi-1.16.jar
CLASSES=\ CLASSES=\
src/game/Main.java \ src/game/Main.java \
src/game/Game.java \ src/game/Game.java \
@ -36,13 +37,13 @@ P2=
all: $(CLASSES) $(FXMLS_BUILD) all: $(CLASSES) $(FXMLS_BUILD)
$(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH) $(CLASSES) -d $(BUILDS) $(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH):$(LIBARIES) $(CLASSES) -d $(BUILDS)
run: $(CLASSES) run: $(CLASSES)
$(JAVA) -cp $(BUILDS) $(MAIN) $(P1) $(P2) $(JAVA) -cp $(BUILDS):$(LIBARIES) $(MAIN) $(P1) $(P2)
simulate: $(CLASSES) simulate: $(CLASSES)
$(JAVA) -cp $(BUILDS) $(MAIN) $(N) $(JAVA) -cp $(BUILDS):$(LIBARIES) $(MAIN) $(N)
doc: $(CLASSES) doc: $(CLASSES)
$(JAVADOC) $(JAVADOC_OPTIONS) -cp $(CLASSPATH):$(LIBARIES) $(CLASSES) -d $(DOC) $(JAVADOC) $(JAVADOC_OPTIONS) -cp $(CLASSPATH):$(LIBARIES) $(CLASSES) -d $(DOC)

BIN
lib/jansi-1.16.jar Normal file

Binary file not shown.

View file

@ -1,6 +1,9 @@
package game; package game;
import java.util.Random; import java.util.Random;
import org.fusesource.jansi.AnsiConsole;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color;
import player.Player; import player.Player;
import player.maurizio.MaurizioAI; import player.maurizio.MaurizioAI;
@ -113,6 +116,8 @@ public class Game {
* @return the winner of the game (0=draw) * @return the winner of the game (0=draw)
*/ */
public int start(boolean output, boolean rand) { public int start(boolean output, boolean rand) {
// Initialize Jansi
AnsiConsole.systemInstall();
// Set starting player. // Set starting player.
if(rand){ if(rand){
Random ran = new Random(); Random ran = new Random();
@ -131,7 +136,7 @@ public class Game {
makeMove(output); makeMove(output);
currentP = other(currentP); currentP = other(currentP);
if(output){ if(output){
logGame(); logGame(hist.getLast());
} }
int state = checkState(output); int state = checkState(output);
if(state != (-1)){ if(state != (-1)){
@ -140,9 +145,13 @@ public class Game {
System.out.println("Thanks for Playing!"); System.out.println("Thanks for Playing!");
System.out.println("*******************"); System.out.println("*******************");
} }
// Uninstall Jansi
AnsiConsole.systemUninstall();
return state; // the result of the game return state; // the result of the game
} }
} }
// Uninstall Jansi
AnsiConsole.systemUninstall();
System.out.println("BUG!"); System.out.println("BUG!");
return checkState(false); // it is impossible to reach this, but it makes the compiler happy ;) return checkState(false); // it is impossible to reach this, but it makes the compiler happy ;)
} }
@ -264,17 +273,30 @@ public class Game {
/** /**
* Prints the current board to stdout. * Prints the current board to stdout.
*/ */
private void logGame() { private void logGame(GameEntry entry) {
Ansi playerX = Ansi.ansi().render("|@|red X|@");
Ansi playerO = Ansi.ansi().render("|@|green O|@");
Ansi playerXBold = Ansi.ansi().render("|@|magenta,bold X|@");
Ansi playerOBold = Ansi.ansi().render("|@|magenta,bold O|@");
for(int i = 0; i < board[0].length; i++){ for(int i = 0; i < board[0].length; i++){
for(int j = 0;j < board.length; j++){ for(int j = 0;j < board.length; j++){
if(board[j][i] == 0){ if(i == entry.getRow() && j == entry.getColumn()) {
System.out.print("| ");
}
if(board[j][i] == 1){ if(board[j][i] == 1){
System.out.print("|X"); System.out.print(playerXBold);
} }
if (board[j][i] == 2){ if (board[j][i] == 2){
System.out.print("|O"); System.out.print(playerOBold);
}
} else {
if(board[j][i] == 1){
System.out.print(playerX);
}
if (board[j][i] == 2){
System.out.print(playerO);
}
}
if(board[j][i] == 0){
System.out.print("| ");
} }
} }
System.out.println("|"); System.out.println("|");