Tic-Tac-Toe array Win Check
I'm having a hard time figuring out how to check for a win in my tic-tac-toe board. I have to check to see if the winner is X or O or check to see if there is no winner yet (return N) or the board is full and it is a tie (return F). The method in question is named checkForWin. It is the last included method included. I have included the whole program, but the only part that requires some addition is that last method, checkForWin. All else is okay (sufficiently).
Code :
package lab07;
import java.util.*;
public class Lab07 {
/**
* @param args the command line arguments Do not change main()
*/
public static void main(String[] args) {
char[][] board;
char win;
char player;
int size = getBoardSize();
while (size >= 3) {
board = createEmptyBoard(size);
player = getStartingPlayer();
do {
getMove(board, player);
player = player == 'O' ? 'X' : 'O'; // switch players
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);
}
displayBoard(board);
size = getBoardSize();
} // end play games loop
} // end main
/**
* returns 3, 4 or 5 for the size of the play board or -1 to quit This is
* complete, no changes necessary
*/
public static int getBoardSize() {
int retval;
Scanner keyboard = new Scanner(System.in);
do {
System.out.printf("Enter board size 3, 4, 5 or -1 to quit: ");
retval = keyboard.nextInt();
} while (retval != 3 && retval != 4 && retval != 5 && retval != -1);
keyboard.nextLine(); // you should know why by now
return retval; // this MUST BE LAST LINE in this method()
} // getBoardSize
/**
* randomly selects X or O as the starting player
*
*/
public static char getStartingPlayer() {
char retval = 'X';
int switchRan;
Random ranGen = new Random();
switchRan = ranGen.nextInt(2);
switch (switchRan) {
case 0:
retval = 'O';
break;
case 1:
retval = 'X';
break;
}
System.out.printf("\n\tNew Game. Starting player is %c\n", retval);
return retval; // this MUST BE LAST LINE in this method()
} // end getStartingPlayer
/**
* gets new memory for a board and fills all cells with a minus sign '-'
*
* @param size the board if 3 is 3 x 3, if 4 is 4 x 4 etc.
*/
public static char[][] createEmptyBoard(int size) {
char[][] board = new char[size][size];
for (int row = 0; row < board.length; row++) {
for (int column = 0; column < board[row].length; column++) {
board[row][column] = '-';
}
}
return board; // this MUST BE LAST LINE in this method()
} // end createEmptyBoard
/**
* 1. displays board and 2. gets row column indexes for next move 3. makes
* sure the cell has a '-' is a legal move 4. loops till legal 5. puts the
* player's X or O at that cell
*
* @param board the current play board
* @param the X or O of the current player
*/
public static void getMove(char[][] board, char player) {
Scanner keyboard = new Scanner(System.in);
int row, col;
System.out.printf("Player %c move\n", player);
displayBoard(board);
do {
System.out.print("Enter row: >=0 and <=" + (board.length - 1) + " ");
row = keyboard.nextInt();
System.out.print("Enter col: >=0 and <=" + (board.length - 1) + " ");
col = keyboard.nextInt();
} while (board[row][col] != '-' && (board[row][col] != player));//CHECK THIS ONCE checkForWin IS COMPLETE
board[row][col] = player;
} // end getMove
/**
* displays the current play board to the console
*
* @param board the current play board to be displayed
*/
public static void displayBoard(char[][] board) {
for (int row = 0; row < board.length; row++) {
System.out.println();
for (int column = 0; column < board[row].length; column++) {
System.out.printf("%c\t", board[row][column]);
}
}
System.out.println();
} // end displayBoard
/**
* returns X, O, F or N returns X for X wins O for O wins F for board Full
* no winner or N for No winner yet
*
* @param board, the current play board to be checked
*/
public static char checkForWin(char[][] board) {
char retval = 'N';
int dashCount = board.length * board.length;//set total count of dashes to check for full board vs. open spaces
int oCount, xCount;
/**
* Check for horizontal wins
*/
for (int row = 0; row < board.length; row++) {
oCount = xCount = 0;//Reset X and O counters
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == 'X') {
xCount++;
} else if (board[row][col] == 'O') {
oCount++;
} else {
dashCount--;
}
if (xCount == board.length) {
retval = 'X';
}
if (oCount == board.length) {
retval = 'O';
}
if (dashCount == 0) {
retval = 'F';
}
}
}
return retval; // this MUST BE LAST LINE in this method()
} // end checkForWin
}
Re: Tic-Tac-Toe array Win Check
Think of how many ways to win.
3 horizontal wins
3 vertical wins
2 diagonal wins
Does your method check each possibility?
Re: Tic-Tac-Toe array Win Check
No, I know it doesn't. That's just as far as i could figure out. I managed to get the horizontal wins. I'm not sure how to use the for-loops to do the others. Should i just reuse that same bit and put it in the for-loop that holds the second? Would that get me the vertical wins?
Re: Tic-Tac-Toe array Win Check
Quote:
Think of how many ways to win.
3 horizontal wins
3 vertical wins
2 diagonal wins
Well with 8 maximum possible cases, even brute force is fast...
0 1 2
3 4 5
6 7 8
if ( winOn(0 1 2) ) { //top row wins }
if ( winOn(3 4 5) ) { //mid row wins }
if ( winOn(6 7 8) ) { //low row wins }
if ( winOn(0 3 6) ) { //left col wins }
if ( winOn(1 4 7) ) { //mid col wins }
if ( winOn(2 5 8) ) { //right col wins }
if ( winOn(0 4 8) ) { //top left to bottom right diag wins }
if ( winOn(2 4 6) ) { //top right to bottom left diag wins }
There are many different ways to handle this. See which way fits your mind set. Consider possibilities. For example if the game board position 0 has no value, three possible wins are eliminated. Or if position 4 has no value, 4 possible wins need not be checked.
You mentioned for loops. See what you can come up with.
Re: Tic-Tac-Toe array Win Check
Well, good morning from Greece. I just finished doing a tic tac toe program for myself in my free time. This is what i used to check
if the current player won the game. Customize it to your needs.
"The array positions has the moves that player x or o used and moves_played is the moves that player x or o played when you called the method.
Quote:
Code java:
/**code removed by moderator*/
Re: Tic-Tac-Toe array Win Check
@ Icetaller
Hi and welcome. Please read the problem with spoonfeeding.
Re: Tic-Tac-Toe array Win Check
Quote:
Originally Posted by
jps
Hey jps. I'm sorry that wasn't my intention. I just learned programming by reading and understanding other people's code. I asked them their thought behind each problem they solved with their code. Then I went on to make another program that came into my mind using the knowledge I acquired from that. Maybe, not all people work that way and they are lazy. It's just I have a different opinion on the "Spoon feeding". But, then again maybe a better way of learning is Socrates' method of extracting the knowledge through the student. I'll try and use the second method if that's how this forum works.