javacode_tictactoe

tictactoe

package dev.aiken.tictactoe;

import java.awt.Color;
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.JOptionPane;

public class TicTacToe implements ActionListener {

	private JFrame frame;
	private JButton[][] button;

	private String turn = "X";
	private int move = 0;

	public TicTacToe() {
		frame = new JFrame();
		frame.setTitle("TicTacToe");
		frame.setSize(320, 320);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLayout(new GridLayout(3, 3));

		button = new JButton[3][3];
		for (int y = 0; y < 3; y++) {
			for (int x = 0; x < 3; x++) {
				button[x][y] = new JButton();
				frame.add(button[x][y]);
				button[x][y].setBackground(Color.LIGHT_GRAY);
				button[x][y].setFont(new Font("Arial", Font.PLAIN, 90));
				button[x][y].addActionListener(this);
			}
		}
	}

	public void turn(int x, int y) {
		if (!(button[x][y].getText() == ""))
			return;
		if (turn == "X") {
			button[x][y].setText(turn);
			turn = "O";
		} else if (turn == "O") {
			button[x][y].setText(turn);
			turn = "X";
		}

	}

	public void check(int x, int y) {
		boolean win = false;
		if (x == 0 && y == 0 || x == 1 && y == 0 || x == 2 && y == 0) {
			if (button[x][y].getText() == button[1][0].getText() && button[x][y].getText() == button[2][0].getText()
					&& button[x][y].getText() == button[0][0].getText())
				win = true;
		}
		if (x == 0 && y == 0 || x == 0 && y == 1 || x == 0 && y == 2) {
			if (button[x][y].getText() == button[0][1].getText() && button[x][y].getText() == button[0][2].getText()
					&& button[x][y].getText() == button[0][0].getText())
				win = true;
		}
		if (x == 2 && y == 2 || x == 1 && y == 2 || x == 0 && y == 2) {
			if (button[x][y].getText() == button[2][2].getText() && button[x][y].getText() == button[1][2].getText()
					&& button[x][y].getText() == button[0][2].getText())
				win = true;
		}
		if (x == 2 && y == 2 || x == 2 && y == 1 || x == 2 && y == 0) {
			if (button[x][y].getText() == button[2][2].getText() && button[x][y].getText() == button[2][1].getText()
					&& button[x][y].getText() == button[2][0].getText())
				win = true;
		}
		if (x == 2 && y == 2 || x == 1 && y == 1 || x == 0 && y == 0) {
			if (button[x][y].getText() == button[2][2].getText() && button[x][y].getText() == button[1][1].getText()
					&& button[x][y].getText() == button[0][0].getText())
				win = true;
		}
		if (x == 2 && y == 0 || x == 1 && y == 1 || x == 0 && y == 2) {
			if (button[x][y].getText() == button[2][0].getText() && button[x][y].getText() == button[1][1].getText()
					&& button[x][y].getText() == button[0][2].getText())
				win = true;
		}
		if (x == 1 && y == 0 || x == 1 && y == 1 || x == 1 && y == 2) {
			if (button[x][y].getText() == button[1][0].getText() && button[x][y].getText() == button[1][1].getText()
					&& button[x][y].getText() == button[1][2].getText())
				win = true;
		}
		if (x == 0 && y == 1 || x == 1 && y == 1 || x == 2 && y == 1) {
			if (button[x][y].getText() == button[0][1].getText() && button[x][y].getText() == button[1][1].getText()
					&& button[x][y].getText() == button[2][1].getText())
				win = true;
		}

		if (move != 9 && !win)
			return;
		if (win)
			JOptionPane.showMessageDialog(null, button[x][y].getText() + " WIN! ");
		else
			JOptionPane.showMessageDialog(null, " DRAW! ");


		frame.dispose();
		new TicTacToe();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for (int y = 0; y < 3; y++) {
			for (int x = 0; x < 3; x++) {
				if (e.getSource() == button[x][y]) {
					move++;
					turn(x, y);
					check(x, y);
				}

			}
		}

	}

	public static void main(String[] args) {
		new TicTacToe();
	}
}
Advertisements