因为我刚开始系统性地学习java,所以程序写的很简陋,这实例也是在培训时由老师教导着写的,当然有部分功能是我自己写的,写的很初级,勉强够看。
这个博客我个人只是拿来用作记录我学习java的过程,所以基本上是不回去看评论的。
以下是源代码:
import java.util.Scanner;
public class wuziqi {
public static void main(String[] args) {
char[] bianhao = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[][] chess = new char[15][15];
int sum = 0, count = 0;
int i = 0, j = 0;
for (i = 0; i < chess.length; i++) {
for (j = 0; j < chess[i].length; j++) {
chess[i][j] = '+';
}
}
chess[3][3] = chess[3][7] = chess[3][11] = '#';
chess[7][3] = chess[7][7] = chess[7][11] = '#';
chess[11][3] = chess[11][7] = chess[11][11] = '#';
// qipan[3][4]=qipan[3][5]=qipan[3][6]=qipan[3][7]=qipan[3][8]='0';
while (true) {
System.out.print(" ");
for (int k = 0; k < bianhao.length; k++) {
System.out.print(bianhao[k] + " ");
}
System.out.println();
for (i = 0; i < chess.length; i++) {
System.out.print(bianhao[i] + " ");
for (j = 0; j < chess[i].length; j++) {
System.out.print(chess[i][j] + " ");
}
System.out.println();
}
Scanner scanner = new Scanner(System.in);// 创建屏幕输入对象
System.out.print("请输入Y坐标:");
int y = scanner.nextInt();
if (y > 14 || y < 0) {
System.out.println("请输入正确的Y坐标(0~14)");
scanner.nextLine();
scanner.nextLine();
continue;
}
System.out.print("请输入X坐标:");
int x = scanner.nextInt();
if (y > 14 || y < 0) {
System.out.println("请输入正确的X坐标(0~14)");
scanner.nextLine();
scanner.nextLine();
continue;
}
if (chess[y][x] == '@' || chess[y][x] == '0') {
System.out.println("该坐标已经有子,请重新下子!");
scanner.nextLine();
scanner.nextLine();
continue;
}
// 棋子颜色不用判断
count++;
if (count % 2 == 1) {
chess[y][x] = '@';
} else {
chess[y][x] = 'O';
} // count判断下棋顺序
int x1 = x;
int y1 = y;
while (true) {
x1++;
if (x1 > 14) {
break;
}
char value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
x1 = x;
y1 = y;
while (true) {
x1--;
if (x1 < 0) {
break;
}
int value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
System.out.println("该方胜利!");
break;
}
sum = 0;
x1 = x;
y1 = y;
while (true) {
y1++;
if (y1 > 14) {
break;
}
char value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
x1 = x;
y1 = y;
while (true) {
y1--;
if (y1 < 0) {
break;
}
int value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
System.out.println("该方胜利!");
break;
}
sum = 0;
x1 = x;
y1 = y;
while (true) {
y1++;
x1--;
if (y1 > 14 || x1 < 0) {
break;
}
char value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
x1 = x;
y1 = y;
while (true) {
y1--;
x1++;
if (y1 < 0 || x1 > 14) {
break;
}
int value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
System.out.println("该方胜利!");
break;
}
sum = 0;
x1 = x;
y1 = y;
while (true) {
y1++;
x1++;
if (y1 > 14 || x1 > 14) {
break;
}
char value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
x1 = x;
y1 = y;
while (true) {
y1--;
x1--;
if (y1 < 0 || x1 < 0) {
break;
}
int value = chess[y1][x1];
if (value == chess[y][x]) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
System.out.println("该方胜利!");
break;
}
}
}
}