上期简单介绍了落子和连珠的判断,本期来讲解如何让电脑下棋,实现人机大战。
一、简介
要想让电脑下棋首先要让电脑知道整个棋盘的棋局,并让电脑判断连珠的情况。因此我们需要将不同情况的棋局描述出来,并赋予一定的权值,这样设置后,当电脑开始下棋的时候就会根据权值在对应的位置下棋。
二、功能介绍
1、数据结构
为了使不同的棋局能够对应不同的权值,因此要引入能够保存键值对的数据结构,在Java语言中,可以使用哈希表来保存这种数据结构。在这种键值对中可以用一个字符串来描述棋局,并用一个int型给定权值。之后创建一个方法,并向哈希表中添加不同棋局。
HashMap<String , Integer> chessWeight = new HashMap<>();
public void weightSet(){
//1为黑棋 , 2为白棋
chessWeight.put("1" , 3);
chessWeight.put("11" , 400);
chessWeight.put("111" , 10000);
chessWeight.put("1111" , 1000000);
chessWeight.put("2" , 3);
chessWeight.put("22" , 400);
chessWeight.put("222" , 10000);
chessWeight.put("2222" , 1000000);
chessWeight.put("11112" , 1000000);
chessWeight.put("1112" , 5000);
chessWeight.put("112" , 20);
chessWeight.put("22221" , 1000000);
chessWeight.put("2221" , 5000);
chessWeight.put("221" , 20);
}
2、权值保存
此时需要新建一个二维数组,在这个数组中需要在棋子的对应位置存入相应的权值,因此首先需要创建一个和棋盘大小一致的数组。此时通过构造方法将棋局数组传入
public newRobot(int[][] arr) {
this.arr = arr;
}
3、棋盘扫描
和棋子连珠判断一样,棋局判断也需要从八个方向开始,只不过在扫描过程中需要将对应的权值存入数组中。关于连珠判断可以阅读上一期内容:(66条消息) Java五子棋(2)_️771的博客-CSDN博客
接下来使代码实现
public void Judge(){
weightSet();
for (int k = 0; k < arr.length; k++) {
for (int l = 0; l < arr[0].length; l++) {
if(arr[k][l] == 0){
String code = "";
int color = 0;
//向右
for (int i = l + 1 ; i < arr[0].length ; i ++){
if(arr[k][i] == 0){
break;
}else {
if(color == 0){
color = arr[k][i];
code += arr[k][i];
}else if(arr[k][i] == color){
code += arr[k][i];
}else {
code += arr[k][i];
break;
}
}
}
Integer value = chessWeight.get(code);
if(value != null){
chessValue[k][l] += value;
}
//向左
code = "";
color = 0;
for(int i = l - 1 ; i >= 0 ; i --){
if(arr[k][i] == 0){
break;
}else {
if(color == 0){
color = arr[k][i];
code += arr[k][i];
}else if(arr[k][i] == color){
code += arr[k][i];
}else {
code += arr[k][i];
break;
}
}
}
Integer value2 = chessWeight.get(code);
if(value2 != null){
chessValue[k][l] += value2;
}
//向上
code = "";
color = 0;
for(int i = k + 1 ; i < arr.length ; i ++){
if(arr[i][l] == 0){
break;
}else {
if(color == 0){
color = arr[i][l];
code += arr[i][l];
}else if(arr[i][l] == color){
code += arr[i][l];
}else {
code += arr[i][l];
break;
}
}
}
Integer value3 = chessWeight.get(code);
if(value3 != null){
chessValue[k][l] += value3;
}
//向下
code = "";
color = 0;
for (int i = k - 1 ; i >= 0 ; i --){
if(arr[i][l] == 0){
break;
}else {
if(color == 0){
color = arr[i][l];
code += arr[i][l];
}else if(arr[i][l] == color){
code += arr[i][l];
}else {
code += arr[i][l];
break;
}
}
}
Integer value4 = chessWeight.get(code);
if(value4 != null){
chessValue[k][l] += value4;
}
//对角线1
code = "";
color = 0;
for (int i = k + 1 , a = l - 1 ; i < arr.length && a >= 0 ; i ++ , a --){
if(arr[i][a] == 0){
break;
}else {
if(color == 0){
color = arr[i][a];
code += arr[i][a];
}else if(arr[i][a] == color){
code += arr[i][a];
}else {
code += arr[i][a];
break;
}
}
}
Integer value5 = chessWeight.get(code);
if(value5 != null){
chessValue[k][l] += value5;
}
//对角线2
for (int j = l + 1 , b = k - 1 ; j < arr.length && b >= 0 ; j ++ , b--){
if(arr[b][j] == 0){
break;
}else {
if(color == 0){
color = arr[b][j];
code += arr[b][j];
}else if(arr[b][j] == color){
code += arr[b][j];
}else {
code += arr[b][j];
break;
}
}
}
Integer value6 = chessWeight.get(code);
if(value6 != null){
chessValue[k][l] += value6;
}
//对角线3
for (int i = l - 1 , a = k - 1 ; i >= 0 && a >= 0 ; i -- , a --){
if(arr[a][i] == 0){
break;
}else {
if(color == 0){
color = arr[a][i];
code += arr[a][i];
}else if(arr[a][i] == color){
code += arr[a][i];
}else {
code += arr[a][i];
break;
}
}
}
Integer value7 = chessWeight.get(code);
if(value7 != null){
chessValue[k][l] += value7;
}
//对角线4
for (int i = l + 1 , b = k + 1 ; i < arr.length && b < arr.length ; i ++ , b ++){
if(arr[b][i] == 0){
break;
}else {
if(color == 0){
color = arr[b][i];
code += arr[b][i];
}else if(arr[b][i] == color){
code += arr[b][i];
}else {
code += arr[b][i];
break;
}
}
}
Integer value8 = chessWeight.get(code);
if(value8 != null){
chessValue[k][l] += value8;
}
}
}
}
}
扫描完毕后,将会在二维数组中留下权值,之后只需要找出最大权值的坐标并返回给画笔即可
public void search(){
Judge();
for (int i = 0; i < chessValue.length; i++) {
for (int j = 0; j < chessValue[0].length; j++) {
if(chessValue[i][j] > maxWeight){
maxWeight = chessValue[i][j];
x = j;
y = i;
}
}
}
}