Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.66 KB | None | 0 0
  1. package com.example.mathias.pendumtc;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.Button;
  11. import android.widget.GridLayout;
  12. import android.widget.ImageView;
  13. import android.widget.LinearLayout;
  14. import android.widget.ListView;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import java.util.ArrayList;
  19.  
  20.  
  21. public class Jeu extends ActionBarActivity {
  22.  
  23.     Intent i = getIntent();
  24.  
  25.     private ArrayAdapter<String> itemsAdapter;
  26.     private ArrayList<String> items;
  27.     private ListView lvItems;
  28.     private int vie = 10;
  29.     private String motATrouver = choixMot();
  30.     private String motDevine;                          //Mot devine = la première lettre du mot à trouver
  31.     public int nombreWin = 0;
  32.     public int nombreLose = 0;
  33.     private String[] listeFacile= {"attention", "camarade", "copain", "coquin","travail",
  34.             "directeur", "directrice", "droit", "effort","enfant",
  35.             "fatigue", "faute", "fille","gardien", "madame",
  36.             "mensonge", "ordre", "personne", "retard", "sourire"};
  37.     private String[] listeMoyen= {};
  38.     private String[] listeDifficile= {};
  39.     private String[] listeHardcore= {"molletisme", "hemorroisse", "gimblette", "enchytrisme", "celicole",
  40.             "anastylose", "singultueux", "psychomachie", "uchronie", "taphonique",
  41.             "tartesien", "zigotte", "parenetique", "lossif", "hypotaxe",
  42.             "escamper", "esquinancie", "epithanie", "chirophanie", "caudines"};
  43.  
  44.  
  45.     int Max = 25; //Nombre de mots -1
  46.     //Intent i = getIntent();
  47.  
  48.  
  49.     @Override
  50.     protected void onCreate(Bundle savedInstanceState) {
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.activity_jeu);
  53.         //Affichage de la Liste des Lettres Fausses
  54.         lvItems = (ListView) findViewById(R.id.ListeFaux);
  55.         items = new ArrayList<String>();
  56.         itemsAdapter = new ArrayAdapter<String>(this,
  57.                 android.R.layout.simple_list_item_1, items);
  58.         lvItems.setAdapter(itemsAdapter);
  59.         //Affichage de la 1ère lettre
  60.         TextView motTrouve = (TextView) findViewById(R.id.TMotATrouver);        //Champ où s'affiche le mot trouvé
  61.         int compteur = 0;
  62.         motDevine = motATrouver.substring(0,1);
  63.         while (compteur < motATrouver.length()-1) {                             //Met des "_" pour chaque lettre non trouvée
  64.             motDevine = motDevine + "-";
  65.             compteur = compteur + 1;
  66.         }
  67.         motTrouve.setText(motDevine);                                           //On affiche la première lettre du mot à trouver
  68.         //Affichage du pendu
  69.         ImageView imagePendu = (ImageView) findViewById(R.id.ImagePendu);
  70.         imagePendu.setImageResource(R.drawable.zero);
  71.     }
  72.  
  73.     public String choixMot () {
  74.         String mot = "anticonstitutionnellement";
  75.         int alea = (int) (Math.random() % Max);
  76.         String difficulte = null;
  77.         if (i != null) {difficulte = i.getStringExtra("Difficulté");
  78.         }
  79.         if (difficulte!= null) {
  80.             switch (difficulte) {
  81.                 case "Facile":
  82.                     mot = listeFacile[alea];
  83.                     break;
  84.                 case "Moyen":
  85.                     mot = listeMoyen[alea];
  86.                     break;
  87.                 case "Difficile":
  88.                     mot = listeDifficile[alea];
  89.                     break;
  90.                 case "Hardcore":
  91.                     mot = listeHardcore[alea];
  92.                     break;
  93.             }
  94.         }
  95.         return mot;
  96.     }
  97.  
  98.     public void retourAccueil (View v) {
  99.         Intent i = new Intent(this, Accueil.class);
  100.         i.putExtra("Win", nombreWin);
  101.         i.putExtra("Lose", nombreLose);
  102.         finish();
  103.         startActivity(i);
  104.     }
  105.  
  106.     public void rejouer (View v) {      //Action du bouton rejouer
  107.         Intent i = getIntent();
  108.         finish();
  109.         startActivity(i);
  110.     }
  111.  
  112.     public void ajoutLettre (View v) {
  113.         int compteur = 0;
  114.         TextView motTrouve = (TextView) findViewById(R.id.TMotATrouver);        //Champ où s'affiche le mot trouvé
  115.         String lettreChoisie = "Erreur";                                                   //On initialise lettreChoisie
  116.         ImageView imagePendu = (ImageView) findViewById(R.id.ImagePendu);
  117.         Button bouton;
  118.  
  119.         switch (v.getId()) {
  120.             case R.id.a:
  121.                 lettreChoisie = "a";
  122.                 bouton =(Button) this.findViewById(R.id.a);
  123.                 bouton.setVisibility(View.GONE);
  124.                 break;
  125.             case R.id.b:
  126.                 lettreChoisie = "b";
  127.                 bouton =(Button) this.findViewById(R.id.b);
  128.                 bouton.setVisibility(View.GONE);
  129.                 break;
  130.             case R.id.c:
  131.                 lettreChoisie = "c";
  132.                 bouton =(Button) this.findViewById(R.id.c);
  133.                 bouton.setVisibility(View.GONE);
  134.                 break;
  135.             case R.id.d:
  136.                 lettreChoisie = "d";
  137.                 bouton =(Button) this.findViewById(R.id.d);
  138.                 bouton.setVisibility(View.GONE);
  139.                 break;
  140.             case R.id.e:
  141.                 lettreChoisie = "e";
  142.                 bouton =(Button) this.findViewById(R.id.e);
  143.                 bouton.setVisibility(View.GONE);
  144.                 break;
  145.             case R.id.f:
  146.                 lettreChoisie = "f";
  147.                 bouton =(Button) this.findViewById(R.id.f);
  148.                 bouton.setVisibility(View.GONE);
  149.                 break;
  150.             case R.id.g:
  151.                 lettreChoisie = "g";
  152.                 bouton =(Button) this.findViewById(R.id.g);
  153.                 bouton.setVisibility(View.GONE);
  154.                 break;
  155.             case R.id.h:
  156.                 lettreChoisie = "h";
  157.                 bouton =(Button) this.findViewById(R.id.h);
  158.                 bouton.setVisibility(View.GONE);
  159.                 break;
  160.             case R.id.i:
  161.                 lettreChoisie = "i";
  162.                 bouton =(Button) this.findViewById(R.id.i);
  163.                 bouton.setVisibility(View.GONE);
  164.                 break;
  165.             case R.id.j:
  166.                 lettreChoisie = "j";
  167.                 bouton =(Button) this.findViewById(R.id.j);
  168.                 bouton.setVisibility(View.GONE);
  169.                 break;
  170.             case R.id.k:
  171.                 lettreChoisie = "k";
  172.                 bouton =(Button) this.findViewById(R.id.k);
  173.                 bouton.setVisibility(View.GONE);
  174.                 break;
  175.             case R.id.l:
  176.                 lettreChoisie = "l";
  177.                 bouton =(Button) this.findViewById(R.id.l);
  178.                 bouton.setVisibility(View.GONE);
  179.                 break;
  180.             case R.id.m:
  181.                 lettreChoisie = "m";
  182.                 bouton =(Button) this.findViewById(R.id.m);
  183.                 bouton.setVisibility(View.GONE);
  184.                 break;
  185.             case R.id.n:
  186.                 lettreChoisie = "n";
  187.                 bouton =(Button) this.findViewById(R.id.n);
  188.                 bouton.setVisibility(View.GONE);
  189.                 break;
  190.             case R.id.o:
  191.                 lettreChoisie = "o";
  192.                 bouton =(Button) this.findViewById(R.id.o);
  193.                 bouton.setVisibility(View.GONE);
  194.                 break;
  195.             case R.id.p:
  196.                 lettreChoisie = "p";
  197.                 bouton =(Button) this.findViewById(R.id.p);
  198.                 bouton.setVisibility(View.GONE);
  199.                 break;
  200.             case R.id.q:
  201.                 lettreChoisie = "q";
  202.                 bouton =(Button) this.findViewById(R.id.q);
  203.                 bouton.setVisibility(View.GONE);
  204.                 break;
  205.             case R.id.r:
  206.                 lettreChoisie = "r";
  207.                 bouton =(Button) this.findViewById(R.id.r);
  208.                 bouton.setVisibility(View.GONE);
  209.                 break;
  210.             case R.id.s:
  211.                 lettreChoisie = "s";
  212.                 bouton =(Button) this.findViewById(R.id.s);
  213.                 bouton.setVisibility(View.GONE);
  214.                 break;
  215.             case R.id.t:
  216.                 lettreChoisie = "t";
  217.                 bouton =(Button) this.findViewById(R.id.t);
  218.                 bouton.setVisibility(View.GONE);
  219.                 break;
  220.             case R.id.u:
  221.                 lettreChoisie = "u";
  222.                 bouton =(Button) this.findViewById(R.id.u);
  223.                 bouton.setVisibility(View.GONE);
  224.                 break;
  225.             case R.id.v:
  226.                 lettreChoisie = "v";
  227.                 bouton =(Button) this.findViewById(R.id.v);
  228.                 bouton.setVisibility(View.GONE);
  229.                 break;
  230.             case R.id.w:
  231.                 lettreChoisie = "w";
  232.                 bouton =(Button) this.findViewById(R.id.w);
  233.                 bouton.setVisibility(View.GONE);
  234.                 break;
  235.             case R.id.x:
  236.                 lettreChoisie = "x";
  237.                 bouton =(Button) this.findViewById(R.id.x);
  238.                 bouton.setVisibility(View.GONE);
  239.                 break;
  240.             case R.id.y:
  241.                 lettreChoisie = "y";
  242.                 bouton =(Button) this.findViewById(R.id.y);
  243.                 bouton.setVisibility(View.GONE);
  244.                 break;
  245.             case R.id.z:
  246.                 lettreChoisie = "z";
  247.                 bouton =(Button) this.findViewById(R.id.z);
  248.                 bouton.setVisibility(View.GONE);
  249.                 break;
  250.         }
  251.  
  252.         compteur = 0;                                                           //On réinitialise compteur
  253.  
  254.         if (!(lettreChoisie.isEmpty())) {
  255.             if (lettreChoisie.length() > 1) {
  256.                 Toast.makeText(getApplicationContext(), "Erreur : UNE LETTRE, UNE SEULE",
  257.                         Toast.LENGTH_SHORT).show();
  258.             } else {
  259.                 if (lettreExiste(lettreChoisie, motATrouver)) {
  260.                     while (compteur < motATrouver.length()) {
  261.                         if (motATrouver.substring(compteur, compteur + 1).equals(lettreChoisie)) {
  262.                             motDevine = motDevine.substring(0, compteur) + lettreChoisie + motDevine.substring(compteur + 1);
  263.                         }
  264.                         compteur = compteur + 1;
  265.                     }
  266.                     motTrouve.setText(motDevine);
  267.                 } else {
  268.                     int lettreFausse = itemsAdapter.getPosition(lettreChoisie);
  269.                     if (lettreFausse == -1) {
  270.                         itemsAdapter.add(lettreChoisie);
  271.                         vie = vie - 1;
  272.                         switch (vie) {
  273.                             case 9:
  274.                                 imagePendu.setImageResource(R.drawable.un);
  275.                                 break;
  276.                             case 8:
  277.                                 imagePendu.setImageResource(R.drawable.deux);
  278.                                 break;
  279.                             case 7:
  280.                                 imagePendu.setImageResource(R.drawable.trois);
  281.                                 break;
  282.                             case 6:
  283.                                 imagePendu.setImageResource(R.drawable.quatre);
  284.                                 break;
  285.                             case 5:
  286.                                 imagePendu.setImageResource(R.drawable.cinq);
  287.                                 break;
  288.                             case 4:
  289.                                 imagePendu.setImageResource(R.drawable.six);
  290.                                 break;
  291.                             case 3:
  292.                                 imagePendu.setImageResource(R.drawable.sept);
  293.                                 break;
  294.                             case 2:
  295.                                 imagePendu.setImageResource(R.drawable.huit);
  296.                                 break;
  297.                             case 1:
  298.                                 imagePendu.setImageResource(R.drawable.neuf);
  299.                                 break;
  300.                         }
  301.  
  302.                     } else {
  303.                         Toast.makeText(getApplicationContext(), "Erreur : T'as déjà choisie cette lettre ! Regarde la liste !",
  304.                                 Toast.LENGTH_SHORT).show();
  305.                     }
  306.                 }
  307.             }
  308.         }else {
  309.             Toast.makeText(getApplicationContext(), "Erreur : vous n'avez pas saisi de lettre",
  310.                     Toast.LENGTH_SHORT).show();
  311.         }
  312.         if (vie == 1) {
  313.             Toast.makeText(getApplicationContext(), "PERDU ! Le mot était " + motATrouver,
  314.                     Toast.LENGTH_LONG).show();
  315.             LinearLayout layout = (LinearLayout) findViewById(R.id.BoutonsFinDePartie);
  316.             layout.setVisibility(View.VISIBLE);
  317.             GridLayout alphabet = (GridLayout) findViewById(R.id.Alphabet);
  318.             alphabet.setVisibility(View.GONE);
  319.             nombreLose = nombreLose + 1;
  320.         }
  321.         if (motDevine.equals(motATrouver)) {
  322.             Toast.makeText(getApplicationContext(), "GAGNE ! Allez-là !",
  323.                     Toast.LENGTH_LONG).show();
  324.             LinearLayout layout = (LinearLayout) findViewById(R.id.BoutonsFinDePartie);
  325.             layout.setVisibility(View.VISIBLE);
  326.             GridLayout alphabet = (GridLayout) findViewById(R.id.Alphabet);
  327.             alphabet.setVisibility(View.GONE);
  328.             nombreWin = nombreWin +1;
  329.         }
  330.     }
  331.  
  332.     /*Renvoie 1 si la lettre rentrée par l'utilisateur est contenue dans le mot à trouver, 0 sinon
  333.     * et stock l'emplacement de l'apparition de la lettre dans placeLettre*/
  334.     public boolean lettreExiste (String LettreRentree, String MotATrouver ) {
  335.         if (MotATrouver.contains(LettreRentree)) {
  336.             return true;
  337.         }else {
  338.             return false;
  339.         }
  340.  
  341.     }
  342.  
  343.  
  344.     @Override
  345.     public boolean onCreateOptionsMenu(Menu menu) {
  346.         // Inflate the menu; this adds items to the action bar if it is present.
  347.         getMenuInflater().inflate(R.menu.menu_jeu, menu);
  348.         return true;
  349.     }
  350.  
  351.     @Override
  352.     public boolean onOptionsItemSelected(MenuItem item) {
  353.         // Handle action bar item clicks here. The action bar will
  354.         // automatically handle clicks on the Home/Up button, so long
  355.         // as you specify a parent activity in AndroidManifest.xml.
  356.         int id = item.getItemId();
  357.  
  358.         //noinspection SimplifiableIfStatement
  359.         if (id == R.id.action_settings) {
  360.             return true;
  361.         }
  362.  
  363.         return super.onOptionsItemSelected(item);
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement