import java.util.Scanner; import java.util.Random; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rng = new Random(); int game = 0; int t = 1; int [][] map = { {0,41,51,31,11,21,31,51,41}, {1,61,61,61,61,61,61,61,61}, {2,10,10,10,10,10,10,10,10}, {3,10,10,10,10,10,10,10,10}, {4,10,10,10,10,10,10,10,10}, {5,10,10,10,10,10,10,10,10}, {6,62,62,62,62,62,62,62,62}, {7,42,52,32,22,12,32,52,42}, {10,101,102,103,104,105,106,107,108} }; while (game == 0) { for (int i = 0; i < 50; i ++) System.out.println(""); for (int y = 0; y < 9; y++) { for (int x = 0; x < 9; x++) { if (map [y][x] == 41) System.out.print("♜ "); else if (map [y][x] == 51) System.out.print("♞ "); else if (map [y][x] == 31) System.out.print("♝ "); else if (map [y][x] == 21) System.out.print("♛ "); else if (map [y][x] == 11) System.out.print("♚ "); else if (map [y][x] == 61) System.out.print("♟ "); else if (map [y][x] == 42) System.out.print("♖ "); else if (map [y][x] == 52) System.out.print("♘ "); else if (map [y][x] == 32) System.out.print("♗ "); else if (map [y][x] == 22) System.out.print("♕ "); else if (map [y][x] == 12) System.out.print("♔ "); else if (map [y][x] == 62) System.out.print("♙ "); else if (map [y][x] >= 101 && map [y][x] <= 108) System.out.print((map[y][x]-100) + " "); else if (map [y][x] == 0) System.out.print("0 "); else if (map [y][x] == 10) System.out.print(" "); else System.out.print(map[y][x] + " "); } System.out.println(""); } if (t == 1) { System.out.println("プレイヤーのターンです"); System.out.println("♔キング:全方向1マス | ♕クイーン:全方向どこまでも | ♖ルーク:縦横どこまでも | ♗ビショップ:斜めどこまでも | ♘ナイト:L字型に跳び越す | ♙ポーン:前1マス(初手2マス、取る時は斜め前)"); System.out.println("3 1 4 1のように動かしたい駒の横ます目 空白 縦ます目 空白 移動先横ます目 空白 移動先縦ます目という風に入力してください(英数で)"); int playerY1 = in.nextInt(); int playerX1 = in.nextInt(); int playerY2 = in.nextInt(); int playerX2 = in.nextInt(); if (playerY1 < 0 || playerY1 > 8 || playerX1 < 0 || playerX1 > 8 || playerY2 < 0 || playerY2 > 8 || playerX2 < 0 || playerX2 > 8) continue; int selectPiece = map[playerY1][playerX1]; if (selectPiece != 12 && selectPiece != 22 && selectPiece != 32 && selectPiece != 42 && selectPiece != 52 && selectPiece != 62) continue; int targetSquare = map[playerY2][playerX2]; if (targetSquare == 12 || targetSquare == 22 || targetSquare == 32 || targetSquare == 42 || targetSquare == 52 || targetSquare == 62 || targetSquare <= 8 || targetSquare >= 101) continue; int diffY = Math.abs(playerY2 - playerY1); int diffX = Math.abs(playerX2 - playerX1); if (selectPiece == 12 && (diffY > 1 || diffX > 1 || (diffY == 0 && diffX == 0))) continue; else if (selectPiece == 22 && (diffY != diffX && diffY != 0 && diffX != 0)) continue; else if (selectPiece == 32 && (diffY != diffX || diffY == 0)) continue; else if (selectPiece == 42 && (diffY != 0 && diffX != 0)) continue; else if (selectPiece == 52 && !((diffY == 2 && diffX == 1) || (diffY == 1 && diffX == 2))) continue; else if (selectPiece == 62 && (diffX != 0 || playerY2 != playerY1 - 1)) continue; boolean isPathClear = true; int stepY = (playerY2 > playerY1) ? 1 : (playerY2 < playerY1 ? -1 : 0); int stepX = (playerX2 > playerX1) ? 1 : (playerX2 < playerX1 ? -1 : 0); if (selectPiece != 52) { int checkY = playerY1 + stepY; int checkX = playerX1 + stepX; while (checkY != playerY2 || checkX != playerX2) { if (map[checkY][checkX] != 10) { isPathClear = false; break; } checkY += stepY; checkX += stepX; } } if (!isPathClear) continue; map[playerY2][playerX2] = map[playerY1][playerX1]; map[playerY1][playerX1] = 10; t = 2;
// 【チェスプログラム 後半】 } else if (t == 2) { System.out.println("CPUの番です。考え中..."); try { Thread.sleep(1000); } catch (InterruptedException e) {} boolean moved = false; while (!moved) { int cy1 = rng.nextInt(8); int cx1 = rng.nextInt(8) + 1; int selectPiece = map[cy1][cx1]; if (selectPiece % 10 != 1) continue; int cy2 = rng.nextInt(8); int cx2 = rng.nextInt(8) + 1; int targetSquare = map[cy2][cx2]; if (targetSquare % 10 == 1) continue; int cdy = Math.abs(cy2 - cy1); int cdx = Math.abs(cx2 - cx1); if (selectPiece == 11 && (cdy > 1 || cdx > 1 || (cdy == 0 && cdx == 0))) continue; else if (selectPiece == 21 && (cdy != cdx && cdy != 0 && cdx != 0)) continue; else if (selectPiece == 31 && (cdy != cdx || cdy == 0)) continue; else if (selectPiece == 41 && (cdy != 0 && cdx != 0)) continue; else if (selectPiece == 51 && !((cdy == 2 && cdx == 1) || (cdy == 1 && cdx == 2))) continue; else if (selectPiece == 61 && (cdx != 0 || cy2 != cy1 + 1)) continue; boolean cIsPathClear = true; int cStepY = (cy2 > cy1) ? 1 : (cy2 < cy1 ? -1 : 0); int cStepX = (cx2 > cx1) ? 1 : (cx2 < cx1 ? -1 : 0); if (selectPiece != 51) { int cCheckY = cy1 + cStepY; int cCheckX = cx1 + cStepX; while (cCheckY != cy2 || cCheckX != cx2) { if (map[cCheckY][cCheckX] != 10) { cIsPathClear = false; break; } cCheckY += cStepY; cCheckX += cStepX; } } if (!cIsPathClear) continue; map[cy1][cx1] = 10; map[cy2][cx2] = selectPiece; System.out.println("CPUが駒を動かしました"); moved = true; t = 1; } } } } }