Level Hard on Tic Tac Toe
Author |
Message |
programmer1337
|
Posted: Wed Jun 13, 2012 10:03 pm Post subject: Level Hard on Tic Tac Toe |
|
|
I am making a Tic Tac Toe game, now I have the easy, and medium done, all I need is the hard, here is my code at the moment code: | public static void hard(String phrase) {
int winner = 0;
board[1][1] = " X ";
int c = 2;
phrase = getPhrase();
while (winner == 0) {
if (c % 2 == 0) {
rowColumn(phrase, "1", 2);
} else {
System.out.println("hello");
if (board[0][0].equals(" - ")) {
board[0][0] = " X ";
} else if (!board[0][0].equals(" - ") && board[0][2].equals(" - ")) {
board[0][2] = " X ";
} else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && board[2][0].equals(" - ")) {
board[2][0] = " X ";
} else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && !board[2][0].equals(" - ") && board[2][2].equals(" - ")) {
board[2][2] = " X ";
}
}
phrase = getPhrase();
winner = determineWin();
c++;
}
if (winner == 2) {
System.out.println("you win");
} else if (winner == 1) {
System.out.println("cpu wins");
}
} | Now I dont want help with coding or anything, but the code above will put an X according to which corners are open, and I wouldnt know which part of the if statement I would need to put another if statment to check if there is a spot open for a win, like for example if you have(below), which part of the if statement would you make the computer put an x where it
would win
Quote: X - X
- X O
- - O |
|
|
|
|
|
Sponsor Sponsor
|
|
|
programmer1337
|
Posted: Wed Jun 13, 2012 11:18 pm Post subject: RE:Level Hard on Tic Tac Toe |
|
|
code: | public static void hard(String phrase) {
int winner = 0;
board[1][1] = " X ";
int c = 2;
int d = 0;
int xVal = 0;
phrase = getPhrase();
while (winner == 0) {
if (c % 2 == 0) {
rowColumn(phrase, "1", 2);
} else {
if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && !board[2][0].equals(" - ") && !board[2][2].equals(" - ")) {
if (board[0][0].equals(" X ") && board[0][2].equals(" X ")) {
board[0][1] = " X ";
} else if (board[0][0].equals(" X ") && board[2][0].equals(" X ")) {
board[1][0] = " X ";
} else if (board[2][2].equals(" X ") && board[0][2].equals(" X ")) {
board[1][2] = " X ";
} else if (board[2][2].equals(" X ") && board[2][0].equals(" X ")) {
board[2][1] = " X ";
}
}
if (board[0][0].equals(" - ")) {
board[0][0] = " X ";
} else if (!board[0][0].equals(" - ") && board[0][2].equals(" - ")) {
board[0][2] = " X ";
} else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && board[2][0].equals(" - ")) {
board[2][0] = " X ";
} else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && !board[2][0].equals(" - ") && board[2][2].equals(" - ")) {
board[2][2] = " X ";
}
}
phrase = getPhrase();
winner = determineWin();
c++;
}
if (winner == 2) {
JOptionPane.showMessageDialog(null, "You Win");
} else if (winner == 1) {
JOptionPane.showMessageDialog(null, "CPU Wins");
}
}
| My new code, still not unbeatable |
|
|
|
|
|
Tony
|
|
|
|
|
|
|