Ajouter un commentaire

olihya
Ajouter des niveaux à mon jeux

Bonjour à vous.
Bonjour Niroken.
Pour continuer le post, j'ai essayé de faire une version à ma façon du reverso.
Moins sophistiquée que la version de Niroken mais j'ai aussi moins de connaissance en java.
Cela dit ta version m'a beaucoup appris.
J'ai reussi à faire le premier niveau et cela fonctionne.
Maintenant, je veux implanter les différents niveaux.
Et là je bloque.

J'y ai mis pour commencer 2 niveaux (le placement des pions du premier niveau est identique au second mais je le changerai après).
Ce qui m'interesse c'est que lorsque je clique sur le JmenuItem niveau2 cela joue le code du niveau2.
Voici mon code:

package reverso;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Fenetre extends JFrame implements ActionListener, Icon {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel pan = new JPanel();
	private JMenuBar barreMenus;
	private JMenu niveaux;
	private JMenuItem niveau1, niveau2, niveau3, niveau4;
	protected ImageIcon b1;
	protected ImageIcon b2;
	protected JButton[] boutons;

	public Fenetre() {

		this.setTitle("Reverso");
		this.setSize(450, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		barreMenus = new JMenuBar();
		setJMenuBar(barreMenus);
		niveaux = new JMenu("Niveaux");
		barreMenus.add(niveaux);
		niveau1 = new JMenuItem("Niveau1");
		niveaux.add(niveau1);
		niveau1.addActionListener(this);
		niveau2 = new JMenuItem("Niveau2");
		niveaux.add(niveau2);
		niveau2.addActionListener(this);
		niveau3 = new JMenuItem("Niveau3");
		niveaux.add(niveau3);
		niveau3.addActionListener(this);
		niveau4 = new JMenuItem("Niveau4");
		niveaux.add(niveau4);
		niveau4.addActionListener(this);

		setLayout(new GridLayout(5, 5, 0, 0));
		JButton[] b = new JButton[25];
		this.boutons = b;
		for (int i = 0; i < b.length; i++) {
			b1 = new ImageIcon("b1.gif");
			b2 = new ImageIcon("b2.gif");
			b[i] = new JButton(b1);
			b[i].setSelectedIcon(b2);
			b[i].addActionListener(this);
			pan.add(b[i]);
		}
		setContentPane(pan);
	}

	{

		this.setContentPane(pan);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if (e.equals(niveau1)) {
			for (int i = 6; i <= 8; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 11; i <= 13; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}

			for (int i = 16; i <= 18; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 0; i <= 0; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 4; i <= 4; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 20; i <= 20; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 24; i <= 24; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 1; i <= 3; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 5; i <= 5; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 10; i <= 10; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 15; i <= 15; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 21; i <= 23; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 9; i <= 9; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 14; i <= 14; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 19; i <= 19; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
		}
		if (e.equals(niveau2)) {
			for (int i = 6; i <= 8; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 2].setSelected(!boutons[i + 2].isSelected());
					boutons[i - 2].setSelected(!boutons[i - 2].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 11; i <= 13; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}

			for (int i = 16; i <= 18; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 0; i <= 0; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 4; i <= 4; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 20; i <= 20; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 24; i <= 24; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 1; i <= 3; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 5; i <= 5; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 10; i <= 10; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 15; i <= 15; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 21; i <= 23; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i + 1].setSelected(!boutons[i + 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
				}
			}
			for (int i = 9; i <= 9; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 14; i <= 14; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
			for (int i = 19; i <= 19; i++) {
				if (source == boutons[i]) {
					boutons[i].setSelected(!boutons[i].isSelected());
					boutons[i - 1].setSelected(!boutons[i - 1].isSelected());
					boutons[i - 5].setSelected(!boutons[i - 5].isSelected());
					boutons[i + 5].setSelected(!boutons[i + 5].isSelected());
				}
			}
		}
	}

	@Override
	public int getIconHeight() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getIconWidth() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		// TODO Auto-generated method stub

	}

}

Si quelqu'un à une idée, elle est la bienvenue.
Bonne continuation

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
  QQQ    PPPP   III  BBBB   FFFF 
Q Q P P I B B F
Q Q PPPP I BBBB FFF
Q QQ P I B B F
QQQQ P III BBBB F
Q