java五子棋代码(控制台输出)


import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        FiveInFive fiveInFive = new FiveInFive();
        fiveInFive.menuView();
    }
}
//五子棋
//属性:棋盘:0空,1表示黑子,2表示白子,flag回合数
class FiveInFive{
    public int map[][] = new int[19][19];
    public int flag = 0;
    public void init(){
        /*
            负责人: 张三
            功能: 初始化游戏数据
            将棋盘的值初始化为0
            当前回合设为黑棋(flag设为0)
            参数: void
            返回值: void
        */
        int map[][] = new int[19][19];
        flag = 0;
    }
    public int left_right( int r, int c) {
        // 定义一个计数器
        int count = 0;
        // 取出当前下的棋子
        int chessNum = map[r][c];
        // 遍历判断 先向右 列向右遍历,行不变
        // 遍历起点: 当前棋子的列+1
        for (int i = c + 1; i < map[r].length; i++) {
            // 判断右边棋子是否与当前棋子一致
            if (chessNum == map[r][i]) {
                count++;// 找到了一颗相同数值的棋子
            } else {
                break;// 找到了一颗不同数值的棋子  不再继续找
            }
        }
        //向左
        for(int i = c-1;i>=0;i--){
            if(chessNum == map[r][i]){
                count++;
            }else{
                break;
            }
        }
        count++;// 包括当前下的这颗棋子
        return count;
    }
    public int up_down(int r, int c){
        int count = 0;
        int chessNum = map[r][c];
        //向上
        for(int i = c-1; i>=0;i--){
            if(chessNum == map[i][c]){
                count++;
            }else{
                break;
            }
        }
        //向下
        for(int i = c+1;i<map.length;i++){
            if(chessNum == map[i][c]){
                count++;
            }else{
                break;
            }
        }
        count++;
        return count;
    }
    public int leftUp_RightDown(int r, int c){
        int count = 0;
        int chessNum = map[r][c];
        //右下
        for(int i = r+1,j=c+1;i<map.length&&j<map[r].length;i++,j++){
            if(chessNum == map[i][j]){
                count++;
            }else{
                break;
            }
        }
        //左上
        for(int i=r-1,j=c-1;i>=0&&j>=0;i--,j--){
            if(chessNum == map[i][j]){
                count++;
            }else{
                break;
            }
        }
        count++;
        return count;
    }
    public int leftDown_RightUp( int r, int c){
        int count = 0;
        int chessNum = map[r][c];
        //左下
        for(int i = r+1,j=c-1;i<map.length&&j>=0;i++,j--){
            if(chessNum == map[i][j]){
                count++;
            }else{
                break;
            }
        }
        //右上
        for(int i=r-1,j=c+1;i>=0&&j<map[r].length;i--,j++){
            if(chessNum == map[i][j]){
                count++;
            }else{
                break;
            }
        }
        count++;
        return count;
    }
    public int isWin( int x, int y){
        /*
            *难点1
            负责人: 张三
            功能: 根据传入的坐标(map对应位置)和flag值 判断落点后是否获胜
            参数:
            x: 当前回合落子的x坐标
            y: 当前回合落子的y坐标
            返回值:
            0表示没有获胜
            1表示黑子胜利
            2表示白子胜利
        */
        if(left_right (x, y) >= 5
                || up_down (x, y) >= 5
                || leftUp_RightDown( x, y) >= 5
        ){
            if(flag % 2 ==0){
                return 1;
            }else{
                return 2;
            }

        }
        return 0;
    }
    public int playerMove(int x, int y){
        /*
            负责人: 张三
            功能: 在指定位置落子
            如果map[x][y]是空地 则修改map[x][y]的值:改为相应颜色(flag对应颜色)
            否则不操作
            参数:
            x: 当前回合落子的x坐标
            y: 当前回合落子的y坐标
            返回值:
            0表示落子失败 (棋盘已经有子)
            1表示落子成功
        */
        if(map[x][y] == 0){
            if(flag % 2 == 0){
                map[x][y] = 1;
            }else{
                map[x][y] = 2;
            }
            return 1;
        }
        return 0;
    }
    public void menuView(){
        boolean fla = true;
        do {

            System.out.println("===============菜单==============");
            System.out.println("1.进入游戏:调用游戏界面函数gameView();");
            System.out.println("2.进入设置: 敬请期待...");
            System.out.println("3.退出游戏: 调用exit(0);");
            System.out.print("请选择(1-3):");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.next();
            switch (choice){
                case "1":
                    System.out.println("1.进入游戏");
                    gameView();
                    break;
                case "2":
                    System.out.println("2.进入设置");
                    System.out.println("敬请期待...");
                    break;
                case "3":
                    System.out.println("3.退出游戏");
                    System.out.println("已退出");
                    fla =false;
                    break;
                default:
                    System.out.println("输入错误");
            }

        }while (fla);

    }
    public void gameView_ShowMap(){
        /*
            负责人: 张三
            功能: 根据map数组 打印游戏棋盘
            参数: void
            返回值: void
        */
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public void winView(){
        /*
            负责人: 张三
            功能: 根据flag的值 打印游戏胜利界面 用户可以按任意键回到主菜单
            参数: void
            返回值: void
        */
        System.out.println("==========================");
        System.out.println("==========================");
        if(flag %2 != 0){
            System.out.println("========黑棋游戏胜利=========");
        }else{
            System.out.println("========白棋游戏胜利=========");
        }

        System.out.println("==========================");
        System.out.println("==========================");
        System.out.println("==========================");
    }
    public void gameView(){
        /*
            *难点2
            负责人: 张三
            功能: 游戏界面整合
            初始化游戏数据(调用函数init())
            while(1){
            打印游戏界面(调用函数gameView_ShowMap())
            接收玩家坐标输入
            落子(调用落子函数playerMove())
            (如果落子失败 重新开始循环)
            判断游戏是否胜利(调用胜利判断函数isWin())
            (如果游戏胜利 调用胜利界面函数 然后结束当前界面)
            切换玩家(修改flag值)
            }
            参数: void
            返回值: void
       */
        init();
        Scanner scanner = new Scanner(System.in);
        gameView_ShowMap();
        while(true){
            if(flag % 2 == 0){
                System.out.println("现在是黑棋走:");
            }else{
                System.out.println("现在是白棋走:");
            }
            int x = scanner.nextInt();
            int y = scanner.nextInt();

            if(playerMove(x,y) == 1){
                flag ++;
            }

            gameView_ShowMap();
            if(isWin(x,y) != 0){
                winView();
                break;
            }

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值