Tic Tac Toe issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Steel546
    New Member
    • Mar 2009
    • 25

    Tic Tac Toe issue

    So I'm working on my final lab, and I'm fixing a few errors. But the one I can't get right now is how to return who is the winner. Everytime I try to write the code for findWinner, it says missing return value, despite it being (what I think is) correct. There's two classes. And yes, there's an Infinite loop I'm trying to find, but I'll get to that next.

    Code:
    public class TicTacToeGameFirstLast {
    
        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
                   
            TicTacToePlayerFirstLast p1 = new TicTacToePlayerFirstLast();  // create the players
            TicTacToePlayerFirstLast p2 = new TicTacToePlayerFirstLast();
            
            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("\n---------");
            } 
        }
        
        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.
    
            if (theBoard[0][0] == 'X' && theBoard[0][1] =='X' && theBoard[0][2] == 'X'){
    
                return theBoard[0][0];
            
            }
                               
        }
       
     }
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Ryan Garlick
     */
    import java.util.Random;
    
    public class TicTacToePlayerFirstLast {
    
        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;      
            
        }
        
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Steel546
    So I'm working on my final lab, and I'm fixing a few errors. But the one I can't get right now is how to return who is the winner. Everytime I try to write the code for findWinner, it says missing return value, despite it being (what I think is) correct.
    Code:
        public static char findWinner(final char[][] theBoard){
    
            if (theBoard[0][0] == 'X' && theBoard[0][1] =='X' && theBoard[0][2] == 'X'){
    
                return theBoard[0][0];
            
            }
                               
        }
    What does your method return if the test in that if-statement is false? Indeed, you don't return anything and your compiler was clever enough to figure that out.

    kind regards,

    Jos

    ps. Also read an article on Tic Tac Toe in the 'advanced' math section; it contains a simple method that checks for a winner.

    Comment

    Working...