java五子棋算法

 写五子棋算法,我也是闲的太狠了。

算法我只完成了在左上和右下的方向的判断是否伍连猪,伍连猪后进行消除操作。仅此而已,算法不难就是有点烦人,下面是运行结果。10*10的矩阵构成棋盘,$代表黑子,,#代表白子。一次输入3 3进行下棋操作。

而已矣。(没啥东西)

$ * * * * * * * * * 
* $ * * * * * * * * 
* * $ * * * * * * * 
* * * $ * * * * * * 
* * * * $ # * * * * 
* * * * * * * * * * 
* * * * * * * * # # 
* * * * * * * * # * 
* * * * * * * * * * 
* * * * * * * * * * 
成功伍连猪
消除炒作
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * # * * * * 
* * * * * * * * * * 
* * * * * * * * # # 
* * * * * * * * # * 
* * * * * * * * * * 
* * * * * * * * * * 
 

package Long;

import java.util.Scanner;

/*
  10*10矩阵,如上第三题,实现五子棋的功能,白子#表示,黑子$表示,*表示棋盘
 */
public class E6 {
    public static void main(String[] args) {
        String a[][] = new String[10][10];
        init(a);
        show(a);
        Scanner in = new Scanner(System.in);
        while (true) {
            int count = 1;
            int x;
            int y;
            int i;
            int j;
            //下棋
            //黑子下棋部分
            x = in.nextInt();
            y = in.nextInt();
            //输入合理性判断
            if (x < 10 && x >= 0 && y >= 0 && y < 10) {
                heizou(x, y, a);
                show(a);
            } else {
                x = in.nextInt();
                y = in.nextInt();
            }
            //判断以x,y黑子为中心是否伍连猪
            checkheizi(count, x, y, a);

            //白子下棋部分
            i = in.nextInt();
            j = in.nextInt();
            if (i < 10 && i >= 0 && j >= 0 && j < 10) {
                baizou(x, y, a);
                show(a);
            } else {
                i = in.nextInt();
                j = in.nextInt();
            }
            //判断白子是否伍连猪


        }
    }

    //初始化10*10矩阵
    public static void init(String a[][]) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                a[i][j] = "*";
            }
        }
    }

    //下棋功能
    //黑子走
    public static void heizou(int x, int y, String a[][]) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (x == i && y == j) {
                    a[i][j] = "$";
                }

            }
        }
    }

    //白子走
    public static void baizou(int x, int y, String a[][]) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (x == i && y == j) {
                    a[i][j] = "#";
                }
            }
        }
    }

    //显示下棋之后的图形
    public static void show(String a[][]) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }

    //判断函数
    //判断黑子时候伍连猪
    public static void checkheizi(int count, int x, int y, String a[][]) {

        //左上右下
        int hx = x;
        int hy = y;

        //左右
        int rx = x;
        int ry = y;
        int rx1 = x;
        int ry2 = y;

        //左下,右上
        int rbx = x;
        int rby = y;
        int rux = x;
        int ruy = y;

        //上下
        int sx = x;
        int sy = y;
        int xx = x;
        int xy = y;

        //外层左上右下
        while (true) {
            if (--x >= 0 && --y >= 0 && a[x][y].equals("$")) {
                count++;
            } else {
                break;
            }
        }
        if (checkwu(count)) {
            //左上不够的时候,进行右下判断,这时候count是叠加的
            while (true) {
                if (++hx >= 0 && ++hy >= 0 && hx <= 9 && hy <= 9 && a[hx][hy].equals("$")) {
                    count++;
                    if (count == 5) {
                        System.out.println("成功伍连猪");
                        //消除炒作
                        System.out.println("消除炒作");
                        for (int i = 0; i < 5; i++) {
                            System.out.println(hx + "  " + hy);
                            a[hx][hy] = "*";
                            hx--;
                            hy--;
                        }
                        show(a);
                        break;
                    }
                } else {
                    count = 1;
                    break;
                }
            }
            ///         /
            //此时左上右下都不满足   其他方向进行判断
            //判断左右
           // while (true) {
               /*
                int rx = x;
                int ry = y;
                int rx1 = x;
                int ry2 = y;
                */


          //  }
/
        } else {
            //左上够五个的时候进行消除
            x = x + 1;
            for (int i = 0; i < 5; i++) {
                System.out.println(x + "   " + y);
                a[x][y] = "*";
                x++;
                y++;
            }
            System.out.println("恭喜伍连猪");
            show(a);
        }


    }

    //判断count是否大于五
    public static Boolean checkwu(int x) {
        if (x >= 5) {
            return false;
        } else {
            return true;
        }
    }


}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值