Advertisement
Guest User

Deminor - Kaybward

a guest
Mar 31st, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.03 KB | None | 0 0
  1. package demineur;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.GridLayout;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9.  
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.util.InputMismatchException;
  20. import java.util.Random;
  21. import java.util.Scanner;
  22.  
  23. import javax.swing.*;
  24.  
  25. public class terrain extends JFrame implements ActionListener, MouseListener {
  26.  
  27.     // Variables d'instance & Content Pane & JLabel
  28.     // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  29.  
  30.     int lignes = 9;
  31.     int colonnes = 9;
  32.     int numMines = 10;
  33.     int secondes;
  34.  
  35.     Random r = new Random();
  36.     int v;
  37.  
  38.     boolean timer = false;
  39.     int[] grid = new int[lignes * colonnes];
  40.     boolean[] bombes = new boolean[lignes * colonnes];
  41.     JButton[] boutons = new JButton[lignes * colonnes];
  42.     int[] nombres = new int[lignes * colonnes];
  43.     boolean echec = false;
  44.     boolean victoire = false;
  45.     boolean[] clickdone = new boolean[lignes * colonnes];
  46.     boolean[] clickable = new boolean[lignes * colonnes];
  47.  
  48.     JPanel defaut = new JPanel();
  49.     BorderLayout disposition = new BorderLayout();
  50.  
  51.     JPanel champ = new JPanel();
  52.     GridLayout grille = new GridLayout(lignes, colonnes);
  53.  
  54.     JPanel bas = new JPanel();
  55.     BorderLayout cases = new BorderLayout();
  56.  
  57.     JLabel time = new JLabel("Temps : 0");
  58.     JLabel mines = new JLabel("Mines : 10");
  59.  
  60.     // Methode Main
  61.     // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  62.  
  63.     public static void main(String[] args) {
  64.  
  65.         new terrain();
  66.  
  67.     }
  68.  
  69.     // Constructeur de la fenêtre
  70.     // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  71.  
  72.     public terrain() {
  73.  
  74.         bas.setBackground(Color.WHITE);
  75.         champ.setBackground(Color.GRAY);
  76.  
  77.         defaut.setLayout(disposition);
  78.         bas.setLayout(cases);
  79.         champ.setLayout(grille);
  80.  
  81.         time.setPreferredSize(new Dimension(200, 35));
  82.         mines.setPreferredSize(new Dimension(200, 35));
  83.  
  84.         // Le Panel Bas est au Sud de Border. Il contient deux Label à l'Ouest
  85.         // et à l'Est **
  86.  
  87.         defaut.add(champ, disposition.CENTER);
  88.         defaut.add(bas, disposition.SOUTH);
  89.         bas.add(time, cases.WEST);
  90.         bas.add(mines, cases.EAST);
  91.  
  92.         time.setHorizontalAlignment(JLabel.CENTER);
  93.         time.setBorder(BorderFactory.createLineBorder(Color.black));
  94.         mines.setHorizontalAlignment(JLabel.CENTER);
  95.         mines.setBorder(BorderFactory.createLineBorder(Color.black));
  96.  
  97.         // Mise en ecoute des boutons
  98.         // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  99.  
  100.         for (int x = 0; x < lignes; x++) {
  101.             for (int y = 0; y < colonnes; y++) {
  102.                 bombes[(lignes * y) + x] = false;
  103.                 clickdone[(lignes * y) + x] = false;
  104.                 clickable[(lignes * y) + x] = true;
  105.                 boutons[(lignes * y) + x] = new JButton();
  106.                 boutons[(lignes * y) + x].addActionListener(this);
  107.                 boutons[(lignes * y) + x].addMouseListener(this);
  108.             }
  109.         }
  110.  
  111.         // Instaurer les boutons de la grille
  112.         // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  113.  
  114.         for (int i = 0; i < grid.length; i++) {
  115.             champ.add(boutons[i]);
  116.         }
  117.  
  118.         // Gestion du temps en Secondes par la class Timer
  119.         // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  120.  
  121.         TimerTask task = new TimerTask() {
  122.             public void run() {
  123.                 if (timer == true) {
  124.                     secondes++;
  125.                     time.setText("Temps : " + secondes);
  126.                 }
  127.             }
  128.         };
  129.  
  130.         Timer timer = new Timer();
  131.         timer.scheduleAtFixedRate(task, 0, 1000);
  132.  
  133.         // Génération des Mines
  134.         // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  135.  
  136.         for (int i = 0; i < numMines; i++) {
  137.             v = r.nextInt((lignes * colonnes));
  138.             if (grid[v] == 0) // si il n'y a pas déja une mine
  139.             {
  140.                 grid[v] = 1; // on place une mine
  141.             } else {
  142.                 i--;
  143.             }
  144.         }
  145.  
  146.         for (int x = 0; x < colonnes; x++) {
  147.             for (int y = 0; y < lignes; y++) {
  148.                 if (grid[(lignes * y) + x] == 0)// pas de Mine
  149.                 {
  150.                     boutons[(lignes * y) + x].setText("");
  151.                 } else // Mine
  152.                 {
  153.                     boutons[(lignes * y) + x].setText("");
  154.                     bombes[(lignes * y) + x] = true;
  155.                 }
  156.             }
  157.         }
  158.  
  159.         // Génération des Chiffres
  160.         // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  161.  
  162.         for (int x = 0; x < lignes; x++) {
  163.             for (int y = 0; y < colonnes; y++) {
  164.                 int cur = (lignes * y) + x;
  165.                 if (bombes[cur]) {
  166.                     nombres[cur] = 0;
  167.                     continue;
  168.                 }
  169.                 int temp = 0;
  170.                 boolean l = (x - 1) >= 0;
  171.                 boolean r = (x + 1) < lignes;
  172.                 boolean u = (y - 1) >= 0;
  173.                 boolean d = (y + 1) < colonnes;
  174.                 int left = (lignes * (y)) + (x - 1);
  175.                 int right = (lignes) * (y) + (x + 1);
  176.                 int up = (lignes * (y - 1)) + (x);
  177.                 int upleft = (lignes * (y - 1)) + (x - 1);
  178.                 int upright = (lignes * (y - 1)) + (x + 1);
  179.                 int down = (lignes * (y + 1)) + (x);
  180.                 int downleft = (lignes * (y + 1)) + (x - 1);
  181.                 int downright = (lignes * (y + 1)) + (x + 1);
  182.                 if (u) {
  183.                     if (bombes[up]) {
  184.                         temp++;
  185.                     }
  186.                     if (l) {
  187.                         if (bombes[upleft]) {
  188.                             temp++;
  189.                         }
  190.                     }
  191.                     if (r) {
  192.                         if (bombes[upright]) {
  193.                             temp++;
  194.                         }
  195.                     }
  196.                 }
  197.                 if (d) {
  198.                     if (bombes[down]) {
  199.                         temp++;
  200.                     }
  201.                     if (l) {
  202.                         if (bombes[downleft]) {
  203.                             temp++;
  204.                         }
  205.                     }
  206.                     if (r) {
  207.                         if (bombes[downright]) {
  208.                             temp++;
  209.                         }
  210.                     }
  211.                 }
  212.                 if (l) {
  213.                     if (bombes[left]) {
  214.                         temp++;
  215.                     }
  216.                 }
  217.                 if (r) {
  218.                     if (bombes[right]) {
  219.                         temp++;
  220.                     }
  221.                 }
  222.                 nombres[cur] = temp;
  223.             }
  224.         }
  225.  
  226.         // Configuration de la fenêtre
  227.         // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  228.  
  229.         this.setTitle("Démineur - Kaybward");
  230.         this.setSize(400, 460);
  231.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  232.         this.setLocationRelativeTo(null);
  233.         this.setContentPane(defaut);
  234.         this.setVisible(true);
  235.     }
  236.  
  237.     // Listener & Action
  238.     // -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  239.  
  240.     public void actionPerformed(ActionEvent e) {
  241.  
  242.         if (victoire == false) {
  243.             for (int x = 0; x < lignes; x++) {
  244.                 for (int y = 0; y < colonnes; y++) {
  245.                     if (e.getSource() == boutons[(lignes * y) + x]
  246.                             && victoire == false && clickable[(lignes * y) + x]) {
  247.                         // Verification du terrain
  248.                         int cur = (lignes * y) + x;
  249.                         boolean l = (x - 1) >= 0;
  250.                         boolean r = (x + 1) < lignes;
  251.                         boolean u = (y - 1) >= 0;
  252.                         boolean d = (y + 1) < colonnes;
  253.                         int left = (lignes * (y)) + (x - 1);
  254.                         int right = (lignes * (y)) + (x + 1);
  255.                         int up = (lignes * (y - 1)) + (x);
  256.                         int upleft = (lignes * (y - 1)) + (x - 1);
  257.                         int upright = (lignes * (y - 1)) + (x + 1);
  258.                         int down = (lignes * (y + 1)) + (x);
  259.                         int downleft = (lignes * (y + 1)) + (x - 1);
  260.                         int downright = (lignes * (y + 1)) + (x + 1);
  261.  
  262.                         clickdone[cur] = true;
  263.                         boutons[cur].setEnabled(false);
  264.                         if (nombres[cur] == 0 && bombes[cur] == false && echec == false && echec == false) {
  265.                             if (u && victoire == false) {
  266.                                 if (clickdone[up] == false
  267.                                         && bombes[up] == false) {
  268.                                     clickdone[up] = true;
  269.                                     boutons[up].doClick();
  270.                                 }
  271.                                 if (l && victoire == false) {
  272.                                     if (clickdone[upleft] == false
  273.                                             && nombres[upleft] != 0
  274.                                             && bombes[upleft] == false) {
  275.                                         clickdone[upleft] = true;
  276.                                         boutons[upleft].doClick();
  277.                                     }
  278.                                 }
  279.                                 if (r && victoire == false) {
  280.                                     if (clickdone[upright] == false && nombres[upright] != 0 && bombes[upright] == false) {
  281.                                         clickdone[upright] = true;
  282.                                         boutons[upright].doClick();
  283.                                     }
  284.                                 }
  285.                             }
  286.                             if (d && victoire == false) {
  287.                                 if (clickdone[down] == false && bombes[down] == false) {
  288.                                     clickdone[down] = true;
  289.                                     boutons[down].doClick();
  290.                                 }
  291.                                 if (l && victoire == false) {
  292.                                     if (clickdone[downleft] == false && nombres[downleft] != 0 && bombes[downleft] == false) {
  293.                                         clickdone[downleft] = true;
  294.                                         boutons[downleft].doClick();
  295.                                     }
  296.                                 }
  297.                                 if (r && victoire == false) {
  298.                                     if (clickdone[downright] == false && nombres[downright] != 0 && bombes[downright] == false) {
  299.                                         clickdone[downright] = true;
  300.                                         boutons[downright].doClick();
  301.                                     }
  302.                                 }
  303.                             }
  304.                             if (l && victoire == false) {
  305.                                 if (clickdone[left] == false && bombes[left] == false) {
  306.                                     clickdone[left] = true;
  307.                                     boutons[left].doClick();
  308.                                 }
  309.                             }
  310.                             if (r && victoire == false) {
  311.                                 if (clickdone[right] == false && bombes[right] == false) {
  312.                                     clickdone[right] = true;
  313.                                     boutons[right].doClick();
  314.                                 }
  315.                             }
  316.                         } else {
  317.                             boutons[cur].setText("" + nombres[cur]);
  318.                             if (bombes[cur] == false && nombres[cur] == 0) {
  319.                                 boutons[cur].setText("");
  320.                             }
  321.                         }
  322.                         if (bombes[cur] && victoire == false) {
  323.                             boutons[cur].setText("x");
  324.  
  325.                             // En cas de perte
  326.  
  327.                             if (echec == false && victoire == false) {
  328.                                 echec = true;
  329.                                 timer = false;
  330.                                 for (int i = 0; i < lignes * colonnes; i++) {
  331.                                     if (clickdone[i] == false) {
  332.                                         boutons[i].doClick(0);
  333.                                     }
  334.                                 }
  335.                                 JOptionPane.showMessageDialog(null, "Partie perdue", "Echec !", JOptionPane.ERROR_MESSAGE);
  336.                                 this.dispose();
  337.                                 new terrain();
  338.                             }
  339.                         }
  340.  
  341.                         // Fin de la Verification du terrain
  342.  
  343.                     }
  344.                 }
  345.             }
  346.         }
  347.  
  348.         // Check de la Victoire
  349.  
  350.         for (int x = 0; x < lignes; x++) {
  351.             for (int y = 0; y < colonnes; y++) {
  352.                 int cur = (lignes * y) + x;
  353.                 if (clickdone[cur] == false) {
  354.                     if (bombes[cur]) {
  355.                         continue;
  356.                     } else {
  357.                         return;
  358.                     }
  359.                 }
  360.             }
  361.         }
  362.  
  363.         // En cas de victoire
  364.  
  365.         if (echec == false) {
  366.             victoire = true;
  367.             timer = false;
  368.             JOptionPane.showMessageDialog(null, "Partie gagnée", "Victoire !", JOptionPane.INFORMATION_MESSAGE);
  369.             this.dispose();
  370.             new terrain();
  371.         }
  372.  
  373.     }
  374.  
  375.     public void mouseEntered(MouseEvent e) {
  376.  
  377.     }
  378.  
  379.     public void mouseExited(MouseEvent e) {
  380.  
  381.     }
  382.  
  383.     public void mousePressed(MouseEvent e) {
  384.  
  385.     }
  386.  
  387.     public void mouseReleased(MouseEvent e) {
  388.  
  389.     }
  390.  
  391.     public void mouseClicked(MouseEvent e) {
  392.  
  393.         timer = true; // Démarre le Timer
  394.  
  395.     }
  396.  
  397. }
  398.  
  399. //Deux méthodes du MineSweeper de SciWizEH (Ce code reprend son algorithme)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement