GUI Tic-Tac-Toe in Java Revisited

In my post found here I discussed a tic-tac-toe game coded in Java which was a version using a Graphical User Interface. The code listing for the classes and interfaces can be found in the same post. In this post I will discuss a Graphical User Interface (GUI) version of the same application with some additional features. The GUI tic-tac-toe app is based on a console application version found here. I shall only provide the changes and additions to the code listings here. Note that I will use the NetBeans IDE.

We will primarily make changes to the AWTBoard class. After making these changes, the user will be asked whether he really wants to quit when a game is in progress. If he clicks Cancel, the game will continue. If he clicks OK, the game will be terminated. If the game is over or not yet begun, the user will be able to stop the game as usual. See the video below the code listing to understand how it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.tictactoe.components;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
 
/**
 *
 * @author Dell
 */
public class AWTBoard extends Board implements ActionListener{
 
    private Button[][] buttons = new Button[3][3];
    private Frame f;
    private Label l;
 
    private Move currMove;
    private Game myGame;
 
    public AWTBoard()
    {
        super();
        f = new Frame("Tic-Tac-Toe");
        f.setSize(300, 300);
 
 
        Panel topPanel = new Panel();
 
        topPanel.setLayout(new BorderLayout());
 
        Panel p = new Panel();
 
        GridLayout grd = new GridLayout(3,3);
 
        p.setLayout(grd);
 
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                buttons[i][j] = new Button();
 
                buttons[i][j].addActionListener(this);
 
                p.add(buttons[i][j], i, j); //problem
 
            }
        }
 
        topPanel.add(p, BorderLayout.CENTER);
 
        l = new Label();
 
        topPanel.add(l, BorderLayout.SOUTH);
 
        f.add(topPanel);
 
        /****Begin changes****/
        f.addWindowListener(new WindowAdapter(){ 
 
            public void windowClosing(WindowEvent e){ 
 
                if(myGame.isGameOver() || Game.getMovesTillNow() == 0)
                {
                    f.dispose(); 
                    System.exit(0);
                }else{
                    int result = JOptionPane.showConfirmDialog((Component) null, "Game in progress. Are you sure you want to quit?",
        "Message", JOptionPane.OK_CANCEL_OPTION);
 
                    if(result == 0)
                    {
                        f.dispose(); 
                        System.exit(0);
                    }
 
                }
            } 
 
        });
        /****End changes****/        
 
        //f.pack();
        f.setBounds(200, 200, f.getWidth(), f.getHeight());
 
        f.setVisible(true);
    }
 
    public void setGame(Game game)
    {
        myGame = game;
    }
 
    public void display()
    {
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++) 
            {  
 
                buttons[i][j].setLabel( ""+cells[i][j] );
 
            } 
 
        }  
    }
 
    public void markCell(Move move, char mark)
    {
        if(move.isIsValid())
        {
            int i = move.getRow(); 
            int j = move.getColumn(); 
            cells[i - 1][j - 1] = mark;
 
            buttons[i - 1][j - 1].setLabel(""+mark);
        }
        else
        {
            //System.out.println("INVALID MOVE!\n\n");
            l.setText("INVALID MOVE!");
        }
    }
 
    public Label getStatusLabel()
    {
        return l;
    }
 
    public void actionPerformed(ActionEvent e)
    {
        Button b = (Button)e.getSource();
 
        for(int i = 0 ; i < 3; i++ )
        {
            for(int j = 0; j < 3; j++)
            {
                if(b == buttons[i][j])
                {
                    currMove = new Move();
                    currMove.setRow(i + 1);
                    currMove.setColumn(j + 1);
                    currMove.setIsValid(true);
                }
            }
        }
 
 
        synchronized(this)
        {
            this.notify();
        }
 
    }
 
    public Move getCurrMove()
    {
        return currMove;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *