Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.util.Random;
- class buttonGame
- {
- public static void main(String[] args) {
- buttonGameFrame myFrame = new buttonGameFrame();
- myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
- class buttonGameFrame extends JFrame implements ActionListener {
- JButton[][] buttons = new JButton[5][4];
- JPanel infoPanel = new JPanel();
- JPanel buttonsPanel = new JPanel();
- JPanel leftPanel = new JPanel();
- JPanel rightPanel = new JPanel();
- JPanel bottomPanel = new JPanel();
- JTextArea textArea = new JTextArea();
- Container container = getContentPane();
- int randomRow;
- int randomCol;
- int counter = 0;
- public buttonGameFrame() {
- buttonGenerator(); //wywolanie metody generującej buttony
- generateRandomShot(); //wywołanie metody generującej dwie randomowe liczby (randomowy button)
- changeSide(); //wywolanie metody ustalającej ktory gracz wykonuje ruch (raz na starcie, zeby od razu byla podswietlona ktoras strona)
- buttonsPanel.setLayout(new GridLayout(5,4));
- infoPanel.setLayout(new BorderLayout());
- infoPanel.add(buttonsPanel, BorderLayout.CENTER);
- infoPanel.add(leftPanel, BorderLayout.WEST);
- infoPanel.add(rightPanel, BorderLayout.EAST);
- bottomPanel.add(textArea);
- textArea.setText("Pozostało ruchów: 20");
- infoPanel.add(bottomPanel, BorderLayout.SOUTH);
- container.add(infoPanel);
- setTitle("Egzamin 25.02.2020");
- setSize(800,400);
- setLocation(200,200);
- setVisible(true);
- }
- //metoda generujaca buttony i ustawiajaca actionCommand na wzor "buttonX_Y" zeby bylo wiadomo ktory button jest naciskany
- private void buttonGenerator() {
- for(int i=0;i<5;i++)
- for(int j=0;j<4;j++) {
- buttons[i][j] = new JButton();
- buttons[i][j].setBackground(Color.yellow);
- buttons[i][j].setActionCommand("button" +i +"_" +j);
- buttons[i][j].addActionListener(this);
- buttonsPanel.add(buttons[i][j]);
- }}
- //metoda generujaca randomowe dwie liczby i wypisujaca w konsoli
- private void generateRandomShot(){
- Random generator = new Random();
- randomRow = generator.nextInt(4);
- randomCol = generator.nextInt(3);
- System.out.println("Kolumna: " +(randomCol+1) + ", Wiersz: " + (randomRow+1));
- }
- //zamiana graczy (podswietlenie raz lewa raz prawa, w zaleznosci od parzystosci licznika
- public void changeSide(){
- if(counter%2==0) {
- leftPanel.setBackground(Color.red);
- rightPanel.setBackground(Color.white);
- } else if(counter%2!=0){
- leftPanel.setBackground(Color.white);
- rightPanel.setBackground(Color.red);
- }}
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand().contains("button"))
- {
- //wyciaganie numeru licznika regexpem
- int i = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll("_", "").charAt(0)));
- int j = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll("_", "").charAt(1)));
- //sprawdzanie czy gracz przegral
- if(j==randomCol && i==randomRow){
- JOptionPane.showMessageDialog(null, "Przegrałeś!");
- System.exit(0);
- }
- //zwiekszanie licznika do 20, zamiana stron
- if(counter<20){
- counter++;
- changeSide();
- textArea.setText("Pozostało ruchów: " + (20-counter));
- } buttons[i][j].setBackground(Color.blue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement