package com.game;
public class AircraftModelTestDriver {
public static void main(String[] args) {
BombAirplaneGame game = new BombAirplaneGame();
game.startGame();
}
}
package com.game;
class AircraftModel {
int ROW = 4;
int COLUMN = 5;
int[] head;
int[][] position = new int[10][2];
public int[] getHead() {
head = new int[]{position[0][0], position[0][1]};
return head;
}
public int[][] directionUp() {
position = NewPosition();
return position;
}
public int[][] directionDown() {
position = makeNegate(NewPosition());
return position;
}
public int[][] directionRight() {
position = makeExchange(NewPosition());
return position;
}
public int[][] directionLeft() {
position = makeExchange(makeNegate(NewPosition()));
return position;
}
public int[][] updateLocation(String location) {
for (int i = 0; i < position.length; i++) {
position[i][0] = position[i][0] + (int) location.charAt(0) - (int) ('0');
position[i][1] = position[i][1] + (int) location.charAt(1) - (int) ('0');
}
return position;
}
public int[][] updateLocation(int[] location) {
for (int i = 0; i < position.length; i++) {
position[i][0] = position[i][0] + location[0];
position[i][1] = position[i][1] + location[1];
}
return position;
}
public int[][] updateLocation(int[] location, int[][] data) {
for (int i = 0; i < data.length; i++) {
data[i][0] = data[i][0] + location[0];
data[i][1] = data[i][1] + location[1];
}
return data;
}
public int[][] getRandomLocation() {
int num = (int) (Math.random() * 4);
switch (num) {
case 0:
position = directionUp();
break;
case 1:
position = directionDown();
break;
case 2:
position = directionLeft();
break;
case 3:
position = directionRight();
break;
}
return position;
}
private int[][] NewPosition() {
return new int[][]{
{2, 3},
{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2},
{2, 1},
{1, 0}, {2, 0}, {3, 0},
};
}
private int[][] makeNegate(int[][] data) {
for (int i = 0; i < data.length; i++) {
data[i][1] = ROW - data[i][1];
}
return data;
}
private int[][] makeExchange(int[][] data) {
for (int i = 0; i < data.length; i++) {
int a = data[i][0];
int b = data[i][1];
data[i][0] = b;
data[i][1] = a;
}
return data;
}
}
package com.game;
import java.util.Scanner;
class BombAirplaneGame {
AircraftModel airmodel1;
AircraftModel airmodel2;
AircraftModel airmodel3;
int SIZE = 10;
int[][] CHECKERBOARD = new int[SIZE][SIZE];
int[][] CHECKERBOARD_GUESS = new int[SIZE][SIZE];
int KILL_NUM = 0;
String DRAWCHESSBOARD_1 = " *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*";
String DRAWCHESSBOARD_2 = " 0 1 2 3 4 5 6 7 8 9 ";
public void startGame() {
airmodel1 = new AircraftModel();
airmodel2 = new AircraftModel();
airmodel3 = new AircraftModel();
do {
checkerboardLayout(airmodel1);
checkerboardLayout(airmodel2);
checkerboardLayout(airmodel3);
} while (!aircraftModelToCheckerBoard(airmodel1, airmodel2, airmodel3));
int n = 1;
while (true) {
printCheckerBoardGuess();
System.out.print("请输入一个 00~99 的位置坐标(" + n + "):");
Scanner input = new Scanner(System.in);
String coordinate = input.next();
n++;
if (writeGuessPosition(coordinate)) {
System.out.flush();
if (KILL_NUM == 3) {
break;
}
}
}
printCheckerBoardGuess();
System.out.println("游戏结束~ 共猜想 " + n + " 次!");
printCheckerBoard();
}
public void checkerboardLayout(AircraftModel airmodel) {
do {
int[] coordinate = getRandomList();
airmodel.updateLocation(coordinate, airmodel.getRandomLocation());
} while (!boundaryJudgment(airmodel.position));
}
public boolean aircraftModelToCheckerBoard(AircraftModel... airmodels) {
int[][] checkerboard = new int[SIZE][SIZE];
for (AircraftModel airmodel : airmodels) {
for (int[] i : airmodel.position) {
if (checkerboard[i[0]][i[1]] == 0) {
checkerboard[i[0]][i[1]] = 1;
} else {
return false;
}
}
checkerboard[airmodel.getHead()[0]][airmodel.getHead()[1]] = 2;
}
CHECKERBOARD = checkerboard;
return true;
}
public boolean boundaryJudgment(int[][] data) {
for (int[] i : data) {
try {
int j = CHECKERBOARD[i[0]][i[1]];
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
}
return true;
}
public boolean writeGuessPosition(String coordinate) {
int y = (int) coordinate.charAt(0) - (int) ('0');
int x = (int) coordinate.charAt(1) - (int) ('0');
try {
if (CHECKERBOARD_GUESS[x][y] != 0) {
System.out.println("重复坐标!请重新输出!");
return false;
}
switch (CHECKERBOARD[x][y]) {
case 0:
CHECKERBOARD_GUESS[x][y] = 1;
break;
case 1:
CHECKERBOARD_GUESS[x][y] = 2;
break;
case 2:
CHECKERBOARD_GUESS[x][y] = 3;
KILL_NUM++;
break;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("坐标异常!请重新输出!");
return false;
}
return true;
}
public int[] getRandomList() {
int[] nums = new int[2];
nums[0] = (int) (Math.random() * 10);
nums[1] = (int) (Math.random() * 10);
return nums;
}
public void printCheckerBoard() {
System.out.println(DRAWCHESSBOARD_1);
for (int i = CHECKERBOARD.length - 1; i >= 0; i--) {
StringBuilder printText = new StringBuilder(i + " *");
for (int j = 0; j < CHECKERBOARD[i].length; j++) {
switch (CHECKERBOARD[i][j]) {
case 0:
printText.append(" |");
break;
case 1:
printText.append(" ● |");
break;
case 2:
printText.append(" ✪ |");
break;
}
}
System.out.println(printText);
System.out.println(DRAWCHESSBOARD_1);
}
System.out.println(DRAWCHESSBOARD_2);
}
public void printCheckerBoardGuess() {
System.out.println(DRAWCHESSBOARD_1);
for (int i = CHECKERBOARD_GUESS.length - 1; i >= 0; i--) {
StringBuilder printText = new StringBuilder(i + " *");
for (int j = 0; j < CHECKERBOARD_GUESS[i].length; j++) {
switch (CHECKERBOARD_GUESS[i][j]) {
case 0:
printText.append(" |");
break;
case 1:
printText.append(" △ |");
break;
case 2:
printText.append(" ● |");
break;
case 3:
printText.append(" ✪ |");
break;
}
}
System.out.println(printText);
System.out.println(DRAWCHESSBOARD_1);
}
System.out.println(DRAWCHESSBOARD_2);
}
}