首页 > 编程语言 >8---阶段项目:五子棋(附带源码)

8---阶段项目:五子棋(附带源码)

时间:2024-09-08 15:21:37浏览次数:3  
标签:chessboard 五子棋 System --- 源码 static println public out

8阶段项目:五子棋


8.1-技术实现


1.静态变量
静态变量只能定义在类中,不能定义在方法中。静态变量可以在static修饰的方法中使用,也可以在非静态的方法中访问。主要解决在静态方法中不能访问非静态的变量。
2.静态方法
静态方法就相当于一个箱子,只是这个箱子中装的是代码,需要使用这些代码的时候,就把这个箱子放在指定的位置即可。
 

/**
 * 静态变量和静态方法
 */
public class staticTest {
    //静态变量只能定义在类中,不能定义在方法中
    public static String name="张三";

    public static void main(String[] args) {
//        System.out.println(name);
//        System.out.println("张三");
//        System.out.println("男");
//        System.out.println("20");
//        System.out.println("张三");
//        System.out.println("男");
//        System.out.println("20");
        show();
    }
    public static void show(){
        System.out.println("张三");
        System.out.println("男");
        System.out.println("20");
    }
}

---------------------------分割线--------------------------------------------------------------

8.2-棋盘制作


实现步骤分析

1.制作棋盘

a.使用输入法中的制表符在控制台直接打印出棋盘,然后寻找落子位置的特征
 

/**
 * 五子棋
 */
public class Gobang {
    public static void main(String[] args) {
        System.out.println("┌────┬────┬────┬────┬────┬────┬────┬────┬────┐");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
        System.out.println("│    │    │    │    │    │    │    │    │    │");
        System.out.println("└────┴────┴────┴────┴────┴────┴────┴────┴────┘");
    }
}

b.利用二维数组重新制作棋盘

public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

    public static void main(String[] args) {
        
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }
            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }
}


c.棋盘在玩家使用过程中会反复展示,需要使用方法来进行优化

public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

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

    public static void showChessboard(){
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }

            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }

}


2.落子

a.玩家A、B会交替落子

public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

    public static void main(String[] args) {
       showChessboard();
       int totalPosition=chessboard.length*chessboard[0].length;
       for(int times=0;times<totalPosition;times++)
       {
           System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");
       }
    }

    public static void showChessboard(){
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }

            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }

}


b.落子位置必须是0-100之间的整数,且不能使用已经存在棋子的位置

import java.util.Scanner;

public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

    public static char pieceA='○';
    public static char pieceB='■';

    public static void main(String[] args) {
       showChessboard();
       int totalPosition=chessboard.length*chessboard[0].length;
        Scanner sc=new Scanner(System.in);
       for(int times=0;times<totalPosition;times++)
       {
           System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");

               while(true){//保证落子成功的一个循环
                   if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入
                   int position=sc.nextInt();
                     if(position>=0&&position<totalPosition){
                         char currentPiece=times%2==0?pieceA:pieceB;
                         int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号
                         int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号
                         if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){
                             System.out.println("非法落子,请重新选择落子位置");
                             continue;
                         }else{
                             chessboard[row][col]=currentPiece;
                             break;
                         }

                      }else{
                           System.out.println("非法落子,请重新选择落子位置");
                      }
                   }else{
                       System.out.println("非法落子,请重新选择落子位置");
                       sc.next();//将Scanner中存储的数据取出,防止死循环
                   }
                }
               //落子完成后,棋盘需要重新展示
               showChessboard();
           }
       }

    public static void showChessboard(){
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }

            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }

}


c.落子完成后,需要校验是否获胜

import java.util.Scanner;


public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

    public static char pieceA='○';
    public static char pieceB='■';

    public static void main(String[] args) {
       showChessboard();
       int totalPosition=chessboard.length*chessboard[0].length;
        Scanner sc=new Scanner(System.in);
        outer://标记外层循环
       for(int times=0;times<totalPosition;times++)
       {
           System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");
           char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子
               while(true){//保证落子成功的一个循环
                   if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入
                   int position=sc.nextInt();
                     if(position>=0&&position<totalPosition){

                         int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号
                         int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号
                         if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){
                             System.out.println("非法落子,请重新选择落子位置");
                             continue;
                         }else{
                             chessboard[row][col]=currentPiece;
                             break;
                         }

                      }else{
                           System.out.println("非法落子,请重新选择落子位置");
                      }
                   }else{
                       System.out.println("非法落子,请重新选择落子位置");
                       sc.next();//将Scanner中存储的数据取出,防止死循环
                   }
                }
               //落子完成后,棋盘需要重新展示
               showChessboard();
           //判断获胜情况,一共4种

           for(int i=0;i<chessboard.length;i++){
               for(int j=0;j<chessboard[i].length;j++)
               {
                   //第一种:水平方向上存在同一玩家的连续5颗棋子
                   //(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)
                   boolean case1=(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i][j+1]==currentPiece
                           &&chessboard[i][j+2]==currentPiece
                           &&chessboard[i][j+3]==currentPiece
                           &&chessboard[i][j+4]==currentPiece;
                   //第二种:水平方向上存在同一玩家的连续五颗棋子
                   //(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)
                   boolean case2=(i+4<chessboard.length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i+1][j]==currentPiece
                           &&chessboard[i+2][j]==currentPiece
                           &&chessboard[i+3][j]==currentPiece
                           &&chessboard[i+4][j]==currentPiece;
                   //第三种:135度角上存在同一玩家的连续5颗棋子
                   //(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)
                    boolean case3=(i+4<chessboard.length)
                            &&(j+4<chessboard[i].length)
                            &&chessboard[i][j]==currentPiece
                            &&chessboard[i+1][j+1]==currentPiece
                            &&chessboard[i+2][j+2]==currentPiece
                            &&chessboard[i+3][j+3]==currentPiece
                            &&chessboard[i+4][j+4]==currentPiece;
                    //第四种:45度角上存在同一玩家的连续五颗棋子
                    //(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)
                   boolean case4=(i>4)&&(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i-1][j+1]==currentPiece
                           &&chessboard[i-2][j+2]==currentPiece
                           &&chessboard[i-3][j+3]==currentPiece
                           &&chessboard[i-4][j+4]==currentPiece;
                   if(case1||case2||case3||case4){
                       System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");
                       break outer;
                   }
               }

             }
           }
       }

    public static void showChessboard(){
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }

            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }

}


d.棋盘使用完毕还未分出胜负,需要提示

import java.util.Scanner;

/**
 * 五子棋
 */
public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

    public static char pieceA='○';//玩家A的棋子
    public static char pieceB='■';//玩家B的棋子

    public static int times=0;//记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子

    public static void main(String[] args) {
       showChessboard();
       int totalPosition=chessboard.length*chessboard[0].length;
        Scanner sc=new Scanner(System.in);
        outer://标记外层循环
       while(times<totalPosition)
       {
           System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");
           char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子
               while(true){//保证落子成功的一个循环
                   if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入
                   int position=sc.nextInt();
                     if(position>=0&&position<totalPosition){

                         int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号
                         int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号
                         if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){
                             System.out.println("非法落子,请重新选择落子位置");
                             continue;
                         }else{
                             chessboard[row][col]=currentPiece;
                             break;
                         }

                      }else{
                           System.out.println("非法落子,请重新选择落子位置");
                      }
                   }else{
                       System.out.println("非法落子,请重新选择落子位置");
                       sc.next();//将Scanner中存储的数据取出,防止死循环
                   }
                }
               //落子完成后,棋盘需要重新展示
               showChessboard();
           //判断获胜情况,一共4种

           for(int i=0;i<chessboard.length;i++){
               for(int j=0;j<chessboard[i].length;j++)
               {
                   //第一种:水平方向上存在同一玩家的连续5颗棋子
                   //(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)
                   boolean case1=(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i][j+1]==currentPiece
                           &&chessboard[i][j+2]==currentPiece
                           &&chessboard[i][j+3]==currentPiece
                           &&chessboard[i][j+4]==currentPiece;
                   //第二种:水平方向上存在同一玩家的连续五颗棋子
                   //(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)
                   boolean case2=(i+4<chessboard.length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i+1][j]==currentPiece
                           &&chessboard[i+2][j]==currentPiece
                           &&chessboard[i+3][j]==currentPiece
                           &&chessboard[i+4][j]==currentPiece;
                   //第三种:135度角上存在同一玩家的连续5颗棋子
                   //(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)
                    boolean case3=(i+4<chessboard.length)
                            &&(j+4<chessboard[i].length)
                            &&chessboard[i][j]==currentPiece
                            &&chessboard[i+1][j+1]==currentPiece
                            &&chessboard[i+2][j+2]==currentPiece
                            &&chessboard[i+3][j+3]==currentPiece
                            &&chessboard[i+4][j+4]==currentPiece;
                    //第四种:45度角上存在同一玩家的连续五颗棋子
                    //(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)
                   boolean case4=(i>4)&&(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i-1][j+1]==currentPiece
                           &&chessboard[i-2][j+2]==currentPiece
                           &&chessboard[i-3][j+3]==currentPiece
                           &&chessboard[i-4][j+4]==currentPiece;
                   if(case1||case2||case3||case4){
                       System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");
                       break outer;
                   }
               }

             }
           times++;
           }
            if(times==totalPosition){//说明棋盘已经用完还未分出胜负
                System.out.println("平局");
            }
       }

    public static void showChessboard(){
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }

            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }

}


3.声音特效


为落子、非法落子及获胜添加音效

import javax.print.DocFlavor;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.util.Scanner;

/**
 * 五子棋
 */
public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

    public static char pieceA='○';//玩家A的棋子
    public static char pieceB='■';//玩家B的棋子

    public static int times=0;//记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子

    public static void main(String[] args) {
       showChessboard();
       int totalPosition=chessboard.length*chessboard[0].length;
        Scanner sc=new Scanner(System.in);
        outer://标记外层循环
       while(times<totalPosition)
       {
           System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");
           char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子
               while(true){//保证落子成功的一个循环
                   if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入
                   int position=sc.nextInt();
                     if(position>=0&&position<totalPosition){

                         int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号
                         int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号
                         if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){
                             System.out.println("非法落子,请重新选择落子位置");
                             playAudio("illegal.wav");
                             continue;
                         }else{
                             chessboard[row][col]=currentPiece;
                             playAudio("fill.wav");
                             break;
                         }

                      }else{
                           System.out.println("非法落子,请重新选择落子位置");
                         playAudio("illegal.wav");
                      }
                   }else{
                       System.out.println("非法落子,请重新选择落子位置");
                       playAudio("illegal.wav");
                       sc.next();//将Scanner中存储的数据取出,防止死循环
                   }
                }
               //落子完成后,棋盘需要重新展示
               showChessboard();
           //判断获胜情况,一共4种

           for(int i=0;i<chessboard.length;i++){
               for(int j=0;j<chessboard[i].length;j++)
               {
                   //第一种:水平方向上存在同一玩家的连续5颗棋子
                   //(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)
                   boolean case1=(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i][j+1]==currentPiece
                           &&chessboard[i][j+2]==currentPiece
                           &&chessboard[i][j+3]==currentPiece
                           &&chessboard[i][j+4]==currentPiece;
                   //第二种:水平方向上存在同一玩家的连续五颗棋子
                   //(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)
                   boolean case2=(i+4<chessboard.length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i+1][j]==currentPiece
                           &&chessboard[i+2][j]==currentPiece
                           &&chessboard[i+3][j]==currentPiece
                           &&chessboard[i+4][j]==currentPiece;
                   //第三种:135度角上存在同一玩家的连续5颗棋子
                   //(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)
                    boolean case3=(i+4<chessboard.length)
                            &&(j+4<chessboard[i].length)
                            &&chessboard[i][j]==currentPiece
                            &&chessboard[i+1][j+1]==currentPiece
                            &&chessboard[i+2][j+2]==currentPiece
                            &&chessboard[i+3][j+3]==currentPiece
                            &&chessboard[i+4][j+4]==currentPiece;
                    //第四种:45度角上存在同一玩家的连续五颗棋子
                    //(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)
                   boolean case4=(i>4)&&(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i-1][j+1]==currentPiece
                           &&chessboard[i-2][j+2]==currentPiece
                           &&chessboard[i-3][j+3]==currentPiece
                           &&chessboard[i-4][j+4]==currentPiece;
                   if(case1||case2||case3||case4){
                       System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");
                       playAudio("win.wav");
                       break outer;
                   }
               }

             }
           times++;
           }
            if(times==totalPosition){//说明棋盘已经用完还未分出胜负
                System.out.println("平局");
            }
       }

    public static void showChessboard(){
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }

            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }

    /**
     * 音效播放
     * @param fileName
     */
    public static void playAudio(String fileName){
        URL url =Gobang.class.getResource(fileName);
        AudioClip clip = Applet.newAudioClip(url);
        clip.play();
        try {
            Thread.sleep(50L);
        } catch (InterruptedException e) {
        }
    }

}

-------------------------------分割线-----------------------------------------------

源码

import javax.print.DocFlavor;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.util.Scanner;

/**
 * 五子棋
 */
public class Gobang {

    public static char[][] chessboard={
            {'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
    };

    public static String separator="─────";

    public static char pieceA='○';//玩家A的棋子
    public static char pieceB='■';//玩家B的棋子

    public static int times=0;//记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子

    public static void main(String[] args) {
       showChessboard();
       int totalPosition=chessboard.length*chessboard[0].length;
        Scanner sc=new Scanner(System.in);
        outer://标记外层循环
       while(times<totalPosition)
       {
           System.out.println(times%2==0?"请玩家A落子:":"请玩家B落子:");
           char currentPiece=times%2==0?pieceA:pieceB;//当前使用的棋子
               while(true){//保证落子成功的一个循环
                   if(sc.hasNextInt()){//检测Scanner中是否有输入的数据并且判断是否为整数,如果没有数据,就需要从控制台输入
                   int position=sc.nextInt();
                     if(position>=0&&position<totalPosition){

                         int row=position/chessboard.length;//位置除以棋盘数组的长度得到行号
                         int col=position%chessboard[0].length;//位置取模棋盘数组的总列数得到列号
                         if(chessboard[row][col]==pieceA||chessboard[row][col]==pieceB){
                             System.out.println("非法落子,请重新选择落子位置");
                             playAudio("illegal.wav");
                             continue;
                         }else{
                             chessboard[row][col]=currentPiece;
                             playAudio("fill.wav");
                             break;
                         }

                      }else{
                           System.out.println("非法落子,请重新选择落子位置");
                         playAudio("illegal.wav");
                      }
                   }else{
                       System.out.println("非法落子,请重新选择落子位置");
                       playAudio("illegal.wav");
                       sc.next();//将Scanner中存储的数据取出,防止死循环
                   }
                }
               //落子完成后,棋盘需要重新展示
               showChessboard();
           //判断获胜情况,一共4种

           for(int i=0;i<chessboard.length;i++){
               for(int j=0;j<chessboard[i].length;j++)
               {
                   //第一种:水平方向上存在同一玩家的连续5颗棋子
                   //(i,j)(i,j+1)(i,j+2)(i,j+3)(i,j+4)
                   boolean case1=(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i][j+1]==currentPiece
                           &&chessboard[i][j+2]==currentPiece
                           &&chessboard[i][j+3]==currentPiece
                           &&chessboard[i][j+4]==currentPiece;
                   //第二种:水平方向上存在同一玩家的连续五颗棋子
                   //(i,j)(i+1,j)(i+2,j)(i+3,j)(i+4,j)
                   boolean case2=(i+4<chessboard.length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i+1][j]==currentPiece
                           &&chessboard[i+2][j]==currentPiece
                           &&chessboard[i+3][j]==currentPiece
                           &&chessboard[i+4][j]==currentPiece;
                   //第三种:135度角上存在同一玩家的连续5颗棋子
                   //(i,j)(i+1,j+1)(i+2,j+2)(i+3,j+3)(i+4,j+4)
                    boolean case3=(i+4<chessboard.length)
                            &&(j+4<chessboard[i].length)
                            &&chessboard[i][j]==currentPiece
                            &&chessboard[i+1][j+1]==currentPiece
                            &&chessboard[i+2][j+2]==currentPiece
                            &&chessboard[i+3][j+3]==currentPiece
                            &&chessboard[i+4][j+4]==currentPiece;
                    //第四种:45度角上存在同一玩家的连续五颗棋子
                    //(i,j)(i-1,j+1)(i-2,j+2)(i-3,j+3)(i-4,j+4)
                   boolean case4=(i>4)&&(j+4<chessboard[i].length)
                           &&chessboard[i][j]==currentPiece
                           &&chessboard[i-1][j+1]==currentPiece
                           &&chessboard[i-2][j+2]==currentPiece
                           &&chessboard[i-3][j+3]==currentPiece
                           &&chessboard[i-4][j+4]==currentPiece;
                   if(case1||case2||case3||case4){
                       System.out.println(times%2==0?"玩家A获得胜利":"玩家B获得胜利");
                       playAudio("win.wav");
                       break outer;
                   }
               }

             }
           times++;
           }
            if(times==totalPosition){//说明棋盘已经用完还未分出胜负
                System.out.println("平局");
            }
       }

    public static void showChessboard(){
        System.out.println("   0     1     2     3     4     5     6     7     8     9");
        for(int i=0;i<chessboard.length;i++){//外层循环控制行
            System.out.print(i+"   ");
            for(int j=0;j<chessboard[i].length;j++){//内层循环控制列
                if(j==chessboard[i].length-1){  //最后一列
                    System.out.print(chessboard[i][j]);
                }
                else{
                    System.out.print(chessboard[i][j]+separator);
                }

            }
            System.out.println();
            if(i<chessboard.length-1){
                System.out.println("    │     │     │     │     │     │     │     │     │     │");
            }
        }
    }

    /**
     * 音效播放
     * @param fileName
     */
    public static void playAudio(String fileName){
        URL url =Gobang.class.getResource(fileName);
        AudioClip clip = Applet.newAudioClip(url);
        clip.play();
        try {
            Thread.sleep(50L);
        } catch (InterruptedException e) {}
    }

}


 

标签:chessboard,五子棋,System,---,源码,static,println,public,out
From: https://blog.csdn.net/2301_80300263/article/details/141999171

相关文章

