欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lowb版五子棋

程序员文章站 2022-07-04 22:09:39
...

*创建char二维数组储存棋盘状态(作为静态全局变量)
*初始化棋盘
*创建标志flag控制黑白交替下棋
*开始下棋 直到gameover
*创建isGameOver();来判断是否五子连珠
*创建printBoard();负责打印,每下一次棋打印一次
*创建takeChess(flag);负责下棋

import java.util.*;
    class gobang{
        static Scanner input = new Scanner(System.in);
        static String[][] chessBoard = new String[15][15];
        public static void main(String[] args){
            for(int i=0;i<15;i++){
                Arrays.fill(chessBoard[i] , ".");
            }
            int flag = 0;
            boolean gameOver = true;
            while(gameOver){
                printBoard();
                takeChess(flag);
                flag++;
                gameOver = isGameOver();
            }
            System.out.printf("游戏结束了!");
        }
        public static void printBoard(){
            System.out.print("   ");
            for(int k=0;k<15;k++){
                System.out.printf("%3d",k+1);
            } 
            System.out.println();
            System.out.println();
            for(int i=0;i<15;i++){
                System.out.printf("%3d",i+1);
                for(int j=0;j<15;j++){
                    System.out.printf("%3s",chessBoard[i][j]);
                }
                    System.out.println();
                    System.out.println();
                }
            }
        public static void takeChess(int flag){
            if(flag%2==0){
                do{
                    System.out.print("系统:>>>请白方下棋:(先输入纵坐标,再输入横坐标,两次回车) ");
                    int x = input.nextInt();
                    int y = input.nextInt();
                    if ((x<=1||x>=15)&&(y<=1||x>=14)){
                        System.out.println("输入错误,请重新输入。");
                        continue;
                    }
                    else if(!chessBoard[x-1][y-1].equals(".")){
                        System.out.println("输入错误,请重新输入。");
                        continue;
                    }
                    chessBoard[x-1][y-1]="o";
                    break;
                }while(true);
            }
            else{
                do{
                    System.out.print("系统:>>>请黑方下棋:(先输入纵坐标,再输入横坐标,两次回车) ");
                    int x = input.nextInt();
                    int y = input.nextInt();
                    if ((x<=0||x>15)&&(y<=0||y>=15)){
                        System.out.println("输入错误,请重新输入。");
                        continue;
                    }
                    else if(!chessBoard[x-1][y-1].equals(".")){
                        System.out.println("输入错误,请重新输入。");
                        continue;
                    }
                    chessBoard[x-1][y-1]="*";
                    break;
                }while(true);
            }
        }
        public static boolean isGameOver(){
            for(int i=4;i<11;i++){
                for(int j=0;j<11;j++){
                    if(chessBoard[i][j]!="."){
                        int count =1;
                        for(int k=0;k<5;k++){//向右判断
                            if(chessBoard[i][j]==chessBoard[i][j+k+1]){
                                count++;
                            }
                            if(count>=5){
                                return false;
                            }
                        }
                        count =1;
                        for(int k=0;k<4;k++){//向下判断
                            if(chessBoard[i][j]==chessBoard[i+k+1][j]){
                                count++;
                            }
                            if(count>=5){
                                return false;
                            }
                        }
                        count =1;
                        for(int k=0;k<4;k++){//向右上判断
                            if(chessBoard[i][j]==chessBoard[i+k+1][j+k+1]){
                                count++;
                            }
                            if(count>=5){
                                return false;
                            }
                        }
                        count =1;
                        for(int k=0;k<4;k++){//向右下判断
                            if(chessBoard[i][j]==chessBoard[i-k-1][j+k+1]){
                                count++;
                            }
                            if(count>=5){
                                return false;
                            }
                        }
                    }
                }

            }
           return true;
        }
    }
相关标签: java