Search code examples
javaarraystic-tac-toe

Fillgrid method not working in Tic-Tac-Toe program


I'm writing a tic-tac-toe program for my AP computer science class but the fillgrid method (which is meant to set the initial value for each space on the board to "*") isn't running. Eclipse says there's a mismatch of String to char but my teacher told me to use char. Any advice?

Program:

public class tictacotoe {

    public static void main(String[] args) {
        char[][] grid = new char[3][3];
        char player1 = 'X', player2 = 'O';
        char currentPlayer = player1;
        int turn = 0;
    }//end main

    public static void fillGrid(char[][] g) { //This will make the initial grid that it prints out
        for(int row=0; row < g.length; row++) {
            for(int col=0; col < g[0].length; col++) {
                g[row][col] = "*";
            }
        }
    }

    public void printGrid(char[][] g) { //This will print the grid that you made above
        /*
        String output = "";
        for(int row = 0; row < grid.length; col++) {
            output += grid[row][col];
            if(col != grid[0].length-1) output += "|";
        }
        if(row != grid.length-1) output += "\n--------------\n";
        return output;
        */
    }

}//end tictacotoe

Solution

  • Instead of assigning a string in g[row][col] = "*"; try assigning a char g[row][col] = '*'; (single quotes ' instead of double qupotes ").