I've massively changed it:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class TicTacToe extends JFrame implements MouseListener{
//Declarations
JPanel boardPanel = new JPanel();
byte[][] squares = {{0}/*ignore*/, {0/*ignore*/, 0, 0, 0}, {0/*ignore*/, 0, 0, 0}, {0/*ignore*/, 0, 0, 0}}; //ignored some so square 1 is squares[1][1]
ImageIcon s1 = new ImageIcon("pictures/square1.png");
ImageIcon s2 = new ImageIcon("pictures/square2.png");
ImageIcon s3 = new ImageIcon("pictures/square3.png");
ImageIcon s4 = new ImageIcon("pictures/square4.png");
ImageIcon s5 = new ImageIcon("pictures/square5.png");
ImageIcon s6 = new ImageIcon("pictures/square6.png");
ImageIcon s7 = new ImageIcon("pictures/square7.png");
ImageIcon s8 = new ImageIcon("pictures/square8.png");
ImageIcon s9 = new ImageIcon("pictures/square9.png");
ImageIcon s1x = new ImageIcon("pictures/square1x.png");
ImageIcon s2x = new ImageIcon("pictures/square2x.png");
ImageIcon s3x = new ImageIcon("pictures/square3x.png");
ImageIcon s4x = new ImageIcon("pictures/square4x.png");
ImageIcon s5x = new ImageIcon("pictures/square5x.png");
ImageIcon s6x = new ImageIcon("pictures/square6x.png");
ImageIcon s7x = new ImageIcon("pictures/square7x.png");
ImageIcon s8x = new ImageIcon("pictures/square8x.png");
ImageIcon s9x = new ImageIcon("pictures/square9x.png");
ImageIcon s1o = new ImageIcon("pictures/square1o.png");
ImageIcon s2o = new ImageIcon("pictures/square2o.png");
ImageIcon s3o = new ImageIcon("pictures/square3o.png");
ImageIcon s4o = new ImageIcon("pictures/square4o.png");
ImageIcon s5o = new ImageIcon("pictures/square5o.png");
ImageIcon s6o = new ImageIcon("pictures/square6o.png");
ImageIcon s7o = new ImageIcon("pictures/square7o.png");
ImageIcon s8o = new ImageIcon("pictures/square8o.png");
ImageIcon s9o = new ImageIcon("pictures/square9o.png");
JLabel q1 = new JLabel(s1);
JLabel q2 = new JLabel(s2);
JLabel q3 = new JLabel(s3);
JLabel q4 = new JLabel(s4);
JLabel q5 = new JLabel(s5);
JLabel q6 = new JLabel(s6);
JLabel q7 = new JLabel(s7);
JLabel q8 = new JLabel(s8);
JLabel q9 = new JLabel(s9);
JLabel q1x = new JLabel(s1x);
JLabel q2x = new JLabel(s2x);
JLabel q3x = new JLabel(s3x);
JLabel q4x = new JLabel(s4x);
JLabel q5x = new JLabel(s5x);
JLabel q6x = new JLabel(s6x);
JLabel q7x = new JLabel(s7x);
JLabel q8x = new JLabel(s8x);
JLabel q9x = new JLabel(s9x);
JLabel q1o = new JLabel(s1o);
JLabel q2o = new JLabel(s2o);
JLabel q3o = new JLabel(s3o);
JLabel q4o = new JLabel(s4o);
JLabel q5o = new JLabel(s5o);
JLabel q6o = new JLabel(s6o);
JLabel q7o = new JLabel(s7o);
JLabel q8o = new JLabel(s8o);
JLabel q9o = new JLabel(s9o);
//End
private TicTacToe() {
boardPanel.setLayout(new GridLayout(3, 3));
//Adding squares
boardPanel.add(q1);
boardPanel.add(q2);
boardPanel.add(q3);
boardPanel.add(q4);
boardPanel.add(q5);
boardPanel.add(q6);
boardPanel.add(q7);
boardPanel.add(q8);
boardPanel.add(q9);
//End
this.setTitle("Tic Tac Toe");
this.setSize(605, 630);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.add(boardPanel);
this.setVisible(true);
}
public static void main(String[] args) {
TicTacToe mainTTT = new TicTacToe();
mainTTT.addMouseListener(mainTTT);
}
private void removeAllSquares() {
boardPanel.remove(q1);
boardPanel.remove(q2);
boardPanel.remove(q3);
boardPanel.remove(q4);
boardPanel.remove(q5);
boardPanel.remove(q6);
boardPanel.remove(q7);
boardPanel.remove(q8);
boardPanel.remove(q9);
}
private void addXAndO(byte[][] squares) {
if (squares[1][1] == 0) {
boardPanel.add(q1);
} else if (squares[1][1] == 1) {
boardPanel.add(q1x);
} else {
boardPanel.add(q1o);
}
if (squares[1][2] == 0) {
boardPanel.add(q2);
} else if (squares[1][2] == 1) {
boardPanel.add(q2x);
} else {
boardPanel.add(q2o);
}
if (squares[1][3] == 0) {
boardPanel.add(q3);
} else if (squares[1][3] == 1) {
boardPanel.add(q3x);
} else {
boardPanel.add(q3o);
}
if (squares[2][1] == 0) {
boardPanel.add(q4);
} else if (squares[2][1] == 1) {
boardPanel.add(q4x);
} else {
boardPanel.add(q4o);
}
if (squares[2][2] == 0) {
boardPanel.add(q5);
} else if (squares[2][2] == 1) {
boardPanel.add(q5x);
} else {
boardPanel.add(q5o);
}
if (squares[2][3] == 0) {
boardPanel.add(q6);
} else if (squares[2][3] == 1) {
boardPanel.add(q6x);
} else {
boardPanel.add(q6o);
}
if (squares[3][1] == 0) {
boardPanel.add(q7);
} else if (squares[3][1] == 1) {
boardPanel.add(q7x);
} else {
boardPanel.add(q7o);
}
if (squares[3][2] == 0) {
boardPanel.add(q8);
} else if (squares[3][2] == 1) {
boardPanel.add(q8x);
} else {
boardPanel.add(q8o);
}
if (squares[3][3] == 0) {
boardPanel.add(q9);
} else if (squares[3][3] == 1) {
boardPanel.add(q9x);
} else {
boardPanel.add(q9o);
}
}
private byte getSquare(int x, int y) {
if (x < 200) {
if (y < 200) {
return 1;
} else if (y < 400) {
return 4;
} else {
return 7;
}
} else if (x < 400) {
if (y < 200) {
return 2;
} else if (y < 400) {
return 5;
} else {
return 8;
}
} else {
if (y < 200) {
return 3;
} else if (y < 400) {
return 6;
} else {
return 9;
}
}
}
private byte useAI(byte[][] squaresList) {
//code for winning when have two in a row
if (squaresList[1][1] == 2) {
if (squaresList[1][2] == 2) {
if (squaresList[1][3] == 0) {
return 3;
}
}
if (squaresList[1][3] == 2) {
if (squaresList[1][2] == 0) {
return 2;
}
}
if (squaresList[2][1] == 2) {
if (squaresList[3][1] == 0) {
return 7;
}
}
if (squaresList[2][2] == 2) {
if (squaresList[3][3] == 0) {
return 9;
}
}
if (squaresList[3][1] == 2) {
if (squaresList[2][1] == 0) {
return 4;
}
}
if (squaresList[3][3] == 2) {
if (squaresList[2][2] == 0) {
return 5;
}
}
}
if (squaresList[1][2] == 2) {
if (squaresList[1][3] == 2) {
if (squaresList[1][1] == 0) {
return 1;
}
}
if (squaresList[2][2] == 2) {
if (squaresList[3][2] == 0) {
return 8;
}
}
if (squaresList[3][2] == 2) {
if (squaresList[2][2] == 0) {
return 5;
}
}
}
if (squaresList[1][3] == 2) {
if (squaresList[2][2] == 2) {
if (squaresList[3][1] == 0) {
return 7;
}
}
if (squaresList[2][3] == 2) {
if (squaresList[3][3] == 0) {
return 9;
}
}
if (squaresList[3][1] == 2) {
if (squaresList[2][2] == 0) {
return 5;
}
}
if (squaresList[3][3] == 2) {
if (squaresList[2][3] == 0) {
return 6;
}
}
}
if (squaresList[2][1] == 2) {
if (squaresList[2][2] == 2) {
if (squaresList[2][3] == 0) {
return 6;
}
}
if (squaresList[2][3] == 2) {
if (squaresList[2][2] == 0) {
return 5;
}
}
if (squaresList[3][1] == 2) {
if (squaresList[1][1] == 0) {
return 1;
}
}
}
if (squaresList[2][2] == 2) {
if (squaresList[1][1] == 2) {
if (squaresList[3][3] == 0) {
return 9;
}
}
if (squaresList[1][2] == 2) {
if (squaresList[3][2] == 0) {
return 8;
}
}
if (squaresList[1][3] == 2) {
if (squaresList[3][1] == 0) {
return 7;
}
}
if (squaresList[2][1] == 2) {
if (squaresList[2][3] == 0) {
return 6;
}
}
if (squaresList[2][3] == 2) {
if (squaresList[2][1] == 0) {
return 4;
}
}
if (squaresList[3][1] == 2) {
if (squaresList[1][3] == 0) {
return 3;
}
}
if (squaresList[3][2] == 2) {
if (squaresList[1][2] == 0) {
return 2;
}
}
if (squaresList[3][3] == 2) {
if (squaresList[1][1] == 0) {
return 1;
}
}
}
if (squaresList[2][2] == 1) {
for(byte counter = 1; counter <= 3; counter ++) {
for(byte counter2 = 1; counter <=3; counter ++) {
if (counter == 2 && counter2 == 2) {
continue;
}
if (squaresList[counter][counter2] == 1) {
byte opp = getOpposite(counter, counter2);
byte row, column;
switch (opp) {
case 1:
row = 1;
column = 1;
break;
case 2:
row = 1;
column = 2;
break;
case 3:
row = 1;
column = 3;
break;
case 4:
row = 2;
column = 1;
break;
case 5:
row = 2;
column = 2;
break;
case 6:
row = 2;
column = 3;
break;
case 7:
row = 3;
column = 1;
break;
case 8:
row = 3;
column = 2;
break;
case 9:
row = 3;
column = 3;
break;
default:
row = 4;
column = 4;
System.exit(0);
}
if (squaresList[row][column] == 0) {
return opp;
}
}
}
} //if the code gets to here, they have only the center. playing a corner:
if (squaresList[1][1] == 0) {
return 1;
}
if (squaresList[1][3] == 0) {
return 3;
}
if (squaresList[3][1] == 0) {
return 7;
}
if (squaresList[3][3] == 0) {
return 9;
}
}
//if the code gets to here, the center is not theirs.
//code for blocking when the center is not theirs:
if (squaresList[1][1] == 1) {
if (squaresList[1][2] == 1) {
if (squaresList[1][3] == 0) {
return 3;
}
}
if (squaresList[1][3] == 1) {
if (squaresList[1][2] == 0) {
return 2;
}
}
if (squaresList[2][1] == 1) {
if (squaresList[3][1] == 0) {
return 7;
}
}
if (squaresList[3][1] == 1) {
if (squaresList[2][1] == 0) {
return 4;
}
}
if (squaresList[3][3] == 1) {
if (squaresList[2][2] == 0) {
return 5;
}
}
} if (squaresList[1][2] == 1) {
if (squaresList[1][3] == 1) {
if (squaresList[1][1] == 0) {
return 1;
}
}
if (squaresList[3][2] == 1) {
if (squaresList[2][2] == 0) {
return 5;
}
}
} if (squaresList[1][3] == 1) {
if (squaresList[2][3] == 1) {
if (squaresList[3][3] == 0) {
return 9;
}
}
if (squaresList[3][1] == 1) {
if (squaresList[2][2] == 0) {
return 5;
}
}
if (squaresList[3][3] == 1) {
if (squaresList[2][3] == 0) {
return 6;
}
}
} if (squaresList[2][1] == 1) {
if (squaresList[2][3] == 1) {
if (squaresList[2][2] == 0) {
return 5;
}
}
if (squaresList[3][1] == 1) {
if (squaresList[1][1] == 0) {
return 1;
}
}
} if (squaresList[2][3] == 1) {
if (squaresList[3][3] == 1) {
if (squaresList[1][3] == 0) {
return 3;
}
}
} if (squaresList[3][1] == 1) {
if (squaresList[3][2] == 1) {
if (squaresList[3][3] == 0) {
return 9;
}
}
if (squaresList[3][3] == 1) {
if (squaresList[3][2] == 0) {
return 8;
}
}
} if (squaresList[3][2] == 1) {
if (squaresList[3][3] == 1) {
if (squaresList[3][1] == 0) {
return 7;
}
}
}
//now the BASICS, and I mean BASICS
//play the center
if (squaresList[2][2] == 0) {
return 5;
}
//play the opposite corner
if (squaresList[1][1] == 1 && squaresList[3][3] == 0) {
return 9;
} if (squaresList[1][3] == 1 && squaresList[3][1] == 0) {
return 7;
} if (squaresList[3][1] == 1 && squaresList[1][3] == 0) {
return 3;
} if (squaresList[3][3] == 1 && squaresList[1][1] == 0) {
return 1;
}
//play a corner
if (squaresList[1][1] == 0) {
return 1;
} if (squaresList[1][3] == 0) {
return 3;
} if (squaresList[3][1] == 0) {
return 7;
} if (squaresList[3][3] == 0) {
return 9;
}
//play a side
if (squaresList[1][2] == 0) {
return 2;
} if (squaresList[2][1] == 0) {
return 4;
} if (squaresList[2][3] == 0) {
return 6;
} if (squaresList[3][2] == 0) {
return 8;
}
//if the code gets here, the board is full. TODO: check for a win
return 10;
}
private byte getOpposite(byte row, byte column) {
if ((row - column == 0) || (row - column == 2)) {
if (row == 1) {
if (column == 1) {
return 9;
} return 7;
}
if (column == 1) {
return 3;
} return 1;
}
if (row == 2 || row == 1) {
switch (column) {
case 1:
return 6;
case 2:
return 8;
case 3:
return 4;
default:
System.exit(0);
}
}
return 2;
}
/*private boolean checkForWin(byte[][] input) {
TODO: Important! check for win
}*/
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
byte row = 0, row2 = 0;
byte column = 0, column2 = 0;
byte squareNum = 0;
int x = arg0.getX();
int y = arg0.getY();
byte square = getSquare(x, y);
switch (square) {
case 1:
row = 1;
column = 1;
break;
case 2:
row = 1;
column = 2;
break;
case 3:
row = 1;
column = 3;
break;
case 4:
row = 2;
column = 1;
break;
case 5:
row = 2;
column = 2;
break;
case 6:
row = 2;
column = 3;
break;
case 7:
row = 3;
column = 1;
break;
case 8:
row = 3;
column = 2;
break;
case 9:
row = 3;
column = 3;
break;
default:
System.exit(0);
}
squares[row][column] = 1;
removeAllSquares();
addXAndO(squares);
repaint();
//checkForWin(squares);
squareNum = useAI(squares);
switch (squareNum) {
case 1:
row2 = 1;
column2 = 1;
break;
case 2:
row2 = 1;
column2 = 2;
break;
case 3:
row2 = 1;
column2 = 3;
break;
case 4:
row2 = 2;
column2 = 1;
break;
case 5:
row2 = 2;
column2 = 2;
break;
case 6:
row2 = 2;
column2 = 3;
break;
case 7:
row2 = 3;
column2 = 1;
break;
case 8:
row2 = 3;
column2 = 2;
break;
case 9:
row2 = 3;
column2 = 3;
break;
default:
System.exit(0);
}
squares[row2][column2] = 2;
repaint();
//checkForWin(squares);
}
}
But the problem still exists and the AI does nothing!
I'll try validate. Please hold.
It worked! The AI has problems though, on the first move it does nothing. help?