2016-06-01 5 views
0

Ich habe 3 Klassen für jedes Level (easy 4 * 4, medium 6 * 4, hard 6 * 6) in einem Memory-Spiel erstellt. Sie bestehen aus demselben Code, haben jedoch unterschiedliche Anzahlen der Array-Länge und andere Variablen. Ich möchte den Code effizienter machen, indem ich die drei Klassen zu nur einem kombiniere. Hast du irgendwelche Vorschläge? Wenn es hilft ich die drei verschiedenen Klassen unter :)Verwenden einer Varible, um die Array-Länge für mehrere Klassen in einer Klasse zu bestimmen Java

Arrays und Variablen aus den drei verschiedenen Klassen eingefügt haben:

JButton[] button = new JButton[16]; 
JButton[] button = new JButton[24]; 
JButton[] button = new JButton[36]; 

int[] StoreCards = new int[16]; 
int[] StoreCards = new int[24]; 
int[] StoreCards = new int[36]; 

static int[] card = new int[9]; 
static int[] card = new int[13]; 
static int[] card = new int[19]; 

Leicht Level:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.SwingConstants; 

class EasyWindow extends JFrame implements ActionListener, MouseListener { 
    JLabel Score = new JLabel("Score: - "); 

    JLabel Welcome = new JLabel("Welcome " + StartWindow.user + "!"); 

    ImageIcon Back = new ImageIcon("mback.png"); 
    ImageIcon musicicon = new ImageIcon("musicicon.png"); 
    ImageIcon themeicon = new ImageIcon("themeicon.png"); 
    ImageIcon difficultyicon = new ImageIcon("difficulty.png"); 
    ImageIcon pointsicon = new ImageIcon("pointsicon.png"); 
    ImageIcon studentsicon = new ImageIcon("studentsicon.png"); 

    JButton AnOtherLevel = new JButton(
      "Click here if you want to change level."); 
    JButton Quit = new JButton("Quit Game!"); 

    JButton[] button = new JButton[16]; 

    JMenuBar menuBar = new JMenuBar(); 

    JMenu Settings = new JMenu("Settings"); 
    JMenu Theme = new JMenu("Theme"); 
    JMenu Rules = new JMenu("Rules"); 
    JMenu Creators = new JMenu("Creators"); 

    JMenuItem Music = new JMenuItem("Music", musicicon); 

    JMenuItem Celebrities = new JMenuItem("Celebrities", themeicon); 
    JMenuItem Cities = new JMenuItem("Cities", themeicon); 
    JMenuItem Memes = new JMenuItem("Memes", themeicon); 

    JMenuItem Difficulty = new JMenuItem("Difficulty", difficultyicon); 
    JMenuItem Points = new JMenuItem("Points", pointsicon); 

    JMenuItem Ava = new JMenuItem("Ava Baghchesara", studentsicon); 
    JMenuItem Michelle = new JMenuItem("Michelle Bill", studentsicon); 

    int[] StoreCards = new int[16]; 

    static int[] cardChecker = new int[2]; 
    static int[] card = new int[9]; 

    int[] Button = new int[2]; 

    static int flipped = 0; 
    static int score = 0; 

    String imageType = ".png"; 
    String back = ".png"; 

    JPanel Top = new JPanel(new GridLayout(1, 1, 5, 15)); 
    JPanel Center = new JPanel(new GridLayout(4, 4, 5, 5)); 
    JPanel Bottom = new JPanel(new GridLayout(1, 2, 0, 0)); 
    JPanel Right = new JPanel(new GridLayout(2, 2, 0, 0)); 
    JPanel Left = new JPanel(new GridLayout(1, 1, 0, 0)); 

    static Container contentArea; 

