44 frame =
new JFrame(
"Tic Tac Toe");
45 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48 JPanel topPanel =
new JPanel();
49 frame.add(topPanel, BorderLayout.NORTH);
50 welcomeLabel =
new JLabel(
"Enter your player name...");
51 topPanel.add(welcomeLabel);
53 JPanel bottomPanel =
new JPanel();
54 bottomPanel.setLayout(
new GridBagLayout());
55 frame.add(bottomPanel, BorderLayout.SOUTH);
57 JPanel playerNameInputElements =
new JPanel();
58 bottomPanel.add(playerNameInputElements);
59 playerNameInputElements.setLayout(
new GridBagLayout());
61 JLabel playerNameLabel =
new JLabel(
"Enter your name: ");
62 playerNameInputElements.add(playerNameLabel);
64 playerNameField =
new JTextField(20);
65 playerNameInputElements.add(playerNameField);
67 submitButton =
new JButton(
"Submit");
68 playerNameInputElements.add(submitButton);
69 submitButton.addActionListener(
new ActionListener() {
70 public void actionPerformed(ActionEvent event) {
71 String input = playerNameField.getText();
72 welcomeLabel.setText(
"WELCOME " + input.toUpperCase());
73 frame.setTitle(
"Tic Tac Toe - Player: " + input);
75 playerNameField.setEnabled(
false);
76 submitButton.setEnabled(
false);
78 Component[] components = gameBoard.getComponents();
79 for (Component component : components) {
80 component.setEnabled(
true);
87 GridBagConstraints c =
new GridBagConstraints();
89 bottomPanel.add(timeLabel, c);
91 Timer timer =
new Timer(1000,
new ActionListener() {
92 public void actionPerformed(ActionEvent e) {
98 JMenuBar menuBar =
new JMenuBar();
99 frame.setJMenuBar(menuBar);
101 JMenu controlMenu =
new JMenu(
"Control");
102 menuBar.add(controlMenu);
103 JMenuItem exit =
new JMenuItem(
"Exit");
104 exit.addActionListener(
new ActionListener() {
105 public void actionPerformed(ActionEvent event) {
109 controlMenu.add(exit);
111 JMenu helpMenu =
new JMenu(
"Help");
112 menuBar.add(helpMenu);
113 JMenuItem instruction =
new JMenuItem(
"Instruction");
114 helpMenu.add(instruction);
115 instruction.addActionListener(
new ActionListener() {
116 public void actionPerformed(ActionEvent event) {
117 Object[] options = {
"Yes"};
118 JOptionPane.showOptionDialog(frame,
"Some information about the game:\n"
119 +
"- The move is not occupied by any mark.\n"
120 +
"- The move is made in the player's turn.\n"
121 +
"- The move is made within the 3 x 3 board.\n"
122 +
"The game would continue and switch among the player and the computer until it reaches either one of the following conditions:\n"
124 +
"- Computer wins.\n"
125 +
"- Draw.",
"Instructions", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
130 gameBoard =
new JPanel();
131 frame.add(gameBoard, BorderLayout.CENTER);
132 gameBoard.setLayout(
new GridLayout(3,3));
133 for (
int i = 0; i < 9; i++) {
134 JButton button =
new JButton();
137 button.setBackground(Color.WHITE);
139 button.setFocusPainted(
false);
141 button.setEnabled(
false);
142 gameBoard.add(button);
144 final int const_i = i;
145 button.addActionListener(
new ActionListener() {
146 public void actionPerformed(ActionEvent event) {
148 if (button.getText().equals(
"X") || button.getText().equals(
"O")) {
154 button.setFont(
new Font(
"Arial", Font.BOLD, 40));
155 button.setForeground(Color.GREEN);
159 welcomeLabel.setText(
"Valid move, waiting for your opponent.");
164 Component[] components = gameBoard.getComponents();
166 for (Component component : components) {
167 JButton button = (JButton)component;
168 button.setEnabled(
false);
170 button.setUI(
new MetalButtonUI() {
171 protected Color getDisabledTextColor() {
172 return button.getForeground();
177 Timer timer =
new Timer(2000,
new ActionListener() {
178 public void actionPerformed(ActionEvent e) {
180 int x = (int)(Math.random() * 3);
181 int y = (int)(Math.random() * 3);
182 while (board[x][y] !=
State.
EMPTY && board_filled < 9) {
183 x = (int)(Math.random() * 3);
184 y = (int)(Math.random() * 3);
189 int index = x * 3 + y;
190 JButton computerButton = (JButton)components[index];
191 computerButton.setText(
"O");
192 computerButton.setFont(
new Font(
"Arial", Font.BOLD, 40));
193 computerButton.setForeground(Color.RED);
195 for (Component component : components) {
196 component.setEnabled(
true);
199 welcomeLabel.setText(
"Your opponent has moved, now is your turn.");
204 timer.setRepeats(
false);
210 scorePanel =
new JPanel();
211 scorePanel.setBorder(BorderFactory.createTitledBorder(
"Score"));
212 frame.add(scorePanel, BorderLayout.EAST);
214 scorePanel.setLayout(
new GridBagLayout());
215 GridBagConstraints c2 =
new GridBagConstraints();
216 c2.anchor = GridBagConstraints.WEST;
220 JLabel playerScoreText =
new JLabel(
"Player Wins:");
222 scorePanel.add(playerScoreText, c2);
223 playerScore =
new JLabel(
"0");
225 scorePanel.add(playerScore, c2);
227 JLabel computerScoreText =
new JLabel(
"Computer Wins:");
230 scorePanel.add(computerScoreText, c2);
231 computerScore =
new JLabel(
"0");
233 scorePanel.add(computerScore, c2);
235 JLabel drawScoreText =
new JLabel(
"Draws:");
238 scorePanel.add(drawScoreText, c2);
239 drawScore =
new JLabel(
"0");
241 scorePanel.add(drawScore, c2);
243 frame.setSize(500,500);
244 frame.setVisible(
true);
265 String message_delivered =
"";
267 message_delivered =
"Player wins!";
268 playerScore.setText(Integer.parseInt(playerScore.getText()) + 1 +
"");
270 message_delivered =
"Computer wins!";
271 computerScore.setText(Integer.parseInt(computerScore.getText()) + 1 +
"");
273 message_delivered =
"It's a draw!";
274 drawScore.setText(Integer.parseInt(drawScore.getText()) + 1 +
"");
278 Object[] options = {
"Yes"};
279 JOptionPane.showOptionDialog(frame, message_delivered,
"Game Over", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
281 for (
int i = 0; i < 3; i++) {
282 for (
int j = 0; j < 3; j++) {
287 Component[] components = gameBoard.getComponents();
288 for (Component component : components) {
289 JButton button = (JButton)component;