Well I'm getting ready for my second semester of Java programing and I figured I would try and get back in the swing (no pun intended) of things by creating a simple tic tac toe game. I was able to make a two player game with in a matter of hours so I figured I would try and make a one player game with AI and thats where im having some issues. Here is my code for two person tic tac toe
package mytictactoe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToeV2 implements ActionListener {
/*Instance Variables*/
private int[][] winCombinations = new int[][] {
{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, //horizontal wins
{1, 4, 7}, {2, 5, 8}, {3, 6, 9}, //virticle wins
{1, 5, 9}, {3, 5, 7} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[10];
private int count = 0;
private String letter = "";
private boolean win = false;
public TicTacToeV2(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
/*Add Buttons To The Window*/
for(int i=1; i<=9; i++){
buttons[i] = new JButton();
window.add(buttons);
buttons[i].addActionListener(this);
}
/*Make The Window Visible*/
window.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
count++;
/*Calculate whose turn it is*/
if(count % 2 == 0){
letter = "O";
} else {
letter = "X";
}
/*Write the letter to the button and deactivate it*/
for(int i=1; i<=9; i++){
if(a.getSource() == buttons[i]){
buttons[i].setText(letter);
buttons[i].setEnabled(false);
}
}
/*Determine who won*/
for(int i=0; i<=7; i++){
if( buttons[winCombinations[i][0]].getText() == buttons[winCombinations[i][1]].getText() &&
buttons[winCombinations[i][1]].getText() == buttons[winCombinations[i][2]].getText() &&
buttons[winCombinations[i][0]].getText() != ""){
win = true;
}
}
/*Show a dialog when game is over*/
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game!");
System.exit(0);
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
System.exit(0);
}
}
public static void main(String[] args){
TicTacToeV2 starter = new TicTacToeV2();
}
}
Now for my program with AI I'm essentially using the same code and just for clarity I removed all the code that determines who won (as it isnt needed to get the AI to work) This is what I have so far
package mytictactoe;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class TicTacToeV3 implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[10];
private int count = 0;
public TicTacToeV3(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
/*Add Buttons To The Window*/
for(int i = 1; i<=9; i++){
buttons[i] = new JButton();
window.add(buttons);
buttons[i].addActionListener(this);
}
/*Make The Window Visible*/
window.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
count++;
/*Write the letter to the button and deactivate it*/
for(int i = 1; i<= 9; i++){
if(a.getSource() == buttons[i]){
buttons[i].setText("X");
buttons[i].setEnabled(false);
}
}
AI();
}
public void AI(){
Random x = new Random();
int y = 1 + x.nextInt(9);
if(buttons[y].getText() == "X" || buttons[y].getText() == "O" ){
AI();
} else {
buttons[y].setText("O");
buttons[y].setEnabled(false);
}
}
public static void main(String[] args){
new TicTacToeV3();
}
}
It kind of works because each time i choose a box the "computer" also chooses one, but since it is so random the chances the computer wins is very limited. I then tried to do something like this in the AI() method and it seems to be on the right track but once one of the if's are satisfied the all the AI logic stops
public void AI(){
//horizontal AI defencive code
if(buttons[1].getText().equals(buttons[2].getText()) && buttons[1].getText() != ""){
buttons[3].setText("O");
buttons[3].setEnabled(false);
} else if(buttons[2].getText().equals(buttons[3].getText()) && buttons[2].getText() != ""){
buttons[1].setText("O");
buttons[1].setEnabled(false);
} else if(buttons[4].getText().equals(buttons[5].getText()) && buttons[4].getText() != ""){
buttons[6].setText("O");
buttons[6].setEnabled(false);
} else if(buttons[5].getText().equals(buttons[6].getText()) && buttons[5].getText() != ""){
buttons[4].setText("O");
buttons[4].setEnabled(false);
} else if(buttons[7].getText().equals(buttons[8].getText()) && buttons[7].getText() != ""){
buttons[9].setText("O");
buttons[9].setEnabled(false);
} else if(buttons[8].getText().equals(buttons[9].getText()) && buttons[8].getText() != ""){
buttons[7].setText("O");
buttons[7].setEnabled(false);
} else {
Random x = new Random();
int y = 1 + x.nextInt(9);
if(buttons[y].getText() == "X" || buttons[y].getText() == "O" ){
AI();
} else {
buttons[y].setText("O");
buttons[y].setEnabled(false);
}
}
}
And basically what that does is it checks if I have two X's in a row horizontally, If I do it will place an O in the box preventing me from getting three in a row (only in horizontal rows). Of course I would iterate this over and over for all possible combinations of offensive moves and defensive moves and probably do it using a for loop but this is just some scratch code I threw together to show you my thoughts. And again my problem is once one of the conditions are satisfied it never checks the conditions again and I'm not sure why that is...
So I'm looking for some ideas and/or some pseudo code to help me out a little.
Thanks