Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.25 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.Random;
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JTextField;
  7. import javax.swing.JButton;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.Font;
  11. import javax.swing.SwingConstants;
  12. import javax.swing.UIManager;
  13. import javax.swing.border.LineBorder;
  14.  
  15. public class OX {
  16.  
  17.     static Random rand = new Random();
  18.    
  19.     private JFrame frame;
  20.    
  21.     static int turn;
  22.     static  int [][] board = new int [3][3];
  23.    
  24.     private JTextField textTurn;
  25.     private JTextField textEnd;
  26.    
  27.     public void whoStarts()
  28.     {
  29.        
  30.         turn = rand.nextInt(2);
  31.  
  32.     }
  33.    
  34.     public static void randomBoard()
  35.     {
  36.         for(int i=0;i<3;i++)
  37.             for(int j=0;j<3;j++)
  38.                 board[i][j]=rand.nextInt(10000);               
  39.     }
  40.    
  41.    
  42.     public void isThisTheEnd()
  43.     {
  44.         //vertical
  45.         for(int i=0;i<3;i++)
  46.             if(board[0][i]==board[1][i] && board[1][i]==board[2][i])
  47.                 textEnd.setText((turn==1?"X":"O") + " wins!");
  48.        
  49.         //horizontal
  50.         for(int i=0;i<3;i++)
  51.             if(board[i][0]==board[i][1] && board[i][1]==board[i][2])
  52.                 textEnd.setText((turn==1?"X":"O") + " wins!");
  53.  
  54.         //diagonal
  55.         if((board[0][0]==board[1][1] && board[1][1]==board[2][2]) || (board[2][0]==board[1][1] && board[1][1]==board[0][2]))
  56.             textEnd.setText((turn==1?"X":"O") + " wins!");
  57.         else
  58.             nextTurn();
  59.     }
  60.    
  61.     public void nextTurn()
  62.     {
  63.         if(turn==1)
  64.             turn=0;
  65.         else
  66.             turn=1;
  67.        
  68.         textTurn.setText((turn==1?"X":"O") + " move");
  69.     }
  70.    
  71.     public static void main(String[] args) {
  72.         EventQueue.invokeLater(new Runnable() {
  73.  
  74.             public void run() {
  75.                 try {
  76.                     OX window = new OX();
  77.                     window.frame.setVisible(true);
  78.                 } catch (Exception e) {
  79.                     e.printStackTrace();
  80.                 }
  81.             }
  82.         });
  83.        
  84.        
  85.        
  86.     }
  87.  
  88.     public OX() {
  89.         initialize();
  90.     }
  91.    
  92.  
  93.     private void initialize() {
  94.        
  95.         whoStarts();
  96.         randomBoard(); 
  97.  
  98.  
  99.         JFrame window = new JFrame();
  100.         window.getContentPane().setEnabled(false);
  101.         window.setBounds(10,10,502,605);
  102.         window.setTitle("Kolko krzyzyk");
  103.         window.setResizable(false);
  104.         window.setVisible(true);
  105.         window.getContentPane().setLayout(null);
  106.         window.getContentPane().setBackground(Color.DARK_GRAY);
  107.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  108.        
  109.        
  110.        
  111.         JButton btn1 = new JButton("");
  112.         btn1.setFont(new Font("Verdana", Font.PLAIN, 77));
  113.         btn1.setBackground(UIManager.getColor("Button.disabledForeground"));
  114.         btn1.setBounds(36, 86, 120, 120);
  115.         window.getContentPane().add(btn1);
  116.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  117.         btn1.setBorder(new LineBorder(Color.WHITE));
  118.         btn1.addActionListener(new ActionListener() {
  119.             public void actionPerformed(ActionEvent e) {
  120.                 btn1.setText((turn==1?"X":"O"));
  121.                 board[0][0]=turn;
  122.                 isThisTheEnd();
  123.                 btn1.setEnabled(false);
  124.             }
  125.         });
  126.        
  127.        
  128.        
  129.         JButton btn2 = new JButton("");
  130.         btn2.setFont(new Font("Verdana", Font.PLAIN, 77));
  131.         btn2.setBackground(UIManager.getColor("Button.disabledForeground"));
  132.         btn2.setBounds(185, 86, 120, 120);
  133.         window.getContentPane().add(btn2);
  134.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  135.         btn2.setBorder(new LineBorder(Color.WHITE));
  136.         btn2.addActionListener(new ActionListener() {
  137.             public void actionPerformed(ActionEvent e) {
  138.                 btn2.setText((turn==1?"X":"O"));
  139.                 board[0][1]=turn;
  140.                 isThisTheEnd();
  141.                 btn2.setEnabled(false);
  142.             }
  143.         });
  144.        
  145.  
  146.        
  147.         JButton btn3 = new JButton("");
  148.         btn3.setFont(new Font("Verdana", Font.PLAIN, 77));
  149.         btn3.setBackground(UIManager.getColor("Button.disabledForeground"));
  150.         btn3.setLocation(333, 86);
  151.         btn3.setSize(120, 120);
  152.         window.getContentPane().add(btn3);
  153.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  154.         btn3.setBorder(new LineBorder(Color.WHITE));
  155.         btn3.addActionListener(new ActionListener() {
  156.             public void actionPerformed(ActionEvent e) {
  157.                 btn3.setText((turn==1?"X":"O"));
  158.                 board[0][2]=turn;
  159.                 isThisTheEnd();
  160.                 btn3.setEnabled(false);
  161.             }
  162.         });
  163.        
  164.        
  165.         JButton btn4 = new JButton("");
  166.         btn4.setFont(new Font("Verdana", Font.PLAIN, 77));
  167.         btn4.setBackground(UIManager.getColor("Button.disabledForeground"));
  168.         btn4.setLocation(36, 228);
  169.         btn4.setSize(120, 120);
  170.         window.getContentPane().add(btn4);
  171.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  172.         btn4.setBorder(new LineBorder(Color.WHITE));
  173.         btn4.addActionListener(new ActionListener() {
  174.             public void actionPerformed(ActionEvent e) {
  175.                 btn4.setText((turn==1?"X":"O"));
  176.                 board[1][0]=turn;
  177.                 isThisTheEnd();
  178.                 btn4.setEnabled(false);
  179.             }
  180.         });
  181.        
  182.        
  183.         JButton btn5 = new JButton("");
  184.         btn5.setFont(new Font("Verdana", Font.PLAIN, 77));
  185.         btn5.setBackground(UIManager.getColor("Button.disabledForeground"));
  186.         btn5.setLocation(185, 228);
  187.         btn5.setSize(120, 120);
  188.         window.getContentPane().add(btn5);
  189.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  190.         btn5.setBorder(new LineBorder(Color.WHITE));
  191.         btn5.addActionListener(new ActionListener() {
  192.             public void actionPerformed(ActionEvent e) {
  193.                 btn5.setText((turn==1?"X":"O"));
  194.                 board[1][1]=turn;
  195.                 isThisTheEnd();
  196.                 btn5.setEnabled(false);
  197.             }
  198.         });
  199.        
  200.        
  201.         JButton btn6 = new JButton("");
  202.         btn6.setFont(new Font("Verdana", Font.PLAIN, 77));
  203.         btn6.setBackground(UIManager.getColor("Button.disabledForeground"));
  204.         btn6.setLocation(333, 228);
  205.         btn6.setSize(120, 120);
  206.         window.getContentPane().add(btn6);
  207.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  208.         btn6.setBorder(new LineBorder(Color.WHITE));
  209.         btn6.addActionListener(new ActionListener() {
  210.             public void actionPerformed(ActionEvent e) {
  211.                 btn6.setText((turn==1?"X":"O"));
  212.                 board[1][2]=turn;
  213.                 isThisTheEnd();
  214.                 btn6.setEnabled(false);
  215.             }
  216.         });
  217.        
  218.        
  219.         JButton btn7 = new JButton("");
  220.         btn7.setFont(new Font("Verdana", Font.PLAIN, 77));
  221.         btn7.setBackground(UIManager.getColor("Button.disabledForeground"));
  222.         btn7.setLocation(36, 373);
  223.         btn7.setSize(120, 120);
  224.         window.getContentPane().add(btn7);
  225.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  226.         btn7.setBorder(new LineBorder(Color.WHITE));
  227.         btn7.addActionListener(new ActionListener() {
  228.             public void actionPerformed(ActionEvent e) {
  229.                 btn7.setText((turn==1?"X":"O"));
  230.                 board[2][0]=turn;
  231.                 isThisTheEnd();
  232.                 btn7.setEnabled(false);
  233.             }
  234.         });
  235.        
  236.        
  237.         JButton btn8 = new JButton("");
  238.         btn8.setFont(new Font("Verdana", Font.PLAIN, 77));
  239.         btn8.setBackground(UIManager.getColor("Button.disabledForeground"));
  240.         btn8.setLocation(185, 373);
  241.         btn8.setSize(120, 120);
  242.         window.getContentPane().add(btn8);
  243.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  244.         btn8.setBorder(new LineBorder(Color.WHITE));
  245.         btn8.addActionListener(new ActionListener() {
  246.             public void actionPerformed(ActionEvent e) {
  247.                 btn8.setText((turn==1?"X":"O"));
  248.                 board[2][1]=turn;
  249.                 isThisTheEnd();
  250.                 btn8.setEnabled(false);
  251.             }
  252.         });
  253.        
  254.        
  255.         JButton btn9 = new JButton("");
  256.         btn9.setFont(new Font("Verdana", Font.PLAIN, 77));
  257.         btn9.setBackground(UIManager.getColor("Button.disabledForeground"));
  258.         btn9.setLocation(333, 373);
  259.         btn9.setSize(120, 120);
  260.         window.getContentPane().add(btn9);
  261.         btn9.setBorder(new LineBorder(Color.WHITE));
  262.         btn9.addActionListener(new ActionListener() {
  263.             public void actionPerformed(ActionEvent e) {
  264.                 btn9.setText((turn==1?"X":"O"));
  265.                 board[2][2]=turn;
  266.                 isThisTheEnd();
  267.                 btn9.setEnabled(false);
  268.             }
  269.         });
  270.        
  271.         textTurn = new JTextField();
  272.         textTurn.setForeground(Color.LIGHT_GRAY);
  273.         textTurn.setEditable(false);
  274.         textTurn.setHorizontalAlignment(SwingConstants.LEFT);
  275.         textTurn.setFont(new Font("Verdana", Font.BOLD, 20));
  276.         textTurn.setBackground(Color.DARK_GRAY);
  277.         textTurn.setBounds(36, 21, 200, 40);
  278.         window.getContentPane().add(textTurn);
  279.         textTurn.setColumns(10);
  280.         textTurn.setText((turn==1?"X":"O") + " move");
  281.         textTurn.setBorder(null);
  282.        
  283.         textEnd = new JTextField();
  284.         textEnd.setHorizontalAlignment(SwingConstants.CENTER);
  285.         textEnd.setForeground(Color.LIGHT_GRAY);
  286.         textEnd.setFont(new Font("Verdana", Font.BOLD, 30));
  287.         textEnd.setEditable(false);
  288.         textEnd.setColumns(10);
  289.         textEnd.setBorder(null);
  290.         textEnd.setBackground(Color.DARK_GRAY);
  291.         textEnd.setBounds(105, 515, 284, 50);
  292.         window.getContentPane().add(textEnd);
  293.        
  294.         JButton btnClear = new JButton("Clear!");
  295.         btnClear.addActionListener(new ActionListener() {
  296.             public void actionPerformed(ActionEvent e) {
  297.                 btn1.setText("");
  298.                 btn2.setText("");
  299.                 btn3.setText("");
  300.                 btn4.setText("");
  301.                 btn5.setText("");
  302.                 btn6.setText("");
  303.                 btn7.setText("");
  304.                 btn8.setText("");
  305.                 btn9.setText("");
  306.                 textEnd.setText("");
  307.                
  308.                 btn1.setEnabled(true);
  309.                 btn2.setEnabled(true);
  310.                 btn3.setEnabled(true);
  311.                 btn4.setEnabled(true);
  312.                 btn5.setEnabled(true);
  313.                 btn6.setEnabled(true);
  314.                 btn7.setEnabled(true);
  315.                 btn8.setEnabled(true);
  316.                 btn9.setEnabled(true);
  317.                
  318.                 whoStarts();
  319.                 randomBoard();
  320.                 textTurn.setText((turn==1?"X":"O") + " move");
  321.                
  322.             }
  323.         });
  324.         btnClear.setForeground(Color.LIGHT_GRAY);
  325.         btnClear.setBorder(new LineBorder(Color.WHITE));
  326.         btnClear.setFont(new Font("Verdana", Font.BOLD, 23));
  327.         btnClear.setBackground(UIManager.getColor("Button.disabledForeground"));
  328.         btnClear.setBounds(333, 20, 120, 38);
  329.         window.getContentPane().add(btnClear);
  330.  
  331.        
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement