Advertisement
Guest User

Java

a guest
Nov 23rd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1. package com.isecurityportal.hangman3d;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import java.util.Arrays;
  15.  
  16.  
  17. public class MultiPlayerGame extends ActionBarActivity {
  18.  
  19.     String myword = "word";
  20.  
  21.     int failcounter=0;
  22.     int guesswords=0;
  23.     int points;
  24.  
  25.     int runtest=0;
  26.  
  27.     String alreadyadded="";
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_game_multiplayer);
  33.         //randomword();
  34.         String mystring = getIntent().getStringExtra("THE_TEXT");
  35.         createlayout(mystring);
  36.         myword = mystring.toUpperCase();
  37.  
  38.     }
  39.  
  40.     //create layout
  41.     public void createlayout(String word){
  42.         LinearLayout mylayout1 = (LinearLayout) findViewById(R.id.correctshow);
  43.         for (int i=0;i<word.length();i++){
  44.             TextView newtextview = (TextView) getLayoutInflater().inflate(R.layout.mytextview,null);
  45.             mylayout1.addView(newtextview);
  46.         }
  47.  
  48.     }
  49.  
  50.  
  51.     // click button method
  52.     public void checkletterfun(View v) {
  53.         EditText myinput = (EditText) findViewById(R.id.inputletter);
  54.         String letter = myinput.getText().toString();
  55.         TextView wrongshow = (TextView) findViewById(R.id.wrongtextdsp);
  56.         ImageView mainimage = (ImageView) findViewById(R.id.mainimage);
  57.  
  58.         if (letter.length() > 0) {
  59.             //check already matched
  60.  
  61.  
  62.             if(alreadyadded.length()>1) {
  63.                 //create array
  64.                 String[] mytypecorrectwords = alreadyadded.split(",");
  65.                 if(Arrays.asList(mytypecorrectwords).contains(letter.toUpperCase())){
  66.                     Log.d("Return", "FunctionRturn");
  67.                     return;
  68.                 }
  69.             }
  70.  
  71.  
  72.             //only if true
  73.             if (testletter(letter.toUpperCase())) {
  74.                 //empty string
  75.                 myinput.setText("");
  76.                 guesswords=guesswords+runtest;
  77.  
  78.                 //add words to string
  79.                 alreadyadded=alreadyadded+","+letter.toUpperCase();
  80.  
  81.                 //game finished
  82.                 if(guesswords==myword.length()){
  83.                     //TODO new screen and game ending
  84.                    finish();
  85.                   //  randomword();
  86.                 }
  87.  
  88.             } else {
  89.                 if(!testletter_wrong(wrongshow.getText().toString(), myinput.getText().toString())) {
  90.  
  91.  
  92.                     switch(failcounter){
  93.                         case 0:
  94.                             mainimage.setBackgroundResource(R.drawable.hangdroid_1);
  95.                             failcounter++;
  96.                         break;
  97.  
  98.                         case 1:
  99.                             mainimage.setBackgroundResource(R.drawable.hangdroid_2);
  100.                             failcounter++;
  101.                         break;
  102.  
  103.                         case 2:
  104.                             mainimage.setBackgroundResource(R.drawable.hangdroid_3);
  105.                             failcounter++;
  106.                         break;
  107.  
  108.                         case 3:
  109.                             mainimage.setBackgroundResource(R.drawable.hangdroid_4);
  110.                             failcounter++;
  111.                         break;
  112.  
  113.                         case 4:
  114.                             mainimage.setBackgroundResource(R.drawable.hangdroid_5);
  115.                             failcounter++;
  116.                         break;
  117.  
  118.                         case 5:
  119.                             //TODO game over multiplayer
  120.                             //game over screen
  121.                             Intent gamoverintent= new Intent(this,GameOver.class);
  122.                             gamoverintent.putExtra("SCORE_VALUE",points);
  123.                             startActivity(gamoverintent);
  124.                         break;
  125.  
  126.                     }
  127.                     if(failcounter!=5) {
  128.                         //add to show wrong guess
  129.                         String addtowrongstring = wrongshow.getText().toString() + myinput.getText().toString();
  130.                         wrongshow.setText(addtowrongstring);
  131.                     }
  132.  
  133.                 }
  134.               //  Toast.makeText(this, "wrong", Toast.LENGTH_SHORT).show();
  135.                 myinput.setText("");
  136.             }
  137.  
  138.         } else {
  139.             Toast.makeText(this, "Please introduce a letter", Toast.LENGTH_SHORT).show();
  140.         }
  141.     }
  142.  
  143. //game end
  144.     public void clearscreen(){
  145.         TextView wrongshow = (TextView) findViewById(R.id.wrongtextdsp);
  146.         wrongshow.setText("");
  147.         guesswords=0;
  148.         failcounter=0;
  149.         ImageView mainimage = (ImageView) findViewById(R.id.mainimage);
  150.         mainimage.setBackgroundResource(R.drawable.hangdroid_0);
  151.         LinearLayout mylayoutnew = (LinearLayout) findViewById(R.id.correctshow);
  152.         for (int i=0;i<mylayoutnew.getChildCount();i++){
  153.             TextView mytext = (TextView) mylayoutnew.getChildAt(i);
  154.             mytext.setText("");
  155.  
  156.         }
  157.  
  158.     }
  159.  
  160.  
  161.     //test the letter matched
  162.     public boolean testletter(String s) {
  163.         char char1 = s.charAt(0);
  164.         runtest=0;
  165.         for (int i = 0; i < myword.length(); i++) {
  166.             char char2 = myword.charAt(i);
  167.             if (char1 == char2) {
  168.                 //show the letter
  169.                 showletter(i, char1);
  170.                 runtest++;
  171.             }
  172.         }
  173.         if(runtest!=0){
  174.             return true;
  175.         }
  176.         else
  177.         {
  178.             return false;
  179.         }
  180.     }
  181.  
  182.  
  183.     //show the correct letter
  184.     public void showletter(int postition, char string) {
  185.         LinearLayout mylayoutfound = (LinearLayout) findViewById(R.id.correctshow);
  186.         TextView mytextView = (TextView) mylayoutfound.getChildAt(postition);
  187.         mytextView.setText(Character.toString(string));
  188.     }
  189.  
  190.  
  191.     //test the letter matched in already wrong
  192.     public boolean testletter_wrong(String fulltext, String s) {
  193.         char char1 = s.charAt(0);
  194.         for (int i = 0; i < fulltext.length(); i++) {
  195.             char char2 = fulltext.charAt(i);
  196.             if (char1 == char2) {
  197.                 //show the letter
  198.                 return true;
  199.             }
  200.  
  201.         }
  202.         return false;
  203.  
  204.     }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement