五子棋算法(未加入Ai)
程序员文章站
2022-05-25 14:22:44
...
基本原理如下
从落子点开始向下匹配(最多4次)与其值相同的元素,每匹配一个相同元素count++(初始值为1)
若不相同,开始向上匹配
匹配结束count>=5则说明至少五子相连
代码如下:
package test000;
import java.util.Scanner;
public class Test0001 {
public static void main(String[] args) {
//棋盘长度
int length = 20;
//创建棋盘
String[][] goBang = new String[length][length];
//棋盘下标
String[] nums = new String[] {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
//棋盘素材
String add = "╋";
String black = "·";
String white = "○";
//初始化数据
for (int i = 0; i < goBang.length; i++) {
for (int j = 0; j < goBang[i].length; j++) {
if (j==length-1) {
goBang[i][j] = nums[i];
}else if (i==length-1) {
goBang[i][j] = nums[j];
}else {
goBang[i][j] = add;
}
}
}
//打印棋盘
for (String[] strings : goBang) {
for (String string : strings) {
System.out.print(string);
}
System.out.println();
}
boolean isBlack = true;//黑白手 黑true,白false,在while最后置反
Scanner scanner = new Scanner(System.in);
String getPut;//获取输入
int x,y;
//落子
while(true) {
System.out.println("请输入"+(isBlack?"黑":"白")+"子落子坐标(x,y):");
getPut = scanner.next();//用户输入
String[] getInput = getPut.split(",");//获取分割后的字符串数组
x = Integer.parseInt(getInput[0]);//string转int
y = Integer.parseInt(getInput[1]);
//落子
//输入越界
if ((x<1||x>length-1)||(y<1||y>length-1)) {
System.out.println("输入越界");
continue;
}
//已有棋子
if (goBang[y-1][x-1]!=add) {
System.out.println("已有棋子");
continue;
}
goBang[y-1][x-1] = isBlack?black:white;
//打印棋盘
for (String[] strings : goBang) {
for (String string : strings) {
System.out.print(string);
}
System.out.println();
}
//判断输赢
if (isWin(goBang,x,y)) {
System.out.println((isBlack?"黑":"白")+"棋赢了...");
break;
}
//置反
isBlack = !isBlack;
}
scanner.close();
}
//判断是否五子相连
public static boolean isWin(String[][] goBang,int x,int y) {
//代表goBang[y-1][x-1]
boolean flag =false;
if (left_right(goBang, x, y)||UpAndDown(goBang, x, y)||TopleftAndDownrihgt(goBang, x, y)||DownleftAndTopright(goBang, x, y)) {
return true;
}
return flag;
}
//左右棋子的判断
public static boolean left_right(String[][] goBang,int x,int y) {
//原本有一个,从一开始
int count =1;
String judge = goBang[y-1][x-1];
//左右寻找
//往左
for (int i = 1; i < 5; i++) {
//保证不越界
if (x-1-i<0) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1][x-1-i]!=judge) {
break;
}
if (goBang[y-1][x-1-i]==judge) {
count++;
//System.out.println("count="+count);//测试用
continue;
}
}
//往右
for (int i = 1; i < 5; i++) {
//保证不越界
if (x-1+i>19) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1][x-1+i]!=judge) {
break;
}
if (goBang[y-1][x-1+i]==judge) {
count++;
//System.out.println("count="+count);//测试用
continue;
}
}
if (count>=5) {
return true;
}
return false;
}
//上下棋子的判段
public static boolean UpAndDown(String[][] goBang,int x,int y) {
//原本有一个,从一开始
int count =1;
String judge = goBang[y-1][x-1];
//上下寻找
//往上
for (int i = 1; i < 5; i++) {
//保证不越界
if (y-1-i<0) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1-i][x-1]!=judge) {
break;
}
if (goBang[y-1-i][x-1]==judge) {
count++;
//System.out.println("count="+count);
continue;
}
}
//往下
for (int i = 1; i < 5; i++) {
//保证不越界
if (y-1+i>19) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1+i][x-1]!=judge) {
break;
}
if (goBang[y-1+i][x-1]==judge) {
count++;
//System.out.println("count="+count);
continue;
}
}
if (count>=5) {
return true;
}
return false;
}
//左上与右下棋子的判断
public static boolean TopleftAndDownrihgt(String[][] goBang,int x,int y) {
//原本有一个,从一开始
int count =1;
String judge = goBang[y-1][x-1];
//左上与右下寻找
//往左上
for (int i = 1; i < 5; i++) {
//保证不越界
if (y-1-i<0||x-1-i<0) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1-i][x-1-i]!=judge) {
break;
}
if (goBang[y-1-i][x-1-i]==judge) {
count++;
//System.out.println("count="+count);
continue;
}
}
//往右下
for (int i = 1; i < 5; i++) {
//保证不越界
if (y-1+i>19||x-1+i>19) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1+i][x-1+i]!=judge) {
break;
}
if (goBang[y-1+i][x-1+i]==judge) {
count++;
//System.out.println("count="+count);
continue;
}
}
if (count>=5) {
return true;
}
return false;
}
//左下与右上棋子的判断
public static boolean DownleftAndTopright(String[][] goBang,int x,int y) {
//原本有一个,从一开始
int count =1;
String judge = goBang[y-1][x-1];
//左下与右上寻找
//往左下
for (int i = 1; i < 5; i++) {
//保证不越界
if (y-1+i>19||x-1-i<0) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1+i][x-1-i]!=judge) {
break;
}
if (goBang[y-1+i][x-1-i]==judge) {
count++;
//System.out.println("count="+count);
continue;
}
}
//往右上
for (int i = 1; i < 5; i++) {
//保证不越界
if (y-1-i<0||x-1+i>19) {
break;
}
//判断是否与需要判断的棋子相同
if (goBang[y-1-i][x-1+i]!=judge) {
break;
}
if (goBang[y-1-i][x-1+i]==judge) {
count++;
//System.out.println("count="+count);
continue;
}
}
if (count>=5) {
return true;
}
return false;
}
}
上一篇: 利用java数组做了一个五子棋对战程序
下一篇: 慕课软件质量保证与测试(习题集)