Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class tictactoeFrame extends JFrame
- {
- private GridBagLayout layout;//gridbaglayout declaration
- private GridBagConstraints gbc;
- private JTextField winner;//prints out winner
- private JLabel winnerLabel;//label for winner
- private JLabel space;//helps with the format
- private JButton square1;//buttons for game grid
- private JButton square2;
- private JButton square3;
- private JButton square4;
- private JButton square5;
- private JButton square6;
- private JButton square7;
- private JButton square8;
- private JButton square9;
- private JButton quitButton;//button to quit game
- private JButton newGameButton;//button to clear board
- private JRadioButton xButton;//radio button for first player
- private JRadioButton oButton;//radio button for first player
- private ButtonGroup radioGroup;//button group so radio buttons can't be selected at the same time
- private String num1="";//variable to help check for win
- private String num2="";//variable to help check for win
- private String num3="";//variable to help check for win
- private String num4="";//variable to help check for win
- private String num5="";//variable to help check for win
- private String num6="";//variable to help check for win
- private String num7="";//variable to help check for win
- private String num8="";//variable to help check for win
- private String num9="";//variable to help check for win
- private int numMoves=0;//counts number of moves
- private int counter=0;//counter for win arrays
- private int maxMoves=9;//max num moves
- private boolean[] xWin = {false, false, false, false, false, false, false, false, false};//array to store moves
- private boolean[] oWin = {false, false, false, false, false, false, false, false, false};//array to store moves
- private boolean win=false;//if there's a winner its true
- private boolean xTurn=false;//checks x turn
- private tictactoeOps tictacfunc;
- public tictactoeFrame()
- {
- super("Tic-Tac-Toe");
- layout = new GridBagLayout();
- setLayout(layout);//sets layout as gridbag
- gbc = new GridBagConstraints();
- space = new JLabel(" ");
- gbc.fill = GridBagConstraints.HORIZONTAL;//space is used to help the layout
- addComponent(space,0,0,1,1);
- space = new JLabel(" ");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(space,1,0,1,1);
- space = new JLabel(" ");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(space,2,0,1,1);
- space = new JLabel(" ");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(space,3,0,1,1);
- space = new JLabel(" ");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(space,4,0,1,1);
- space = new JLabel(" ");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(space,5,0,1,1);
- space = new JLabel(" ");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(space,6,0,1,1);
- //adds and places all buttons
- square1 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 1
- addComponent(square1,1,1,1,1);
- square2 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 2
- addComponent(square2,1,2,1,1);
- square3 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 3
- addComponent(square3,1,3,1,1);
- square4 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 4
- addComponent(square4,2,1,1,1);
- square5 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 5
- addComponent(square5,2,2,1,1);
- square6 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 6
- addComponent(square6,2,3,1,1);
- square7 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 7
- addComponent(square7,3,1,1,1);
- square8 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 8
- addComponent(square8,3,2,1,1);
- square9 = new JButton("");
- gbc.fill = GridBagConstraints.HORIZONTAL;//button 9
- addComponent(square9,3,3,1,1);
- //adds radio buttons
- xButton = new JRadioButton("X",false);
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(xButton,4,1,1,1);
- oButton = new JRadioButton("O",false);
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(oButton,4,2,1,1);
- radioGroup = new ButtonGroup();
- radioGroup.add(xButton);
- radioGroup.add(oButton);
- //adds winner label, text field, and new game and quit buttons
- winnerLabel = new JLabel("Winner:");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(winnerLabel,5,1,1,1);
- winner = new JTextField(1);
- winner.setEnabled(false);
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(winner,5,2,1,1);
- newGameButton = new JButton("New ");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(newGameButton,6,1,1,1);
- quitButton = new JButton("Quit");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- addComponent(quitButton,6,3,1,1);
- buttonhandler myhandler = new buttonhandler();//button handler for all the squares
- square1.addActionListener(myhandler); //and newGame and quit buttons
- square2.addActionListener(myhandler);
- square3.addActionListener(myhandler);
- square4.addActionListener(myhandler);
- square5.addActionListener(myhandler);
- square6.addActionListener(myhandler);
- square7.addActionListener(myhandler);
- square8.addActionListener(myhandler);
- square9.addActionListener(myhandler);
- newGameButton.addActionListener(myhandler);
- quitButton.addActionListener(myhandler);
- tictacfunc = new tictactoeOps();
- }
- private class buttonhandler implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- //select first move
- if(xButton.isSelected())
- {
- xTurn=true;
- radioGroup.clearSelection();
- }
- else if(oButton.isSelected())
- {
- xTurn=false;
- radioGroup.clearSelection();
- }
- //checks for button clicks
- if(event.getSource()==square1)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square1.setText("X");
- square1.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square1.setText("O");
- square1.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square2)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square2.setText("X");
- square2.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square2.setText("O");
- square2.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square3)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square3.setText("X");
- square3.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square3.setText("O");
- square3.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square4)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square4.setText("X");
- square4.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square4.setText("O");
- square4.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square5)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square5.setText("X");
- square5.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square5.setText("O");
- square5.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square6)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square6.setText("X");
- square6.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square6.setText("O");
- square6.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square7)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square7.setText("X");
- square7.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square7.setText("O");
- square7.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square8)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square8.setText("X");
- square8.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square8.setText("O");
- square8.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==square9)
- {
- if(xTurn==true)//checks if x's turn
- {
- xTurn=false;
- square9.setText("X");
- square9.setEnabled(false);
- }
- else//if o's turn
- {
- xTurn=true;
- square9.setText("O");
- square9.setEnabled(false);
- }
- numMoves++;
- }
- else if(event.getSource()==newGameButton)
- {
- square1.setText("");//sets all button texts to blank
- square2.setText("");
- square3.setText("");
- square4.setText("");
- square5.setText("");
- square6.setText("");
- square7.setText("");
- square8.setText("");
- square9.setText("");
- winner.setText("");//sets winner field to blank
- radioGroup.clearSelection();//deselects radio buttons
- square1.setEnabled(true);//enables buttons to play
- square2.setEnabled(true);
- square3.setEnabled(true);
- square4.setEnabled(true);
- square5.setEnabled(true);
- square6.setEnabled(true);
- square7.setEnabled(true);
- square8.setEnabled(true);
- square9.setEnabled(true);
- numMoves=0;//resets numMoves
- }
- else if(event.getSource()==quitButton)
- System.exit(0);
- if(numMoves>=5 && numMoves<maxMoves)//after 5 moves it starts to check for a winner
- {
- num1=square1.getText();//gets text from buttons
- num2=square2.getText();//to compare in checkWin
- num3=square3.getText();
- num4=square4.getText();
- num5=square5.getText();
- num6=square6.getText();
- num7=square7.getText();
- num8=square8.getText();
- num9=square9.getText();
- win=tictactoeOps.checkWin(num1, num2, num3, num4, num5, num6, num7, num8, num9);//checks win
- if(win==true && xTurn==false)//sets winner field to X if X wins
- { //due to the change of turn after every turn
- winner.setText("X"); //xTurn must equal false for X to be in winner box if X wins
- tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
- }
- else if(win==true && xTurn==true)//sets winner to O if O wins
- { //same reason for X winning and xTurn being false is
- winner.setText("O"); //reason xTurn is true for O winning
- tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
- }
- }
- else if(numMoves == maxMoves)//after 9 moves no more moves can be made and last check for win or tie
- {
- num1=square1.getText();
- num2=square2.getText();
- num3=square3.getText();
- num4=square4.getText();
- num5=square5.getText();
- num6=square6.getText();
- num7=square7.getText();
- num8=square8.getText();
- num9=square9.getText();
- win=tictactoeOps.checkWin(num1, num2, num3, num4, num5, num6, num7, num8, num9);
- if(win==true && xTurn==false)//sets winner field to X if X wins
- { //due to the change of turn after every turn
- winner.setText("X"); //xTurn must equal false for X to be in winner box if X wins
- tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
- }
- else if(win==true && xTurn==true)//sets winner to O if O wins
- { //same reason for X winning and xTurn being false is
- winner.setText("O"); //reason xTurn is true for O winning
- tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
- }
- else
- {
- winner.setText("Tie");//sets winner field to tie if no one wins
- tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
- }
- }
- }
- }
- private void addComponent(Component component, int row, int column, int width, int height)
- {
- gbc.gridx = column;//small function to make it easier to add components
- gbc.gridy = row;
- gbc.gridwidth = width;
- gbc.gridheight = height;
- layout.setConstraints(component, gbc);
- add(component);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement