Advertisement
Guest User

Untitled

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