Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jsx;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- public class Game extends JPanel {
- int CELL_SIZE = 100;
- int CELLS_MARGIN = 15;
- int[] field = new int[9];
- Result result = Result.GAME;
- boolean player = false; // O - true, X - false
- enum Result {
- GAME, OWIN, XWIN, TIE;
- }
- public Game() {
- setPreferredSize(new Dimension(367, 389));
- setFocusable(true);
- addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if (result == Result.GAME) {
- int click = getMouseClickedCell(e.getX(), e.getY());
- if (click >= 0 && field[click] == 0) {
- field[click] = player ? 1 : -1;
- check();
- player = !player;
- repaint();
- }
- } else if (result != Result.GAME) {
- init();
- repaint();
- }
- }
- });
- init();
- }
- int getMouseClickedCell(int x, int y) {
- x = CoordToInt(x);
- y = CoordToInt(y);
- if (x == -1 || y == -1) {
- return -1;
- }
- while (y > 0) {
- x += 3;
- y--;
- }
- return x;
- }
- int CoordToInt(int i) {
- if (i > 15 && i < 116) {
- return 0;
- } else if (i > 130 && i < 231) {
- return 1;
- } else if (i > 245 && i < 346) {
- return 2;
- }
- return -1;
- }
- void init() {
- for (int i = 0; i < field.length; i++) {
- field[i] = 0;
- }
- result = Result.GAME;
- player = !player;
- }
- void check() {
- int[] summ = new int[8];
- summ[0] = field[0] + field[1] + field[2];
- summ[1] = field[3] + field[4] + field[5];
- summ[2] = field[6] + field[7] + field[8];
- summ[3] = field[0] + field[3] + field[6];
- summ[4] = field[1] + field[4] + field[7];
- summ[5] = field[2] + field[5] + field[8];
- summ[6] = field[0] + field[4] + field[8];
- summ[7] = field[2] + field[4] + field[6];
- for (int i = 0; i < summ.length; i++) {
- if (summ[i] == 3) {
- result = Result.OWIN;
- return;
- } else if (summ[i] == -3) {
- result = Result.XWIN;
- return;
- }
- }
- result = hasEmpty() ? Result.GAME : Result.TIE;
- }
- boolean hasEmpty() {
- for (int i = 0; i < 9; i++) {
- if (field[i] == 0) {
- return true;
- }
- }
- return false;
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(new Color(255, 223, 94));
- g.fillRect(0, 0, this.getSize().width, this.getSize().height);
- for (int y = 0; y < 3; y++) {
- for (int x = 0; x < 3; x++) {
- drawCell(g, x + y * 3, x, y);
- }
- }
- }
- void drawCell(Graphics gr, int cel, int x, int y) {
- Graphics2D g = ((Graphics2D) gr);
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
- int oX = x * (CELLS_MARGIN + CELL_SIZE) + CELLS_MARGIN;
- int oY = y * (CELLS_MARGIN + CELL_SIZE) + CELLS_MARGIN;
- g.setColor(new Color(252, 255, 183));
- g.fillRect(oX, oY, CELL_SIZE, CELL_SIZE);
- if (field[cel] == -1) {
- g.setColor(new Color(166, 0, 14));
- Font font = new Font("Cambria", Font.BOLD, 72);
- g.setFont(font);
- FontMetrics fm = getFontMetrics(font);
- int w = fm.stringWidth("X");
- int h = -(int) fm.getLineMetrics("X", g).getBaselineOffsets()[2];
- g.drawString("X", oX + (CELL_SIZE - w) / 2, oY + CELL_SIZE - (CELL_SIZE - h) / 2 - 10);
- }
- if (field[cel] == 1) {
- g.setColor(new Color(11, 0, 171));
- Font font = new Font("Cambria", Font.BOLD, 72);
- g.setFont(font);
- FontMetrics fm = getFontMetrics(font);
- int w = fm.stringWidth("O");
- int h = -(int) fm.getLineMetrics("O", g).getBaselineOffsets()[2];
- g.drawString("O", oX + (CELL_SIZE - w) / 2, oY + CELL_SIZE - (CELL_SIZE - h) / 2 - 10);
- }
- if (result != Result.GAME) {
- g.setColor(new Color(255, 255, 255, 30));
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(new Color(44, 44, 44));
- g.setFont(new Font("Cambria", Font.BOLD, 80));
- if (result == Result.XWIN) {
- g.drawString("X win!", 67, 201);
- } else if (result == Result.OWIN) {
- g.drawString("O win!", 63, 201);
- } else if (result == Result.TIE) {
- g.drawString("Tie!", 110, 201);
- }
- }
- }
- public static void main(String[] args) {
- JFrame game = new JFrame();
- game.setTitle("TicTacToe Game (GUI)");
- game.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- game.setSize(367, 389);
- game.setResizable(false);
- game.add(new Game());
- game.setLocationRelativeTo(null);
- game.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement