# Tic Tac Toe in Java



## lilLB (Mar 19, 2006)

Gotta make a tic tac toe game in java
The user has to be allowed to choose a 3by3, 4by4, or 5by5 game board
i think i've got that part down and I've gotten the x's and o's to show when u click the board but... i can't figure out how to determine who wins 
if anyone can think of a way to determine the winner no matter what size board (eg 3 in a row for 3by3 or 5 in a row for 5by5) don't need to write the code for me just give me an idea of a good way to do it
thanks here's my code:

```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe implements ActionListener {
	
	
	private JFrame window = new JFrame("Tic-Tac-Toe");
	private JPanel inputPanel = new JPanel();
	private JPanel boardPanel = new JPanel();
	private JButton button3 = new JButton("3 by 3");
	private JButton button4 = new JButton("4 by 4");
	private JButton button5 = new JButton("5 by 5");
	private JLabel label = new JLabel ("Click a button to choose game board size:");
	private JButton buttons[][];
	private int boardSize=0;
	private int count = -1;
	private String letter = "";
	private boolean win = false;

	public TicTacToe(){
	//Create Window
	window.setSize(500,500);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	
	//Add Label and buttons to inputPanel
	inputPanel.add(label);
	inputPanel.add(button3);
	inputPanel.add(button4);
	inputPanel.add(button5);
	button3.addActionListener(this);
	button4.addActionListener(this);
	button5.addActionListener(this);
	inputPanel.setSize(500,200);
	window.add(inputPanel,"North");
	
	
	
	//Make The Window Visible
	window.setVisible(true);
	}
	
	/**
	 When an object is clicked, perform an action.
	 @param a action event object
	 */
	public void actionPerformed(ActionEvent a) {
		if(count==-1){
			letter="-";
			count=0;
		}
		else{
		count++;
		
		//Calculate whose turn it is
		
		if(count % 2 == 0){
			letter = "O";
		} else {
			letter = "X";
		}
		}
		
		if (a.getSource() == button3) {
			boardSize=3;
		}
		else if (a.getSource()== button4){
			boardSize=4;
		}
		else if (a.getSource() == button5){
			boardSize=5;
		}
		if ((a.getSource() == button3)||(a.getSource()==button4)||(a.getSource()==button5)){
			button3.setEnabled(false);
			button4.setEnabled(false);
			button5.setEnabled(false);
			buttons = new JButton[boardSize][boardSize];
			for(int i=0; i<boardSize; i++)
				for (int j=0; j<boardSize; j++){
					buttons[i][j] = new JButton();
					boardPanel.add(buttons[i][j]);
					buttons[i][j].addActionListener(this);
				}
			
			boardPanel.setSize(300,300);
			boardPanel.setLayout(new GridLayout(boardSize,boardSize));
			window.add(boardPanel,"Center");
		}
		
		//Write the letter to the button and deactivate it
		 JButton pressedButton = (JButton)a.getSource(); 
		 pressedButton.setText(letter);
		 pressedButton.setEnabled(false);
		
		//Determine winner
		
		
		//Show a dialog when game is over
		if(win == true){
			JOptionPane.showMessageDialog(null, letter + " wins the game!");
			System.exit(0);
		} else if(count == (boardSize*boardSize) && win == false){
			JOptionPane.showMessageDialog(null, "The game was tie!");
			System.exit(0);
		}	
		
	}
	
	public static void main(String[] args){
		TicTacToe starter = new TicTacToe();
	}
}
```


----------



## Chicon (Jul 29, 2004)

Hi lilLB,

Just write a method that will pass as arguments the x and y of the last button pressed, that will browse the array of buttons (horizontally, vertically and diagonally) from the passed location and that will return a boolean set to true if the player won.
There's a _getText_ method on the _JButton_ you may use to compare the button letter.
Also, when you initialize the game buttons, you may use the _setName_ method to give each button a specific name that may be used to determine its location on the board.
Example :

```
[SIZE=2]
for (int i=0; i<boardSize; i++) {
    for (int j=0; j<boardSize; j++){
        buttons[i][j] = new JButton();
        boardPanel.add(buttons[i][j]);
        buttons[i][j].addActionListener(this);
        [COLOR=BLUE]buttons[i][j].setName(String.valueOf((i*10)+j);[/COLOR]
    }
}[/SIZE]
```
In the _actionPerformed_ method, you retrieve the x and y of the pressed button like this :

```
[SIZE=2]
JButton pressedButton = (JButton)a.getSource(); 
pressedButton.setText(letter);
pressedButton.setEnabled(false);
[COLOR=BLUE]int value = Integer.parseInt(pressedButton.getName());
int x = value / 10;
int y = value % 10;[/COLOR]
[/SIZE]
```


----------



## lilLB (Mar 19, 2006)

awesome thanks for the help ill try it out


----------

