Search code examples
javaconsoletic-tac-toe

TicTacToe 5x5 in Java console


I'm writing a game of noughts and crosses with a 5x5 console sales. Faced with the following problems:

1) For some reason, game can be finished after the first stroke and the second, depending on what type of cell chosen. For example, when i choosing 0 and 6 cells, X already a winner. Or if you choose the 22nd, the game also ends.

2) A beautiful cell borders. It turns out to make nice or for single-digit or double-digit for. How to make a beautiful table?? Code:

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;
    }
}

Solution

  • I can help you to make a beautiful cell borders:

    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("     |     |     |     |     |");
    }
    

    For your isGameOver() method, I think you can use technique from this blog: See the isWin() method and change constraint 3 to 5.