Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class ascii_chess {
- public static void main(String[] args) {
- String boardIn = "1r3rk1/1pnnq1bR/p1pp2B1/P2P1p2/1PP1pP2/2B3P1/5PK1/2Q4R";
- String[] boardLines = initializeBoard(boardIn);
- printBoard(boardLines);
- }
- public static String[] initializeBoard(String boardIn){
- String[] boardLines = boardIn.split("/");
- boardLines = fillEmpties(boardLines);
- return boardLines;
- }
- public static void printBoard(String[] boardLines){
- //Each line in boardLines is a string representing the pieces("E" is empty square)
- //Print it out ascii style
- int width = 4;
- int height = 4;
- int numCells = 8;
- //Width =4, plus 2 side elements. 8 cells. Need 3 extra for box and corner
- int numElements = ((width+2)*numCells)+3;
- String topLine="";
- for (int i =0; i<numElements; i++){
- topLine+="-";
- }
- for (int row = 0; row<boardLines.length; row++){
- //Each row is 4 tall. They have tops but not bottoms
- //For each row, get the 4 individual row strings
- //Then print them on different lines
- String currentPieceString = boardLines[row];
- String[] rowLines = new String[height];
- Arrays.fill(rowLines, "");
- for (int i = 0;i<rowLines.length;i++){
- rowLines[i]+="|";
- if (i==0){
- rowLines[i]+=Integer.toString(8-row);
- }
- else{
- rowLines[i]+=" ";
- }
- }
- for (int column = 0; column<boardLines[row].length(); column++){
- //Get the row strings from the char at boardLines[row][column]
- String currentPiece = currentPieceString.substring(column,column+1);
- String currentPieceUp = currentPiece.toUpperCase();
- String[] pieceStrings;
- if (currentPiece.equals(currentPieceUp)){
- pieceStrings = ChessPiece.valueOf(currentPieceUp).getPieceStrings("o");
- }
- else {
- pieceStrings = ChessPiece.valueOf(currentPieceUp).getPieceStrings("x");
- }
- for (int i = 0;i<rowLines.length;i++){
- rowLines[i]+="|"+pieceStrings[i];
- }
- }
- for (int i = 0;i<rowLines.length;i++){
- rowLines[i]+="|";
- }
- //Print the top line and piece lines
- System.out.println(topLine);
- for (int i = 0;i<rowLines.length;i++){
- System.out.println(rowLines[i]);
- }
- }
- //Print the alphabetic coords
- System.out.println(topLine);
- String alphaString="| ";
- for (int i=0; i<numCells; i++){
- alphaString+="|";
- alphaString+=getAlpha(i);
- for (int j=0;j<width;j++){
- alphaString+=" ";
- }
- }
- alphaString+="|";
- System.out.println(alphaString);
- //Print the bottom line
- System.out.println(topLine);
- }
- public static char getAlpha(int num){
- return (char)(num+97);
- }
- public static void printBoardSimple(String[] boardLines){
- //Loop through all strings and print them line after line
- for (int i = 0; i<boardLines.length; i++){
- System.out.println(boardLines[i]);
- }
- }
- public static String[] fillEmpties(String[] boardLines){
- //For every line, replace any number representing empty squares with "E"
- for (int i = 0; i< boardLines.length; i++){
- //For every line
- String currLine = boardLines[i];
- String emptiedLine = "";
- for (int j=0; j<currLine.length(); j++){
- //For every character
- Character currentChar = currLine.charAt(j);
- if (Character.isDigit(currentChar)){
- int numEmpties = Character.getNumericValue(currentChar);
- for (; numEmpties>0; numEmpties--){
- emptiedLine += "E";
- }
- }
- else {
- emptiedLine += currentChar;
- }
- }
- boardLines[i] = emptiedLine;
- emptiedLine = "";
- }
- return boardLines;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement