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.
-
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 code:
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].addActionListener(this);
super.add(board[i]);
}
}
public void actionPerformed(ActionEvent ae)
{
JButton player = (JButton)ae.getSource();
//Checking Which Players turn it is
if(turn)
{
if(!player.getText().equals(""))
return;
player.setText("X");
boxFilled++;
if(checkWinner("X"))
{
JOptionPane.showMessageDialog(this,"X wins!\nResetting...");
resetBoard();
return;
}
}
else
{
if(!player.getText().equals(""))
return;
player.setText("O");
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.setText("");
}
}
public static void main(String[] args)
{
new TicTacToe();
}
}
However i need to replace the button label text with pictures instead
I beleive i am supposed to use some sort of combobox coding/ contol to provide the pictures
can someone help me with this coding to implement a .png file
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)