  • 计算机三级 - 数据库技术 - 第十一章 故障管理 笔记
    第十一章故障管理内容提要:了解故障管理类型及数据库恢复技术了解数据转储技术了解如何利用日志文件进行数据恢复了解硬件容错方案11.1故障管理概述故障类型及解决方案:事务内部故障:导致数据不一致预期的事务内部故障:可通过事务过程本身发现解决办......
  • 一起学RISC-V汇编第6讲之伪指令列表
    一起学RISC-V汇编第6讲之伪指令伪指令是方便程序员使用,相当于为实际指令取的别名,编程时可以直接使用伪指令。上一章已经列出了RISC-V中的伪指令,只是比较分散,这一章以另一个视角重新整理一下伪指令,表格来源于《RISC-V开放架构设计之道1.0.0》1RISC-V伪指令列表伪指令一共60......
  • 【多变量输入超前多步预测】基于CNN-BiLSTM-Attention的光伏功率预测研究(Matlab代码实
                       ......
  • 音频-语言大模型原理
    重磅推荐专栏:《大模型AIGC》《课程大纲》《知识星球》本专栏致力于探索和讨论当今最前沿的技术趋势和应用领域,包括但不限于ChatGPT和StableDiffusion等。我们将深入研究大型模型的开发和应用,以及与之相关的人工智能生成内容(AIGC)技术。通过深入的技术解析和实践经验......
  • Python入门教程-Python 中的字符串及常用操作有哪些
    字符串是编程语言中最常见和最基础的数据类型之一。在Python中,字符串(string)是用于表示文本数据的序列。无论是处理用户输入、文件读写,还是处理网络数据,字符串都是编程中的关键工具之一。Python提供了许多方便的操作和方法来处理字符串数据。本文将带你从基础入门,详细介绍......
  • 一起学RISC-V汇编第5讲之常用指令及伪指令列表
    一起学RISC-V汇编第5讲之常用指令及伪指令列表这一篇介绍一下RISC-V常用的汇编指令,整理成表,便于查阅。1RISC-V指令命名以slt指令为例,如下示意图:大括号{}内列举了每组指令的所有变体,这些变体通过带下滑线的字母(单独的下划线_表示空字段),从左到右连接带下滑线的字母即可组成完整......
  • vulhub spring 远程命令执行漏洞(CVE-2016-4977)
    步骤一:执行以下命令启动靶场环境并在浏览器访问cd/vulhub/spring/CVE-2016-4977#进入漏洞环境所在目录docker-compose up-d#启动靶场 docker ps#查看容器信息步骤二:访问环境步骤三:192.168.0.107:8080/oauth/authorize?response_type=${2*2}&client_id=acme&scope=o......
  • 2024-2025年最值得选的Java毕业设计选题大全推荐:热门选题
    一、前言......
  • MySQL基础(8)- 单行函数(1)
    目录一、函数的理解二、数值函数1.基本函数2.取随机数3.四舍五入截断操作 4.单行函数嵌套5.角度与弧度 6.三角函数7.指数和对数8. 进制间的转换 三、字符串函数 四、日期和时间1.获取日期、时间2.日期与时间戳3.获取月份、星期、星期数、天数等函数 4.......
  • 虚拟机网络模式(桥接、NAT、Host-only)
    虚拟机网络模式(桥接、NAT、Host-only)VMware提供虚拟机服务的时候。不得不提到的就是网络服务。一般情况VMware提供了三种虚拟机的网络模式(桥接、NAT、Host-only),接下来来介绍一下三种模式的区别NAT网络随着Internet的发展和网络应用的增多,IPv4地址枯竭已成为制约网络发展的瓶......