Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demineur;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.InputMismatchException;
- import java.util.Random;
- import java.util.Scanner;
- import javax.swing.*;
- public class terrain extends JFrame implements ActionListener, MouseListener {
- // Variables d'instance & Content Pane & JLabel
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- int lignes = 9;
- int colonnes = 9;
- int numMines = 10;
- int secondes;
- Random r = new Random();
- int v;
- boolean timer = false;
- int[] grid = new int[lignes * colonnes];
- boolean[] bombes = new boolean[lignes * colonnes];
- JButton[] boutons = new JButton[lignes * colonnes];
- int[] nombres = new int[lignes * colonnes];
- boolean echec = false;
- boolean victoire = false;
- boolean[] clickdone = new boolean[lignes * colonnes];
- boolean[] clickable = new boolean[lignes * colonnes];
- JPanel defaut = new JPanel();
- BorderLayout disposition = new BorderLayout();
- JPanel champ = new JPanel();
- GridLayout grille = new GridLayout(lignes, colonnes);
- JPanel bas = new JPanel();
- BorderLayout cases = new BorderLayout();
- JLabel time = new JLabel("Temps : 0");
- JLabel mines = new JLabel("Mines : 10");
- // Methode Main
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- public static void main(String[] args) {
- new terrain();
- }
- // Constructeur de la fenêtre
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- public terrain() {
- bas.setBackground(Color.WHITE);
- champ.setBackground(Color.GRAY);
- defaut.setLayout(disposition);
- bas.setLayout(cases);
- champ.setLayout(grille);
- time.setPreferredSize(new Dimension(200, 35));
- mines.setPreferredSize(new Dimension(200, 35));
- // Le Panel Bas est au Sud de Border. Il contient deux Label à l'Ouest
- // et à l'Est **
- defaut.add(champ, disposition.CENTER);
- defaut.add(bas, disposition.SOUTH);
- bas.add(time, cases.WEST);
- bas.add(mines, cases.EAST);
- time.setHorizontalAlignment(JLabel.CENTER);
- time.setBorder(BorderFactory.createLineBorder(Color.black));
- mines.setHorizontalAlignment(JLabel.CENTER);
- mines.setBorder(BorderFactory.createLineBorder(Color.black));
- // Mise en ecoute des boutons
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- for (int x = 0; x < lignes; x++) {
- for (int y = 0; y < colonnes; y++) {
- bombes[(lignes * y) + x] = false;
- clickdone[(lignes * y) + x] = false;
- clickable[(lignes * y) + x] = true;
- boutons[(lignes * y) + x] = new JButton();
- boutons[(lignes * y) + x].addActionListener(this);
- boutons[(lignes * y) + x].addMouseListener(this);
- }
- }
- // Instaurer les boutons de la grille
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- for (int i = 0; i < grid.length; i++) {
- champ.add(boutons[i]);
- }
- // Gestion du temps en Secondes par la class Timer
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- TimerTask task = new TimerTask() {
- public void run() {
- if (timer == true) {
- secondes++;
- time.setText("Temps : " + secondes);
- }
- }
- };
- Timer timer = new Timer();
- timer.scheduleAtFixedRate(task, 0, 1000);
- // Génération des Mines
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- for (int i = 0; i < numMines; i++) {
- v = r.nextInt((lignes * colonnes));
- if (grid[v] == 0) // si il n'y a pas déja une mine
- {
- grid[v] = 1; // on place une mine
- } else {
- i--;
- }
- }
- for (int x = 0; x < colonnes; x++) {
- for (int y = 0; y < lignes; y++) {
- if (grid[(lignes * y) + x] == 0)// pas de Mine
- {
- boutons[(lignes * y) + x].setText("");
- } else // Mine
- {
- boutons[(lignes * y) + x].setText("");
- bombes[(lignes * y) + x] = true;
- }
- }
- }
- // Génération des Chiffres
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- for (int x = 0; x < lignes; x++) {
- for (int y = 0; y < colonnes; y++) {
- int cur = (lignes * y) + x;
- if (bombes[cur]) {
- nombres[cur] = 0;
- continue;
- }
- int temp = 0;
- boolean l = (x - 1) >= 0;
- boolean r = (x + 1) < lignes;
- boolean u = (y - 1) >= 0;
- boolean d = (y + 1) < colonnes;
- int left = (lignes * (y)) + (x - 1);
- int right = (lignes) * (y) + (x + 1);
- int up = (lignes * (y - 1)) + (x);
- int upleft = (lignes * (y - 1)) + (x - 1);
- int upright = (lignes * (y - 1)) + (x + 1);
- int down = (lignes * (y + 1)) + (x);
- int downleft = (lignes * (y + 1)) + (x - 1);
- int downright = (lignes * (y + 1)) + (x + 1);
- if (u) {
- if (bombes[up]) {
- temp++;
- }
- if (l) {
- if (bombes[upleft]) {
- temp++;
- }
- }
- if (r) {
- if (bombes[upright]) {
- temp++;
- }
- }
- }
- if (d) {
- if (bombes[down]) {
- temp++;
- }
- if (l) {
- if (bombes[downleft]) {
- temp++;
- }
- }
- if (r) {
- if (bombes[downright]) {
- temp++;
- }
- }
- }
- if (l) {
- if (bombes[left]) {
- temp++;
- }
- }
- if (r) {
- if (bombes[right]) {
- temp++;
- }
- }
- nombres[cur] = temp;
- }
- }
- // Configuration de la fenêtre
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- this.setTitle("Démineur - Kaybward");
- this.setSize(400, 460);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- this.setContentPane(defaut);
- this.setVisible(true);
- }
- // Listener & Action
- // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- public void actionPerformed(ActionEvent e) {
- if (victoire == false) {
- for (int x = 0; x < lignes; x++) {
- for (int y = 0; y < colonnes; y++) {
- if (e.getSource() == boutons[(lignes * y) + x]
- && victoire == false && clickable[(lignes * y) + x]) {
- // Verification du terrain
- int cur = (lignes * y) + x;
- boolean l = (x - 1) >= 0;
- boolean r = (x + 1) < lignes;
- boolean u = (y - 1) >= 0;
- boolean d = (y + 1) < colonnes;
- int left = (lignes * (y)) + (x - 1);
- int right = (lignes * (y)) + (x + 1);
- int up = (lignes * (y - 1)) + (x);
- int upleft = (lignes * (y - 1)) + (x - 1);
- int upright = (lignes * (y - 1)) + (x + 1);
- int down = (lignes * (y + 1)) + (x);
- int downleft = (lignes * (y + 1)) + (x - 1);
- int downright = (lignes * (y + 1)) + (x + 1);
- clickdone[cur] = true;
- boutons[cur].setEnabled(false);
- if (nombres[cur] == 0 && bombes[cur] == false && echec == false && echec == false) {
- if (u && victoire == false) {
- if (clickdone[up] == false
- && bombes[up] == false) {
- clickdone[up] = true;
- boutons[up].doClick();
- }
- if (l && victoire == false) {
- if (clickdone[upleft] == false
- && nombres[upleft] != 0
- && bombes[upleft] == false) {
- clickdone[upleft] = true;
- boutons[upleft].doClick();
- }
- }
- if (r && victoire == false) {
- if (clickdone[upright] == false && nombres[upright] != 0 && bombes[upright] == false) {
- clickdone[upright] = true;
- boutons[upright].doClick();
- }
- }
- }
- if (d && victoire == false) {
- if (clickdone[down] == false && bombes[down] == false) {
- clickdone[down] = true;
- boutons[down].doClick();
- }
- if (l && victoire == false) {
- if (clickdone[downleft] == false && nombres[downleft] != 0 && bombes[downleft] == false) {
- clickdone[downleft] = true;
- boutons[downleft].doClick();
- }
- }
- if (r && victoire == false) {
- if (clickdone[downright] == false && nombres[downright] != 0 && bombes[downright] == false) {
- clickdone[downright] = true;
- boutons[downright].doClick();
- }
- }
- }
- if (l && victoire == false) {
- if (clickdone[left] == false && bombes[left] == false) {
- clickdone[left] = true;
- boutons[left].doClick();
- }
- }
- if (r && victoire == false) {
- if (clickdone[right] == false && bombes[right] == false) {
- clickdone[right] = true;
- boutons[right].doClick();
- }
- }
- } else {
- boutons[cur].setText("" + nombres[cur]);
- if (bombes[cur] == false && nombres[cur] == 0) {
- boutons[cur].setText("");
- }
- }
- if (bombes[cur] && victoire == false) {
- boutons[cur].setText("x");
- // En cas de perte
- if (echec == false && victoire == false) {
- echec = true;
- timer = false;
- for (int i = 0; i < lignes * colonnes; i++) {
- if (clickdone[i] == false) {
- boutons[i].doClick(0);
- }
- }
- JOptionPane.showMessageDialog(null, "Partie perdue", "Echec !", JOptionPane.ERROR_MESSAGE);
- this.dispose();
- new terrain();
- }
- }
- // Fin de la Verification du terrain
- }
- }
- }
- }
- // Check de la Victoire
- for (int x = 0; x < lignes; x++) {
- for (int y = 0; y < colonnes; y++) {
- int cur = (lignes * y) + x;
- if (clickdone[cur] == false) {
- if (bombes[cur]) {
- continue;
- } else {
- return;
- }
- }
- }
- }
- // En cas de victoire
- if (echec == false) {
- victoire = true;
- timer = false;
- JOptionPane.showMessageDialog(null, "Partie gagnée", "Victoire !", JOptionPane.INFORMATION_MESSAGE);
- this.dispose();
- new terrain();
- }
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- public void mouseClicked(MouseEvent e) {
- timer = true; // Démarre le Timer
- }
- }
- //Deux méthodes du MineSweeper de SciWizEH (Ce code reprend son algorithme)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement