Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-29-2012, 12:54 AM   PM User | #1
Madarawolf
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Madarawolf is an unknown quantity at this point
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 03:13 AM..
Madarawolf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 PM.


Advertisement
Log in to turn off these ads.