Title: TicTacToe Gui
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Tic Tac Toe Gui

In this video, we will create a GUI for Tic Tac Toe. We won't spend any efforts creating the actual game to determine a winner or a loser, so our game will be a bit "Brain Dead".

The final results of the programs created on this video show up as follows:

TicTacToe.java

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TicTacToe extends JFrame implements ActionListener{

    JButton[][] buttons= new JButton[3][3];
    JButton resetButton = new JButton("Reset");
    JTextField status = new JTextField(20);
    Font bigFont = new Font("TimesRoman", Font.PLAIN, 72);
    Font mediumFont = new Font("TimesRoman", Font.PLAIN, 18);
    int turn=0;

    private void reset()
    {
        status.setText("O's turn  ");
        turn = 0;
        for (int row=0; row < 3; row++)
            for (int col=0; col < 3; col++)
                buttons[row][col].setText("");
    }

    private void setupCenter()
    {
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(3,3,5,5));
        add(BorderLayout.CENTER, mainPanel);

        for (int row=0; row < 3; row++)
            for (int col=0; col < 3; col++)
            {
                JButton b = new JButton("");
                b.addActionListener(this);
                b.setFont(bigFont);
                buttons[row][col] = b;
                mainPanel.add(b);
            }
    }
    private void setupTop()
    {
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        status.setFont(mediumFont);
        resetButton.setFont(mediumFont);
        topPanel.add(status);
        topPanel.add(resetButton);
        resetButton.addActionListener(this);
        add(BorderLayout.NORTH, topPanel);
    }

    TicTacToe ()
    {
        super("Tic-Tac-Toe");

        setLayout(new BorderLayout());
        setupCenter();
        setupTop();

        setSize(600,600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    private String player()
    {
        if (turn % 2 == 0)
            return "O";
        else
            return "X";
    }
    private void updateButton(JButton b)
    {
        String errStr="";
        if ("".equals(b.getText()))
        {
            b.setText(player());
            turn += 1;
        }
        else
        {
            errStr="Cell already chosen ";
        }
        String statusStr = errStr + player()+"'s turn";
        status.setText(statusStr);
        System.out.println("statusStr="+ statusStr);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object ob = e.getSource();
        if (ob == resetButton)
        {
            reset();
        }
        else
        {
            for (int row=0; row < 3; row++)
                for (int col=0; col < 3; col++)
                {
                    if (ob == buttons[row][col])
                    {
                        updateButton((JButton)ob);
                        return;
                    }
                }
        }

    }

    public static void main(String[] args) {
        TicTacToe ttt = new TicTacToe();

    }

}



Last Updated: November 18, 2013 11:53 PM