fork download
  1. import java.util.*;
  2. import static java.lang.Math.*;
  3.  
  4. public class TurnAllPanels {
  5. int[][] board;
  6. int rowMax, columnMax;
  7.  
  8. public TurnAllPanels(int rowMax, int columnMax) {
  9. this.rowMax = rowMax;
  10. this.columnMax = columnMax;
  11. this.board = new int[rowMax][columnMax];
  12. }
  13.  
  14. public void turnOver(int row, int column) {
  15. int cur = board[row][column];
  16. board[row][column] = (cur == 0 ? 1 : 0);
  17. }
  18.  
  19. public void turnPlaceSetting(int row, int column) {
  20. for(int r=-1; r<=1; r++) {
  21. for(int c=-1; c<=1; c++) {
  22. if(row+r < 0) { continue; }
  23. if(rowMax <= row+r) { continue; }
  24. if(column+c < 0) { continue; }
  25. if(columnMax <= column+c) { continue; }
  26. int dist = abs(r) + abs(c);
  27. if(dist <= 1) {
  28. turnOver(row+r, column+c);
  29. }
  30. }
  31. }
  32. }
  33.  
  34. public void showBoard() {
  35. for(int r=0; r<rowMax; r++) {
  36. for(int c=0; c<columnMax; c++) {
  37. System.out.print(board[r][c]);
  38. }
  39. System.out.println();
  40. }
  41. }
  42.  
  43. public int checkBoard() {
  44. for(int r=0; r<rowMax; r++) {
  45. for(int c=0; c<columnMax; c++) {
  46. if(board[r][c] == 0) { return 1; }
  47. }
  48. }
  49. return 0;
  50. }
  51.  
  52. public static void main(String[] args) {
  53. Scanner keyBoardScanner = new Scanner(System.in);
  54. TurnAllPanels self = null;
  55. int r = 0, c = 0;
  56. for( ; ; ) {
  57. try {
  58. System.out.print("盤面の横の大きさを指定してください: ");
  59. c = keyBoardScanner.nextInt();
  60. if(c < 1) { continue; }
  61. break;
  62. } catch(Exception ex) {
  63. ex.printStackTrace();
  64. keyBoardScanner = new Scanner(System.in);
  65. }
  66. }
  67. for( ; ; ) {
  68. try {
  69. System.out.print("盤面の縦の大きさを指定してください: ");
  70. r = keyBoardScanner.nextInt();
  71. if(r < 1) { continue; }
  72. self = new TurnAllPanels(r, c);
  73. break;
  74. } catch(Exception ex) {
  75. ex.printStackTrace();
  76. keyBoardScanner = new Scanner(System.in);
  77. }
  78. }
  79. for( ;self.checkBoard() != 0; ) {
  80. self.showBoard();
  81. System.out.print("横座標を指定してください(左端が1): ");
  82. try {
  83. int cc = keyBoardScanner.nextInt();
  84. cc--;
  85. if(cc < 0 || c <= cc) { System.out.println("不正な値です: " + (cc+1)); continue; }
  86. System.out.print("縦座標を指定してください(上端が1): ");
  87. int rr = keyBoardScanner.nextInt();
  88. rr--;
  89. if(rr < 0 || r <= rr) { System.out.println("不正な値です: " + (rr+1)); continue; }
  90. self.turnPlaceSetting(rr, cc);
  91. } catch(Exception ex) {
  92. ex.printStackTrace();
  93. keyBoardScanner = new Scanner(System.in);
  94. }
  95. }
  96. self.showBoard();
  97. System.out.println("全て1になりました。ゲームを終了します。");
  98. }
  99. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty