Home > Software design >  TicTacToe - checkWinner
TicTacToe - checkWinner

Time:10-29

I wrote a code for a simple console aplication, but however it doesn't output wheter there is a winner or not. Here is my code. I tried to search for errors, but couldn't find it myself. `

public class TicTacToe {

    static ArrayList<Integer> plaeyer1position = new ArrayList<Integer>();
    static ArrayList<Integer> plaeyer2position = new ArrayList<Integer>();
    public static void main(String[] args) {
        char[][] board = {{' ', '|', ' ', '|', ' '},
                {'-', ' ', '-', ' ', '-'},
                {' ', '|', ' ', '|', ' '},
                {'-', ' ', '-', ' ', '-'},
                {' ', '|', ' ', '|', ' '}};

        gameboard(board);

        while(true) {
            Scanner scn = new Scanner(System.in);
            System.out.println("Geben Sie die Position ein (1-9):");
            int position = scn.nextInt();
            bestimmmung_der_position(board, position, "Spieler 1");

            System.out.println("Geben Sie die Position ein (1-9):");
            int position2 = scn.nextInt();
            bestimmmung_der_position(board, position2, "Spieler 2");

            gameboard(board);

            String result = checkWinner();
            System.out.println(result);
        }


        }



    public static void gameboard(char[][] board) {
        for (char[] row : board) {
            for (char c : row) {
                System.out.print(c);
            }
            System.out.println();
        }
    }

    public static void bestimmmung_der_position(char[][] board,int position, String spieler) {

        char symbol = ' ';

        if(spieler.equals("Spieler 1")){
            symbol = 'X';
            plaeyer1position.add(position);
        } else if (spieler.equals("Spieler 2")) {
            symbol = 'O';
            plaeyer2position.add(position);
        }


        switch (position) {
            case 1:
                board[0][0] = symbol;
                break;
            case 2:
                board[0][2] = symbol;
                break;
            case 3:
                board[0][4] = symbol;
                break;
            case 4:
                board[2][0] = symbol;
                break;
            case 5:
                board[2][2] = symbol;
                break;
            case 6:
                board[2][4] = symbol;
                break;
            case 7:
                board[4][0] = symbol;
                break;
            case 8:
                board[4][2] = symbol;
                break;
            case 9:
                board[4][4] = symbol;
                break;
            default:
                break;

        }
        gameboard(board);
    }

    public static String checkWinner(){

        List top_zeile = Arrays.asList(1,2,3);
        List mid_zeile = Arrays.asList(4,5,6);
        List bot_zeile = Arrays.asList(7,8,9);

        List links_reihe = Arrays.asList(1,4,7);
        List mitte_reihe = Arrays.asList(2,5,8);
        List rechts_reihe = Arrays.asList(3,6,9);

        List diagonale_rechts = Arrays.asList(1,5,9);
        List diagonale_links = Arrays.asList(7,5,3);

        List<List> gewinner = new ArrayList<List>();
        gewinner.add(top_zeile);
        gewinner.add(mid_zeile);
        gewinner.add(bot_zeile);
        gewinner.add(links_reihe);
        gewinner.add(mitte_reihe);
        gewinner.add(rechts_reihe);
        gewinner.add(diagonale_links);
        gewinner.add(diagonale_rechts);

        for(List l: gewinner){
            if(plaeyer1position.contains(l)){
                return "Spieler 1 gewonnen";
            } else if (plaeyer2position.contains(l)) {
                return "Spieler 2 gewonnen";
            }
            else if(plaeyer1position.size() plaeyer2position.size()==9){
                return "Kein Spieler hat gewonnen.\n";
            }
        }

        return "";
    }
}

`

I expected my funktion "checkWinner" to check wheter there is a winning combination and output a winner if the combination has been matched.

CodePudding user response:

The issue is with the following condition

plaeyer1position.contains(l)

This code will check if ArrayList<Integer> plaeyer1position contains List<Integer> l

However, plaeyer1position does not contain List objects; it contains Integer objects

So, the condition will never be true

Try using this instead (same for plaeyer2position)

plaeyer1position.containsAll(l)

X.containsAll(Y) will check if X contains all elements found in Y

  • Related