Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class NoughtsCrosses implements ActionListener {
- //variable setup for GUI + game
- JFrame frame = new JFrame("Noughts and Crosses");
- JButton square[] = new JButton[9];
- JPanel panel = new JPanel();
- JPanel grid = new JPanel(new GridBagLayout());
- JLabel playergo = new JLabel("It is player 1's go!");
- String letter = ("");;
- private boolean win = false;
- int move = 0;
- int player;
- public NoughtsCrosses() {
- //GUI setup
- frame.setSize(300,300);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.add(grid);
- //set up panel grid w/ gridbagconstraints, c
- panel = new JPanel();
- panel.setBackground(Color.WHITE);
- GridBagConstraints c = new GridBagConstraints();
- grid.setPreferredSize(new Dimension(270, 180));
- //put the squares into the grid, labelled 0-1-2 | 3-4-5 | 6-7-8
- for(int n = 0; n < 3; n++) {
- square[n] = new JButton();
- square[n].setText("");
- c.gridx=n; c.gridy=0;
- square[n].setPreferredSize(new Dimension (90, 60));
- grid.add(square[n], c);
- square[n].addActionListener(this);
- }
- for(int n = 3 ;n < 6; n++) {
- square[n] = new JButton();
- square[n].setText("");
- c.gridx=(n-3); c.gridy=1;
- square[n].setPreferredSize(new Dimension (90, 60));
- grid.add(square[n], c);
- square[n].addActionListener(this);
- }
- for(int n =6 ; n < 9; n++) {
- square[n] = new JButton();
- square[n].setText("");
- c.gridx=(n-6); c.gridy=2;
- square[n].setPreferredSize(new Dimension (90, 60));
- grid.add(square[n], c);
- square[n].addActionListener(this);
- }
- //bar at top to show who's go it is
- playergo.setFont(new Font("Arial", Font.PLAIN, 16));
- playergo.setBackground(Color.WHITE);
- playergo.setForeground(Color.BLACK);
- frame.getContentPane().add(playergo, BorderLayout.NORTH);
- playergo.setHorizontalAlignment(JTextField.CENTER);
- }
- public void actionPerformed (ActionEvent e) {
- //one more move has gone by, calculate player turn + player letter
- move++;
- if (move % 2 == 0 || move == 0) {
- player = 1; letter = "X";
- }else{
- player = 2; letter = "O";
- }
- playergo.setText("It is player " + player + "'s go!");
- //set square letter to player's letter, disable square so no further moves can be made there
- for (int n=0; n<=8; n++){
- if (e.getSource() == square[n]){
- square[n].setText(letter);
- square[n].setEnabled(false);
- }
- }
- for(int n=0; n<=7; n++){
- if( square[winningLines[n][0]].getText().equals(square[winningLines[n][1]].getText()) &&
- square[winningLines[n][1]].getText().equals(square[winningLines[n][2]].getText()) &&
- square[winningLines[n][0]].getText() != ""){
- win = true;
- }
- }
- //set end game conditions, and disable all squares in the event of a win
- if (win == true) {
- playergo.setText("Player " + player + " has won!");
- for (int n = 0; n < 9; n++) {
- square[n].setEnabled(false);
- }
- }else if (win == false && move == 9) {
- playergo.setText("Draw game. Play again?");
- }
- }//ActionPerformed method body
- 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}};
- public static void main(String[] args){
- new NoughtsCrosses();
- }//main body
- }//NoughtsCrosses class body
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement