Advertisement
darkhelmet125

tictactoeFrame.java

Oct 14th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.77 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class tictactoeFrame extends JFrame
  6. {
  7.     private GridBagLayout layout;//gridbaglayout declaration
  8.     private GridBagConstraints gbc;
  9.    
  10.     private JTextField winner;//prints out winner
  11.     private JLabel winnerLabel;//label for winner
  12.     private JLabel space;//helps with the format
  13.     private JButton square1;//buttons for game grid
  14.     private JButton square2;
  15.     private JButton square3;
  16.     private JButton square4;
  17.     private JButton square5;
  18.     private JButton square6;
  19.     private JButton square7;
  20.     private JButton square8;
  21.     private JButton square9;
  22.     private JButton quitButton;//button to quit game
  23.     private JButton newGameButton;//button to clear board
  24.     private JRadioButton xButton;//radio button for first player
  25.     private JRadioButton oButton;//radio button for first player
  26.     private ButtonGroup radioGroup;//button group so radio buttons can't be selected at the same time
  27.     private String num1="";//variable to help check for win
  28.     private String num2="";//variable to help check for win
  29.     private String num3="";//variable to help check for win
  30.     private String num4="";//variable to help check for win
  31.     private String num5="";//variable to help check for win
  32.     private String num6="";//variable to help check for win
  33.     private String num7="";//variable to help check for win
  34.     private String num8="";//variable to help check for win
  35.     private String num9="";//variable to help check for win
  36.     private int numMoves=0;//counts number of moves
  37.     private int counter=0;//counter for win arrays
  38.     private int maxMoves=9;//max num moves
  39.     private boolean[] xWin = {false, false, false, false, false, false, false, false, false};//array to store moves
  40.     private boolean[] oWin = {false, false, false, false, false, false, false, false, false};//array to store moves
  41.     private boolean win=false;//if there's a winner its true
  42.     private boolean xTurn=false;//checks x turn
  43.    
  44.     private tictactoeOps tictacfunc;
  45.    
  46.     public tictactoeFrame()
  47.     {
  48.         super("Tic-Tac-Toe");
  49.         layout = new GridBagLayout();
  50.         setLayout(layout);//sets layout as gridbag
  51.         gbc = new GridBagConstraints();
  52.        
  53.         space = new JLabel("    ");
  54.         gbc.fill = GridBagConstraints.HORIZONTAL;//space is used to help the layout
  55.         addComponent(space,0,0,1,1);
  56.         space = new JLabel("    ");
  57.         gbc.fill = GridBagConstraints.HORIZONTAL;
  58.         addComponent(space,1,0,1,1);
  59.         space = new JLabel("    ");
  60.         gbc.fill = GridBagConstraints.HORIZONTAL;
  61.         addComponent(space,2,0,1,1);
  62.         space = new JLabel("    ");
  63.         gbc.fill = GridBagConstraints.HORIZONTAL;
  64.         addComponent(space,3,0,1,1);
  65.         space = new JLabel("    ");
  66.         gbc.fill = GridBagConstraints.HORIZONTAL;
  67.         addComponent(space,4,0,1,1);
  68.         space = new JLabel("    ");
  69.         gbc.fill = GridBagConstraints.HORIZONTAL;
  70.         addComponent(space,5,0,1,1);
  71.         space = new JLabel("    ");
  72.         gbc.fill = GridBagConstraints.HORIZONTAL;
  73.         addComponent(space,6,0,1,1);
  74.         //adds and places all buttons
  75.         square1 = new JButton("");
  76.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 1
  77.         addComponent(square1,1,1,1,1);
  78.         square2 = new JButton("");
  79.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 2
  80.         addComponent(square2,1,2,1,1);
  81.         square3 = new JButton("");
  82.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 3
  83.         addComponent(square3,1,3,1,1);
  84.         square4 = new JButton("");
  85.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 4
  86.         addComponent(square4,2,1,1,1);
  87.         square5 = new JButton("");
  88.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 5
  89.         addComponent(square5,2,2,1,1);
  90.         square6 = new JButton("");
  91.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 6
  92.         addComponent(square6,2,3,1,1);
  93.         square7 = new JButton("");
  94.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 7
  95.         addComponent(square7,3,1,1,1);
  96.         square8 = new JButton("");
  97.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 8
  98.         addComponent(square8,3,2,1,1);
  99.         square9 = new JButton("");
  100.         gbc.fill = GridBagConstraints.HORIZONTAL;//button 9
  101.         addComponent(square9,3,3,1,1);
  102.         //adds radio buttons
  103.         xButton = new JRadioButton("X",false);
  104.         gbc.fill = GridBagConstraints.HORIZONTAL;
  105.         addComponent(xButton,4,1,1,1);
  106.         oButton = new JRadioButton("O",false);
  107.         gbc.fill = GridBagConstraints.HORIZONTAL;
  108.         addComponent(oButton,4,2,1,1);
  109.         radioGroup = new ButtonGroup();
  110.         radioGroup.add(xButton);
  111.         radioGroup.add(oButton);
  112.         //adds winner label, text field, and new game and quit buttons
  113.         winnerLabel = new JLabel("Winner:");
  114.         gbc.fill = GridBagConstraints.HORIZONTAL;
  115.         addComponent(winnerLabel,5,1,1,1);
  116.         winner = new JTextField(1);
  117.         winner.setEnabled(false);
  118.         gbc.fill = GridBagConstraints.HORIZONTAL;
  119.         addComponent(winner,5,2,1,1);
  120.         newGameButton = new JButton("New ");
  121.         gbc.fill = GridBagConstraints.HORIZONTAL;
  122.         addComponent(newGameButton,6,1,1,1);
  123.         quitButton = new JButton("Quit");
  124.         gbc.fill = GridBagConstraints.HORIZONTAL;
  125.         addComponent(quitButton,6,3,1,1);
  126.        
  127.         buttonhandler myhandler = new buttonhandler();//button handler for all the squares
  128.         square1.addActionListener(myhandler);           //and newGame and quit buttons
  129.         square2.addActionListener(myhandler);
  130.         square3.addActionListener(myhandler);
  131.         square4.addActionListener(myhandler);
  132.         square5.addActionListener(myhandler);
  133.         square6.addActionListener(myhandler);
  134.         square7.addActionListener(myhandler);
  135.         square8.addActionListener(myhandler);
  136.         square9.addActionListener(myhandler);
  137.         newGameButton.addActionListener(myhandler);
  138.         quitButton.addActionListener(myhandler);
  139.         tictacfunc = new tictactoeOps();
  140.     }
  141.    
  142.     private class buttonhandler implements ActionListener
  143.     {
  144.         public void actionPerformed(ActionEvent event)
  145.         {
  146.             //select first move
  147.             if(xButton.isSelected())
  148.             {
  149.                 xTurn=true;
  150.                 radioGroup.clearSelection();
  151.             }
  152.             else if(oButton.isSelected())
  153.             {
  154.                 xTurn=false;
  155.                 radioGroup.clearSelection();
  156.             }
  157.             //checks for button clicks
  158.             if(event.getSource()==square1)
  159.             {
  160.                 if(xTurn==true)//checks if x's turn
  161.                 {
  162.                     xTurn=false;
  163.                     square1.setText("X");
  164.                     square1.setEnabled(false);
  165.                 }
  166.                 else//if o's turn
  167.                 {
  168.                     xTurn=true;
  169.                     square1.setText("O");
  170.                     square1.setEnabled(false);
  171.                 }
  172.                 numMoves++;
  173.             }
  174.             else if(event.getSource()==square2)
  175.             {
  176.                 if(xTurn==true)//checks if x's turn
  177.                 {
  178.                     xTurn=false;
  179.                     square2.setText("X");
  180.                     square2.setEnabled(false);
  181.                 }
  182.                 else//if o's turn
  183.                 {
  184.                     xTurn=true;
  185.                     square2.setText("O");
  186.                     square2.setEnabled(false);
  187.                 }
  188.                 numMoves++;
  189.             }
  190.             else if(event.getSource()==square3)
  191.             {
  192.                 if(xTurn==true)//checks if x's turn
  193.                 {
  194.                     xTurn=false;
  195.                     square3.setText("X");
  196.                     square3.setEnabled(false);
  197.                 }
  198.                 else//if o's turn
  199.                 {
  200.                     xTurn=true;
  201.                     square3.setText("O");
  202.                     square3.setEnabled(false);
  203.                 }
  204.                 numMoves++;
  205.             }
  206.             else if(event.getSource()==square4)
  207.             {
  208.                 if(xTurn==true)//checks if x's turn
  209.                 {
  210.                     xTurn=false;
  211.                     square4.setText("X");
  212.                     square4.setEnabled(false);
  213.                 }
  214.                 else//if o's turn
  215.                 {
  216.                     xTurn=true;
  217.                     square4.setText("O");
  218.                     square4.setEnabled(false);
  219.                 }
  220.                 numMoves++;
  221.             }
  222.             else if(event.getSource()==square5)
  223.             {
  224.                 if(xTurn==true)//checks if x's turn
  225.                 {
  226.                     xTurn=false;
  227.                     square5.setText("X");
  228.                     square5.setEnabled(false);
  229.                 }
  230.                 else//if o's turn
  231.                 {
  232.                     xTurn=true;
  233.                     square5.setText("O");
  234.                     square5.setEnabled(false);
  235.                 }
  236.                 numMoves++;
  237.             }
  238.             else if(event.getSource()==square6)
  239.             {
  240.                 if(xTurn==true)//checks if x's turn
  241.                 {
  242.                     xTurn=false;
  243.                     square6.setText("X");
  244.                     square6.setEnabled(false);
  245.                 }
  246.                 else//if o's turn
  247.                 {
  248.                     xTurn=true;
  249.                     square6.setText("O");
  250.                     square6.setEnabled(false);
  251.                 }
  252.                 numMoves++;
  253.             }
  254.             else if(event.getSource()==square7)
  255.             {
  256.                 if(xTurn==true)//checks if x's turn
  257.                 {
  258.                     xTurn=false;
  259.                     square7.setText("X");
  260.                     square7.setEnabled(false);
  261.                 }
  262.                 else//if o's turn
  263.                 {
  264.                     xTurn=true;
  265.                     square7.setText("O");
  266.                     square7.setEnabled(false);
  267.                 }
  268.                 numMoves++;
  269.             }
  270.             else if(event.getSource()==square8)
  271.             {
  272.                 if(xTurn==true)//checks if x's turn
  273.                 {
  274.                     xTurn=false;
  275.                     square8.setText("X");
  276.                     square8.setEnabled(false);
  277.                 }
  278.                 else//if o's turn
  279.                 {
  280.                     xTurn=true;
  281.                     square8.setText("O");
  282.                     square8.setEnabled(false);
  283.                 }
  284.                 numMoves++;
  285.             }
  286.             else if(event.getSource()==square9)
  287.             {
  288.                 if(xTurn==true)//checks if x's turn
  289.                 {
  290.                     xTurn=false;
  291.                     square9.setText("X");
  292.                     square9.setEnabled(false);
  293.                 }
  294.                 else//if o's turn
  295.                 {
  296.                     xTurn=true;
  297.                     square9.setText("O");
  298.                     square9.setEnabled(false);
  299.                 }
  300.                 numMoves++;
  301.             }
  302.             else if(event.getSource()==newGameButton)
  303.             {
  304.                 square1.setText("");//sets all button texts to blank
  305.                 square2.setText("");
  306.                 square3.setText("");
  307.                 square4.setText("");
  308.                 square5.setText("");
  309.                 square6.setText("");
  310.                 square7.setText("");
  311.                 square8.setText("");
  312.                 square9.setText("");
  313.                 winner.setText("");//sets winner field to blank
  314.                 radioGroup.clearSelection();//deselects radio buttons
  315.                 square1.setEnabled(true);//enables buttons to play
  316.                 square2.setEnabled(true);
  317.                 square3.setEnabled(true);
  318.                 square4.setEnabled(true);
  319.                 square5.setEnabled(true);
  320.                 square6.setEnabled(true);
  321.                 square7.setEnabled(true);
  322.                 square8.setEnabled(true);
  323.                 square9.setEnabled(true);
  324.                 numMoves=0;//resets numMoves
  325.             }
  326.             else if(event.getSource()==quitButton)
  327.                 System.exit(0);
  328.             if(numMoves>=5 && numMoves<maxMoves)//after 5 moves it starts to check for a winner
  329.             {
  330.                 num1=square1.getText();//gets text from buttons
  331.                 num2=square2.getText();//to compare in checkWin
  332.                 num3=square3.getText();
  333.                 num4=square4.getText();
  334.                 num5=square5.getText();
  335.                 num6=square6.getText();
  336.                 num7=square7.getText();
  337.                 num8=square8.getText();
  338.                 num9=square9.getText();
  339.                 win=tictactoeOps.checkWin(num1, num2, num3, num4, num5, num6, num7, num8, num9);//checks win
  340.                 if(win==true && xTurn==false)//sets winner field to X if X wins
  341.                 {                           //due to the change of turn after every turn
  342.                     winner.setText("X");    //xTurn must equal false for X to be in winner box if X wins
  343.                     tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
  344.                 }
  345.                 else if(win==true && xTurn==true)//sets winner to O if O wins
  346.                 {                               //same reason for X winning and xTurn being false is
  347.                     winner.setText("O");        //reason xTurn is true for O winning
  348.                     tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
  349.                 }
  350.             }
  351.             else if(numMoves == maxMoves)//after 9 moves no more moves can be made and last check for win or tie
  352.             {
  353.                 num1=square1.getText();
  354.                 num2=square2.getText();
  355.                 num3=square3.getText();
  356.                 num4=square4.getText();
  357.                 num5=square5.getText();
  358.                 num6=square6.getText();
  359.                 num7=square7.getText();
  360.                 num8=square8.getText();
  361.                 num9=square9.getText();
  362.                 win=tictactoeOps.checkWin(num1, num2, num3, num4, num5, num6, num7, num8, num9);
  363.                 if(win==true && xTurn==false)//sets winner field to X if X wins
  364.                 {                           //due to the change of turn after every turn
  365.                     winner.setText("X");    //xTurn must equal false for X to be in winner box if X wins
  366.                     tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
  367.                 }
  368.                 else if(win==true && xTurn==true)//sets winner to O if O wins
  369.                 {                               //same reason for X winning and xTurn being false is
  370.                     winner.setText("O");        //reason xTurn is true for O winning
  371.                     tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
  372.                 }
  373.                 else
  374.                 {
  375.                     winner.setText("Tie");//sets winner field to tie if no one wins
  376.                     tictactoeOps.gameWon(square1,square2,square3,square4,square5,square6,square7,square8,square9);
  377.                 }
  378.             }
  379.         }
  380.     }
  381.  
  382.     private void addComponent(Component component, int row, int column, int width, int height)
  383.     {
  384.         gbc.gridx = column;//small function to make it easier to add components
  385.         gbc.gridy = row;
  386.         gbc.gridwidth = width;
  387.         gbc.gridheight = height;
  388.         layout.setConstraints(component, gbc);
  389.         add(component);
  390.     }
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement