Java控制台中的TicTacToe 5x5

时间:2017-02-11 10:08:00

标签: java console tic-tac-toe

我正在编写一款游戏,并以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 个答案:

答案 0 :(得分: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.