Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class gra extends JFrame implements ActionListener{
  8.  
  9.     private String gracz = "O";
  10.     private JButton nowaGra;
  11.     private JButton zakoncz;
  12.     private JLabel wynik;
  13.     private JButton sprawdz;
  14.     private JButton[] b = new JButton[9];
  15.    
  16.     private boolean wygrana(String s)
  17.     {
  18.         if( (b[0].getLabel() == s && b[1].getLabel() == s && b[2].getLabel() == s) ||
  19.                 (b[3].getLabel() == s && b[4].getLabel() == s && b[5].getLabel() == s) ||
  20.                 (b[6].getLabel() == s && b[7].getLabel() == s && b[8].getLabel() == s) ||
  21.                 (b[0].getLabel() == s && b[4].getLabel() == s && b[8].getLabel() == s) ||
  22.                 (b[2].getLabel() == s && b[4].getLabel() == s && b[6].getLabel() == s) ||
  23.                 (b[0].getLabel() == s && b[3].getLabel() == s && b[6].getLabel() == s) ||
  24.                 (b[1].getLabel() == s && b[4].getLabel() == s && b[7].getLabel() == s) ||
  25.                 (b[2].getLabel() == s && b[5].getLabel() == s && b[8].getLabel() == s) )
  26.                 return true;
  27.                 return false;
  28.     }
  29.    
  30.     private JPanel plansza()
  31.     {
  32.         JPanel tlo = new JPanel();
  33.         tlo.setBackground(Color.LIGHT_GRAY);
  34.         tlo.setPreferredSize(new Dimension(500, 100));
  35.         tlo.setLayout(new BoxLayout(tlo, BoxLayout.Y_AXIS));
  36.        
  37.         JPanel menu = new JPanel();
  38.         nowaGra = new JButton("Nowa gra");
  39.         nowaGra.setBackground(Color.GRAY);
  40.         nowaGra.setFont(new Font("Comic Sans", Font.BOLD, 20));
  41.         nowaGra.setPreferredSize(new Dimension(150, 50));
  42.         nowaGra.addActionListener(this);
  43.         menu.add(nowaGra);
  44.        
  45.         sprawdz = new JButton("Wynik");
  46.         sprawdz.setPreferredSize(new Dimension(150, 50));
  47.         sprawdz.setBackground(Color.GRAY);
  48.         sprawdz.setFont(new Font("Comic Sans", Font.BOLD, 20));
  49.         sprawdz.addActionListener(this);
  50.         menu.add(sprawdz); 
  51.        
  52.         zakoncz = new JButton("Zakończ");
  53.         zakoncz.setPreferredSize(new Dimension(150, 50));
  54.         zakoncz.setBackground(Color.GRAY);
  55.         zakoncz.setFont(new Font("Comic Sans", Font.BOLD, 20));
  56.         zakoncz.addActionListener(this);
  57.         menu.add(zakoncz);
  58.        
  59.         tlo.add(menu);
  60.        
  61.         JPanel mp = new JPanel();
  62.         for(int i=0;i<9;i++)
  63.         {
  64.             b[i] = new JButton();
  65.             b[i].addActionListener(this);
  66.             b[i].setPreferredSize(new Dimension(20,20));
  67.             b[i].setFont(new Font("Comic Sans", Font.BOLD, 40));
  68.             b[i].setForeground(Color.BLACK);
  69.             b[i].setBackground(Color.LIGHT_GRAY);
  70.         }
  71.         mp.setLayout(new GridLayout(3,3));
  72.         mp.add(b[0]); mp.add(b[1]); mp.add(b[2]);
  73.         mp.add(b[3]); mp.add(b[4]); mp.add(b[5]);
  74.         mp.add(b[6]); mp.add(b[7]); mp.add(b[8]);
  75.         tlo.add(mp);
  76.        
  77.         wynik = new JLabel();
  78.         wynik.setPreferredSize(new Dimension(100,50));
  79.         wynik.setFont(new Font("Comic Sans", Font.BOLD, 40));
  80.         menu.add(wynik);
  81.        
  82.         return tlo;
  83.     }
  84.    
  85.     public gra()
  86.     {
  87.         super();
  88.         this.getContentPane().add(this.plansza());
  89.     }
  90.    
  91.     public static void okno()
  92.     {
  93.         gra g = new gra();
  94.         g.setDefaultCloseOperation(EXIT_ON_CLOSE);
  95.         g.pack();
  96.         g.setLocation(400, 0);
  97.         g.setTitle("GRA: KÓŁKO I KRZYŻYK");
  98.         g.setSize(700, 700);
  99.         g.setResizable(true);
  100.         g.setVisible(true);
  101.     }
  102.  
  103.     @Override
  104.     public void actionPerformed(ActionEvent action) {
  105.         Object source = action.getSource();
  106.         if(((JButton)source).getLabel()=="" && gracz == "X")
  107.         {
  108.             ((JButton)source).setLabel("X");
  109.             gracz = "O";
  110.             repaint();
  111.         }
  112.         else if(((JButton)source).getLabel()=="" && gracz == "O")
  113.         {
  114.             ((JButton)source).setLabel("O");
  115.             gracz = "X";
  116.         }
  117.         else if(source == zakoncz)
  118.         {
  119.             zakoncz.setBackground(Color.RED);
  120.             zakoncz.setForeground(Color.BLACK);
  121.             int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz zamknąć program?", "Pytanie", JOptionPane.YES_NO_OPTION);
  122.             if(w == JOptionPane.YES_OPTION)
  123.             dispose();
  124.             else
  125.             {
  126.                 zakoncz.setBackground(Color.LIGHT_GRAY);
  127.                 zakoncz.setForeground(Color.RED);
  128.             }
  129.         }
  130.         else if(source == nowaGra)
  131.         {
  132.             int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz rozpocząć nową grę?", "Pytanie", JOptionPane.YES_NO_OPTION);
  133.             if(w == JOptionPane.YES_OPTION)
  134.             for(int i=0; i<9; i++)
  135.             {
  136.                 b[i].setLabel("");
  137.                 gracz = "O";
  138.                 repaint();
  139.             }
  140.         }
  141.         else if(source == sprawdz)
  142.         {
  143.             if(wygrana("X"))
  144.             {
  145.                 wynik.setText("Wygrał zawodnik: X");
  146.             }
  147.             else if(wygrana("O"))
  148.             {
  149.                 wynik.setText("Wygrał zawodnik: O");
  150.             }
  151.             else
  152.             {
  153.                 for(int i=0;i<9;i++)
  154.                 {
  155.                     if(b[i].getLabel()=="")
  156.                     {
  157.                         wynik.setText("Pola są puste!");
  158.                         break;
  159.                     }
  160.                     if(i==8)
  161.                         wynik.setText("Remis!!!");
  162.                 }
  163.             }
  164.         }
  165.        
  166.     }
  167.  
  168.     public static void main(String[] args){
  169.         EventQueue.invokeLater(new Runnable() {
  170.             public void run() {
  171.                 okno();
  172.             }
  173.             });
  174.     }
  175.    
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement