java使用二维数组和控制台实现五子棋单机版

这次我们使用二维数组在控制台上实现五子棋的游戏,先看成果图:

 思考下这个项目要怎么做,其实很简单:15x15的棋盘,黑子先下,并且只能下在中间。当一个棋子下到棋盘后,以这个棋子为中心向其他方向延伸:上下,左右,左切角或者右切角每个角度单独有连续出现同颜色的5个或以上数量的棋子就胜利


先创建一些变量,用于表示我们游戏内的一些元素。

    //棋盘
    public static String[][] GoBang = new String[15][15];
    //判断是不是黑色方移动
    public static boolean isBlackMove = true;
    //整个棋盘的棋子数量
    public static int piece_count = 0;
    //用于接收玩家输入坐标
    public static Scanner inputManager = new Scanner(System.in);

    //黑色棋子
    public static final String BLACKPIECE = "●";
    //白色棋子
    public static final String WHITEPIECE = "○";
    //空位
    public static final String CLEARPIECE = "+";

创建完基本数据后,可以开始编写我们的游戏了,首先编写一个初始化棋盘数据的方法。

    public static void initGobang()
    {
        for (int x = 0;x<15;x++)
        {
            for (int i = 0;i<15;i++)
            {
                GoBang[x][i] = CLEARPIECE;
            }
        }
    }

非常简单,只是往数组里填充数据而已。


接下来为了方便玩家观察棋盘,另外编写一个打印棋盘的方法。

    public static void printGobang()
    {
        System.out.print("\t");
        for (int x = 1;x<=15;x++)
        {
            System.out.print(x + "\t");
        }
        System.out.println();
        for (int x = 1;x<=15;x++)
        {
            System.out.print(x + "\t");
            for (int i = 1;i<=15;i++)
            {
                System.out.print(GoBang[x-1][i-1] + "\t");
            }
            System.out.println();
        }
    }

总之怎样看着舒服怎样来....


然后我们再编写游戏的核心,判断游戏的输赢,当一个棋子下到棋盘后判断下棋方是否胜利。

    public static boolean isWin(int x,int y)
    {
        //用于记录已经判断过的棋子数量
        int count = 1;
        int findCount = 1;
        int tempX = x;
        int tempY = y;
        final String PIECE = GoBang[x][y];
        //左切角
        while (true)
        {
            //最多的情况一个方向有四个或以上的相连
            for (int value = 1;value<=4;value++)
            {
                //防止超出数组的索引
                if ((tempX - value != -1) && (tempY - value != -1))
                {
                    //如果是同类型的棋子
                    if (GoBang[tempX - value][tempY - value].equals(PIECE))
                    {
                        //相连数+1,已经进行判断的数量+1
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        //如果不是同类型棋子则代表被不同颜色的棋子或空位截断,把被截断的数量添加到已判断的棋子数量.
                        count += ((4 - value + 1));
                        break;
                    }
                }
                else
                {
                    count += ((4 - value + 1));
                    break;
                }
                //如果这个方向已经有5个棋子,返回true,游戏胜利。
                if (findCount >= 5)
                {
                    return true;
                }
            }
            //左切角的另一个方向,原理相同不再赘述。
            for (int value = 1;value<=4;value++)
            {
                if ((tempX + value != 15) && (tempY + value != 15))
                {
                    if (GoBang[tempX + value][tempY + value].equals(PIECE))
                    {
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        count += ((4 - value + 1));
                        break;
                    }
                }
                else
                {
                    count += ((4 - value + 1));
                    break;
                }
                if (findCount >= 5)
                {
                    return true;
                }
            }
            if (count == 9)
            {
                findCount = 1;
                break;
            }
        }

        //右切角
        while (true)
        {
            for (int value = 1;value<=4;value++)
            {
                if ((tempX + value != 15) && (tempY - value != -1))
                {
                    if (GoBang[tempX + value][tempY - value].equals(PIECE))
                    {
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        count += ((4 - value + 1));
                        break;
                    }
                }
                else
                {
                    count += ((4 - value + 1));
                    break;
                }
                if (findCount >= 5)
                {
                    return true;
                }
            }
            for (int value = 1;value<=4;value++)
            {
                if ((tempX - value != -1) && (tempY + value != 15))
                {
                    if (GoBang[tempX - value][tempY + value].equals(PIECE))
                    {
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        count += ((4 - value + 1));
                        break;
                    }
                }
                else
                {
                    count += ((4 - value + 1));
                    break;
                }
                if (findCount >= 5)
                {
                    return true;
                }
            }
            if (count == 17)
            {
                findCount = 1;
                break;
            }
        }

        //上下
        while (true)
        {
            for (int value = 1;value<=4;value++)
            {
                if ((tempY - value != -1))
                {
                    if (GoBang[tempX][tempY - value].equals(PIECE))
                    {
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        count += ((4 - value + 1));
                        break;
                    }
                }
                else
                {
                    count +=((4 - value + 1));
                    break;
                }
                if (findCount >= 5)
                {
                    return true;
                }
            }
            for (int value = 1;value<=4;value++)
            {
                if ((tempY + value != 15))
                {
                    if (GoBang[tempX][tempY + value].equals(PIECE))
                    {
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        count += (4 - value + 1);
                        break;
                    }
                }
                else
                {
                    count += (4 - value + 1);
                    break;
                }
                if (findCount >= 5)
                {
                    return true;
                }
            }
            if (count == 25)
            {
                findCount = 1;
                break;
            }
        }

        //左右
        while (true)
        {
            for (int value = 1;value<=4;value++)
            {
                if ((tempX - value != -1))
                {
                    if (GoBang[tempX - value][tempY].equals(PIECE))
                    {
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        count += (4 - value + 1);
                        break;
                    }
                }
                else
                {
                    count += (4 - value + 1);
                    break;
                }
                if (findCount >= 5)
                {
                    return true;
                }
            }
            for (int value = 1;value<=4;value++)
            {
                if ((tempX + value != 15))
                {
                    if (GoBang[tempX + value][tempY].equals(PIECE))
                    {
                        findCount += 1;
                        count += 1;
                    }
                    else
                    {
                        count += (4 - value + 1);
                        break;
                    }
                }
                else
                {
                    count += (4 - value + 1);
                    break;
                }
                if (findCount >= 5)
                {
                    return true;
                }
            }
            if (count == 33)
            {
                findCount = 1;
                break;
            }
        }
        //否则游戏继续
        return false;
    }

