Tic Tac Toe in Java with GUI. The assignment is to write a code in Java for
Fantastic news! We've Found the answer you've been seeking!
Question:
Tic Tac Toe in Java with GUI.
The assignment is to write a code in Java for Tic Tac Toe gamewith GUI. I wrote a code where 2 palyers are playing, but wouldlike player vs computer. Please help to update current code so thatgame is player vs computer.
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class TicTacToe extends JPanel { char playerSymbol = 'X'; JButton[] buttons = new JButton[9]; public TicTacToe() { setLayout(new GridLayout(3,3)); setButtons(); } public void setButtons() { for(int i = 0; i <= 8; i++) { buttons[i] = new JButton(); buttons[i].setText(" "); buttons[i].setBackground(Color.white); buttons[i].setOpaque(true); buttons[i].setBorder(BorderFactory.createLineBorder(Color.GRAY, 3)); buttons[i].setFont(new Font(Font.SERIF, Font.BOLD, 100)); buttons[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton buttonClicked = (JButton) e.getSource(); buttonClicked.setText(String.valueOf(playerSymbol)); if(playerSymbol == 'X') { playerSymbol = 'O'; buttonClicked.setBackground(Color.GRAY); } else { playerSymbol ='X'; buttonClicked.setBackground(Color.PINK); } showWinner(); } }); add(buttons[i]); } } public void showWinner() { if(checkWinner() == true) { if(playerSymbol == 'X') playerSymbol = 'O'; else playerSymbol ='X'; JOptionPane pane = new JOptionPane(); int dialogResult = JOptionPane.showConfirmDialog(pane, "Game Over! "+ playerSymbol + " has won. Would you like to play again?","Game over!", JOptionPane.YES_NO_OPTION); if(dialogResult == JOptionPane.YES_OPTION) resetGame(); else System.exit(0); } else if(checkDraw()) { JOptionPane pane = new JOptionPane(); int dialogResult = JOptionPane.showConfirmDialog(pane, "It's a draw! Play again?","Game over!", JOptionPane.YES_NO_OPTION); if(dialogResult == JOptionPane.YES_OPTION) resetGame(); else System.exit(0); } } private void resetGame() { playerSymbol = 'X'; for(int i =0;i<9;i++) { buttons[i].setText(" "); buttons[i].setBackground(Color.white); } } public boolean checkDraw() { boolean full = true; for(int i = 0 ; i<9;i++) { if(buttons[i].getText().charAt(0) == ' ') { full = false; } } return full; } public boolean checkWinner() { return (checkRows() == true || checkColumns() == true || checkDiagonals() == true); } public boolean checkRows() { int i = 0; for(int j = 0;j<3;j++) { if( buttons[i].getText().equals(buttons[i+1].getText()) && buttons[i].getText().equals(buttons[i+2].getText()) && buttons[i].getText().charAt(0) != ' ') { return true; } i = i+3; } return false; } public boolean checkColumns() { int i = 0; for(int j = 0;j<3;j++) { if( buttons[i].getText().equals(buttons[i+3].getText()) && buttons[i].getText().equals(buttons[i+6].getText()) && buttons[i].getText().charAt(0) != ' ') { return true; } i++; } return false; } public boolean checkDiagonals() { if(buttons[0].getText().equals(buttons[4].getText()) && buttons[0].getText().equals(buttons[8].getText()) && buttons[0].getText().charAt(0) !=' ') return true; else return (buttons[2].getText().equals(buttons[4].getText()) && buttons[2].getText().equals(buttons[6].getText()) && buttons[2].getText().charAt(0) !=' '); }}
import javax.swing.*;public class Main { public static void main(String[] args) { JFrame board = new JFrame("TIC TAC TOE"); board.getContentPane().add(new TicTacToe()); board.setBounds(550,550,550,550); board.setVisible(true); board.setLocationRelativeTo(null); board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
Related Book For
Posted Date: