0

I'm trying to follow a YouTube tutorial on creating a TicTacToe game.

I'm following along, and the instructions and I'm trying to create the playerPositions and computerPositions as ArrayLists. These need to be public/global/static so methods can read them. But if I try to add "public", "static", "protected", etc in front of the ArrayList, it provides an error and suggests I remove the "public", etc.

I think this is an error specifically for Java 17 to Java 20. Other versions don't seem to have this error.

I've indicated where the ArrayLists are declared and where they're used.

package youtube.alexlee;

import java.util.*;

public class TicTacToe {
    public static void main(String[] args) {
        
        // ***** ⬇️⬇️⬇️⬇️
        ArrayList<Integer> playerPositions = new ArrayList<Integer>();
        ArrayList<Integer> computerPositions = new ArrayList<Integer>();

        char[][] gameBoard = {
                {'1', '|', '2', '|', '3' },
                {'-', '+', '-', '+', '-' },
                {'4', '|', '5', '|', '6' },
                {'-', '+', '-', '+', '-' },
                {'7', '|', '8', '|', '9' }
        };

        while (true) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter number (1-9) to place marker.");
            int playerPos = scan.nextInt();
            System.out.println("You selected: " + playerPos);
            placePiece(gameBoard, playerPos, "player");

            Random rand = new Random();
            int compPos = rand.nextInt(9) + 1;
            placePiece(gameBoard, compPos, "computer");
            System.out.println("Computer selected: " + compPos);

            printGameBoard(gameBoard);

            String winning = checkWinner();
            System.out.println(winning);
            if (winning.equals("Congratulations, you win!") || winning.equals("Sorry! Computer wins!")){
                System.exit(0);
            }
        }
    }

    private static void placePiece(char[][] gameBoard, int pos, String user) {

        char symbol = ' ';

        if (user.equals("player")) {
            symbol = 'X';
            // ***** ⬇️⬇️⬇️⬇️
            playerPositions.add(pos);
        } else if (user.equals("computer")) {
            symbol = 'O';
            // ***** ⬇️⬇️⬇️⬇️
            computerPositions.add(pos);
        }

        switch (pos) {
            case 1 -> gameBoard[0][0] = symbol;
            case 2 -> gameBoard[0][2] = symbol;
            case 3 -> gameBoard[0][4] = symbol;
            case 4 -> gameBoard[2][0] = symbol;
            case 5 -> gameBoard[2][2] = symbol;
            case 6 -> gameBoard[2][4] = symbol;
            case 7 -> gameBoard[4][0] = symbol;
            case 8 -> gameBoard[4][2] = symbol;
            case 9 -> gameBoard[4][4] = symbol;
        }
    }

    private static void printGameBoard(char[][] gameBoard) {
        for (char[] row : gameBoard) {
            for (char c : row) {
                System.out.print(c);
            }
            System.out.println();
        }
    }

    public static String checkWinner() {
        List<Integer> topRow = Arrays.asList(1, 2, 3);
        List<Integer> middleRow = Arrays.asList(4, 5, 6);
        List<Integer> bottomRow = Arrays.asList(7, 8, 9);

        List<Integer> leftCol = Arrays.asList(1, 4, 7);
        List<Integer> middleCol = Arrays.asList(2, 5, 8);
        List<Integer> rightCol = Arrays.asList(3, 6, 9);

        List<Integer> cross1 = Arrays.asList(1, 5, 9);
        List<Integer> cross2 = Arrays.asList(7, 5, 3);

        List<List> winningConditions = new ArrayList<List>();
        winningConditions.add(topRow);
        winningConditions.add(middleRow);
        winningConditions.add(bottomRow);

        winningConditions.add(leftCol);
        winningConditions.add(middleCol);
        winningConditions.add(rightCol);

        winningConditions.add(cross1);
        winningConditions.add(cross2);

        for (List l : winningConditions){
            // ***** ⬇️⬇️⬇️⬇️
            if(playerPositions.containsAll(l)){
                return "Congratulations, you win!";
            }
            // ***** ⬇️⬇️⬇️⬇️
            else if(computerPositions.contains(l)){
                return "Sorry! Computer wins!";
            }
            // ***** ⬇️⬇️⬇️⬇️
            else if(playerPositions.size() + computerPositions.size() == 9){
                return "It's a tie!";
            }
        }

        return "";
    }
}

I expected to use static as (e.g.) static ArrayList<Integer> playerPositions = new ArrayList<Integer>(); but it results in an error. I also tried public static and protected static among other combinations.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 2
    You declare local variables (since they are inside a method), they can't be `static` or have access modifiers. You seem to want to define fields (those are inside classes, but outside of their methods). – Joachim Sauer Jun 30 '23 at 23:29
  • @JoachimSauer Yup. I see it now. D'oh! Just need someone to say it to me to make me realise. The ArrayLists are meant to be declared above the PSVM. Thanks. I feel so silly. Also, I would like to point out it's 12:43am. Yup, I'm going to take a break. Thanks again. – Joey Tatú Jun 30 '23 at 23:43
  • In that case, please consider deleting the question. – Old Dog Programmer Jul 01 '23 at 00:25

0 Answers0