用二维数组实现一个简单的五子棋

文章描述了一个Java程序,用于实现一个20x20的黑白棋游戏,包括棋盘初始化、用户输入坐标落子、以及判断四个方向和斜向的连续棋子数量,以决定是否达到胜利条件。
摘要由CSDN通过智能技术生成

代码如下: 

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。判断获胜的条件是上下左右或斜方向的黑子或白子大于或等于五,如果黑子或白子满足则会结束掉循环。

  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值