Java --- ステージプロジェクト --- バックギャモン

Java --- ステージプロジェクト --- バックギャモン


ここに画像の説明を挿入

ニーズの表明

バックギャモン ボードは 10 × 10 の正方形で、2 人のバックギャモン プレイヤー (A、B) がいます。A がボードに駒を置いた後、B が別の駒を置き、というように、一方が勝つかボード上のスペースがなくなるまで続けます。条件は、
図に示すように、線またはスラッシュ上に同時に A または B の連続する 5 つの部分があることです。
ここに画像の説明を挿入

技術的な実現

静的変数:

  • 文法:
public static 数据类型 变量名 = 变量值;

staticがない場合は非静的変数です


  • 静的変数はクラス内でのみ定義でき、メソッド内では定義できないことを説明します。
    静的変数は、静的に変更されたメソッドで使用でき、非静的メソッドでもアクセスできます。主
    な解決策は、静的メソッドでは非静的変数にアクセスできないことです。
public class study {
    
          //类
    //静态变量只能定义在类中,不能定义在方法中
    public static  String  name = "张三";
    public static void main(String[] args) {
    
        //方法
        System.out.println(name);	//可以调用
    }
}

静的メソッド:

  • 文法
public static 返回值类型 方法名(){
    
    

}
  • 解説
    静的メソッドはボックスに相当しますが、コードはボックス内に配置されますので、これらのコードを使用する必要がある場合は、指定された位置にボックスを配置するだけです。
public class study {
    
          //类
    public static void main(String[] args) {
    
        //方法
        show();
    }
    public static void show(){
    
    
        System.out.println("张三");
        System.out.println("男");
        System.out.println("20");
    }
}

出力:

チャン サン
男性
20

基板製作

1. チェス盤を作る

  • 入力メソッドでタブ文字を使用してチェス盤をコンソールに直接出力し、その位置の特徴を調べます。
  • 2 次元配列を使用してチェス盤を再作成する
public class study {
    
    
    public static char[][] chess_map={
    
    
            {
    
    '┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}

    };
    public static String row = "────";
    public static void main(String[] args) {
    
    
        System.out.println(" 0    1    2    3    4    5    6    7    8    9");
        for(int i=0;i<chess_map.length;i++) {
    
    //外层循环控制行
            System.out.print(i);
            for(int j=0;j<chess_map[i].length;j++){
    
    //内层循环控制列
                if(j==chess_map[i].length-1){
    
    //最后一行不打印——
                    System.out.print(chess_map[i][j]);
                }else{
    
    
                    System.out.print(chess_map[i][j]+row);
                }
            }
            System.out.println();
            if(i<chess_map.length-1){
    
    //排除最后一行
                System.out.println(" │    │    │    │    │    │    │    │    │    │");
            }
        }
    }
}
  • チェス盤はプレーヤーの使用中に繰り返し表示されるため、チェス盤を最適化するにはメソッドを使用する必要があります。
    チェス盤をメソッドに入れて直接呼び出します。

2. 横たわる

  • プレイヤーAとBが交互にプレイします
  • チェスの駒の位置は 0 ~ 100 の整数である必要があり、既存のチェスの駒は使用できません
    public static char pieceA = '○';//玩家A的棋子
    public static char pieceB = '■';//玩家B的棋子
    public static void main(String[] args) {
    
    
        draw_map();
        int sum = chess_map.length*chess_map[0].length;
        Scanner sc = new Scanner(System.in);
        for(int i=0;i<sum;i++){
    
    
            System.out.println(i%2==0 ? "玩家A落子:":"玩家B落子:");
            int position;
            while(true){
    
    
                //保证落子成功
                if(sc.hasNextInt()){
    
    //判断Scanner中是否有输入的数据
                    position = sc.nextInt();
                    if(position >= 0 && position < sum){
    
    
                        char currentpiece = (i%2==0) ? pieceA : pieceB;
                        int row = position / chess_map.length;//位置除以棋盘数组长度得到行号
                        int col = position % chess_map[0].length;//位置取模棋盘数组的总列数得到列号
                        if(chess_map[row][col]==pieceA || chess_map[row][col]==pieceB) {
    
    
                            System.out.println("该位置已经有棋子,请重新输入:");
                            continue;
                        }else {
    
    
                            chess_map[row][col] = currentpiece;
                            break;
                        }
                    }else{
    
    
                        System.out.println("非法输入");
                    }
                }else{
    
    
                    System.out.println("非法输入");
                    sc.next();//将Scanner中的数据取出来,防止死循环
                }
            }
            //落子成功后棋盘需要重新打印
            draw_map();
        }
    }
  • ドロップが完了したら、当選したかどうかを確認する必要があります
  • チェス盤はまだ使用されていないため、勝者にプロンプ​​トを表示する必要があります
            for (int m = 0; m < chess_map.length; m++) {
    
    
                for (int j = 0; j < chess_map[0].length; j++) {
    
    
                    //第一种,水平方向
                    boolean case1 = (j + 4 < chess_map[0].length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m][j + 1] == currentpiece
                            && chess_map[m][j + 2] == currentpiece
                            && chess_map[m][j + 3] == currentpiece
                            && chess_map[m][j + 4] == currentpiece;
                    //第二种,垂直方向
                    boolean case2 = (m + 4 < chess_map.length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m + 1][j] == currentpiece
                            && chess_map[m + 2][j] == currentpiece
                            && chess_map[m + 3][j] == currentpiece
                            && chess_map[m + 4][j] == currentpiece;
                    //第三种,135°角
                    boolean case3 = (i + 4 < chess_map.length)
                            && (j + 4 < chess_map[0].length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m + 1][j + 1] == currentpiece
                            && chess_map[m + 2][j + 2] == currentpiece
                            && chess_map[m + 3][j + 3] == currentpiece
                            && chess_map[m + 4][j + 4] == currentpiece;
                    //第四种,45°角
                    boolean case4 = (m > 4) && (j + 4 < chess_map[0].length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m - 1][j + 1] == currentpiece
                            && chess_map[m - 2][j + 2] == currentpiece
                            && chess_map[m - 3][j + 3] == currentpiece
                            && chess_map[m - 4][j + 4] == currentpiece;
                    if (case1 || case2 || case3 || case4) {
    
    
                        System.out.println(m % 2 == 0 ? "玩家A胜利" : "玩家B胜利");
                        break outer;
                    }
                }
            }
            i++;
        }
        if(i==100){
    
    
            System.out.println("平局");
        }

3. 効果音

  • 動き、不正な動き、勝利時の効果音を追加しました

書類の準備が必要
ここに画像の説明を挿入

    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 java.util.Scanner;

public class study {
    
    
    public static char[][] chess_map={
    
    
            {
    
    '┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
            {
    
    '└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}

    };
    public static String ro = "────";
    public static char pieceA = '○';//玩家A的棋子
    public static char pieceB = '■';//玩家B的棋子
    public static int i = 0;//总次数
    public static void main(String[] args) {
    
    
        draw_map();
        int sum = chess_map.length * chess_map[0].length;
        Scanner sc = new Scanner(System.in);
        outer:
        while(i<sum){
    
    
            System.out.println(i % 2 == 0 ? "玩家A落子:" : "玩家B落子:");
            char currentpiece = (i % 2 == 0) ? pieceA : pieceB;
//            int position_row,position_col;
            int position;
            while (true) {
    
    
                //保证落子成功
                if (sc.hasNextInt()) {
    
    //判断Scanner中是否有输入的数据
//                    System.out.println("请输入行:");
//                    position_row = sc.nextInt();
//                    System.out.println("请输入列:");
//                    position_col = sc.nextInt();
                    position = sc.nextInt();
//                    if(position_row >= 0 && position_row < chess_map.length && position_col>=0 && position_col < chess_map[0].length){
    
    
                    if (position >= 0 && position < sum) {
    
    
                        int row = position / chess_map.length;//位置除以棋盘数组长度得到行号
                        int col = position % chess_map[0].length;//位置取模棋盘数组的总列数得到列号
//                        int row = position_row;
//                        int col = position_col;
                        if (chess_map[row][col] == pieceA || chess_map[row][col] == pieceB) {
    
    
                            System.out.println("该位置已经有棋子,请重新输入:");
                            continue;
                        } else {
    
    
                            chess_map[row][col] = currentpiece;
                            break;
                        }
                    } else {
    
    
                        System.out.println("非法输入");
                    }
                } else {
    
    
                    System.out.println("非法输入");
                    sc.next();//将Scanner中的数据取出来,防止死循环
                }
            }
            //落子成功后棋盘需要重新打印
            draw_map();
            for (int m = 0; m < chess_map.length; m++) {
    
    
                for (int j = 0; j < chess_map[0].length; j++) {
    
    
                    //第一种,水平方向
                    boolean case1 = (j + 4 < chess_map[0].length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m][j + 1] == currentpiece
                            && chess_map[m][j + 2] == currentpiece
                            && chess_map[m][j + 3] == currentpiece
                            && chess_map[m][j + 4] == currentpiece;
                    //第二种,垂直方向
                    boolean case2 = (m + 4 < chess_map.length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m + 1][j] == currentpiece
                            && chess_map[m + 2][j] == currentpiece
                            && chess_map[m + 3][j] == currentpiece
                            && chess_map[m + 4][j] == currentpiece;
                    //第三种,135°角
                    boolean case3 = (i + 4 < chess_map.length)
                            && (j + 4 < chess_map[0].length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m + 1][j + 1] == currentpiece
                            && chess_map[m + 2][j + 2] == currentpiece
                            && chess_map[m + 3][j + 3] == currentpiece
                            && chess_map[m + 4][j + 4] == currentpiece;
                    //第四种,45°角
                    boolean case4 = (m > 4) && (j + 4 < chess_map[0].length)
                            && chess_map[m][j] == currentpiece
                            && chess_map[m - 1][j + 1] == currentpiece
                            && chess_map[m - 2][j + 2] == currentpiece
                            && chess_map[m - 3][j + 3] == currentpiece
                            && chess_map[m - 4][j + 4] == currentpiece;
                    if (case1 || case2 || case3 || case4) {
    
    
                        System.out.println(m % 2 == 0 ? "玩家A胜利" : "玩家B胜利");
                        break outer;
                    }
                }
            }
            i++;
        }
        if(i==100){
    
    
            System.out.println("平局");
        }
    }
    public static void draw_map () {
    
    
        System.out.println(" 0    1    2    3    4    5    6    7    8    9");
        for (int i = 0; i < chess_map.length; i++) {
    
    //外层循环控制行
            System.out.print(i);
            for (int j = 0; j < chess_map[i].length; j++) {
    
    //内层循环控制列
                if (j == chess_map[i].length - 1) {
    
    //最后一行不打印——
                    System.out.print(chess_map[i][j]);
                } else {
    
    
                    System.out.print(chess_map[i][j] + ro);
                }
            }
            System.out.println();
            if (i < chess_map.length - 1) {
    
    //排除最后一行
                System.out.println(" │    │    │    │    │    │    │    │    │    │");
            }
        }
    }
    
}

おすすめ

転載: blog.csdn.net/weixin_72138633/article/details/131295751