    public EasyWindow() { 
     super("User: " + StartWindow.user + " || Easy Level"); 
     setSize(600, 600); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(true); 
     setLayout(new BorderLayout()); 
     setVisible(true); 

     AnOtherLevel.addActionListener(this); 
     Quit.addActionListener(this); 

     AnOtherLevel.addMouseListener(this); 
     Quit.addMouseListener(this); 

     AnOtherLevel.setBackground(Color.white); 
     AnOtherLevel.setForeground(Color.BLACK); 

     Quit.setBackground(Color.white); 
     Quit.setForeground(Color.BLACK); 

     add(Top, BorderLayout.NORTH); 
     add(Left, BorderLayout.WEST); 
     add(Center, BorderLayout.CENTER); 
     add(Right, BorderLayout.EAST); 
     add(Bottom, BorderLayout.SOUTH); 

     Welcome.setFont(new Font("Serif", Font.PLAIN, 30)); 
     Welcome.setHorizontalAlignment(SwingConstants.CENTER); 
     Welcome.setVerticalAlignment(SwingConstants.CENTER); 

     Top.add(Welcome); 
     Top.setBackground(Color.white); 

     Center.setBackground(Color.white); 

     Right.setBackground(Color.white); 

     Right.add(Score); 

     Bottom.add(AnOtherLevel); 
     Bottom.add(Quit); 
     Bottom.setBackground(Color.white); 

     for (int n = 0; n <= button.length - 1; n++) { 
      button[n] = new JButton(); 
      Center.add(button[n]); 
      button[n].addActionListener(this); 
      button[n].setBackground(Color.white); 
     } 

     contentArea = getContentPane(); 
     contentArea.add("North", Top); 
     contentArea.add("Center", Center); 
     contentArea.add("South", Bottom); 

     menuBar.add(Settings); 
     menuBar.add(Rules); 
     menuBar.add(Creators); 

     setJMenuBar(menuBar); 

     Music.addActionListener(this); 

     Theme.addActionListener(this); 
     Celebrities.addActionListener(this); 
     Cities.addActionListener(this); 
     Memes.addActionListener(this); 

     Difficulty.addActionListener(this); 
     Points.addActionListener(this); 

     Ava.addActionListener(this); 
     Michelle.addActionListener(this); 

     Settings.add(Music); 
     Settings.add(Theme); 

     Theme.add(Celebrities); 
     Theme.add(Cities); 
     Theme.add(Memes); 

     Rules.add(Difficulty); 
     Rules.add(Points); 

     Creators.add(Ava); 
     Creators.add(Michelle); 

     Game(); 
     flipped = 3; 
     Reset(); 

     setContentPane(contentArea); 
     contentArea.setBackground(Color.white); 
    } 

    public void Game() { 
     int number = 0; 
     int x = 0; 

     ImageIcon image[] = new ImageIcon[15]; 

     while (x < 16) { 
      number = (int) RandomNumbers.GetRandomNumber(8); 
      image[number] = new ImageIcon(number + imageType); 

      if (card[number] < 2) { 
       card[number]++; 

       StoreCards[x] = number; 
       System.out.println(number + " Number" + "card nr " + x); 
       x++; 
      } 
     } 
    } 

    public void Reset() { 
     if (flipped > 2) { 
      flipped = 0; 

      for (int n = 0; n <= button.length - 1; n++) { 
       button[n].setIcon(Back); 
      } 
     } 
    } 

    public void Check(int number) { 
     if (cardChecker[0] == cardChecker[1]) { 
      score = score + 2; 
      Score.setText("Score: " + score); 
      DisableButtons(); 

     } else { 
      System.out.println("jj"); 
     } 
     if (score == 16) { 
      setVisible(false); 
      new EndWindow1(); 
     } 
    } 

    public void Card1and2(int number, int button) { 
     if (flipped == 0) { 
      cardChecker[0] = number; 
      Button[0] = button; 
     } 
     if (flipped == 1) { 
      cardChecker[1] = number; 
      Button[1] = button; 

      if (StoreCards[cardChecker[0]] == StoreCards[cardChecker[1]]) { 
       if (Button[0] != Button[1]) 
        Check(number); 
      } 
     } 
    } 

    public void DisableButtons() { 
     for (int n = 0; n <= button.length; n++) { 
      if (Button[0] == n || Button[1] == n) { 
       button[n].setVisible(false); 
      } 
     } 
    } 

    public void actionPerformed(ActionEvent event) { 
     if (event.getSource() == AnOtherLevel) { 
      setVisible(false); 
      new AnOtherWindow(); 
     } 

     if (event.getSource() == Quit) { 
      System.exit(0); 
     } 

     for (int n = 0; n <= button.length - 1; n++) { 
      if (event.getSource() == button[n]) { 

       int number = StoreCards[n]; 
       button[n].setIcon(new ImageIcon(number + imageType)); 

       Card1and2(number, n); 

       flipped++; 
       Reset(); 
      } 
     } 

     if (event.getSource() == Celebrities) { 
      Back = new ImageIcon("ceback.png"); 
      imageType = "c.png"; 
     } 
     if (event.getSource() == Cities) { 
      Back = new ImageIcon("ciback.png"); 
      imageType = ".jpg"; 
     } 

     if (event.getSource() == Memes) { 
      Back = new ImageIcon("mback.png"); 
      imageType = ".png"; 
     } 
    } 

    public void mouseEntered(MouseEvent event) { 
     if (event.getSource() == AnOtherLevel) { 
      AnOtherLevel.setBackground(Color.lightGray); 
      AnOtherLevel.setForeground(Color.BLACK); 
     } 

     if (event.getSource() == Quit) { 
      Quit.setBackground(Color.lightGray); 
      Quit.setForeground(Color.BLACK); 
     } 
    } 

    public void mouseClicked(MouseEvent e) { 
    } 

    public void mouseExited(MouseEvent e) { 
     AnOtherLevel.setBackground(Color.white); 
     AnOtherLevel.setForeground(Color.BLACK); 
     Quit.setBackground(Color.white); 
     Quit.setForeground(Color.BLACK); 
    } 

    public void mousePressed(MouseEvent e) { 
    } 

    public void mouseReleased(MouseEvent e) { 
    } 
} 

public class EasyLevelWindow { 
    public static void main(String[] args) { 
     EasyWindow win = new EasyWindow(); 
    } 

Medium Level:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.SwingConstants; 

class MediumWindow extends JFrame implements ActionListener, MouseListener { 
    JLabel Score = new JLabel("Score: - "); 

    JLabel Welcome = new JLabel("Welcome " + StartWindow.user + "!"); 

    ImageIcon Back = new ImageIcon("mback.png"); 
    ImageIcon musicicon = new ImageIcon("musicicon.png"); 
    ImageIcon themeicon = new ImageIcon("themeicon.png"); 
    ImageIcon difficultyicon = new ImageIcon("difficulty.png"); 
    ImageIcon pointsicon = new ImageIcon("pointsicon.png"); 
    ImageIcon studentsicon = new ImageIcon("studentsicon.png"); 

    JButton AnOtherLevel = new JButton(
      "Click here if you want to change level."); 
    JButton Quit = new JButton("Quit Game!"); 

    JButton[] button = new JButton[24]; 

    JMenuBar menuBar = new JMenuBar(); 

    JMenu Settings = new JMenu("Settings"); 
    JMenu Theme = new JMenu("Theme"); 
    JMenu Rules = new JMenu("Rules"); 
    JMenu Creators = new JMenu("Creators"); 

    JMenuItem Music = new JMenuItem("Music", musicicon); 

    JMenuItem Celebrities = new JMenuItem("Celebrities", themeicon); 
    JMenuItem Cities = new JMenuItem("Cities", themeicon); 
    JMenuItem Memes = new JMenuItem("Memes", themeicon); 

    JMenuItem Difficulty = new JMenuItem("Difficulty", difficultyicon); 
    JMenuItem Points = new JMenuItem("Points", pointsicon); 

    JMenuItem Ava = new JMenuItem("Ava Baghchesara", studentsicon); 
    JMenuItem Michelle = new JMenuItem("Michelle Bill", studentsicon); 

    int[] StoreCards = new int[24]; 

    static int[] cardChecker = new int[2]; 
    static int[] card = new int[13]; 

    int[] Button = new int[2]; 

    static int flipped = 0; 
    static int score = 0; 

    String imageType = ".png"; 
    String back = ".png"; 

    JPanel Top = new JPanel(new GridLayout(2, 1, 5, 15)); 
    JPanel Center = new JPanel(new GridLayout(6, 4, 5, 5)); 
    JPanel Bottom = new JPanel(new GridLayout(1, 1, 0, 0)); 
    JPanel Right = new JPanel(new GridLayout(2, 2, 0, 0)); 
    JPanel Left = new JPanel(new GridLayout(1, 1, 0, 0)); 

    static Container contentArea; 

    public MediumWindow() { 
     super("User: " + StartWindow.user + " || Medium Level"); 
     setSize(600, 600); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(true); 
     setLayout(new BorderLayout()); 
     setVisible(true); 

     AnOtherLevel.addActionListener(this); 
     Quit.addActionListener(this); 

     AnOtherLevel.addMouseListener(this); 
     Quit.addMouseListener(this); 

     AnOtherLevel.setBackground(Color.white); 
     AnOtherLevel.setForeground(Color.BLACK); 

     Quit.setBackground(Color.white); 
     Quit.setForeground(Color.BLACK); 

     add(Top, BorderLayout.NORTH); 
     add(Left, BorderLayout.WEST); 
     add(Center, BorderLayout.CENTER); 
     add(Right, BorderLayout.EAST); 
     add(Bottom, BorderLayout.SOUTH); 

     Welcome.setFont(new Font("Serif", Font.PLAIN, 30)); 
     Welcome.setHorizontalAlignment(SwingConstants.CENTER); 
     Welcome.setVerticalAlignment(SwingConstants.CENTER); 

     Top.add(Welcome); 
     Top.setBackground(Color.white); 

     Center.setBackground(Color.white); 

     Right.setBackground(Color.white); 

     Right.add(Score); 

     Bottom.add(AnOtherLevel); 
     Bottom.add(Quit); 
     Bottom.setBackground(Color.white); 

     for (int n = 0; n <= button.length - 1; n++) { 
      button[n] = new JButton(); 
      Center.add(button[n]); 
      button[n].addActionListener(this); 
      button[n].setBackground(Color.white); 
     } 

     contentArea = getContentPane(); 
     contentArea.add("North", Top); 
     contentArea.add("Center", Center); 
     contentArea.add("South", Bottom); 

     menuBar.add(Settings); 
     menuBar.add(Rules); 
     menuBar.add(Creators); 

     setJMenuBar(menuBar); 

     Music.addActionListener(this); 

     Theme.addActionListener(this); 
     Celebrities.addActionListener(this); 
     Cities.addActionListener(this); 
     Memes.addActionListener(this); 

     Difficulty.addActionListener(this); 
     Points.addActionListener(this); 

     Ava.addActionListener(this); 
     Michelle.addActionListener(this); 

     Settings.add(Music); 
     Settings.add(Theme); 

     Theme.add(Celebrities); 
     Theme.add(Cities); 
     Theme.add(Memes); 

     Rules.add(Difficulty); 
     Rules.add(Points); 

     Creators.add(Ava); 
     Creators.add(Michelle); 

     Game(); 
     flipped = 3; 
     Reset(); 

     setContentPane(contentArea); 
     contentArea.setBackground(Color.white); 
    } 

    public void Game() { 
     int number = 0; 
     int x = 0; 

     ImageIcon image[] = new ImageIcon[23]; 

     while (x < 24) { 
      number = (int) RandomNumbers.GetRandomNumber(12); 
      image[number] = new ImageIcon(number + imageType); 

      if (card[number] < 2) { 
       card[number]++; 

       StoreCards[x] = number; 
       System.out.println(number + " Number" + "card nr " + x); 
       x++; 
      } 
     } 
    } 

    public void Reset() { 
     if (flipped > 2) { 
      flipped = 0; 

      for (int n = 0; n <= button.length - 1; n++) { 
       button[n].setIcon(Back); 
      } 
     } 
    } 

    public void Check(int number) { 
     if (cardChecker[0] == cardChecker[1]) { 
      score = score + 2; 
      Score.setText("Score: " + score); 
      DisableButtons(); 

     } else { 
      System.out.println("jj"); 
     } 
     if (score == 24) { 
      setVisible(false); 
      new EndWindow1(); 
     } 
    } 

    public void Card1and2(int number, int button) { 
     if (flipped == 0) { 
      cardChecker[0] = number; 
      Button[0] = button; 
     } 
     if (flipped == 1) { 
      cardChecker[1] = number; 
      Button[1] = button; 

      if (StoreCards[cardChecker[0]] == StoreCards[cardChecker[1]]) { 
       if (Button[0] != Button[1]) 
        Check(number); 
      } 
     } 
    } 

    public void DisableButtons() { 
     for (int n = 0; n <= button.length; n++) { 
      if (Button[0] == n || Button[1] == n) { 
       button[n].setVisible(false); 
      } 
     } 
    } 

    public void actionPerformed(ActionEvent event) { 
     if (event.getSource() == AnOtherLevel) { 
      setVisible(false); 
      new AnOtherWindow(); 
     } 

     if (event.getSource() == Quit) { 
      System.exit(0); 
     } 

     for (int n = 0; n <= button.length - 1; n++) { 
      if (event.getSource() == button[n]) { 

       int number = StoreCards[n]; 
       button[n].setIcon(new ImageIcon(number + imageType)); 

       Card1and2(number, n); 

       flipped++; 
       Reset(); 
      } 
     } 

     if (event.getSource() == Celebrities) { 
      Back = new ImageIcon("ceback.png"); 
      imageType = "c.png"; 
     } 
     if (event.getSource() == Cities) { 
      Back = new ImageIcon("ciback.png"); 
      imageType = ".jpg"; 
     } 

     if (event.getSource() == Memes) { 
      Back = new ImageIcon("mback.png"); 
      imageType = ".png"; 
     } 
    } 

    public void mouseEntered(MouseEvent event) { 
     if (event.getSource() == AnOtherLevel) { 
      AnOtherLevel.setBackground(Color.lightGray); 
      AnOtherLevel.setForeground(Color.BLACK); 
     } 

     if (event.getSource() == Quit) { 
      Quit.setBackground(Color.lightGray); 
      Quit.setForeground(Color.BLACK); 
     } 
    } 

    public void mouseClicked(MouseEvent e) { 
    } 

    public void mouseExited(MouseEvent e) { 
     AnOtherLevel.setBackground(Color.white); 
     AnOtherLevel.setForeground(Color.BLACK); 
     Quit.setBackground(Color.white); 
     Quit.setForeground(Color.BLACK); 
    } 

    public void mousePressed(MouseEvent e) { 
    } 

    public void mouseReleased(MouseEvent e) { 
    } 
} 

public class MediumLevelWindow { 
    public static void main(String[] args) { 
     MediumWindow win = new MediumWindow(); 
    } 
} 

Harte Ebene:

import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.Container; 
    import java.awt.Font; 
    import java.awt.GridLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.MouseListener; 

    import javax.swing.ImageIcon; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JMenu; 
    import javax.swing.JMenuBar; 
    import javax.swing.JMenuItem; 
    import javax.swing.JPanel; 
    import javax.swing.SwingConstants; 

    class HardWindow extends JFrame implements ActionListener, MouseListener { 
     JLabel Score = new JLabel("Score: - "); 

     JLabel Welcome = new JLabel("Welcome " + StartWindow.user + "!"); 

     ImageIcon Back = new ImageIcon("mback.png"); 
     ImageIcon musicicon = new ImageIcon("musicicon.png"); 
     ImageIcon themeicon = new ImageIcon("themeicon.png"); 
     ImageIcon difficultyicon = new ImageIcon("difficulty.png"); 
     ImageIcon pointsicon = new ImageIcon("pointsicon.png"); 
     ImageIcon studentsicon = new ImageIcon("studentsicon.png"); 

     JButton AnOtherLevel = new JButton(
       "Click here if you want to change level."); 
     JButton Quit = new JButton("Quit Game!"); 

     JButton[] button = new JButton[36]; 

     JMenuBar menuBar = new JMenuBar(); 

     JMenu Settings = new JMenu("Settings"); 
     JMenu Theme = new JMenu("Theme"); 
     JMenu Rules = new JMenu("Rules"); 
     JMenu Creators = new JMenu("Creators"); 

     JMenuItem Music = new JMenuItem("Music", musicicon); 

     JMenuItem Celebrities = new JMenuItem("Celebrities", themeicon); 
     JMenuItem Cities = new JMenuItem("Cities", themeicon); 
     JMenuItem Memes = new JMenuItem("Memes", themeicon); 

     JMenuItem Difficulty = new JMenuItem("Difficulty", difficultyicon); 
     JMenuItem Points = new JMenuItem("Points", pointsicon); 

     JMenuItem Ava = new JMenuItem("Ava Baghchesara", studentsicon); 
     JMenuItem Michelle = new JMenuItem("Michelle Bill", studentsicon); 

     int[] StoreCards = new int[36]; 

     static int[] cardChecker = new int[2]; 
     static int[] card = new int[19]; 

     int[] Button = new int[2]; 

