五子棋优化算法
来源:互联网 发布:网络流行歌曲2016 编辑:程序博客网 时间:2024/06/14 06:00
import java.util.Scanner;public class 完整五子棋 { static char zi[] = { '●', '○' }; // 黑子玩家 9679; 白字玩家 9675public 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;
}
}
- 五子棋优化算法
- 【无优化】五子棋ai算法
- 五子棋算法
- 五子棋算法
- 五子棋算法
- 五子棋算法
- 五子棋算法
- 五子棋算法
- 五子棋算法
- 五子棋算法 + 五子棋AI 估价算法
- 五子棋的核心算法
- 五子棋算法设计随想
- 五子棋的核心算法
- 五子棋的核心算法
- C语言五子棋算法
- 五子棋算法探讨
- 五子棋的核心算法
- 五子棋的核心算法
- MySQL之DML语句(上)
- 梯度下降法的三种形式BGD、SGD以及MBGD
- BZOJ 2115 Xor 线性基介绍(高斯消元 xor线性基)
- AJAX提交到Handler.ashx一般处理程序返回json数据 (字符串拼接方式)
- 11、C#里面随机数的获取、取最大数的实例
- 五子棋优化算法
- mysql基本操作
- 感知与记忆
- iOS资源大全中文版
- Django 安装
- Java面向对象的多态案例
- mysql中geometry类型的简单使用
- 各种进制转换成十进制
- 电池曲线绘制算法