heres's my code :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication11;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * @author Jayson Jude
 */
public class JavaApplication11 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 JavaApplication11(){
	//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!");
			
                            
                                
		} else if(count == (boardSize*boardSize) && win == false){
			JOptionPane.showMessageDialog(null, "The game was tie!");
			
                            
		}	
		
                            
	}
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new JavaApplication11();
    }
}

Recommended Answers

All 2 Replies

it's easy. All you have to do is check if a diagonal, a row, or a column contains only Xs or Os. If you find such a diagonal, row, or column, the game is over, you have a winner. else, keep going. At the end, you have code to check for a tie.

You are using "buttons" 2D array variable to hold the current status of the game, so what you need to do is to check inside the "buttons" variable if there is any consecutive same letter (except "-") up to a number of whatever you want. If there is, the owner of the letter won (in this case it is either "X" or "O").

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.