S
shadow427
I have a fully fuctional java tic tac toe game i created. I have it
now so that the human player can play against a computer player,
however it is too easy to win. The computer player always chooses the
last open space on the board and then goes up to the first space, I
don't know how to make it so that the computer player either always
beats or ties the human player. Basically I need to make the computer
player smarter. Below is the code any help would be greatly
appreciated.
public class TicTacToeBoard {
//data fields
char[][] ticTacToe = new char[3][3];
//constructor
public TicTacToeBoard(){
for (int i=0; i < ticTacToe.length; i++){
for (int j=0; j < ticTacToe.length; j++){
ticTacToe[j]='_';
}
}
}
//set the position on the board to the indicated symbol
public void makeMove(int row, int col, char symbol){
if(ticTacToe[row][col] == '_'){
ticTacToe[row][col] = symbol;
} else {
JOptionPane.showMessageDialog(null, "Illegal Move - Lose a
turn");
}
}
//provide logic for computer to choose its next move
public int[] chooseMove(char symbol){
int[] move = new int[2];
int blockRow = 0;
int blockCol = 0;
int progressRow = 0;
int progressCol = 0;
//THIS IS WHERE IM STUCK
for (int i=0; i < ticTacToe.length; i++){
for (int j=0; j < ticTacToe.length; j++){
if (ticTacToe[j] == '_'){
progressRow = i;
progressCol = j;
} else if (ticTacToe[j] != symbol){ //my opponent has it
} else { //it is mine, see if space around it
}
}
}
if(blockRow != 0 || blockCol != 0){
move[0] = blockRow;
move[1] = blockCol;
} else {
move[0] = progressRow;
move[1] = progressCol;
}
return move;
}
now so that the human player can play against a computer player,
however it is too easy to win. The computer player always chooses the
last open space on the board and then goes up to the first space, I
don't know how to make it so that the computer player either always
beats or ties the human player. Basically I need to make the computer
player smarter. Below is the code any help would be greatly
appreciated.
public class TicTacToeBoard {
//data fields
char[][] ticTacToe = new char[3][3];
//constructor
public TicTacToeBoard(){
for (int i=0; i < ticTacToe.length; i++){
for (int j=0; j < ticTacToe.length; j++){
ticTacToe[j]='_';
}
}
}
//set the position on the board to the indicated symbol
public void makeMove(int row, int col, char symbol){
if(ticTacToe[row][col] == '_'){
ticTacToe[row][col] = symbol;
} else {
JOptionPane.showMessageDialog(null, "Illegal Move - Lose a
turn");
}
}
//provide logic for computer to choose its next move
public int[] chooseMove(char symbol){
int[] move = new int[2];
int blockRow = 0;
int blockCol = 0;
int progressRow = 0;
int progressCol = 0;
//THIS IS WHERE IM STUCK
for (int i=0; i < ticTacToe.length; i++){
for (int j=0; j < ticTacToe.length; j++){
if (ticTacToe[j] == '_'){
progressRow = i;
progressCol = j;
} else if (ticTacToe[j] != symbol){ //my opponent has it
} else { //it is mine, see if space around it
}
}
}
if(blockRow != 0 || blockCol != 0){
move[0] = blockRow;
move[1] = blockCol;
} else {
move[0] = progressRow;
move[1] = progressCol;
}
return move;
}