SuperMonkey

Contests for dummies

Oct 21st, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. class Personne {
  4.     int age;
  5.     String nom, prenom;
  6.     int nbQ2;
  7.    
  8.     public ArrayList<Concours> succes;
  9.     public ArrayList<Concours> defaites;
  10.    
  11.     private static String[] nn= {"Lina", "Inaya", "Yasmine", "Maïssa", "Maya", "Sofia", "Kenza", "Sara", "Aya", "Assia", "Lila", "Nour", "Marwa", "Selma", "Nora", "Sirine", "Leïla", "Amina", "Farah", "Salma", "Adam", "Mohamed", "Rayan", "Naël", "Ilyes", "Soan", "Amine", "Kenzo", "Kaïs", "Noam", "Mehdi", "Ayoub", "Younès", "Wassim", "Amir", "Sohan", "Ibrahim", "Youssef", "Naïm", "Yassine"};
  12.     private static String[] nm= {"Smith", "Backer", "Di Caprio", "Burns", "Brown", "Simpson", "Electra", "Morgan", "De Buaala", "Robinsky", "De gaulle", "Obama", "De borbon y borbon", "Sanchez", "Mosby", "Scherbatsky", "Aldrins", "Eriksen", "Stinson"};
  13.    
  14.     public Personne(String n, String p) {
  15.         nom = n;
  16.         prenom = p;
  17.         age = (int) (Math.random()*91)+8;
  18.        
  19.         succes = new ArrayList<Concours>();
  20.         defaites = new ArrayList<Concours>();
  21.     }
  22.    
  23.     public Personne() {
  24.         this(nm[(int)(Math.random()*nm.length)] , nn[(int)(Math.random()*nn.length)]);
  25.     }
  26.    
  27.     public void addDefaite(Concours c) {
  28.         defaites.add(c);
  29.     }
  30.    
  31.     public void addSucces(Concours c) {
  32.         succes.add(c);
  33.     }
  34.    
  35.     public String toString() {
  36.         return prenom + " " + nom.toUpperCase() + "("+age+" ans)";
  37.     }
  38.    
  39.     public void setQ2(int q) {
  40.         nbQ2 = q;
  41.     }
  42.    
  43.     public int getQ2() {
  44.         return nbQ2;
  45.     }
  46.    
  47.     public int nbVict() {
  48.         return succes.size();
  49.     }
  50. }
  51.  
  52. class Concours {
  53.     private int qId;
  54.     private Personne[] assemblee;
  55.     private ArrayList<Personne> gagnants;
  56.    
  57.     double diff;
  58.    
  59.     public static final int NB_PARTICIPANTS = 20;
  60.    
  61.     public static final String[] questions = {"Quel est le sens de la vie?", "Comment se danse la macarena?", "De quel couleur est le cheval violet d'Henri IV?", "Qui est le président d'Angleterre?", "2x7 - 7 = ?", "Comment fait on les bébés?", "Qu'est-ce qui est mieux : Granola ou BN?", "Quel animal figure sur les paquets de céréales Lion?", "Apple est il l'incarnation de Satan?", "Quel est le site internet le plus visité au monde?", "C++, Java, ou Python?"};
  62.     public static final String[][] reponses  = {{"42", "Obiwan Kenobi", "Les femmes", "La réponse D"},
  63.                             {"Avec émotion", "Tout nu", "En étant manchot", "Ce n'est pas une danse"},
  64.                             {"Violet", "Rose", "Mort", "Arc en ciel"},
  65.                             {"Il n'y en a pas", "Marie Antoinette, reine d'autriche", "Joséphine ange gardien", "George W. Hollande"},
  66.                             {"7", "42", "0", "12"},
  67.                             {"Avec une éprouvette", "En dansant la macarena", "sudo mkbébé", "A l'aide de logarithmes népériens"},
  68.                             {"Granola", "BN", "Pepito", "J'aime pas les gateaux"},
  69.                             {"Un lion", "Un enfant", "Un oiseau", "Une chèvre"},
  70.                             {"Oui, assurément", "C'est pas impossible", "C'est envisageable", "Non, j'aime me faire pigeonner"},
  71.                             {"Google.com", "xkcd.com", "lesjoiesducode.fr", "commitstrip.com"},
  72.                             {"C++", "Java", "Python", "Assembleur, what else?"}
  73.                            };
  74.    
  75.     public Concours(Personne[] p) {
  76.         qId = (int)(Math.random()*questions.length);
  77.        
  78.         assemblee = p;
  79.        
  80.         diff = Math.random();
  81.     }
  82.    
  83.     public void faireQA() {
  84.         System.out.println("\nQuestion du concours : " + questions[qId]);
  85.         ArrayList<Personne> g = new ArrayList<Personne>();
  86.         for(Personne p : assemblee) {
  87.             if(Math.random() > diff) { // reponse OK
  88.                 g.add(p);
  89.                 p.setQ2((int) (Math.random()*NB_PARTICIPANTS - 1) +1);
  90.             }
  91.            
  92.             else { // mauvaise réponse
  93.                 System.out.println(p+ " est éliminé après avoir répondu : " + reponses[qId][(int) (Math.random()*3)+1]);
  94.                 p.addDefaite(this);
  95.             }  
  96.         }
  97.        
  98.         int nbG = g.size(), min = NB_PARTICIPANTS;
  99.         StringBuilder sb = new StringBuilder("\nPersonnes ayant bien répondu : ");
  100.         for(Personne p : g) {
  101.             sb.append(p + ", ");
  102.             if(Math.abs(p.getQ2() - nbG) < min) {
  103.                 min = Math.abs(p.getQ2() - nbG);
  104.             }
  105.         }
  106.         System.out.println(sb);
  107.        
  108.         ArrayList<Personne> pure_winners = new ArrayList<Personne>();
  109.        
  110.         for(Personne p : g) {
  111.             if(Math.abs(p.getQ2() - nbG) == min) {
  112.                 p.addSucces(this);
  113.                 pure_winners.add(p);
  114.             } else {
  115.                 p.addDefaite(this);
  116.             }
  117.         }
  118.        
  119.         gagnants = pure_winners;
  120.         if(g.size() == 0){
  121.             System.out.println("\nQuel dommage, il n'y a aucun gagnant!");
  122.         } else {
  123.             sb = new StringBuilder("\nListe des gagnants : ");
  124.             for(Personne p : pure_winners) {
  125.                 sb.append(p + ", ");
  126.             }
  127.             System.out.println(sb);
  128.         }
  129.         System.out.println("_____________________________________________________________________________");
  130.     }
  131. }
  132.  
  133. public class TestConcours {
  134.     public static void main(String[] args) {
  135.         Personne[] assemblee = new Personne[Concours.NB_PARTICIPANTS];
  136.         for(int i = 0; i < assemblee.length; i++) {
  137.             assemblee[i] = new Personne();
  138.         }
  139.        
  140.         Concours[] cs = new Concours[10];
  141.         for(int i=0; i < cs.length; i++) {
  142.             cs[i] = new Concours(assemblee);
  143.             cs[i].faireQA();
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment