展开全部
import java.util.Scanner;
public class FiveChessGame {
public static void display(char[][] arr) {// 图形显示函数
System.out.print(" ");
for (char i = '0'; i <= '9'; i++)
// 打印行号0~9
System.out.print(i + " ");
for (char j = 'a'; j <= 'f'; j++)
// 打印行号a~f
System.out.print(j + " ");
System.out.println();
char c = '0';
for (int i = 0; i
System.out.print(c++ + " ");
for (int j = 0; j
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
c = 'a';
for (int i = 10; i
System.out.print(c++ + " ");
for (int j = 0; j
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static int getNum(char c) {
int num;
switch (c) {
case '0':
num = 0;
break;
case '1':
num = 1;
break;
case '2':
num = 2;
break;
case '3':
num = 3;
break;
case '4':
num = 4;
break;
case '5':
num = 5;
break;
case '6':
num = 6;
break;
case '7':
num = 7;
break;
case '8':
num = 8;
break;
case '9':
num = 9;
break;
case 'a':
case 'A':
num = 10;
break;
case 'b':
case 'B':
num = 11;
break;
case 'c':
case 'C':
num = 12;
break;
case 'd':
case 'D':
num = 13;
break;
case 'e':
case 'E':
num = 14;
break;
case 'f':
case 'F':
num = 15;
break;
default:
System.out.println("输入有错误!");
return -1;
}
return num;
}
public static int isWinner(char c, char[][] arr, int a, int b) {
if (c == '@') {
int count = 0;
for (int i = 0; i
if (arr[a][i] == '@') {
count++;
if (count >= 5) {
return 1;
}
} else {
count = 0;
}
}
count = 0;
for (int i = 0; i
if (arr[i][b] == '@') {
count++;
if (count >= 5) {
return 1;
}
} else {
count = 0;
}
}
count = 0;
if (a >= b) {
int j = 0;
for (int i = a - b; i <= 15 - a + b; i++) {
if (arr[i][j] == '@') {
count++;
j++;
if (count >= 5) {
return 1;
}
} else {
count = 0;
j++;
}
}
} else {
int j = 0;
for (int i = b - a; i <= 15 - b + a; i++) {
if (arr[j][i] == '@') {
count++;
j++;
if (count >= 5) {
return 1;
}
} else {
count = 0;
j++;
}
}
}
int j = a + b;
count = 0;
if (a + b <= 15) {
for (int i = 0; i <= a + b; i++) {
if (arr[i][j] == '@') {
count++;
j--;
if (count >= 5) {
return 1;
}
} else {
count = 0;
j--;
}
}
} else {
j = 15;
for (int i = a + b - 15; i <= 15; i++) {
if (arr[i][j] == '@') {
count++;
j--;
if (count >= 5) {
return 1;
}
} else {
count = 0;
j--;
}
}
}
} else {// 判断白色胜
int count = 0;
for (int i = 0; i
if (arr[a][i] == 'O') {
count++;
if (count >= 5) {
return 2;
}
} else {
count = 0;
}
}
count = 0;
for (int i = 0; i
if (arr[i][b] == 'O') {
count++;
if (count >= 5) {
return 2;
}
} else {
count = 0;
}
}
count = 0;
if (a >= b) {
int j = 0;
for (int i = a - b; i <= 15 - a + b; i++) {
if (arr[i][j] == 'O') {
count++;
j++;
if (count >= 5) {
return 2;
}
} else {
count = 0;
j++;
}
}
} else {
int j = 0;
for (int i = b - a; i <= 15 - b + a; i++) {
if (arr[j][i] == 'O') {
count++;
j++;
if (count >= 5) {
return 2;
}
} else {
count = 0;
j++;
}
}
}
int j = a + b;
count = 0;
if (a + b <= 15) {
for (int i = 0; i <= a + b; i++) {
if (arr[i][j] == 'O') {
count++;
j--;
if (count >= 5) {
return 2;
}
} else {
count = 0;
j--;
}
}
} else {
j = 15;
for (int i = a + b - 15; i <= 15; i++) {
if (arr[i][j] == 'O') {
count++;
j--;
if (count >= 5) {
return 2;
}
} else {
count = 0;
j--;
}
}
}
}
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
char[][] arr = new char[16][16];
for (int i = 0; i
// 二维数组初始化
for (int j = 0; j
arr[i][j] = '.';
display(arr);
int i = 0;
while (true) {
if (i % 2 == 0) {
System.out.println("请黑方落子32313133353236313431303231363533e58685e5aeb931333335333762:");
String in = sc.next();
char c1 = in.charAt(0);
char c2 = in.charAt(1);
int a = getNum(c1);
int b = getNum(c2);
if (arr[a][b] != '.') {
System.out.println("该位置已经有棋子,请重新输入!");
} else {
arr[a][b] = '@';
display(arr);
int result = isWinner('@', arr, a, b);
if (result == 1) {
System.out.println("黑方获胜!");
return;
}
i++;
}
} else {
System.out.println("请白方落子:");
String in = sc.next();
char c1 = in.charAt(0);
char c2 = in.charAt(1);
int a = getNum(c1);
int b = getNum(c2);
if (arr[a][b] != '.') {
System.out.println("该位置已经有棋子,请重新输入!");
} else {
arr[a][b] = 'O';
display(arr);
int result = isWinner('O', arr, a, b);
if (result == 2) {
System.out.println("白方获胜!");
return;
}
i++;
}
}
}
}
}