感觉这部分写的有点屎山了。。。。大家看着修改


然后我们再来编写一个下棋子的方法,用来判断下棋的位置是否可用。

    public static boolean playChess(int x,int y)
    {
        if (piece_count == 0)
        {
            if (x != 8 || y != 8)
            {
                System.out.println("第一枚棋子必须下在中央!");
                return false;
            }
            else
            {
                GoBang[7][7] = BLACKPIECE;
                isBlackMove = !isBlackMove;
                piece_count += 1;
                return true;
            }
        }
        if (isBlackMove)
        {
            if (!GoBang[x-1][y-1].equals(CLEARPIECE))
            {
                System.out.println("这个地方不是空位!");
                return false;
            }
            else
            {
                GoBang[x-1][y-1] = BLACKPIECE;
                isBlackMove = !isBlackMove;
                piece_count += 1;
                return true;
            }
        }
        else
        {
            if (!GoBang[x-1][y-1].equals(CLEARPIECE))
            {
                System.out.println("这个地方不是空位!");
                return false;
            }
            else
            {
                GoBang[x-1][y-1] = WHITEPIECE;
                isBlackMove = !isBlackMove;
                piece_count += 1;
                return true;
            }
        }
    }

这里-1是因为给玩家打印的界面是没有0索引的(从1开始),如果下棋成功返回true,否则返回false


最后编写游戏主循环方法

    public static void gameLoop()
    {
        int inputX = 0;
        int inputY = 0;
        do {
            printGobang();
            //下满棋盘后
            if (piece_count == 225)
            {
                System.out.println("棋子已经下完。。。没有赢家。");
                break;
            }
            System.out.println("现在是:" + (isBlackMove ? "黑色方行动" : "白色方行动"));
            System.out.println("请先输入想放置的行(X):");
            inputX = inputManager.nextInt();
            System.out.println("请输入想放置的列(Y):");
            inputY = inputManager.nextInt();
            if (!playChess(inputY,inputX))
            {
                gameLoop();
            }
        }
        while (!isWin(inputY-1,inputX-1));
        printGobang();
        if (!isBlackMove && piece_count < 255)
        {
            System.out.println("恭喜黑色方获胜!");
        }
        else if (piece_count < 255)
        {
            System.out.println("恭喜白色方获胜!");
        }
    }

至此整个游戏已经完成,最后在main方法中调用初始化方法,然后调用游戏主循环即可

    public static void main(String[] args)
    {
        initGobang();
        gameLoop();
    }

快去动手试试吧!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值