Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.isecurityportal.hangman3d;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.Arrays;
- public class MultiPlayerGame extends ActionBarActivity {
- String myword = "word";
- int failcounter=0;
- int guesswords=0;
- int points;
- int runtest=0;
- String alreadyadded="";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_game_multiplayer);
- //randomword();
- String mystring = getIntent().getStringExtra("THE_TEXT");
- createlayout(mystring);
- myword = mystring.toUpperCase();
- }
- //create layout
- public void createlayout(String word){
- LinearLayout mylayout1 = (LinearLayout) findViewById(R.id.correctshow);
- for (int i=0;i<word.length();i++){
- TextView newtextview = (TextView) getLayoutInflater().inflate(R.layout.mytextview,null);
- mylayout1.addView(newtextview);
- }
- }
- // click button method
- public void checkletterfun(View v) {
- EditText myinput = (EditText) findViewById(R.id.inputletter);
- String letter = myinput.getText().toString();
- TextView wrongshow = (TextView) findViewById(R.id.wrongtextdsp);
- ImageView mainimage = (ImageView) findViewById(R.id.mainimage);
- if (letter.length() > 0) {
- //check already matched
- if(alreadyadded.length()>1) {
- //create array
- String[] mytypecorrectwords = alreadyadded.split(",");
- if(Arrays.asList(mytypecorrectwords).contains(letter.toUpperCase())){
- Log.d("Return", "FunctionRturn");
- return;
- }
- }
- //only if true
- if (testletter(letter.toUpperCase())) {
- //empty string
- myinput.setText("");
- guesswords=guesswords+runtest;
- //add words to string
- alreadyadded=alreadyadded+","+letter.toUpperCase();
- //game finished
- if(guesswords==myword.length()){
- //TODO new screen and game ending
- finish();
- // randomword();
- }
- } else {
- if(!testletter_wrong(wrongshow.getText().toString(), myinput.getText().toString())) {
- switch(failcounter){
- case 0:
- mainimage.setBackgroundResource(R.drawable.hangdroid_1);
- failcounter++;
- break;
- case 1:
- mainimage.setBackgroundResource(R.drawable.hangdroid_2);
- failcounter++;
- break;
- case 2:
- mainimage.setBackgroundResource(R.drawable.hangdroid_3);
- failcounter++;
- break;
- case 3:
- mainimage.setBackgroundResource(R.drawable.hangdroid_4);
- failcounter++;
- break;
- case 4:
- mainimage.setBackgroundResource(R.drawable.hangdroid_5);
- failcounter++;
- break;
- case 5:
- //TODO game over multiplayer
- //game over screen
- Intent gamoverintent= new Intent(this,GameOver.class);
- gamoverintent.putExtra("SCORE_VALUE",points);
- startActivity(gamoverintent);
- break;
- }
- if(failcounter!=5) {
- //add to show wrong guess
- String addtowrongstring = wrongshow.getText().toString() + myinput.getText().toString();
- wrongshow.setText(addtowrongstring);
- }
- }
- // Toast.makeText(this, "wrong", Toast.LENGTH_SHORT).show();
- myinput.setText("");
- }
- } else {
- Toast.makeText(this, "Please introduce a letter", Toast.LENGTH_SHORT).show();
- }
- }
- //game end
- public void clearscreen(){
- TextView wrongshow = (TextView) findViewById(R.id.wrongtextdsp);
- wrongshow.setText("");
- guesswords=0;
- failcounter=0;
- ImageView mainimage = (ImageView) findViewById(R.id.mainimage);
- mainimage.setBackgroundResource(R.drawable.hangdroid_0);
- LinearLayout mylayoutnew = (LinearLayout) findViewById(R.id.correctshow);
- for (int i=0;i<mylayoutnew.getChildCount();i++){
- TextView mytext = (TextView) mylayoutnew.getChildAt(i);
- mytext.setText("");
- }
- }
- //test the letter matched
- public boolean testletter(String s) {
- char char1 = s.charAt(0);
- runtest=0;
- for (int i = 0; i < myword.length(); i++) {
- char char2 = myword.charAt(i);
- if (char1 == char2) {
- //show the letter
- showletter(i, char1);
- runtest++;
- }
- }
- if(runtest!=0){
- return true;
- }
- else
- {
- return false;
- }
- }
- //show the correct letter
- public void showletter(int postition, char string) {
- LinearLayout mylayoutfound = (LinearLayout) findViewById(R.id.correctshow);
- TextView mytextView = (TextView) mylayoutfound.getChildAt(postition);
- mytextView.setText(Character.toString(string));
- }
- //test the letter matched in already wrong
- public boolean testletter_wrong(String fulltext, String s) {
- char char1 = s.charAt(0);
- for (int i = 0; i < fulltext.length(); i++) {
- char char2 = fulltext.charAt(i);
- if (char1 == char2) {
- //show the letter
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement