Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tictactoe;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- 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.JTextField;
- /**
- *
- * @author nccs
- */
- public class TicTacToe extends JFrame implements ActionListener{
- private JButton[] cells;
- private final String PLAYER1="X";
- private final String PLAYER2="O";
- private String currentPlayer = PLAYER1;
- public TicTacToe() {
- setLayout(new GridLayout(3, 3));
- cells = new JButton[9];
- for (int i = 0; i < cells.length; i++) {
- cells[i] = new JButton();
- cells[i].setFont(new Font(Font.SANS_SERIF, Font.PLAIN,59));
- cells[i].setPreferredSize(new Dimension (100,100));
- cells[i].addActionListener(this);
- add(cells[i]);
- }
- pack();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- TicTacToe ttt = new TicTacToe();
- ttt.setVisible(true);
- ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- // T
- @Override
- public void actionPerformed(ActionEvent e) {
- JButton currentCell = (JButton)e.getSource();
- currentCell.setText(currentPlayer);
- currentPlayer = (currentPlayer == PLAYER1)
- ? PLAYER2
- : PLAYER1;
- }
- public boolean hasWon(String player){
- if(player.equals(cells[0].getText())
- && player.equals(cells[1].getText())
- && player.equals(cells[2].getText())
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement