Jump to content

Tic-Tac-Toe game

- - - - -

  • Please log in to reply
8 replies to this topic

#1
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
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, 0, 0, 0, 0, 0, 0, 0, 0}; //the first one in ignored so square 1 is squares[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 s1, byte s2, byte s3, byte s4, byte s5, byte s6, byte s7, byte s8, byte s9) {

		if (s1 == 0) {

			boardPanel.add(q1);

		} else if (s1 == 1) {

			boardPanel.add(q1x);

		} else {

			boardPanel.add(q1o);

		}

		if (s2 == 0) {

			boardPanel.add(q2);

		} else if (s2 == 1) {

			boardPanel.add(q2x);

		} else {

			boardPanel.add(q2o);

		}

		if (s3 == 0) {

			boardPanel.add(q3);

		} else if (s3 == 1) {

			boardPanel.add(q3x);

		} else {

			boardPanel.add(q3o);

		}

		if (s4 == 0) {

			boardPanel.add(q4);

		} else if (s4 == 1) {

			boardPanel.add(q4x);

		} else {

			boardPanel.add(q4o);

		}

		if (s5 == 0) {

			boardPanel.add(q5);

		} else if (s5 == 1) {

			boardPanel.add(q5x);

		} else {

			boardPanel.add(q5o);

		}

		if (s6 == 0) {

			boardPanel.add(q6);

		} else if (s6 == 1) {

			boardPanel.add(q6x);

		} else {

			boardPanel.add(q6o);

		}

		if (s7 == 0) {

			boardPanel.add(q7);

		} else if (s7 == 1) {

			boardPanel.add(q7x);

		} else {

			boardPanel.add(q7o);

		}

		if (s8 == 0) {

			boardPanel.add(q8);

		} else if (s8 == 1) {

			boardPanel.add(q8x);

		} else {

			boardPanel.add(q8o);

		}

		if (s9 == 0) {

			boardPanel.add(q9);

		} else if (s9 == 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;

			}

		}

	}

	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) {

		int x = arg0.getX();

		int y = arg0.getY();

		byte square = getSquare(x, y);

		System.out.println(square);

		squares[square] = 1;

		removeAllSquares();

		addXAndO(squares[1], squares[2], squares[3], squares[4], squares[5], squares[6], squares[7], squares[8], squares[9]);

		repaint();

	}

}

For some reason when I click on a square, instead of an X appearing in the square, it just disappears!

Picture:
Attached File  umm odd.PNG   17.09K   14 downloads

#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
You need to tell the container that you are done changing its contents and that it needs to do a layout.
Try calling the validate method.

Instead of passing all the elements of an array to addXAndO, why not pass the array.
Then in addXAndO use an array and a loop vs all the individually coded if tests.
The variables named: s1, s2, s3 ...etc look like they should be in an array and accessed this way: s[i]

#3
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
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?

#4
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

The AI has problems though, on the first move it does nothing. help?
Please explain. What is the AI?

If you need to see what the code is doing, try debugging it. One way is to add printlns to print out the value of variables and to show execution flow. The printed output will help you see what the program is doing.
I assume that you know what the program is supposed to do.



You need to study how to use arrays and loops. You have too many lines of repeated code.

#5
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
pictures:
Attached File  onlyx.PNG   20.92K   9 downloads

Attached Files



#6
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
AI is artificial intelligence.

#7
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
What does that show?
Please explain what the problem is.
Try debugging the code as I suggested in my last post.

See you tomorrow.

#8
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
with printlns, I discovered why!!!
thanks

It would store the variable it chose on the first move until the second move. the square that the second move was supposed to use was used on the third move! so now i need to fix it.

---------- Post added at 09:19 PM ---------- Previous post was at 09:05 PM ----------

Fixed! topic.end();

#9
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
You need to study how to use arrays and loops. You have too many lines of repeated code.

You could make the program much much shorter and have much fewer places for there to be bugs.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users