Advertisement
MagisterRain

TicTacToe_gui

Jul 18th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. package jsx;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.RenderingHints;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12.  
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.WindowConstants;
  16.  
  17. public class Game extends JPanel {
  18.  
  19.     int CELL_SIZE = 100;
  20.     int CELLS_MARGIN = 15;
  21.  
  22.     int[] field = new int[9];
  23.  
  24.     Result result = Result.GAME;
  25.     boolean player = false; // O - true, X - false
  26.  
  27.     enum Result {
  28.         GAME, OWIN, XWIN, TIE;
  29.     }
  30.  
  31.     public Game() {
  32.         setPreferredSize(new Dimension(367, 389));
  33.         setFocusable(true);
  34.         addMouseListener(new MouseAdapter() {
  35.             @Override
  36.             public void mouseClicked(MouseEvent e) {
  37.                 if (result == Result.GAME) {
  38.                     int click = getMouseClickedCell(e.getX(), e.getY());
  39.                     if (click >= 0 && field[click] == 0) {
  40.                         field[click] = player ? 1 : -1;
  41.                         check();
  42.                         player = !player;
  43.                         repaint();
  44.                     }
  45.                 } else if (result != Result.GAME) {
  46.                     init();
  47.                     repaint();
  48.                 }
  49.             }
  50.         });
  51.         init();
  52.     }
  53.  
  54.     int getMouseClickedCell(int x, int y) {
  55.         x = CoordToInt(x);
  56.         y = CoordToInt(y);
  57.         if (x == -1 || y == -1) {
  58.             return -1;
  59.         }
  60.         while (y > 0) {
  61.             x += 3;
  62.             y--;
  63.         }
  64.         return x;
  65.     }
  66.  
  67.     int CoordToInt(int i) {
  68.         if (i > 15 && i < 116) {
  69.             return 0;
  70.         } else if (i > 130 && i < 231) {
  71.             return 1;
  72.         } else if (i > 245 && i < 346) {
  73.             return 2;
  74.         }
  75.         return -1;
  76.     }
  77.  
  78.     void init() {
  79.         for (int i = 0; i < field.length; i++) {
  80.             field[i] = 0;
  81.         }
  82.         result = Result.GAME;
  83.         player = !player;
  84.     }
  85.  
  86.     void check() {
  87.         int[] summ = new int[8];
  88.         summ[0] = field[0] + field[1] + field[2];
  89.         summ[1] = field[3] + field[4] + field[5];
  90.         summ[2] = field[6] + field[7] + field[8];
  91.  
  92.         summ[3] = field[0] + field[3] + field[6];
  93.         summ[4] = field[1] + field[4] + field[7];
  94.         summ[5] = field[2] + field[5] + field[8];
  95.  
  96.         summ[6] = field[0] + field[4] + field[8];
  97.         summ[7] = field[2] + field[4] + field[6];
  98.         for (int i = 0; i < summ.length; i++) {
  99.             if (summ[i] == 3) {
  100.                 result = Result.OWIN;
  101.                 return;
  102.             } else if (summ[i] == -3) {
  103.                 result = Result.XWIN;
  104.                 return;
  105.             }
  106.         }
  107.         result = hasEmpty() ? Result.GAME : Result.TIE;
  108.     }
  109.  
  110.     boolean hasEmpty() {
  111.         for (int i = 0; i < 9; i++) {
  112.             if (field[i] == 0) {
  113.                 return true;
  114.             }
  115.         }
  116.         return false;
  117.     }
  118.  
  119.     @Override
  120.     public void paint(Graphics g) {
  121.         super.paint(g);
  122.         g.setColor(new Color(255, 223, 94));
  123.         g.fillRect(0, 0, this.getSize().width, this.getSize().height);
  124.         for (int y = 0; y < 3; y++) {
  125.             for (int x = 0; x < 3; x++) {
  126.                 drawCell(g, x + y * 3, x, y);
  127.             }
  128.         }
  129.     }
  130.  
  131.     void drawCell(Graphics gr, int cel, int x, int y) {
  132.         Graphics2D g = ((Graphics2D) gr);
  133.         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  134.         g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
  135.         int oX = x * (CELLS_MARGIN + CELL_SIZE) + CELLS_MARGIN;
  136.         int oY = y * (CELLS_MARGIN + CELL_SIZE) + CELLS_MARGIN;
  137.         g.setColor(new Color(252, 255, 183));
  138.         g.fillRect(oX, oY, CELL_SIZE, CELL_SIZE);
  139.         if (field[cel] == -1) {
  140.             g.setColor(new Color(166, 0, 14));
  141.             Font font = new Font("Cambria", Font.BOLD, 72);
  142.             g.setFont(font);
  143.  
  144.             FontMetrics fm = getFontMetrics(font);
  145.  
  146.             int w = fm.stringWidth("X");
  147.             int h = -(int) fm.getLineMetrics("X", g).getBaselineOffsets()[2];
  148.  
  149.             g.drawString("X", oX + (CELL_SIZE - w) / 2, oY + CELL_SIZE - (CELL_SIZE - h) / 2 - 10);
  150.         }
  151.         if (field[cel] == 1) {
  152.             g.setColor(new Color(11, 0, 171));
  153.             Font font = new Font("Cambria", Font.BOLD, 72);
  154.             g.setFont(font);
  155.  
  156.             FontMetrics fm = getFontMetrics(font);
  157.  
  158.             int w = fm.stringWidth("O");
  159.             int h = -(int) fm.getLineMetrics("O", g).getBaselineOffsets()[2];
  160.  
  161.             g.drawString("O", oX + (CELL_SIZE - w) / 2, oY + CELL_SIZE - (CELL_SIZE - h) / 2 - 10);
  162.         }
  163.         if (result != Result.GAME) {
  164.             g.setColor(new Color(255, 255, 255, 30));
  165.             g.fillRect(0, 0, getWidth(), getHeight());
  166.             g.setColor(new Color(44, 44, 44));
  167.             g.setFont(new Font("Cambria", Font.BOLD, 80));
  168.             if (result == Result.XWIN) {
  169.                 g.drawString("X win!", 67, 201);
  170.             } else if (result == Result.OWIN) {
  171.                 g.drawString("O win!", 63, 201);
  172.             } else if (result == Result.TIE) {
  173.                 g.drawString("Tie!", 110, 201);
  174.             }
  175.         }
  176.  
  177.     }
  178.  
  179.     public static void main(String[] args) {
  180.         JFrame game = new JFrame();
  181.         game.setTitle("TicTacToe Game (GUI)");
  182.         game.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  183.         game.setSize(367, 389);
  184.         game.setResizable(false);
  185.  
  186.         game.add(new Game());
  187.  
  188.         game.setLocationRelativeTo(null);
  189.         game.setVisible(true);
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement