Project: Tic Tac Toe Simulator Create a GUI application that simulates a game of
ID: 3814974 • Letter: P
Question
Project: Tic Tac Toe Simulator
Create a GUI application that simulates a game of tic tac toe. Following figure shows an example of the application’s window. The window shown in the figure uses nine large JLabel components to display the Xs and Os. One approach in designing this application is to use a two-dimensional int array to simulate the game board in memory. When the user clicks the New Game button, the application should step through the array, storing a random number in the range of 0 through 1 in each element. The number 0 represents the letter O, and the number 1 represents the letter X. The JLabel components should then be updated to display the game board. The application should display a message indicating whether player X won, player Y won, or the game was a tie.
Tic Tac Toe Xoo OXX O XX New Game Exit Message i X Wins!Explanation / Answer
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TicTacToe extends JPanel
{
JButton buttons[] = new JButton[9];
int alternate = 0;//if this number is a even, then put a X. If it's odd, then put an O
public TicTacToe()
{
setLayout(new GridLayout(3,3));
initializebuttons();
}
public void initializebuttons()
{
for(int i = 0; i <= 8; i++)
{
buttons[i] = new JButton();
buttons[i].setText("");
buttons[i].addActionListener(new buttonListener());
add(buttons[i]); //adds this button to JPanel (note: no need for JPanel.add(...)
//because this whole class is a JPanel already
}
}
public void resetButtons()
{
for(int i = 0; i <= 8; i++)
{
buttons[i].setText("");
}
}
// when a button is clicked, it generates an ActionEvent. Thus, each button needs an ActionListener. When it is clicked, it goes to this listener class that I have created and goes to the actionPerformed method. There (and in this class), we decide what we want to do.
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton buttonClicked = (JButton)e.getSource(); //get the particular button that was clicked
if(alternate%2 == 0)
buttonClicked.setText("X");
else
buttonClicked.setText("O");
if(checkForWin() == true)
{
JOptionPane.showConfirmDialog(null, "Game Over.");
resetButtons();
}
alternate++;
}
public boolean checkForWin()
{
//horizontal win check
if( checkAdjacent(0,1) && checkAdjacent(1,2) ) //no need to put " == true" because the default check is for true
return true;
else if( checkAdjacent(3,4) && checkAdjacent(4,5) )
return true;
else if ( checkAdjacent(6,7) && checkAdjacent(7,8))
return true;
//vertical win check
else if ( checkAdjacent(0,3) && checkAdjacent(3,6))
return true;
else if ( checkAdjacent(1,4) && checkAdjacent(4,7))
return true;
else if ( checkAdjacent(2,5) && checkAdjacent(5,8))
return true;
//diagonal win check
else if ( checkAdjacent(0,4) && checkAdjacent(4,8))
return true;
else if ( checkAdjacent(2,4) && checkAdjacent(4,6))
return true;
else
return false;
}
public boolean checkAdjacent(int a, int b)
{
if ( buttons[a].getText().equals(buttons[b].getText()) && !buttons[a].getText().equals("") )
return true;
else
return false;
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Tic-Tac-Toe");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(new TicTacToe());
window.setBounds(300,200,300,300);
window.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.