     static int flipped = 0; 
     static int score = 0; 

     String imageType = ".png"; 
     String back = ".png"; 

     JPanel Top = new JPanel(new GridLayout(2, 1, 5, 15)); 
     JPanel Center = new JPanel(new GridLayout(6, 6, 5, 5)); 
     JPanel Bottom = new JPanel(new GridLayout(1, 1, 0, 0)); 
     JPanel Right = new JPanel(new GridLayout(2, 2, 0, 0)); 
     JPanel Left = new JPanel(new GridLayout(1, 1, 0, 0)); 

     static Container contentArea; 

     public HardWindow() { 
      super("User: " + StartWindow.user + " || Hard Level"); 
      setSize(780, 730); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setResizable(true); 
      setLayout(new BorderLayout()); 
      setVisible(true); 

      AnOtherLevel.addActionListener(this); 
      Quit.addActionListener(this); 

      AnOtherLevel.addMouseListener(this); 
      Quit.addMouseListener(this); 

      AnOtherLevel.setBackground(Color.white); 
      AnOtherLevel.setForeground(Color.BLACK); 

      Quit.setBackground(Color.white); 
      Quit.setForeground(Color.BLACK); 

      add(Top, BorderLayout.NORTH); 
      add(Left, BorderLayout.WEST); 
      add(Center, BorderLayout.CENTER); 
      add(Right, BorderLayout.EAST); 
      add(Bottom, BorderLayout.SOUTH); 

      Welcome.setFont(new Font("Serif", Font.PLAIN, 30)); 
      Welcome.setHorizontalAlignment(SwingConstants.CENTER); 
      Welcome.setVerticalAlignment(SwingConstants.CENTER); 

      Top.add(Welcome); 
      Top.setBackground(Color.white); 

      Center.setBackground(Color.white); 

      Right.setBackground(Color.white); 

      Right.add(Score); 

      Bottom.add(AnOtherLevel); 
      Bottom.add(Quit); 
      Bottom.setBackground(Color.white); 

      for (int n = 0; n <= button.length - 1; n++) { 
       button[n] = new JButton(); 
       Center.add(button[n]); 
       button[n].addActionListener(this); 
       button[n].setBackground(Color.white); 
      } 

      contentArea = getContentPane(); 
      contentArea.add("North", Top); 
      contentArea.add("Center", Center); 
      contentArea.add("South", Bottom); 

      menuBar.add(Settings); 
      menuBar.add(Rules); 
      menuBar.add(Creators); 

      setJMenuBar(menuBar); 

      Music.addActionListener(this); 

      Theme.addActionListener(this); 
      Celebrities.addActionListener(this); 
      Cities.addActionListener(this); 
      Memes.addActionListener(this); 

      Difficulty.addActionListener(this); 
      Points.addActionListener(this); 

      Ava.addActionListener(this); 
      Michelle.addActionListener(this); 

      Settings.add(Music); 
      Settings.add(Theme); 

      Theme.add(Celebrities); 
      Theme.add(Cities); 
      Theme.add(Memes); 

      Rules.add(Difficulty); 
      Rules.add(Points); 

      Creators.add(Ava); 
      Creators.add(Michelle); 

      Game(); 
      flipped = 3; 
      Reset(); 

      setContentPane(contentArea); 
      contentArea.setBackground(Color.white); 
     } 

     public void Game() { 
      int number = 0; 
      int x = 0; 

      ImageIcon image[] = new ImageIcon[35]; 

      while (x < 36) { 
       number = (int) RandomNumbers.GetRandomNumber(18); 
       image[number] = new ImageIcon(number + imageType); 

       if (card[number] < 2) { 
        card[number]++; 

        StoreCards[x] = number; 
        System.out.println(number + " Number" + "card nr " + x); 
        x++; 
       } 
      } 
     } 

     public void Reset() { 
      if (flipped > 2) { 
       flipped = 0; 

       for (int n = 0; n <= button.length - 1; n++) { 
        button[n].setIcon(Back); 
       } 
      } 
     } 

     public void Check(int number) { 
      if (cardChecker[0] == cardChecker[1]) { 
       score = score + 2; 
       Score.setText("Score: " + score); 
       DisableButtons(); 

      } else { 
       System.out.println("jj"); 
      } 
      if (score == 36) { 
       setVisible(false); 
       new EndWindow1(); 
      } 
     } 

     public void Card1and2(int number, int button) { 
      if (flipped == 0) { 
       cardChecker[0] = number; 
       Button[0] = button; 
      } 
      if (flipped == 1) { 
       cardChecker[1] = number; 
       Button[1] = button; 

       if (StoreCards[cardChecker[0]] == StoreCards[cardChecker[1]]) { 
        if (Button[0] != Button[1]) 
         Check(number); 
       } 
      } 
     } 

     public void DisableButtons() { 
      for (int n = 0; n <= button.length; n++) { 
       if (Button[0] == n || Button[1] == n) { 
        button[n].setVisible(false); 
       } 
      } 
     } 

     public void actionPerformed(ActionEvent event) { 
      if (event.getSource() == AnOtherLevel) { 
       setVisible(false); 
       new AnOtherWindow(); 
      } 

      if (event.getSource() == Quit) { 
       System.exit(0); 
      } 

      for (int n = 0; n <= button.length - 1; n++) { 
       if (event.getSource() == button[n]) { 

        int number = StoreCards[n]; 
        button[n].setIcon(new ImageIcon(number + imageType)); 

        Card1and2(number, n); 

        flipped++; 
        Reset(); 
       } 
      } 

      if (event.getSource() == Celebrities) { 
       Back = new ImageIcon("ceback.png"); 
       imageType = "c.png"; 
      } 
      if (event.getSource() == Cities) { 
       Back = new ImageIcon("ciback.png"); 
       imageType = ".jpg"; 
      } 

      if (event.getSource() == Memes) { 
       Back = new ImageIcon("mback.png"); 
       imageType = ".png"; 
      } 
     } 

     public void mouseEntered(MouseEvent event) { 
      if (event.getSource() == AnOtherLevel) { 
       AnOtherLevel.setBackground(Color.lightGray); 
       AnOtherLevel.setForeground(Color.BLACK); 
      } 

      if (event.getSource() == Quit) { 
       Quit.setBackground(Color.lightGray); 
       Quit.setForeground(Color.BLACK); 
      } 
     } 

     public void mouseClicked(MouseEvent e) { 
     } 

     public void mouseExited(MouseEvent e) { 
      AnOtherLevel.setBackground(Color.white); 
      AnOtherLevel.setForeground(Color.BLACK); 
      Quit.setBackground(Color.white); 
      Quit.setForeground(Color.BLACK); 
     } 

     public void mousePressed(MouseEvent e) { 
     } 

     public void mouseReleased(MouseEvent e) { 
     } 
    } 

    public class HardLevelWindow { 
     public static void main(String[] args) { 
      HardWindow win = new HardWindow(); 


} 
} 
+0

Sie sollten zunächst versuchen, und stellen dann eine Frage über etwas sind Sie Probleme haben mit. –

Antwort

0

Sie können eine Parent Klasse z. B. BaseGame erstellen, die abstrakt ist. Wie folgt aus:

public abstract class BaseGame { 

     private abstract int[] getStoreCards(); 

     private abstract JButton[] getButtons(); 

     private abstract int[] getCards(); 

     ... your other code goes here 

    } 

Und dann können Sie Ihr Kind-Klassen erstellen, wo Sie diese Methoden implementieren, z:

public class EasyGame { 

    @Override 
    private int[] getStoreCards() { 
     return new int[16]; 
    } 

    @Override 
    private int[] getCards() { 
     return new int[9]; 
    } 

    @Override 
    private JButton[] getButtons() { 
     return new JButton[16]; 
    } 
} 

So überall in Ihrem Code, wo Sie in der Regel Ihre Arrays initialisieren, diese Methoden stattdessen verwenden.

int[] Button = new int[2]; 

wird

getButtons(); 
0

Sie wollen wahrscheinlich ein ArrayList, wie so verwenden:

ArrayList<JButton> buttons = new ArrayList<>(); 

//... 

for (int n = 0; n < boardSize; n++) { 
    JButton button = new JButton(); 
    buttons.add(button); // add to the end of the expandable list 
    Center.add(button); 
    button.addActionListener(this); 
    button.setBackground(Color.white); 
} 

wo boardSize ein Parameter zu Ihrem Konstruktor ist die Angabe der Größe.

Verwandte Themen