使用java写的五子棋

***这个代码只使用了二维数组和Scanner***

GoBang类:

package com.liu.gobang;

import java.util.Scanner;

public class GoBang {
    public static void main(String[] args) {
        //棋盘
        int[][] qiPan = new int[20][20];

        System.out.println("请选择先手甲/乙");
        Scanner sc = new Scanner(System.in);
        String xianShou = sc.next();
        boolean flag = xianShou.equals("甲") ? true: false;
        lo:
        for (; ; ) {
            showQiJu(qiPan);
            flag = !flag;
            if (!flag) { //xianShou.equals("甲")
            System.out.println("甲方:请输入您的x轴坐标");
            int x = sc.nextInt();
            System.out.println("甲方:请输入您的y轴坐标");
            int y = sc.nextInt();
            qiPan[x - 1][y - 1] = 1;
            if (for5X(qiPan, 1) || for5Y(qiPan, 1) || forX_Y(qiPan, 1)) {
                showQiJu(qiPan);
                System.out.println("甲方获胜!!!");
                break lo;
            }


            }else if (flag){ //xianShou.equals("乙")
                System.out.println("乙方:请输入您的x轴坐标");
                int x = sc.nextInt();
                System.out.println("乙方:请输入您的y轴坐标");
                int y = sc.nextInt();
                qiPan[x-1][y-1] = 2;
                if (for5X(qiPan, 2) || for5Y(qiPan, 2) || forX_Y(qiPan, 2)) {
                    showQiJu(qiPan);
                    System.out.println("乙方获胜!!!");
                    break lo;
                }
            }
        }

    }

    //X-Y轴
    //①斜45角方向分两种情况可以以y=x这条线进行分割分别遍历
    //②第二种情况是以y=-x这条线进行遍历
    private static boolean forX_Y(int[][] qiPan, int qi) {
        for (int i = 0; i < qiPan.length-4; i++) {
            for (int j = 0; j < qiPan[j].length-4; j++) {
                if (qiPan[i][j] ==qi && qiPan[i+1][j+1] ==qi &&qiPan[i+2][j+2] ==qi
                        &&qiPan[i+3][j+3] ==qi&&qiPan[i+4][j+4] ==qi){
                    return true;
                }
            }
        }
        for (int i = 4; i < qiPan.length; i++) {
            for (int j = 0; j <qiPan.length-4; j++) {
                if (qiPan[i][j] ==qi && qiPan[i-1][j+1] ==qi &&qiPan[i-2][j+2] ==qi
                        &&qiPan[i-3][j+3] ==qi&&qiPan[i-4][j+4] ==qi){
                    return true;
                }
            }
        }
        return false;
    }

    //Y轴
    private static boolean for5Y(int[][] qiPan, int qi) {

        int i = 0;
        for (; i < qiPan.length; ) {
            int index = 0;  //下标下移位
            int countQi = 0;
            int count = 0;

            lo:
            for (int j = index; j < index + 5; j++) {
                if (qiPan[j][i] == qi) {
                    countQi++;
                }
                count++;
                if (countQi == 5) {
                    return true;
                }
                if (count == 5) {
                    count = 0;
                    countQi = 0;
                    index++;
                    j = index - 1;
                }
                if (index == 16) {
                    count = 0;
                    countQi = 0;
                    index = 0;
                    i++;
                    break lo;

                }
            }

        }
        return false;
    }

    //X轴
    private static boolean for5X(int[][] qiPan, int qi) {
        int i = 0;
        for (; i < qiPan.length; ) {
            int count = 0;
            int index = 0;  //下标右移
            int countQi = 0;
            lo:
            for (int j = index; j < index + 5; j++) {
                int temp = qiPan[i][j];
                if (temp == qi) {
                    countQi++;
                }
                count++;
                if (countQi == 5) {
                    return true;
                }
                //如果次数大等于5,那么x坐标的向左移动1位继续求和
                if (count == 5) {
                    count = 0;
                    countQi = 0;
                    index++;
                    j = index - 1;
                }
                //统计下标是否越界,如果index大于15就会有越界风险
                if (index == 16) {
                    count = 0;
                    countQi = 0;
                    index = 0;
                    i++;
                    break lo;

                }
            }
        }
        return false;
    }

    private static void showQiJu(int[][] qiPan) {
        //打印棋局
        for (int i = 0; i < qiPan.length; i++) {
            for (int j = 0; j < qiPan.length; j++) {
                if (qiPan[i][j] == 0) {
                    System.out.print("+" + "\t");
                    ;
                }
                if (qiPan[i][j] == 1) {  //1就是甲方
                    System.out.print("●" + "\t");
                }
                if (qiPan[i][j] == 2) {  //2就是乙方
                    System.out.print("@" + "\t");
                }
            }
            System.out.println();
        }
    }
}

Test类:


package com.liu.gobang;
import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) {
        String[][] arr = new String[20][20];

        Scanner scanner = new Scanner(System.in);
        boolean flag = false;// 主要用来记录轮到谁了!!!!
        while (true) {
            System.out.println("----------------");
            printArrArr(arr);
            flag = !flag;
            if (flag) {// ●
                //甲方
                System.out.println("请甲方输入x的坐标");
                int x = scanner.nextInt();//1
                System.out.println("请甲方输入y的坐标");
                int y = scanner.nextInt();//5
                arr[x-1][y -1] = "●";

            } else {
                //乙方  ○
                System.out.println("请乙方输入x的坐标");
                int x = scanner.nextInt();
                System.out.println("请乙方输入y的坐标");
                int y = scanner.nextInt();

                arr[x-1][y -1] = "○";

            }

        }
    }

    public static void printArrArr(String[][] arr) {//new String[20][20]

        for (int i = 0; i < arr.length; i++) {
            String[] arr1 = arr[i];
            for (int j = 0; j < arr1.length; j++) {
                if (arr1[j] == null) {
                    System.out.print(" + ");
                }else{
                    System.out.print(" "+ arr1[j] +" ");
                }
            }
            System.out.println();
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值