有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java控制台中的TictaToe 5x5

我正在写一个游戏的零和交叉与5x5控制台销售。 面临以下问题:

1)由于某种原因,游戏可以在第一次击球和第二次击球后完成,这取决于选择的细胞类型。例如,当我选择0和6个单元格时,X已经是赢家。或者如果你选择22号,游戏也会结束

2)美丽的细胞边界。事实证明,对于一位数或两位数的人来说,这是一个不错的选择。如何制作一张漂亮的桌子?? 代码:

package game;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GameField {

    static int [] canvas = {0,0,0,0,0,
                            0,0,0,0,0,
                            0,0,0,0,0,
                            0,0,0,0,0,
                            0,0,0,0,0}; 

    public static void main(String[] args){

        boolean b;
        boolean isCurrentX = false;
        do {
            isCurrentX = !isCurrentX;
            drawCanvas();
            System.out.println("Ходит " + (isCurrentX ? "X" : "O"));
            int n = getNumber();
            canvas[n] = isCurrentX ? 1 : 2;
            b = !isGameOver(n);
            if (isDraw()){
                System.out.println("Draw");
                return;
            }
        } while (b);
        drawCanvas();
        System.out.println();

        System.out.println("ПОбедитель: " + (isCurrentX ? "X" : "O") + "!");
    }

    static int getNumber(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true){
            try {
                int n = Integer.parseInt(reader.readLine());
                if (n >= 0 && n < canvas.length && canvas[n]==0){
                    return n;
                }
                System.out.println("Выберите свободное поле и введите его порядковый номер");
            } catch (NumberFormatException e) {
                System.out.println("Пожалуйста, введите порядковый номер клетки");
            } catch (IOException e) {
            }
        }
    }

    static boolean isGameOver(int n){
        //
        //  0   1  2  3 4
        //  5   6  7  8 9 
        // 10 11 12 13 14
        // 15 16 17 18 19
        // 20 21 22 23 24

        int row = n-n%5; 
        if (canvas[row]==canvas[row+1] &&
                canvas[row]==canvas[row+2] &&
                        canvas[row]==canvas[row+3] &&
                                canvas[row]==canvas[row+4]) return true;

        int column = n%5; 
        if (canvas[column]==canvas[column+5])
            if (canvas[column]==canvas[column+10])
                if (canvas[column]==canvas[column+15])
                    if (canvas[column]==canvas[column+20])return true;

        if (n%2!=0) return false;

        if (n%4==0){

            if (canvas[0] == canvas[6] &&
                    canvas[0] == canvas[12] &&
                            canvas[0] == canvas[18] &&
                                    canvas[0] == canvas[24]) return true;
            if (n!=4) return false;
        }
        return canvas[2] == canvas[4] &&
                canvas[2] == canvas[8]&&
                        canvas[2] == canvas[12]&&
                                canvas[2] == canvas[16]&&
                                        canvas[2] == canvas[20];
    }

    static void drawCanvas(){
        System.out.println("     |     |     ");
        for (int i = 0; i < canvas.length; i++) {
            if (i!=0){
                if (i%5==0) {
                    System.out.println();
                    System.out.println("_____|_____|_____");
                    System.out.println("     |     |     ");
                }
                else
                    System.out.print("|");
            }

            if (canvas[i]==0) System.out.print("  " + i + "  ");
            if (canvas[i]==1) System.out.print("  X  ");
            if (canvas[i]==2) System.out.print("  O  ");
        }
        System.out.println();
        System.out.println("     |     |     ");
    }

    public static boolean isDraw() {
        for (int n : canvas) if (n==0) return false;
        return true;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    我可以帮助您制作一个漂亮的单元格边框:

    static void drawCanvas(){
        System.out.println("     |     |     |     |     |");
        for (int i = 0; i < canvas.length; i++) {
            if (i!=0){
                System.out.print("|");
                if (i%5==0) {
                    System.out.println();
                    System.out.println("_____|_____|_____|_____|_____|");
                    System.out.println("     |     |     |     |     |");
                }
            }
    
            if (canvas[i]==0) {
                if(i<10) {
                    System.out.print("  " + i + "  ");
                }
                else {
                    System.out.print("  " + i + " ");
                }
            }
            if (canvas[i]==1) System.out.print("  X  ");
            if (canvas[i]==2) System.out.print("  O  ");
        }
        System.out.print("|");
        System.out.println();
        System.out.println("     |     |     |     |     |");
    }
    

    对于您的isGameOver()方法,我认为您可以使用以下blog中的技术: 请参阅isWin()方法并将约束3更改为5