暴力解法:遍历棋盘,寻找所有获胜的方式,有则中止游戏。
import java.util.*;
public class WuZiQi {
public static void main(String[] args) {
int[][] game = new int[10][10];
boolean isSuccess = false;
int role = 1; //2为白,1为黑
int step = 1;
int row;
int col;
Scanner scanner = new Scanner(System.in);
while(!isSuccess) {
if(role == 1) {
System.out.println("------------------------------------------------------------------------------------");
System.out.println("执棋方:黑方\t\t\t\t\t\t\t\t\t第" + step + "步");
System.out.println("\tC1\tC2\tC3\tC4\tC5\tC6\tC7\tC8\tC9\tC10\t");
System.out.println();
} else {
System.out.println("------------------------------------------------------------------------------------");
System.out.println("执棋方:白方\t\t\t\t\t\t\t\t\t第" + step + "步");
System.out.println("\tC1\tC2\tC3\tC4\tC5\tC6\tC7\tC8\tC9\tC10\t");
System.out.println();
}
for(int i = 0; i < game.length; i++) {
System.out.print("R" + (i+1) + "\t");
for(int j =0; j < game[i].length; j++) {
System.out.print(game[i][j] + "\t");
}
System.out.println();
System.out.println();
}
if(role == 1) {
System.out.print("黑方请输入落子行号:");
row = scanner.nextInt() - 1;
System.out.print("黑方请输入落子列号:");
col = scanner.nextInt() - 1;
} else {
System.out.print("白方请输入落子行号:");
row = scanner.nextInt() - 1;
System.out.print("白方请输入落子列号:");
col = scanner.nextInt() - 1;
}
if((row >= 0) && (row < game.length) && (col >= 0) && (col < game[0].length)) {
if(game[row][col] == 0) {
game[row][col] = role;
} else {
System.out.println("落子位置已有棋子,请重新选择落子位置!");
System.out.println();
continue;
}
} else {
System.out.println("输入的数据不在棋盘范围内,请重新输入!");
System.out.println();
continue;
}
/*
判断横行胜利
1 1 1 1 1
*/
for(int i = 0; i < game.length; i++) {
for(int j = 0; j <= game[i].length - 5; j++) {
if(game[i][j] != 0 && game[i][j] == game[i][j+1] && game[i][j+1] == game[i][j+2] && game[i][j+2] ==game[i][j+3] && game[i][j+3] == game[i][j+4] ) {
isSuccess = true;
}
}
}
/*
判断竖列胜利
1
1
1
1
1
*/
for(int i = 0; i <= game.length - 5; i++) {
for(int j = 0; j < game[i].length; j++) {
if(game[i][j] != 0 && game[i][j] == game[i+1][j] && game[i+1][j] == game[i+2][j] && game[i+2][j] ==game[i+3][j] && game[i+3][j] == game[i+4][j] ) {
isSuccess = true;
}
}
}
/*
判断左斜胜利
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
*/
for(int i = 0; i <= game.length - 5; i++) {
for(int j = 4; j < game[i].length; j++) {
if(game[i][j] != 0 && game[i][j] == game[i+1][j-1] && game[i+1][j-1] == game[i+2][j-2] && game[i+2][j-2] ==game[i+3][j-3] && game[i+3][j-3] == game[i+4][j-4] ) {
isSuccess = true;
}
}
}
/*
判断右斜胜利
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
*/
for(int i = 0; i <= game.length - 5; i++) {
for(int j = 0; j <= game[i].length - 5; j++) {
if(game[i][j] != 0 && game[i][j] == game[i+1][j+1] && game[i+1][j+1] == game[i+2][j+2] && game[i+2][j+2] ==game[i+3][j+3] && game[i+3][j+3] == game[i+4][j+4] ) {
isSuccess = true;
}
}
}
role = 3 - role;
step++;
}
role = 3 - role;
System.out.println("------------------------------------------------------------------------------------");
System.out.println("\tC1\tC2\tC3\tC4\tC5\tC6\tC7\tC8\tC9\tC10\t");
for(int i = 0; i < game.length; i++) {
System.out.print("R" + (i+1) + "\t");
for(int j =0; j < game[i].length; j++) {
System.out.print(game[i][j] + "\t");
}
System.out.println();
System.out.println();
}
if(role == 1) {
System.out.println("黑方获胜!");
} else {
System.out.println("白方获胜!");
}
}
}
另一种解法:检测到下子附近有超过4字连线即胜利。
import java.util.*;
public class WuZiQiTwo {
public static void main(String[] args) {
int[][] game = new int[10][10];
boolean isSuccess = false;
int role = 1; //2为白,1为黑
int step = 1;
int row;
int col;
Scanner scanner = new Scanner(System.in);
while(!isSuccess) {
if(role == 1) {
System.out.println("------------------------------------------------------------------------------------");
System.out.println("执棋方:黑方\t\t\t\t\t\t\t\t\t第" + step + "步");
System.out.println("\tC1\tC2\tC3\tC4\tC5\tC6\tC7\tC8\tC9\tC10\t");
System.out.println();
} else {
System.out.println("------------------------------------------------------------------------------------");
System.out.println("执棋方:白方\t\t\t\t\t\t\t\t\t第" + step + "步");
System.out.println("\tC1\tC2\tC3\tC4\tC5\tC6\tC7\tC8\tC9\tC10\t");
System.out.println();
}
for(int i = 0; i < game.length; i++) {
System.out.print("R" + (i+1) + "\t");
for(int j =0; j < game[i].length; j++) {
System.out.print(game[i][j] + "\t");
}
System.out.println();
System.out.println();
}
if(role == 1) {
System.out.print("黑方请输入落子行号:");
row = scanner.nextInt() - 1;
System.out.print("黑方请输入落子列号:");
col = scanner.nextInt() - 1;
} else {
System.out.print("白方请输入落子行号:");
row = scanner.nextInt() - 1;
System.out.print("白方请输入落子列号:");
col = scanner.nextInt() - 1;
}
if((row >= 0) && (row < game.length) && (col >= 0) && (col < game[0].length)) {
if(game[row][col] == 0) {
game[row][col] = role;
} else {
System.out.println("落子位置已有棋子,请重新选择落子位置!");
System.out.println();
continue;
}
} else {
System.out.println("输入的数据不在棋盘范围内,请重新输入!");
System.out.println();
continue;
}
int sum = 0;
/*
判断横行胜利
1 1 1 1 1
*/
for(int i = col - 1,st = 0; i >= 0; i--) {
if(game[row][i] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
for(int i = col + 1,st = 0; i < game[row].length; i++) {
if(game[row][i] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
if(sum >= 4) {
if(role == 1) {
System.out.println("黑方横行获胜!");
} else {
System.out.println("白方横行获胜!");
}
break;
}else {
sum = 0;
}
/*
判断竖列胜利
1
1
1
1
1
*/
for(int i = row - 1,st = 0; i >= 0; i--) {
if(game[i][col] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
for(int i = row + 1,st = 0; i < game.length; i++) {
if(game[i][col] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
if(sum >= 4) {
if(role == 1) {
System.out.println("黑方竖列获胜!");
} else {
System.out.println("白方竖列获胜!");
}
break;
}else {
sum = 0;
}
/*
判断左斜胜利
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
*/
for(int i = row - 1,j = col + 1,st = 0; i >= 0 && j < game[i].length; i--,j++) {
if(game[i][j] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
for(int i = row + 1,j = col - 1,st = 0; i < game.length && j >= 0; i++,j--) {
if(game[i][j] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
if(sum >= 4) {
if(role == 1) {
System.out.println("黑方左斜获胜!");
} else {
System.out.println("白方左斜获胜!");
}
break;
}else {
sum = 0;
}
/*
判断右斜胜利
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
*/
for(int i = row - 1,j = col - 1,st = 0; i >= 0 && j >= 0; i--,j--) {
if(game[i][j] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
for(int i = row + 1,j = col + 1,st = 0; i < game.length && j < game[i].length; i++,j++) {
if(game[i][j] == game[row][col]) {
st++;
sum += step;
} else {
break;
}
}
if(sum >= 4) {
if(role == 1) {
System.out.println("黑方右斜获胜!");
} else {
System.out.println("白方右斜获胜!");
}
break;
}else {
sum = 0;
}
role = 3 -role;
}
}
}