五子棋小游戏(Java)

本文详细介绍了一个简单的五子棋游戏程序设计,使用Java语言,通过二维数组模拟棋盘,实现黑棋和白棋的交替落子,并检查横、竖、斜方向上是否有五个连续的相同颜色棋子以判断胜负。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

五子棋小游戏:

今天写一个五子棋小游戏,大家都玩过,所以就不用介绍规则了,主要是用逻辑实现,2代表黑棋,1代表白棋。

要写这个程序呢,首先我们要有一个棋盘,还是老样子用二维数组实现,当你new一下后就在堆中开辟了一片空间,数组中的默认值都是0。

 public static void main(String[] args) {
        int[][] a = new int[10][10];
        int color = 1;
        Scanner scanner = new Scanner(System.in);
        int x = 0;
        int y = 0;
        System.out.println("黑方先落子");

棋盘现在有了,要判断条件吧,横行竖列满5就会赢,这里我通过总数相加判断,连续的5个棋子为同一颜色时是否获胜。黑棋是2,当黑棋5个相连时,总数应该为10,当然了,白棋5个相连总数就是5。这里以行为例:

public static Boolean isHeng(int[][] a, int x, int y, int color){    //判断行是否获胜
        int count = 1;
        int tempy = y;
        while (y > 0 && a[x][y - 1] == color){
            count++;
            y--;
        }
        y = tempy;
        while (y < a[x].length - 1 && a[x][y + 1] == color){
            count++;
            y++;
        }
        return count >= 5;
    }

 

然后用户要不断的落子,你不知道他什么时候能赢,所以这里我在main()里用了do-while()循环,现在要给循环一个判断条件吧,横行竖列任意一个方向连成5个就会跳出循环。判断条件代码为:

public static Boolean isSuccess(int[][] a, int x, int y, int color){  //判断获胜的条件
        Boolean f = false;
        if(isHeng(a, x, y, color) | isLie(a, x, y, color) | isZuoxie(a, x, y, color) | isYouxie(a, x, y, color)){
            return true;
        }else{
            return f;
        }

    }

好了,大致的框子就搭好了,其实和关灯游戏差不多,只是繁琐了一点。以下是具体代码实现。

import java.sql.SQLOutput;
import java.util.Scanner;

public class 五子棋游戏 {
    public static void main(String[] args) {
        int[][] a = new int[10][10];
        int color = 1;
        Scanner scanner = new Scanner(System.in);
        int x = 0;
        int y = 0;
        System.out.println("黑方先落子");

        do {
            show(a);
            color = 3 - color;
            System.out.println("请输入行:");
            x = scanner.nextInt();
            System.out.println("请输入列:");
            y = scanner.nextInt();
            if (a[x - 1][y - 1] == 0){
                a[x - 1][y - 1] = color;
            }else{
                System.out.println("此处有子了,请重新落子");
                System.out.println();
                color = 3 - color;
                continue;
            }
            System.out.println();
        }while (!isSuccess(a, x - 1, y - 1, color));
        System.out.println(color + "获胜");
    }

    public static void show(int[][] a){    //打印棋盘
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static Boolean isSuccess(int[][] a, int x, int y, int color){  //判断获胜的条件
        Boolean f = false;
        if(isHeng(a, x, y, color) | isLie(a, x, y, color) | isZuoxie(a, x, y, color) | isYouxie(a, x, y, color)){
            return true;
        }else{
            return f;
        }

    }

    public static Boolean isHeng(int[][] a, int x, int y, int color){    //判断行是否获胜
        int count = 1;
        int tempy = y;
        while (y > 0 && a[x][y - 1] == color){
            count++;
            y--;
        }
        y = tempy;
        while (y < a[x].length - 1 && a[x][y + 1] == color){
            count++;
            y++;
        }
        return count >= 5;
    }

    public static Boolean isLie(int[][] a, int x, int y, int color){    //判断列是否获胜
        int count = 1;
        int tempx = x;
        while (x > 0 && a[x - 1][y] == color){
            count++;
            x--;
        }
        x = tempx;
        while (x < a.length - 1 && a[x + 1][y] == color){
            count++;
            x++;
        }
        return count >= 5;
    }

    public static Boolean isZuoxie(int[][] a, int x, int y, int color){    //判断左斜是否获胜
        int count = 1;
        int tempx = x;
        int tempy = y;
        while (x > 0 && y > 0 && a[x - 1][y - 1] == color){
            count++;
            x--;
            y--;
        }
        x = tempx;
        y = tempy;
        while (y < a.length - 1 && x < a.length - 1 && a[x + 1][y + 1] == color){
            count++;
            x++;
            y++;
        }
        return count >= 5;
    }

    public static Boolean isYouxie(int[][] a, int x, int y, int color){    //判断右斜是否获胜
        int count = 1;
        int tempx = x;
        int tempy = y;
        while (x > 0 && y < a.length - 1 && a[x - 1][y + 1] == color){
            count++;
            x--;
            y++;
        }
        x = tempx;
        y = tempy;
        while (y > 0 && x < a.length - 1 && a[x + 1][y - 1] == color){
            count++;
            x++;
            y--;
        }
        return count >= 5;
    }
}

执行结果为:

黑方先落子
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	
请输入行:

其实这个程序不难的,用心一点就会完成的,加油~~~

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值