下落棋

举报
一条coding 发表于 2021/10/19 23:45:50 2021/10/19
【摘要】 能够画出9*9格,有两个棋手A,B,交替输入列数0-9,由于重力原理,输入这个列下几行没有棋子,则自动下落;A显示棋子是X,B显示棋子O,要求当出现四连胜利!A和B交替进行: import java.util.Scanner; public class practice7Arrays{ //悬挂的四子棋 publi...

能够画出9*9格,有两个棋手A,B,交替输入列数0-9,由于重力原理,输入这个列下几行没有棋子,则自动下落;A显示棋子是X,B显示棋子O,要求当出现四连胜利!A和B交替进行:

import java.util.Scanner;


public class practice7Arrays{
    //悬挂的四子棋
    public static void main(String[]args){
        Scanner input = new Scanner(System.in);
        String[][] qi = new String[6][7];
        String yuanShiYuanSu = "|   ";
        String yellow = "| Y ";
        String red = "| R ";
        int count = 0;//谁下子及下了多少子的判断计数器
        //初始化棋子数组
        for(int i = 0;i < qi.length;i++)
            for(int j = 0;j < qi[0].length;j++)
                qi[i][j] = yuanShiYuanSu;
        //画出原始棋盘
        System.out.println("-----------------------------");
        for(int i = qi.length - 1;i >= 0;i--){
            for(int j = 0;j < qi[0].length;j++)
                System.out.print(qi[i][j]);
            System.out.println("|");
            System.out.println("-----------------------------");
        }
        for(int i = 0;i < qi[0].length;i++)
            System.out.print("* "+(i+1)+" ");
        System.out.println("*");

        int row = 0,column = 0;//行列下标初始化
        //下棋循环开始
        while (true) {
            row = 0;
            column = 0;
            if (count % 2 == 0) {//黄方下子
                System.out.print("\n\nY player please drop a yellow disk at column(1~7):");
                while (true) {//黄方下子循环开始
                    column = input.nextInt() - 1;
                    if (column >= 0 && column <= 6) {// 输入合法进行下子
                        for (row = 0; row < qi.length; row++) {
                            if (qi[row][column] == yuanShiYuanSu) {
                                qi[row][column] = yellow;
                                break;
                            }
                        }
                        if (row == qi.length)//该列棋子满,重新输入
                            System.out.print("The column of you enter is full,"
                                    + "please reEnter! : ");
                        else//棋子没满,下子结束
                            break;
                    }
                    else// 输入不合法,重新下子
                        System.out.print("You enter the wrong column,"
                                + "please reEnter! : ");
                }//黄方下子循环结束
            }//if(count % 2 == 0)
            else{//红方下子
                System.out.print("\n\nR player please drop a yellow disk at column(1~7):");
                while (true) {//红方下子循环开始
                    column = input.nextInt() - 1;
                    if (column >= 0 && column <= 6) {// 输入合法进行下子
                        for (row = 0; row < qi.length; row++) {
                            if (qi[row][column] == yuanShiYuanSu) {
                                qi[row][column] = red;
                                break;
                            }
                        }
                        if (row == qi.length)//该列棋子满,重新输入
                            System.out.print("The column of you enter is full,"
                                    + "please reEnter! : ");
                        else//棋子没满,下子结束
                            break;
                    }
                    else// 输入不合法,重新下子
                        System.out.print("You enter the wrong column,"
                                + "please reEnter! : ");
                }//红方下子循环结束
            }//if(count % 2 != 0)


            //画出棋盘
            System.out.println("-----------------------------");
            for(int i = qi.length - 1;i >= 0;i--){
                for(int j = 0;j < qi[0].length;j++)
                    System.out.print(qi[i][j]);
                System.out.println("|");
                System.out.println("-----------------------------");
            }
            for(int i = 0;i < qi[0].length;i++)
                System.out.print("* "+(i+1)+" ");
            System.out.println("*");

            //输赢标志位
            boolean flagForYWin = false;
            boolean flagForRWin = false;

            //只需对当前下子的周围情况进行判断来决定棋局结果
            if (count % 2 == 0) {//yellow player is win?
                //行检测开始
                if (column <= 3) {
                    for (int jj = 0; jj <= column; jj++)
                        if (qi[row][jj] == yellow
                                && qi[row][jj + 1] == yellow
                                && qi[row][jj + 2] == yellow
                                && qi[row][jj + 3] == yellow) {
                            flagForYWin = true;
                            break;
                        }
                }
                else {
                    for (int jj = column - 3; jj <= 3; jj++)
                        if (qi[row][jj] == yellow
                                && qi[row][jj + 1] == yellow
                                && qi[row][jj + 2] == yellow
                                && qi[row][jj + 3] == yellow) {
                            flagForYWin = true;
                            break;
                        }
                }
                if (flagForYWin) {
                    System.out.println("The yellow player win the game!");
                    break;
                }

                //列检测开始
                if (row >= 3) {
                    if (qi[row][column] == yellow
                            && qi[row - 1][column] == yellow
                            && qi[row - 2][column] == yellow
                            && qi[row - 3][column] == yellow)
                        flagForYWin = true;
                }
                if (flagForYWin) {
                    System.out.println("The yellow player win the game!");
                    break;
                }

                //正反对角检测
                if(row >= 3){
                    if(column < 3){
                        if (qi[row][column] == yellow
                                && qi[row - 1][column + 1] == yellow
                                && qi[row - 2][column + 2] == yellow
                                && qi[row - 3][column + 3] == yellow)
                            flagForYWin = true;
                    }
                    else if(column > 3){
                        if (qi[row][column] == yellow
                                && qi[row - 1][column - 1] == yellow
                                && qi[row - 2][column - 2] == yellow
                                && qi[row - 3][column - 3] == yellow)
                            flagForYWin = true;
                    }
                    else{
                        if ((qi[row][column] == yellow
                                && qi[row - 1][column + 1] == yellow
                                && qi[row - 2][column + 2] == yellow
                                && qi[row - 3][column + 3] == yellow)
                                || (qi[row][column] == yellow
                                && qi[row - 1][column - 1] == yellow
                                && qi[row - 2][column - 2] == yellow
                                && qi[row - 3][column - 3] == yellow))
                            flagForYWin = true;
                    }
                }
                if (flagForYWin) {
                    System.out.println("The yellow player win the game!");
                    break;
                }
            }//yellow player is win?
            else{//red player is win?
                //行检测开始
                if (column <= 3) {
                    for (int jj = 0; jj <= column; jj++)
                        if (qi[row][jj] == red
                                && qi[row][jj + 1] == red
                                && qi[row][jj + 2] == red
                                && qi[row][jj + 3] == red) {
                            flagForRWin = true;
                            break;
                        }
                }
                else {
                    for (int jj = column - 3; jj <= 3; jj++)
                        if (qi[row][jj] == red
                                && qi[row][jj + 1] == red
                                && qi[row][jj + 2] == red
                                && qi[row][jj + 3] == red) {
                            flagForRWin = true;
                            break;
                        }
                }
                if (flagForRWin) {
                    System.out.println("The red player win the game!");
                    break;
                }

                //列检测开始
                if (row >= 3) {
                    if (qi[row][column] == red
                            && qi[row - 1][column] == red
                            && qi[row - 2][column] == red
                            && qi[row - 3][column] == red)
                        flagForRWin = true;
                }
                if (flagForRWin) {
                    System.out.println("The red player win the game!");
                    break;
                }

                //正反对角检测
                if(row >= 3){
                    if(column < 3){
                        if (qi[row][column] == red
                                && qi[row - 1][column + 1] == red
                                && qi[row - 2][column + 2] == red
                                && qi[row - 3][column + 3] == red)
                            flagForRWin = true;
                    }
                    else if(column > 3){
                        if (qi[row][column] == red
                                && qi[row - 1][column - 1] == red
                                && qi[row - 2][column - 2] == red
                                && qi[row - 3][column - 3] == red)
                            flagForRWin = true;
                    }
                    else{
                        if ((qi[row][column] == red
                                && qi[row - 1][column + 1] == red
                                && qi[row - 2][column + 2] == red
                                && qi[row - 3][column + 3] == red)
                                || (qi[row][column] == red
                                && qi[row - 1][column - 1] == red
                                && qi[row - 2][column - 2] == red
                                && qi[row - 3][column - 3] == red))
                            flagForRWin = true;
                    }
                }
                if (flagForRWin) {
                    System.out.println("The red player win the game!");
                    break;
                }
            }

            count++;//棋子数加1,并用于谁下棋子的判断

            //棋盘下满棋子,是平局
            if(count == 6*7){
                System.out.println("棋盘棋子已经下满,是平局!");
                break;
            }
        }//下棋循环结束
    }//方法块

}//类块

文章来源: blog.csdn.net,作者:一条coding,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/skylibiao/article/details/81289569

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。