Create a 2-player Tic Tac Toe. Match the given style and implied functionality as closely as possible. X wins After each click, check to see if the game has ended. If X or O wins, show the appropriate message. If there's a tie, show the appropriate message. After the game ends, reset the board. The first click on a empty cell should put X on the button. The next click on an empty cell should put O on the button. Follow this pattern for the rest of the game. If the user clicks on a button that already has an X or O, do nothing.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Question:

Create a 2-player Tic Tac Toe. Match the given style and implied functionality as closely as possible. X wins

After each click, check to see if the game has ended. If X or O wins, show the appropriate message. If there's a tie, show the appropriate message. After the game ends, reset the board. The first click on a empty cell should put X on the button. The next click on an empty cell should put O on the button. Follow this pattern for the rest of the game. If the user clicks on a button that already has an X or O, do nothing.

Replace the button label text with pictures instead

this is my current code:

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

public class TicTacToe extends JFrame implements ActionListener
{
JButton board[]=new JButton[9];
boolean turn=true;
int boxFilled=0;

ImageIcon xIcon = new ImageIcon("x.png");
ImageIcon oIcon = new ImageIcon("o.png");
public TicTacToe()
{
super.setTitle("Tic Tac Toe");
super.setSize(300, 300);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
buildPanel();
super.setVisible(true);
}

public void buildPanel()
{

GridLayout g = new GridLayout(3, 3);
super.setLayout(g);

for(int i=0; i<board.length;i++)
{
board[i]=new JButton();
board[i].setIcon(xIcon);
board[i].addActionListener(this);
super.add(board[i]);
}
}
public void actionPerformed(ActionEvent ae)
{
JButton player = (JButton)ae.getSource();


if(turn)
{

if(!player.getText().equals(""))
return;


player.setIcon(xIcon);


boxFilled++;

if(checkWinner("X"))
{
JOptionPane.showMessageDialog(this,"X wins!\nResetting...");
resetBoard();
return;
}

}
else
{
if(!player.getText().equals(""))
return;

player.setIcon(oIcon);

boxFilled++;

if(checkWinner("O"))
{
JOptionPane.showMessageDialog(this,"O wins!\nResetting...");
resetBoard();
return;
}

}

if(boxFilled==9)
{
JOptionPane.showMessageDialog(this,"Cat's game\nResetting...");
resetBoard();
return;
}

turn=!turn;
}
public boolean checkWinner(String player)
{

if(board[0].getText().equals(player) && board[1].getText().equals(player) && board[2].getText().equals(player))
{
return true;
}
if(board[3].getText().equals(player) && board[4].getText().equals(player) && board[5].getText().equals(player))
{
return true;
}
if(board[6].getText().equals(player) && board[7].getText().equals(player) && board[8].getText().equals(player))
{
return true;
}
if(board[0].getText().equals(player) && board[3].getText().equals(player) && board[6].getText().equals(player))
{
return true;
}
if(board[4].getText().equals(player) && board[1].getText().equals(player) && board[7].getText().equals(player))
{
return true;
}
if(board[8].getText().equals(player) && board[5].getText().equals(player) && board[2].getText().equals(player))
{
return true;
}
if(board[0].getText().equals(player) && board[4].getText().equals(player) && board[8].getText().equals(player))
{
return true;
}
if(board[2].getText().equals(player) && board[4].getText().equals(player) && board[6].getText().equals(player))
{
return true;
}
return false;
}
public void resetBoard()
{
turn=true;
boxFilled=0;
for(JButton btn: board)
{
btn.setIcon(null);
btn.setText("");
}
}

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

I have implemented imageicon method to display o.png and x.png files to replace to x and o in the tictactoe board

however, when i run the code the images do not pop up on screen

Can an expert fix my coding to display the images properly when running the code, i do not know what went wrong

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Web Page
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education