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.
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
Trending now
This is a popular solution!
Step by step
Solved in 3 steps