Advertisement
Guest User

Untitled

a guest
Nov 19th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class ascii_chess {
  4.  
  5. public static void main(String[] args) {
  6. String boardIn = "1r3rk1/1pnnq1bR/p1pp2B1/P2P1p2/1PP1pP2/2B3P1/5PK1/2Q4R";
  7. String[] boardLines = initializeBoard(boardIn);
  8. printBoard(boardLines);
  9.  
  10. }
  11.  
  12. public static String[] initializeBoard(String boardIn){
  13. String[] boardLines = boardIn.split("/");
  14. boardLines = fillEmpties(boardLines);
  15. return boardLines;
  16. }
  17.  
  18. public static void printBoard(String[] boardLines){
  19. //Each line in boardLines is a string representing the pieces("E" is empty square)
  20. //Print it out ascii style
  21. int width = 4;
  22. int height = 4;
  23. int numCells = 8;
  24. //Width =4, plus 2 side elements. 8 cells. Need 3 extra for box and corner
  25. int numElements = ((width+2)*numCells)+3;
  26. String topLine="";
  27. for (int i =0; i<numElements; i++){
  28. topLine+="-";
  29. }
  30. for (int row = 0; row<boardLines.length; row++){
  31. //Each row is 4 tall. They have tops but not bottoms
  32. //For each row, get the 4 individual row strings
  33. //Then print them on different lines
  34. String currentPieceString = boardLines[row];
  35. String[] rowLines = new String[height];
  36. Arrays.fill(rowLines, "");
  37.  
  38.  
  39. for (int i = 0;i<rowLines.length;i++){
  40. rowLines[i]+="|";
  41. if (i==0){
  42. rowLines[i]+=Integer.toString(8-row);
  43. }
  44. else{
  45. rowLines[i]+=" ";
  46. }
  47. }
  48.  
  49. for (int column = 0; column<boardLines[row].length(); column++){
  50.  
  51. //Get the row strings from the char at boardLines[row][column]
  52. String currentPiece = currentPieceString.substring(column,column+1);
  53. String currentPieceUp = currentPiece.toUpperCase();
  54. String[] pieceStrings;
  55. if (currentPiece.equals(currentPieceUp)){
  56. pieceStrings = ChessPiece.valueOf(currentPieceUp).getPieceStrings("o");
  57. }
  58. else {
  59. pieceStrings = ChessPiece.valueOf(currentPieceUp).getPieceStrings("x");
  60. }
  61. for (int i = 0;i<rowLines.length;i++){
  62. rowLines[i]+="|"+pieceStrings[i];
  63. }
  64. }
  65. for (int i = 0;i<rowLines.length;i++){
  66. rowLines[i]+="|";
  67. }
  68. //Print the top line and piece lines
  69. System.out.println(topLine);
  70. for (int i = 0;i<rowLines.length;i++){
  71. System.out.println(rowLines[i]);
  72. }
  73. }
  74. //Print the alphabetic coords
  75. System.out.println(topLine);
  76. String alphaString="| ";
  77. for (int i=0; i<numCells; i++){
  78. alphaString+="|";
  79. alphaString+=getAlpha(i);
  80. for (int j=0;j<width;j++){
  81. alphaString+=" ";
  82. }
  83. }
  84. alphaString+="|";
  85. System.out.println(alphaString);
  86. //Print the bottom line
  87. System.out.println(topLine);
  88. }
  89.  
  90. public static char getAlpha(int num){
  91. return (char)(num+97);
  92. }
  93.  
  94. public static void printBoardSimple(String[] boardLines){
  95. //Loop through all strings and print them line after line
  96. for (int i = 0; i<boardLines.length; i++){
  97. System.out.println(boardLines[i]);
  98. }
  99. }
  100.  
  101. public static String[] fillEmpties(String[] boardLines){
  102. //For every line, replace any number representing empty squares with "E"
  103. for (int i = 0; i< boardLines.length; i++){
  104. //For every line
  105. String currLine = boardLines[i];
  106. String emptiedLine = "";
  107. for (int j=0; j<currLine.length(); j++){
  108. //For every character
  109. Character currentChar = currLine.charAt(j);
  110. if (Character.isDigit(currentChar)){
  111. int numEmpties = Character.getNumericValue(currentChar);
  112. for (; numEmpties>0; numEmpties--){
  113. emptiedLine += "E";
  114. }
  115. }
  116. else {
  117. emptiedLine += currentChar;
  118. }
  119.  
  120. }
  121. boardLines[i] = emptiedLine;
  122. emptiedLine = "";
  123. }
  124. return boardLines;
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement