| Madarawolf |
10-29-2012 12:54 AM |
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);
|