Hello. For an assignment we have been given the shell of a tic tac toe game in java. We are to fix a few things that is wrong with it.

here is the main class:

public class TicTacToeGameJonathanSilverberg {

    public static final int board_size = 3;       // number of rows or columns in the board
    
    public static void main(String[] args) {
        
        char board[][] = new char[board_size][board_size];  // the game board
               
        TicTacToeGameJonathanSilverberg p1 = new TicTacToeGameJonathanSilverberg();  // create the players
        TicTacToeGameJonathanSilverberg p2 = new TicTacToeGameJonathanSilverberg();
        
        initBoard (board);                 // initialize the board to spaces and print it out
        displayBoard (board);
        
        char winner = findWinner(board);
        
        while (winner == ' ') {
            
            p1.getMove(board, 'X');   // player one will be 'X', and will always go first
            
            displayBoard (board);
            
            winner = findWinner(board);
            
            if (winner != ' ') {
                if (winner == 'T') {     System.out.println("Tie Game!");      }
                else {        System.out.println("The Winner is: " + winner);  }
                break;
            }
            
            p2.getMove(board, 'O');   // player two will be 'O', and always go second
            
            displayBoard (board);
            
            winner = findWinner(board);
            
            if (winner != ' ') {
                if (winner == 'T') {     System.out.println("Tie Game!");      }
                else {        System.out.println("The Winner is: " + winner);  }
            }
        }
        
    }

    public static void initBoard(char[][] theBoard) {
        // since this is pass by reference, initializing the Board here resets it in main
        
        for (int row = 0; row < board_size; row++) {
            for (int col = 0; col < board_size; col++) {
                theBoard[row][col] = ' ';                   // row always comes first in a 2d array
            }
        }
        
    }
    
    public static void displayBoard(final char[][] theBoard) {
        // note the final parameter - we won't change the board, just print it.
        
        // Need to format the board display to look like this:
        //  | |
        // -----
        //  | |
        // -----
        //  | |
        
        for (int row = 0; row < board_size; row++) {
            for (int col = 0; col < board_size; col++) {
                System.out.print(theBoard[row][col]);                   // row always comes first in a 2d array
            }
            System.out.println();
        } 
    }
    
    public static char findWinner(final char[][] theBoard){
    
        // this method should return 'X' if X is the winner
        // 'O' if O is the winner, a space (' ') if there is no winner,
        // or 'T' if there is a tie. For now it just returns a space no matter what.
        
        return ' ';
        
        
    }
   
}

And here is the other class:

import java.util.Random;
public class TicTacToePlayerJonathanSilverberg {

    public void getMove(char[][] theBoard, char myPiece) {
     
        // this method should update the game board to place 'myPiece' (X or O)
        // in a space on the board.
        
        // As-is, this method just places an X or O randomly, without even considering
        // if the square is already taken.
        
        Random myRand = new Random();
        
        int myRow = myRand.nextInt(3);
        int myCol = myRand.nextInt(3);
        
        theBoard[myRow][myCol] = myPiece;      
        
    }
    
}

What I need to do.....
1) for some reason when I try to have p1 and p2 get move such as p1.getMove. It is not finding the method get move from the other class.
2) In the findWinner method. I need to make the variable winner public so it can be read from this method as well. When I try to put the variable winner in this method, it cannot refer back to it from the main class.

This article has been dead for over six months. Start a new discussion instead.