Advertisement
Sphyxx

NoughtsCrosses Compilation Errors

Jul 11th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6.  
  7. public class NoughtsCrosses implements ActionListener {
  8.    
  9.     //variable setup for GUI + game
  10.     JFrame frame = new JFrame("Noughts and Crosses");
  11.     JButton square[] = new JButton[9];
  12.     JPanel panel = new JPanel();
  13.     JPanel grid = new JPanel(new GridBagLayout());
  14.     JLabel playergo = new JLabel("It is player 1's go!");
  15.     String letter = ("");;
  16.    
  17.     private boolean win = false;
  18.     int move = 0;
  19.     int player;
  20.    
  21.     public NoughtsCrosses() {
  22.         //GUI setup
  23.         frame.setSize(300,300);
  24.         frame.setVisible(true);
  25.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26.         frame.setResizable(false);
  27.         frame.setLocationRelativeTo(null);
  28.         frame.add(grid);
  29.        
  30.         //set up panel grid w/ gridbagconstraints, c
  31.         panel = new JPanel();
  32.         panel.setBackground(Color.WHITE);
  33.         GridBagConstraints c = new GridBagConstraints();
  34.         grid.setPreferredSize(new Dimension(270, 180));
  35.        
  36.         //put the squares into the grid, labelled 0-1-2 | 3-4-5 | 6-7-8
  37.         for(int n = 0; n < 3; n++) {
  38.             square[n] = new JButton();
  39.             square[n].setText("");
  40.             c.gridx=n; c.gridy=0;
  41.             square[n].setPreferredSize(new Dimension (90, 60));
  42.             grid.add(square[n], c);
  43.             square[n].addActionListener(this);
  44.             }
  45.                    
  46.             for(int n = 3 ;n < 6; n++) {
  47.             square[n] = new JButton();
  48.             square[n].setText("");
  49.             c.gridx=(n-3); c.gridy=1;
  50.             square[n].setPreferredSize(new Dimension (90, 60));
  51.             grid.add(square[n], c);
  52.             square[n].addActionListener(this);
  53.             }
  54.            
  55.             for(int n =6 ; n < 9; n++) {
  56.             square[n] = new JButton();
  57.             square[n].setText("");
  58.             c.gridx=(n-6); c.gridy=2;
  59.             square[n].setPreferredSize(new Dimension (90, 60));
  60.             grid.add(square[n], c);
  61.             square[n].addActionListener(this);
  62.             }
  63.        
  64.             //bar at top to show who's go it is
  65.        
  66.         playergo.setFont(new Font("Arial", Font.PLAIN, 16));
  67.         playergo.setBackground(Color.WHITE);
  68.         playergo.setForeground(Color.BLACK);
  69.         frame.getContentPane().add(playergo, BorderLayout.NORTH);
  70.         playergo.setHorizontalAlignment(JTextField.CENTER);
  71.         }
  72.    
  73.    
  74.     public void actionPerformed (ActionEvent e) {
  75.         //one more move has gone by, calculate player turn + player letter
  76.         move++;
  77.        
  78.         if (move % 2 == 0 || move == 0) {
  79.             player = 1; letter = "X";
  80.         }else{
  81.             player = 2; letter = "O";
  82.         }
  83.        
  84.         playergo.setText("It is player " + player + "'s go!");
  85.        
  86.         //set square letter to player's letter, disable square so no further moves can be made there
  87.         for (int n=0; n<=8; n++){
  88.             if (e.getSource() == square[n]){
  89.                 square[n].setText(letter);
  90.                 square[n].setEnabled(false);
  91.             }
  92.         }
  93.        
  94.         for(int n=0; n<=7; n++){
  95.             if( square[winningLines[n][0]].getText().equals(square[winningLines[n][1]].getText()) &&
  96.                 square[winningLines[n][1]].getText().equals(square[winningLines[n][2]].getText()) &&
  97.                 square[winningLines[n][0]].getText() != ""){
  98.                 win = true;
  99.             }
  100.         }
  101.        
  102.    
  103.         //set end game conditions, and disable all squares in the event of a win
  104.     if (win == true) {
  105.         playergo.setText("Player " + player + " has won!");
  106.         for (int n = 0; n < 9; n++) {
  107.             square[n].setEnabled(false);
  108.         }
  109.     }else if (win == false && move == 9) {
  110.         playergo.setText("Draw game. Play again?");
  111.         }
  112.        
  113.     }//ActionPerformed method body
  114.  
  115.     int winningLines[][] = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
  116.    
  117.     public static void main(String[] args){
  118.         new NoughtsCrosses();
  119.     }//main body
  120.  
  121. }//NoughtsCrosses class body
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement