欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA -简易五子棋小游戏

程序员文章站 2022-04-11 09:49:47
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、Java二维数组二、实现方法1.引入库2.创建棋盘3.创建黑白落子行为函数总结欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导...


前言

相对于推箱子游戏,五子棋游戏加入了判断方法,即是否五子连棋。


一、Java二维数组

通过对Java二维数组的创建、修改、打印和字符判断实现五子棋游戏。

二、实现方法

1.引入库

代码如下:

import java.util.Scanner;

2.创建棋盘

创建长度11*11棋盘,代码如下:

 String[][] arr = new String[11][11];
        arr[0] = new String[] {" 0"," 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9"," 10"};
        arr[1]=new String []{" 1"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[2] =new String[]{" 2"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[3] =new String[]{" 3"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[4] =new String[]{" 4"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[5] =new String[]{" 5"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[6] =new String[]{" 6"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[7] =new String[]{" 7"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[8] =new String[]{" 8"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[9] =new String[]{" 9"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        arr[10] =new String[]{"10"," -"," -"," -"," -"," -"," -"," -"," -"," -"," -"};
        print(arr);

3.创建黑白落子行为函数

数组内容替换,将“-”替换为“黑”或者“白”,代码如下:

static void black(String [] [] arr,Scanner sc) //黑棋落子
    {
        System.out.println("请输入黑子坐标:");
        int blackx =sc.nextInt();
        int blacky=sc.nextInt();
        if(blackx<=10&blacky<=10&blackx>=1&blacky>=1)
        {
            if(arr[blacky][blackx]==" -")
            {
                arr[blacky][blackx]="黑";
                print(arr);
            }
            else
            {
                System.out.println("此处有子!!");
                print(arr);
            }    
        }

    }
    static void white(String[][] arr,Scanner sc) //白棋落子
    {
        System.out.println("请输入白子坐标:");
            int whitex =sc.nextInt();
            int whitey=sc.nextInt();
            if(whitex<=10&whitey<=10&whitex>=1&whitey>=1)
            {
                if(arr[whitey][whitex]==" -")
                {
                    arr[whitey][whitex]="白";
                    print(arr);
                }
                else
                {
                    System.out.println("此处有子!!");
                    print(arr);
                }
            }
    }

4.遍历数组,判断是否五子连棋

数组长度一致,判断横竖和对角线是否五个字符一致,代码如下:

static boolean judge(String [][] arr,String color) //五子连棋判断
    {
        boolean x=false;
        for (int i=1;i<=arr.length-3;i++)
        {
            for(int j=3;j<=arr[i].length-3;j++)
            {
                if(arr[i][j]==arr[i][j+1]&arr[i][j+2]==arr[i][j]&arr[i][j]==arr[i][j-1]&arr[i][j-2]==arr[i][j]&arr[i][j]==color)
                {
                    System.out.println(color+"子"+"胜利!!!");
                    x=true;
                }   
            }
        }
        for(int i=3;i<=arr.length-3;i++)
        {
            for(int j=1;j<arr.length;j++)
            {
                if(arr[i][j]==arr[i+1][j]&arr[i+2][j]==arr[i][j]&arr[i][j]==arr[i-1][j]&arr[i][j]==arr[i-2][j]&arr[i][j]==color)
                {
                    System.out.println(color+"子"+"胜利!!!");
                    x=true;
                }
            }
        }
        for(int i=3;i<=arr.length-3;i++)
        {
            for(int j=3;j<=arr.length-3;j++)
            {
                if(arr[i][j]==arr[i+1][j+1]&arr[i+2][j+2]==arr[i][j]&arr[i-1][j-1]==arr[i][j]&arr[i-2][j-2]==arr[i][j]&arr[i][j]==color)
                {
                    System.out.println(color+"子"+"胜利!!!");
                    x=true; 
                }
            }
        }
        for(int i=3;i<=arr.length-3;i++)
        {
            for(int j=3;j<=arr.length-3;j++)
            {
                if(arr[i][j]==arr[i-1][j+1]&arr[i-2][j+2]==arr[i][j]&arr[i+1][j-1]==arr[i][j]&arr[i+2][j-2]==arr[i][j]&arr[i][j]==color)
                {
                    System.out.println(color+"子"+"胜利!!!");
                    x=true; 
                }
            }
        }
        return x;    
    }

5.创建main主函数

main函数为一循环,黑白双方轮流落子,当黑或白连成五子时,跳出循环,游戏结束。代码如下:

ko:
        for(int num=1;num<=100;num++)
        {
            Scanner sc=new Scanner(System.in);
            white(arr,sc);
            if (judge(arr,"白")==true)
            {break ko;}
            black(arr,sc);
            if (judge(arr,"黑") ==true)
            {break ko;}
        }

6.测试效果

JAVA -简易五子棋小游戏
JAVA -简易五子棋小游戏
JAVA -简易五子棋小游戏

总结

跳出循环的方法 使用xx:为循环命名,当满足条件后使用break xx;跳出循环。

本文地址:https://blog.csdn.net/m0_46655759/article/details/110944239

相关标签: java