fork download
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.*;
  4.  
  5. class Figure
  6. {
  7. String color;
  8.  
  9. protected Figure(String color) {
  10. this.color = color;
  11. }
  12. public String getColor() {
  13. return this.color;
  14. }
  15. }
  16. class Pawn extends Figure
  17. {
  18. public Pawn(String color) {
  19. super(color);
  20. }
  21. public String toString() {
  22. return color.charAt(0) + "P";
  23. }
  24. }
  25. class Queen extends Figure
  26. {
  27. public Queen(String color) {
  28. super(color);
  29. }
  30. public String toString() {
  31. return color.charAt(0) + "Q";
  32. }
  33. }
  34. class None extends Figure
  35. {
  36. public None() {
  37. super("None");
  38. }
  39. public String toString() {
  40. return " ";
  41. }
  42. }
  43. class BoardRow
  44. {
  45. List<Figure> row = new ArrayList<>();
  46.  
  47. public BoardRow() {
  48. for (int i = 0; i < 10; i++) {
  49. row.add(new None());
  50. }
  51. }
  52. public boolean setFigure(int n, Figure figure) {
  53. if (n < row.size()) {
  54. row.set(n, figure);
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. }
  60. public String checkFigure(int n) {
  61. return "Figure at " + n + " position is " + row.get(n);
  62. }
  63. public Figure getFigure(int n) {
  64. return row.get(n);
  65. }
  66. public String toString() {
  67. String s = "";
  68. for(int n = 0; n < row.size(); n++) {
  69. s += "|" + row.get(n);
  70. }
  71. s = s + "|";
  72. return s;
  73. }
  74. }
  75. class Board
  76. {
  77. BoardRow r = new BoardRow();
  78. List<BoardRow> board = new ArrayList<>();
  79. public Board() {
  80. for (int i = 0; i < 10; i++) {
  81. board.add(new BoardRow());
  82. }
  83. }
  84. public boolean setFigure(Figure f, int x, int y) {
  85. if ((x < board.size()) && (y < board.size())) {
  86. board.get(x).setFigure(y, f);
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }
  92. public String getFigure(int x, int y) {
  93. return "Figure at X" + x + " Y" + y + ": " + board.get(x).row.get(y);
  94. }
  95. public String maxX() {
  96. return "Maximum X value is: " + board.size();
  97. }
  98. public String maxY() {
  99. return "Maximum Y value is: " + r.row.size();
  100. }
  101. public void showBoard() {
  102. System.out.println(" 1 2 3 4 5 6 7 8 9 10");
  103. for(int n = 0; n < board.size(); n++) {
  104. System.out.println(board.get(n));
  105. }
  106. }
  107. }
  108. class App
  109. {
  110. public static void main(String[] args) {
  111. BoardRow boardRow = new BoardRow();
  112. Board b = new Board();
  113. b.setFigure(new Pawn("white"), 0, 0);
  114. b.setFigure(new Queen("black"), 9, 9);
  115. b.setFigure(new Queen("white"), 0, 5);
  116. b.showBoard();
  117. System.out.println(b.getFigure(0, 0));
  118. System.out.println(b.maxX());
  119. System.out.println(b.maxY());
  120. }
  121. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
    1    2    3    4    5    6    7    8    9    10
|wP|  |  |  |  |wQ|  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |bQ|
Figure at X0 Y0: wP
Maximum X value is: 10
Maximum Y value is: 10