五子棋优化算法

版权声明:知识共享 https://blog.csdn.net/liyaowen505/article/details/78137788
import java.util.Scanner;
public class 完整五子棋 {
    static char zi[] = { '●', '○' }; // 黑子玩家 9679; 白字玩家 9675
public static void main(String[] args) {
    int arr[][] = new int[12][12];
    printf(arr);
    Scanner input = new Scanner(System.in);
    int x, y;
    int flag = 1; // 下次改哪个玩家下
    int player; // 当前玩家是谁
    do {
        player = flag;
        switch (flag) {
        case 1:
            System.out.println("请 1 玩家(黑子)输入x , y坐标:");
            flag = 2;
            break;
        case 2:
            System.out.println("请 2 玩家(白子)输入x , y坐标:");
            flag = 1;
            break;
        }
        // x = input.nextInt();
        // y = input.nextInt();
        x = (int) (Math.random() * 11 + 1);
        y = (int) (Math.random() * 11 + 1);
        if (x == 0 || y == 0 || x > 11 || y > 11) {
            System.out.println("坐标超出区域,请重新确认!");
            flag = player;
            continue;
        } else if (arr[x][y] != 0) {
            System.out.println("此位置已有棋子,请重新确认!");
            flag = player;
            continue;
        } else if (arr[x][y] == 0)
            arr[x][y] = player;
        printf(arr);
    } while (!isWin(arr, x, y, player));
    System.out.println("玩家" + zi[player - 1] + "赢了");
    input.close();
    return;
}

// 打印棋谱
public static void printf(int a[][]) {
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[0].length; j++) {
            if (i == 0) {
                if (j > 8)
                    System.out.print((j) + "-");
                else
                    System.out.print((j) + "--");
            } else if (j == 0 && i > 0) {
                if (i > 9)
                    System.out.print((i) + ":");
                else
                    System.out.print((i) + ": ");
            } else if (a[i][j] != 0)
                System.out.print(zi[a[i][j] - 1] + "  ");
            else if (a[i][j] == 0)
                System.out.print("-  ");
        }
        System.out.println();
    }
}

/**
* 只判断当前棋子所在的行、列、斜,不扫描棋盘
* @param a
* @param x
* @param y
* @param player
* @return
*/
static boolean isWin(int a[][], int x, int y, int player) {
int row = 0, col = 0, left = 0, right = 0;
if (x == 0 || y == 0 || x > 11 || y > 11) // 出界
return false;
for (int i = 1; i < a.length; i++) {
// 判断所在行
if (a[x][i] == player) {
row++;
if (row == 5)
return true;
} else
row = 0;
// 判断列
if (a[i][y] == player) {
col++;
if (col == 5)
return true;
} else
col = 0;
}
// 判断左斜
int sum1 = x - y;
if (a.length - Math.abs(sum1) - 1 >= 5) { // 第一列是说明,所以去掉1
int i = x < y ? (1 - sum1) : 1; // 左上斜:左下斜
for (int j = 0; j < (a.length - Math.abs(sum1)) - 1; j++) { // 减去一个说明列
if (a[sum1 + i + j][i + j] == player) { // 确定第一个遍历的位置
left++;
if (left == 5)
return true;
} else {
left = 0;
}
}
}
// 判断右斜
int sum2 = x + y;
// 右斜上 个数比sum2少一
if (sum2 <= a.length && (sum2 - 1) >= 5) {
for (int i = 0; i < sum2 - 1; i++) {
if (a[sum2 - 1 - i][i + 1] == player) {
right++;
if (right == 5)
return true;
} else
right = 0;
}
}
// 右斜下,个数+sum2=a.length*2-1
if (sum2 > a.length && (a.length * 2 - 1 - sum2) >= 5) {
for (int i = 0; i < (a.length * 2 - 1 - sum2); i++) {
if (a[a.length - 1 - i][sum2 - (a.length - 1) + i] == player) {
right++;
if (right == 5)
return true;
} else
right = 0;
}
}
return false;
}
}

猜你喜欢

转载自blog.csdn.net/liyaowen505/article/details/78137788