2016-06-01 1 views
0

J'ai fait 3 classes pour chaque niveau (facile 4 * 4, moyen 6 * 4, dur 6 * 6) dans un jeu de mémoire. Ils sont constitués du même code, mais ils ont des nombres différents de la longueur du tableau et d'autres variables. Je veux rendre le code plus efficace en combinant les trois classes en une seule, avez-vous des suggestions? Si elle aide que j'ai collé les trois classes différentes ci-dessous :)en utilisant une variable pour déterminer la longueur du tableau pour plusieurs classes dans une classe Java

Les tableaux et les variables des 3 classes différentes:

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]; 

Niveau facile:

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(); 
    } 

Niveau moyen:

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(); 
    } 
} 

Hard Niveau:

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

Vous devriez essayer d'abord, puis poser une question sur quelque chose que vous avez des problèmes avec. –

Répondre

0

Vous pouvez créer une classe Parent, par exemple: BaseGame, ce qui est abstrait. Comme ça:

public abstract class BaseGame { 

     private abstract int[] getStoreCards(); 

     private abstract JButton[] getButtons(); 

     private abstract int[] getCards(); 

     ... your other code goes here 

    } 

Et vous pouvez créer vos classes d'enfants où vous mettez en œuvre ces méthodes, par exemple:

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]; 
    } 
} 

Donc partout dans votre code où vous initialiser habituellement vos tableaux, utilisez ces méthodes à la place.

int[] Button = new int[2]; 

devient

getButtons(); 
0

Vous voulez probablement utiliser un ArrayList, comme ceci:

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); 
} 

boardSize est un paramètre à votre constructeur spécifiant la taille.