Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 495d95b

Browse files
committed
simple change to print chess board using unicode graphics characters
1 parent 869c6f8 commit 495d95b

File tree

1 file changed

+80
-12
lines changed

1 file changed

+80
-12
lines changed

src-search-game/Chess.java

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import java.io.BufferedReader;
22
import java.io.InputStreamReader;
3+
import java.io.UnsupportedEncodingException;
4+
35

46
/**
57
* Chess game
@@ -16,6 +18,10 @@
1618

1719
public class Chess extends GameSearch {
1820

21+
// note: set the following to false if running in IntelliJ IDE: console does not display
22+
// unicode characters. Usually, you want to set to true (at least try it).
23+
static public boolean USE_UNICODE_CHARS = true; // set to true for graphics characters
24+
1925
/**
2026
* Notes: PROGRAM false -1, HUMAN true 1
2127
*/
@@ -109,20 +115,61 @@ public void printPosition(Position p) {
109115
}
110116
System.out.println();
111117
}
118+
112119
private String pp(int piece, int square_index) {
113120
if (piece == 0) return " ";
114-
String color;
115-
if (piece < 0) color = "B"; else color = "W";
116-
int p = piece; if (p < 0) p = -p;
117-
switch (p) {
118-
case 1: return " " + color + "P";
119-
case 2: return " " + color + "N";
120-
case 3: return " " + color + "B";
121-
case 4: return " " + color + "R";
122-
case 5: return " " + color + "Q";
123-
case 9: return " " + color + "K";
121+
if (USE_UNICODE_CHARS) {
122+
switch (piece) {
123+
case 1:
124+
return " \u2659 ";
125+
case 2:
126+
return " \u2658 ";
127+
case 3:
128+
return " \u2657 ";
129+
case 4:
130+
return " \u2656 ";
131+
case 5:
132+
return " \u2655 ";
133+
case 9:
134+
return " \u2654 ";
135+
case -1:
136+
return " \u265F ";
137+
case -2:
138+
return " \u265E ";
139+
case -3:
140+
return " \u265D ";
141+
case -4:
142+
return " \u265C ";
143+
case -5:
144+
return " \u265B ";
145+
case -9:
146+
return " \u265A ";
147+
}
148+
return "error";
149+
150+
151+
} else {
152+
String color;
153+
if (piece < 0) color = "B";
154+
else color = "W";
155+
int p = piece;
156+
if (p < 0) p = -p;
157+
switch (p) {
158+
case 1:
159+
return " " + color + "P";
160+
case 2:
161+
return " " + color + "N";
162+
case 3:
163+
return " " + color + "B";
164+
case 4:
165+
return " " + color + "R";
166+
case 5:
167+
return " " + color + "Q";
168+
case 9:
169+
return " " + color + "K";
170+
}
171+
return "error";
124172
}
125-
return "error";
126173
}
127174

128175
final public Position [] possibleMoves(Position p, boolean player) {
@@ -190,7 +237,28 @@ public Move createMove() {
190237
return mm;
191238
}
192239

193-
static public void main(String [] args) {
240+
static public void main(String [] args) throws UnsupportedEncodingException {
241+
System.out.println("\u2654");
242+
String unicodeMessage =
243+
"\u2654 " + // white king
244+
"\u2655 " + // white queen
245+
"\u2656 " + // white rook
246+
"\u2657 " + // white bishop
247+
"\u2658 " + // white knight
248+
"\u2659 " + // white pawn
249+
"\n" +
250+
"\u265A " + // black queen
251+
"\u265B " + // black queen
252+
"\u265C " + // black rook
253+
"\u265D " + // black bishop
254+
"\u265E " + // black knight
255+
"\u265F " + // black pawn
256+
"\n" +
257+
"\u2610 " + "\u2612 " + "\u25A0 " + "\u25FC"; // trying for white and black squares
258+
//System.out.println(unicodeMessage);
259+
// PrintStream out = new PrintStream (System.out, true , "UTF8" );
260+
// out.println(unicodeMessage);
261+
194262
ChessPosition p = new ChessPosition();
195263
for (int i=0; i<120; i++) p.board[i] = initialBoard[i];
196264
Chess ttt = new Chess();

0 commit comments

Comments
 (0)