代码如下:
public static void main(String[] args) {
int len = 20;
String [][]goBang = new String [len][len];
String add = "┼";
String []nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
//初始化棋盘
for(int i=0;i<goBang.length;i++)
{
for(int j=0;j<goBang[i].length;j++)
{
if(j==len-1)//每一行的最后一列
{
goBang[i][j] = nums[i];
}
else if(i==len-1)//每一列的最后一行
{
goBang[i][j] = nums[j];
}
else {
goBang[i][j] = add;
}
}
}
//打印棋盘
for(String []array:goBang)
{
for(String element:array)
{
System.out.print(element);
}
System.out.println();
}
Scanner sc = new Scanner(System.in);
//判断黑子还是白子
boolean flag = true;//true为黑子 false为白子
boolean bool = true;
String black = "●";
String white = "○";
while(bool)
{
System.out.println("请"+((flag)?"黑":"白")+"子输入坐标:");
int x =sc.nextInt()-1;
int y =sc.nextInt()-1;
if(x<0||x>len-2||y<0||y>len-2)
{
System.out.println("坐标超出范围,请重新输入");
continue;
}
//判断坐标轴是否有棋子
if(!goBang[x][y].equals(add))
{
System.out.println("该位置已经有棋子");
continue;
}
String pieces = (flag)?black:white;
goBang[x][y] = pieces;
//打印棋盘
for(String []ss:goBang)
{
for(String element:ss)
{
System.out.print(element);
}
System.out.println();
}
int countLeftAndRight = right(goBang,x,y,pieces)+left(goBang, x, y, pieces) + 1;//左右
int countTopAndBottom = top(goBang,x,y,pieces)+bottom(goBang, x, y, pieces)+1;//上下
int countTopRightAndBottomLeft = topRight(goBang,x,y,pieces)+bottomLeft(goBang, x, y, pieces)+1;//右上和左下
int countTopLeftAndBottomRigth = topLeft(goBang,x,y,pieces)+bottomRight(goBang, x, y, pieces)+1;//右下和左上
if(countLeftAndRight>=5||countTopAndBottom>=5||countTopRightAndBottomLeft>=5||countTopLeftAndBottomRigth>=5)
{
System.out.print(((flag)?"黑":"白") +"方赢");
break;
}
flag = !flag;
}
}
//判断右边获胜的条件
public static int right(String goBang[][],int x ,int y,String pieces)
{
int count = 0;
while(y<18)
{
y++;
if(goBang[x][y].equals(pieces))
{
count++;
}
else {
break;
}
}
return count;
}
//判断左边连续的棋子数
public static int left(String goBang[][],int x ,int y,String pieces)
{
int count = 0;
while(y>0)
{
y--;
if(goBang[x][y].equals(pieces))
{
count++;
}
else
{
break;
}
}
return count;
}
//判断上面的棋子数
public static int top(String goBang[][],int x,int y,String pieces) {
int count=0;
while(x>0) {
x--;
if(goBang[x][y].equals(pieces)) {
count++;
}
else
{
break;
}
}
return count;
}
//判断下面的棋子
public static int bottom (String goBang[][],int x,int y,String pieces) {
int count=0;
while(x<18) {
x++;
if(goBang[x][y].equals(pieces)) {
count++;
}
else
{
break;
}
}
return count;
}
//判断右上的棋子
public static int topRight(String goBang[][],int x,int y,String pieces)
{
int count = 0;
while(x>0&&y<18)
{
x--;
y++;
if(goBang[x][y].equals(pieces)) {
count++;
}
else {
break;
}
}
return count;
}
//判断左下的棋子数
public static int bottomLeft(String goBang[][],int x,int y,String pieces)
{
int count = 0;
while(x<18&&y>0)
{
x++;
y--;
if(goBang[x][y].equals(pieces)) {
count++;
}
else {
break;
}
}
return count;
}
//判断左上的棋子数
public static int topLeft(String goBang[][],int x,int y,String pieces) {
int count = 0;
while(x>0&&y>0)
{
x--;
y--;
if(goBang[x][y].equals(pieces)) {
count++;
}
else {
break;
}
}
return count;
}
public static int bottomRight(String goBang[][],int x,int y,String pieces)
{
int count = 0;
while(x<18&&y<18)
{
x++;
y++;
if(goBang[x][y].equals(pieces)) {
count++;
}
else {
break;
}
}
return count;
}
}
实现的结果图如下:
输入x,y的坐标,即可落子。(这里是20*20的坐标轴)如果输入 的x,y大于坐标轴的边界的话则会continue。判断获胜的条件是上下左右或斜方向的黑子或白子大于或等于五,如果黑子或白子满足则会结束掉循环。