Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. class buttonGame
  7. {
  8. public static void main(String[] args) {
  9. buttonGameFrame myFrame = new buttonGameFrame();
  10. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. }
  12. }
  13.  
  14. class buttonGameFrame extends JFrame implements ActionListener {
  15. JButton[][] buttons = new JButton[5][4];
  16. JPanel infoPanel = new JPanel();
  17. JPanel buttonsPanel = new JPanel();
  18. JPanel leftPanel = new JPanel();
  19. JPanel rightPanel = new JPanel();
  20. JPanel bottomPanel = new JPanel();
  21. JTextArea textArea = new JTextArea();
  22. Container container = getContentPane();
  23. int randomRow;
  24. int randomCol;
  25. int counter = 0;
  26.  
  27. public buttonGameFrame() {
  28. buttonGenerator(); //wywolanie metody generującej buttony
  29. generateRandomShot(); //wywołanie metody generującej dwie randomowe liczby (randomowy button)
  30. changeSide(); //wywolanie metody ustalającej ktory gracz wykonuje ruch (raz na starcie, zeby od razu byla podswietlona ktoras strona)
  31. buttonsPanel.setLayout(new GridLayout(5,4));
  32. infoPanel.setLayout(new BorderLayout());
  33. infoPanel.add(buttonsPanel, BorderLayout.CENTER);
  34. infoPanel.add(leftPanel, BorderLayout.WEST);
  35. infoPanel.add(rightPanel, BorderLayout.EAST);
  36. bottomPanel.add(textArea);
  37. textArea.setText("Pozostało ruchów: 20");
  38. infoPanel.add(bottomPanel, BorderLayout.SOUTH);
  39. container.add(infoPanel);
  40. setTitle("Egzamin 25.02.2020");
  41. setSize(800,400);
  42. setLocation(200,200);
  43. setVisible(true);
  44. }
  45. //metoda generujaca buttony i ustawiajaca actionCommand na wzor "buttonX_Y" zeby bylo wiadomo ktory button jest naciskany
  46. private void buttonGenerator() {
  47. for(int i=0;i<5;i++)
  48. for(int j=0;j<4;j++) {
  49. buttons[i][j] = new JButton();
  50. buttons[i][j].setBackground(Color.yellow);
  51. buttons[i][j].setActionCommand("button" +i +"_" +j);
  52. buttons[i][j].addActionListener(this);
  53. buttonsPanel.add(buttons[i][j]);
  54. }}
  55. //metoda generujaca randomowe dwie liczby i wypisujaca w konsoli
  56. private void generateRandomShot(){
  57. Random generator = new Random();
  58. randomRow = generator.nextInt(4);
  59. randomCol = generator.nextInt(3);
  60. System.out.println("Kolumna: " +(randomCol+1) + ", Wiersz: " + (randomRow+1));
  61. }
  62. //zamiana graczy (podswietlenie raz lewa raz prawa, w zaleznosci od parzystosci licznika
  63. public void changeSide(){
  64. if(counter%2==0) {
  65. leftPanel.setBackground(Color.red);
  66. rightPanel.setBackground(Color.white);
  67. } else if(counter%2!=0){
  68. leftPanel.setBackground(Color.white);
  69. rightPanel.setBackground(Color.red);
  70. }}
  71.  
  72. public void actionPerformed(ActionEvent e) {
  73. if(e.getActionCommand().contains("button"))
  74. {
  75. //wyciaganie numeru licznika regexpem
  76. int i = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll("_", "").charAt(0)));
  77. int j = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll("_", "").charAt(1)));
  78. //sprawdzanie czy gracz przegral
  79. if(j==randomCol && i==randomRow){
  80. JOptionPane.showMessageDialog(null, "Przegrałeś!");
  81. System.exit(0);
  82. }
  83. //zwiekszanie licznika do 20, zamiana stron
  84. if(counter<20){
  85. counter++;
  86. changeSide();
  87. textArea.setText("Pozostało ruchów: " + (20-counter));
  88. } buttons[i][j].setBackground(Color.blue);
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement