import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; public class SwingTicTacToe extends JFrame implements ActionListener{ private boolean xTurn = true; private String[] inputs = {"","","","","","","","","",""}; private JLabel output = new JLabel("Welcome to Tic Tac Toe!", SwingConstants.CENTER); private JButton zero = new JButton(); private JButton one = new JButton(); private JButton two = new JButton(); private JButton three = new JButton(); private JButton four = new JButton(); private JButton five = new JButton(); private JButton six = new JButton(); private JButton seven = new JButton(); private JButton eight = new JButton(); JButton[] buttonList = {zero, one, two, three, four, five, six, seven, eight}; /** * Creates the TicTacToe board with nine buttons and Pittsburgh Steeler colors! */ public SwingTicTacToe(){ super("Tic Tac Toe!"); setSize(600,600); this.getContentPane().setBackground(Color.YELLOW); this.setDefaultCloseOperation(EXIT_ON_CLOSE); add(output, BorderLayout.NORTH); JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(3,3)); for(int i = 0; i < buttonList.length; i++){ if(i % 2 == 0){ buttonList[i].setBackground(Color.YELLOW); buttonList[i].setForeground(Color.BLACK); } else{ buttonList[i].setBackground(Color.BLACK); buttonList[i].setForeground(Color.YELLOW); } buttonList[i].setFont(new Font("", Font.BOLD, 80)); buttonList[i].addActionListener(this); buttonList[i].setActionCommand(Integer.toString(i)); buttons.add(buttonList[i]); } add(buttons, BorderLayout.CENTER); } /** * This main frame is its own ActionListener * Therefore, it implements this method that changes the button that the user picks to the user's letter (either X or O) * And it also determines if this move ends the game */ @Override public void actionPerformed(ActionEvent e) { String buttonString = e.getActionCommand(); int index = Integer.parseInt(buttonString); recordTurn(index); boolean tie = checkTie(); boolean xWin = checkWin("X"); boolean oWin = checkWin("O"); int playAgain = 5; if(xWin){ output.setText("X Wins!!!"); JOptionPane.showMessageDialog(this, "X WINS!!!"); playAgain = playAgain(); } else if(oWin){ output.setText("O Wins!!!"); JOptionPane.showMessageDialog(this, "O WINS!!!"); playAgain = playAgain(); } else if(tie){ output.setText("CAT!! (A cat is a TIE in Tic Tac Toe)"); JOptionPane.showMessageDialog(this, "Looks like we have a CAT!! (A cat is a TIE in Tic Tac Toe)"); playAgain = playAgain(); } if(playAgain == 0) resetGame(); else if(playAgain == 1) System.exit(0); } /** * Checks the array of user inputs to see if there is a tie * @return */ private boolean checkTie() { boolean tie = false; String piece = ""; if(!(inputs[0].equalsIgnoreCase(piece) || inputs[1].equalsIgnoreCase(piece) || inputs[2].equalsIgnoreCase(piece) || inputs[3].equalsIgnoreCase(piece) || inputs[4].equalsIgnoreCase(piece) || inputs[5].equalsIgnoreCase(piece) || inputs[6].equalsIgnoreCase(piece) || inputs[7].equalsIgnoreCase(piece) || inputs[8].equalsIgnoreCase(piece))) tie = true; return tie; } /** * Resets the game board resetting all of the buttons to be blank and the Welcome message appears */ private void resetGame() { for(int i = 0; i < buttonList.length; i++){ buttonList[i].setText(""); inputs[i] = ""; } xTurn = true; output.setText("Welcome to Tic Tac Toe!!"); } /** * Asks the user if he/she would like to play again. * @return 0 if the user wants to play again, 1 if user wants to quit */ private int playAgain() { Object[] options = {"Yes, please", "No, thanks"}; int n = JOptionPane.showOptionDialog(this, "Would you like to play again?", "Play Again", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]); return n; } /** * Checks the input array to see if the specified user has won yet * @param piece the user's letter (either X or O) * @return true if the specified user won, false if not */ private boolean checkWin(String piece) { if(horizontalWin(piece) || verticalWin(piece) || diagnolWin(piece)) return true; else return false; } private boolean horizontalWin(String piece) { boolean win = false; for(int i = 0; i < 7; i+=3){ if(inputs[i].equalsIgnoreCase(piece) && inputs[i+1].equalsIgnoreCase(piece) && inputs[i+2].equalsIgnoreCase(piece)){ win = true; break; } } return win; } private boolean diagnolWin(String piece) { boolean win = false; if(inputs[0].equalsIgnoreCase(piece) && inputs[4].equalsIgnoreCase(piece) && inputs[8].equalsIgnoreCase(piece)) win = true; else if(inputs[6].equalsIgnoreCase(piece) && inputs[4].equalsIgnoreCase(piece) && inputs[2].equalsIgnoreCase(piece)) win = true; return win; } private boolean verticalWin(String piece) { boolean win = false; for(int i = 0; i < 3; i++){ if(inputs[i].equalsIgnoreCase(piece) && inputs[i+3].equalsIgnoreCase(piece) && inputs[i+6].equalsIgnoreCase(piece)){ win = true; break; } } return win; } /** * If user picks empty space, it records their move and changes the button's text to the user's letter * If space is not empty, an error message appears at the top of the frame to tell the user to pick an empty space. * This message will show until the user picks an empty space * @param index the space that the user selected (index in the input array) */ private void recordTurn(int index) { if(!inputs[index].equalsIgnoreCase("")){ output.setText("Woah! That is already is selected. Pick a different one."); } else{ if(xTurn){ inputs[index] = "X"; buttonList[index].setText("X"); xTurn = false; } else{ inputs[index] = "O"; buttonList[index].setText("O"); xTurn = true; } output.setText("Select where you want to place your piece!"); } } public static void main(String[] args) { SwingTicTacToe game = new SwingTicTacToe(); game.setVisible(true); } }