用Java写五子棋小游戏

五子棋小游戏使用多个方法组合而成

首先我们需要定义全局变量

static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒","⒓", "⒔", "⒕", "⒖"};
static String line = "十";
static int i = 0;
static int j = 0;

以上全局变量使用的目的为:

创建五子棋棋盘以及输出棋盘。

使用menu();,print();这两个方法来实现菜单的初始化以及输出。

代码如下:

 public static void menu() {
        int k = 0;
        for (i = 0; i < 14; i++) {
            for (j = 0; j < 14; j++) {
                qp[i][j] = line;
            }
        }
        for (j = 0; j < 14; j++) {
            qp[14][j] = num[k++];
        }
        k = 0;
        for (i = 0; i < 14; i++) {
            qp[i][14] = num[k++];
        }
        qp[14][14] = num[14];
    }

    public static void print() {
        for (i = 0; i < qp.length; i++) {
            for (j = 0; j < qp[i].length; j++) {
                System.out.print(qp[i][j]);
            }
            System.out.println();
        }
    }

使用scan();方法来实现黑棋和白棋的输入以及多种情况的判断(包括:黑棋白棋位置重复的问题,黑棋白棋位置越界问题,以及调用方法iswin()来实现对五子棋胜利的判断)

实现代码如下:

 private static void scan() {
        print();
        boolean flag = true;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            if (flag) {
                System.out.println("请黑子下子:");  //定义两个变量来实现黑棋位置的输入
                int black1 = scanner.nextInt()-1;   //减1是因为数组是从0开始的
                int black2 = scanner.nextInt()-1;
                boolean result = determine(black1, black2);
                if (!result) {
                    System.out.println("输入有误,请重新输入:");
                    black1 = scanner.nextInt()-1;
                    black2 = scanner.nextInt()-1;
                }
                qp[black1][black2] = black;
                print();
                boolean blackwin = iswin(black1, black2, black);
                if (blackwin) {
                    System.out.println("黑棋获胜!");
                    break;
                } else {
                    flag = false;
                }
            } else {
                System.out.println("请白子下子:");
                int white1 = scanner.nextInt()-1;
                int white2 = scanner.nextInt()-1;
                boolean result1 = determine(white1, white2);
                if (!result1) {
                    System.out.println("输入有误,请重新输入:");
                    white1 = scanner.nextInt()-1;
                    white2 = scanner.nextInt()-1;
                }
                qp[white1][white2] = white;
                print();
                boolean whitewin = iswin(white1, white2, white);
                if (whitewin) {
                    System.out.println("白棋获胜!");
                    break;
                } else {
                    flag = true;
                }
            }
        }
    }

 创建iswin()方法来实现判断获胜的条件。

分为三种情况:

1.横向满足5个棋子一条线:

用最后一个落子的位置来判断,先向左边查看是否有4个相同颜色且连续的棋子,如果没有记录连续相同颜色棋子的个数然后从最后一个落子的位置向右寻找,如果加上左边的总数满足5个或5个以上则判定胜利。

实现代码如下:

//横向对比
        int spsum = 1;
        for (int lefty = x - 1; lefty >= 0; lefty--) {
            if (qp[x][lefty].equals(yanse)) {
                spsum++;
            } else {
                break;
            }
        }
        if (spsum >= 5) {
            return true;
        }
        for (int righty = x + 1; righty < qp[x].length; righty++) {
            if (qp[x][righty].equals(yanse)) {
                spsum++;
            } else {
                break;
            }
        }
        if (spsum >= 5) {
            return true;

纵向对比和横向对比的思路相同,也是从最后一个落子的位置开始寻找,先向上边查看是否有4个相同颜色且连续的棋子,如果没有记录连续相同颜色棋子的个数然后从最后一个落子的位置向下寻找,如果加上左边的总数满足5个或5个以上则判定胜利。

实现代码如下:

//纵向对比
        int zxsum = 1;
        for (int upx = x-1; upx >= 0; upx--) {
            if (qp[upx][y].equals(yanse)) {
                zxsum++;
            } else {
                break;
            }
        }
        if (zxsum >= 5) {
            return true;
        }
        for (int downx=x+1;downx<15;downx++) {
            if (qp[downx][y].equals(yanse)) {
                zxsum++;
            } else {
                break;
            }
        }
        if (zxsum >= 5) {
            return true;
        }

斜向对比较为繁琐但是思路与以上两种一致

实现代码如下:

//斜向对比
        int xysum = 1;
        for (int x1 = x-1, y1 = y+1; x1 >= 0 && y1 < qp.length; x1--, y1++) {  //向左下查找
            if (qp[x1][y1].equals(yanse)) {
                xysum++;
            } else {
                break;
            }
        }
        if (xysum >= 5) {
            return true;
        }
        for (int x2 = x+1, y2 = y-1; x2 < qp.length-1&& y2 >=0; x2++, y2--) { //向右上角
            if (qp[x2][y2].equals(yanse)) {
                xysum++;
            } else {
                break;
            }
        }
        if (xysum >= 5) {
            return true;
        }
        int yxsum = 1;
        for (int x3 = x-1, y3 =y-1; x3 >= 0 && y3 >=0; x3--, y3--) {  //向左上方
            if (qp[x3][y3].equals(yanse)) {
                yxsum++;
            } else {
                break;
            }
        }
        if (yxsum >= 5) {
            return true;
        }
        for (int x4 = x+1, y4 = y+1; x4 < qp.length && y4<qp.length; x4++, y4++) {
            if (qp[x4][y4].equals(yanse)) {
                yxsum++;
            } else {
                break;
            }
        }
        if (yxsum >= 5) {
            return true;
        }

把他们组合起来形成iswin()方法:

代码如下:

  public static boolean iswin(int x, int y, String yanse) {
        //横向对比
        int spsum = 1;
        for (int lefty = x - 1; lefty >= 0; lefty--) {
            if (qp[x][lefty].equals(yanse)) {
                spsum++;
            } else {
                break;
            }
        }
        if (spsum >= 5) {
            return true;
        }
        for (int righty = x + 1; righty < qp[x].length; righty++) {
            if (qp[x][righty].equals(yanse)) {
                spsum++;
            } else {
                break;
            }
        }
        if (spsum >= 5) {
            return true;
        }
        //纵向对比
        int zxsum = 1;
        for (int upx = x-1; upx >= 0; upx--) {
            if (qp[upx][y].equals(yanse)) {
                zxsum++;
            } else {
                break;
            }
        }
        if (zxsum >= 5) {
            return true;
        }
        for (int downx=x+1;downx<15;downx++) {
            if (qp[downx][y].equals(yanse)) {
                zxsum++;
            } else {
                break;
            }
        }
        if (zxsum >= 5) {
            return true;
        }
        //斜向对比
        int xysum = 1;
        for (int x1 = x-1, y1 = y+1; x1 >= 0 && y1 < qp.length; x1--, y1++) {  //向左下查找
            if (qp[x1][y1].equals(yanse)) {
                xysum++;
            } else {
                break;
            }
        }
        if (xysum >= 5) {
            return true;
        }
        for (int x2 = x+1, y2 = y-1; x2 < qp.length-1&& y2 >=0; x2++, y2--) { //向右上角
            if (qp[x2][y2].equals(yanse)) {
                xysum++;
            } else {
                break;
            }
        }
        if (xysum >= 5) {
            return true;
        }
        int yxsum = 1;
        for (int x3 = x-1, y3 =y-1; x3 >= 0 && y3 >=0; x3--, y3--) {  //向左上方
            if (qp[x3][y3].equals(yanse)) {
                yxsum++;
            } else {
                break;
            }
        }
        if (yxsum >= 5) {
            return true;
        }
        for (int x4 = x+1, y4 = y+1; x4 < qp.length && y4<qp.length; x4++, y4++) {
            if (qp[x4][y4].equals(yanse)) {
                yxsum++;
            } else {
                break;
            }
        }
        if (yxsum >= 5) {
            return true;
        }

        return false;
    }

将上述的集中方法结合起来就可以实现用java来写一个五子棋小游戏

package day04;

import java.util.Scanner;

public class wuziqi {
    static String white = "☆";
    static String black = "★";
    static String[][] qp = new String[15][15];
    static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
    static String line = "十";
    static int i = 0;
    static int j = 0;

    public static void main(String[] args) {
        menu();
        scan();
    }

    public static void menu() {
        int k = 0;
        for (i = 0; i < 14; i++) {
            for (j = 0; j < 14; j++) {
                qp[i][j] = line;
            }
        }
        for (j = 0; j < 14; j++) {
            qp[14][j] = num[k++];
        }
        k = 0;
        for (i = 0; i < 14; i++) {
            qp[i][14] = num[k++];
        }
        qp[14][14] = num[14];
    }

    public static void print() {
        for (i = 0; i < qp.length; i++) {
            for (j = 0; j < qp[i].length; j++) {
                System.out.print(qp[i][j]);
            }
            System.out.println();
        }
    }

    private static void scan() {
        print();
        boolean flag = true;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            if (flag) {
                System.out.println("请黑子下子:");
                int black1 = scanner.nextInt()-1;
                int black2 = scanner.nextInt()-1;
                boolean result = determine(black1, black2);
                if (!result) {
                    System.out.println("输入有误,请重新输入:");
                    black1 = scanner.nextInt()-1;
                    black2 = scanner.nextInt()-1;
                }
                qp[black1][black2] = black;
                print();
                boolean blackwin = iswin(black1, black2, black);
                if (blackwin) {
                    System.out.println("黑棋获胜!");
                    break;
                } else {
                    flag = false;
                }
            } else {
                System.out.println("请白子下子:");
                int white1 = scanner.nextInt()-1;
                int white2 = scanner.nextInt()-1;
                boolean result1 = determine(white1, white2);
                if (!result1) {
                    System.out.println("输入有误,请重新输入:");
                    white1 = scanner.nextInt()-1;
                    white2 = scanner.nextInt()-1;
                }
                qp[white1][white2] = white;
                print();
                boolean whitewin = iswin(white1, white2, white);
                if (whitewin) {
                    System.out.println("白棋获胜!");
                    break;
                } else {
                    flag = true;
                }
            }
        }
    }

    public static boolean determine(int x, int y) {
        if (qp[x][y] == line) {//落子正确
            return true;
        } else {
            return false;
        }
    }

    public static boolean iswin(int x, int y, String yanse) {
        //横向对比
        int spsum = 1;
        for (int lefty = x - 1; lefty >= 0; lefty--) {
            if (qp[x][lefty].equals(yanse)) {
                spsum++;
            } else {
                break;
            }
        }
        if (spsum >= 5) {
            return true;
        }
        for (int righty = x + 1; righty < qp[x].length; righty++) {
            if (qp[x][righty].equals(yanse)) {
                spsum++;
            } else {
                break;
            }
        }
        if (spsum >= 5) {
            return true;
        }
        //纵向对比
        int zxsum = 1;
        for (int upx = x-1; upx >= 0; upx--) {
            if (qp[upx][y].equals(yanse)) {
                zxsum++;
            } else {
                break;
            }
        }
        if (zxsum >= 5) {
            return true;
        }
        for (int downx=x+1;downx<15;downx++) {
            if (qp[downx][y].equals(yanse)) {
                zxsum++;
            } else {
                break;
            }
        }
        if (zxsum >= 5) {
            return true;
        }
        //斜向对比
        int xysum = 1;
        for (int x1 = x-1, y1 = y+1; x1 >= 0 && y1 < qp.length; x1--, y1++) {  //向左下查找
            if (qp[x1][y1].equals(yanse)) {
                xysum++;
            } else {
                break;
            }
        }
        if (xysum >= 5) {
            return true;
        }
        for (int x2 = x+1, y2 = y-1; x2 < qp.length-1&& y2 >=0; x2++, y2--) { //向右上角
            if (qp[x2][y2].equals(yanse)) {
                xysum++;
            } else {
                break;
            }
        }
        if (xysum >= 5) {
            return true;
        }
        int yxsum = 1;
        for (int x3 = x-1, y3 =y-1; x3 >= 0 && y3 >=0; x3--, y3--) {  //向左上方
            if (qp[x3][y3].equals(yanse)) {
                yxsum++;
            } else {
                break;
            }
        }
        if (yxsum >= 5) {
            return true;
        }
        for (int x4 = x+1, y4 = y+1; x4 < qp.length && y4<qp.length; x4++, y4++) {
            if (qp[x4][y4].equals(yanse)) {
                yxsum++;
            } else {
                break;
            }
        }
        if (yxsum >= 5) {
            return true;
        }

        return false;
    }






}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值