Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help Finishing a Program

    I'm trying to write a program that creates a tic-tac-toe board using an array and then lets you play it. I need help writing the code that checks if a player has won using a method.
    Here is what I have:
    Code:
    public static char checkForWin(char [][] board){
            char retval = 'F'; // 
            int xCount = 0;
            int oCount = 0;
            
            for(int row = 0; row < board.length; row++){
                for(int col = 0; col < board.length; col++){
                    if(board[row][col] == 'X')
                        xCount+= 1;
                    else if(board[row][col] == 'O')
                        oCount+= 1;
                }
            }
            
            if(xCount < board.length || oCount < board.length)
                retval = 'N';
            else if(xCount == board.length)
                retval = 'X';
            else if(oCount == board.length)
                retval = 'O';
                        
            return retval;
    then where the method is called:
    Code:
    win = checkForWin(board);
                } while(win == 'N'); // must be F, O or X to leave loop
                if(win == 'F'){
                    System.out.printf("\nThe board is Full: no Winner\n");
                } else {
                    System.out.printf("\nThe winner is %c\n", win);
    Last edited by Madarawolf; 10-29-2012 at 04:13 AM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •