import java.util.*;
import static java.
lang.
Math.
*;
public class TurnAllPanels {
int[][] board;
int rowMax, columnMax;
public TurnAllPanels(int rowMax, int columnMax) {
this.rowMax = rowMax;
this.columnMax = columnMax;
this.board = new int[rowMax][columnMax];
}
public void turnOver(int row, int column) {
int cur = board[row][column];
board[row][column] = (cur == 0 ? 1 : 0);
}
public void turnPlaceSetting(int row, int column) {
for(int r=-1; r<=1; r++) {
for(int c=-1; c<=1; c++) {
if(row+r < 0) { continue; }
if(rowMax <= row+r) { continue; }
if(column+c < 0) { continue; }
if(columnMax <= column+c) { continue; }
int dist = abs(r) + abs(c);
if(dist <= 1) {
turnOver(row+r, column+c);
}
}
}
}
public void showBoard() {
for(int r=0; r<rowMax; r++) {
for(int c=0; c<columnMax; c++) {
System.
out.
print(board
[r
][c
]); }
}
}
public int checkBoard() {
for(int r=0; r<rowMax; r++) {
for(int c=0; c<columnMax; c++) {
if(board[r][c] == 0) { return 1; }
}
}
return 0;
}
public static void main
(String[] args
) { Scanner keyBoardScanner
= new Scanner
(System.
in); TurnAllPanels self = null;
int r = 0, c = 0;
for( ; ; ) {
try {
System.
out.
print("盤面の横の大きさを指定してください: "); c = keyBoardScanner.nextInt();
if(c < 1) { continue; }
break;
ex.printStackTrace();
keyBoardScanner
= new Scanner
(System.
in); }
}
for( ; ; ) {
try {
System.
out.
print("盤面の縦の大きさを指定してください: "); r = keyBoardScanner.nextInt();
if(r < 1) { continue; }
self = new TurnAllPanels(r, c);
break;
ex.printStackTrace();
keyBoardScanner
= new Scanner
(System.
in); }
}
for( ;self.checkBoard() != 0; ) {
self.showBoard();
System.
out.
print("横座標を指定してください(左端が1): "); try {
int cc = keyBoardScanner.nextInt();
cc--;
if(cc
< 0 || c
<= cc
) { System.
out.
println("不正な値です: " + (cc
+1)); continue; } System.
out.
print("縦座標を指定してください(上端が1): "); int rr = keyBoardScanner.nextInt();
rr--;
if(rr
< 0 || r
<= rr
) { System.
out.
println("不正な値です: " + (rr
+1)); continue; } self.turnPlaceSetting(rr, cc);
ex.printStackTrace();
keyBoardScanner
= new Scanner
(System.
in); }
}
self.showBoard();
System.
out.
println("全て1になりました。ゲームを終了します。"); }
}