मैं एक जल्लाद खेल पर काम कर रहा हूँ। गेम काम कर रहा है, सिवाय इसके कि जब मैं एक नया गेम शुरू करता हूं, तो मैं पिछले गेम से संग्रहीत डेटा को साफ़ करने में असमर्थ हूं। तो यह मुझे एक स्क्रीन देता है जो उपयोगकर्ता द्वारा "फिर से खेलने" पर क्लिक करने के बाद निम्न जैसा दिखता है।

जल्लाद गेम का स्क्रीनशॉट - "फिर से खेलें" पर क्लिक करने के बाद यह कैसा दिखना चाहिए

"फिर से खेलें" पर क्लिक करने के बाद जल्लाद गेम का स्क्रीनशॉट

मैं डेटा को कैसे रीसेट करूं ताकि टेक्स्टव्यू और बटन startNewGame विधि के बाद निष्पादित हो जाएं?

रीसेट करने के लिए textView.setText("") का उपयोग करने का प्रयास किया लेकिन यह काम नहीं कर रहा है। सराहना करें कि क्या आप मेरे लिए कुछ प्रकाश डालने में मदद कर सकते हैं। धन्यवाद!

मेरे कोड इस प्रकार हैं।

package com.desmondwong.hangmangame;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import java.util.Random;

public class GameActivity extends AppCompatActivity {

    //To reference the components
    ImageSwitcher imageSwitcher;
    TextView textView;
    TextView textViewScore;
    Button btn [] = new Button[26];

    //Images for the hangman
    int img [] = {R.drawable.img0,
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5,
            R.drawable.img6,
            R.drawable.img7,
            R.drawable.img8};

    AlertDialog helpAlert;

    //Variables
    String strSecret = "", strGuess="", strText="";

    String strWords[] = {"APPLE", "ORANGE","BANANA"};

    int intError = 0; //Error made by player

    int livesRemaining = 8; //Lives remaining by player

    int numCorr = 0; //Correct letter guess by player

    Random random = new Random(); //Random generator


    public void startNewGame(){

        intError = 0; //Error made by player

        livesRemaining = 8; //Lives remaining by player

        numCorr = 0; //Correct letter guess by player

        imageSwitcher.removeAllViews();



        //textViewScore.setText(String.valueOf(livesRemaining));

        //textView.setText("");

        setupImageSwitcher();
        setup26Buttons();
        getSecretWord();

    }


    //To create help icon at top right
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //To create help icon at top right
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //case android.R.id.home:
               // NavUtils.navigateUpFromSameTask(this);
                //return true;
            case R.id.action_help:
                showHelp();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        //Retrieve the reference
        imageSwitcher = findViewById(R.id.imageSwitcher);
        textView = findViewById(R.id.textView);
        textViewScore = findViewById(R.id.textViewScore);
        textViewScore.setText(String.valueOf(livesRemaining));

        setupImageSwitcher();
        setup26Buttons();
        getSecretWord();

    }


    private void setup26Buttons() {

        GridLayout g = findViewById(R.id.gridLayout);


        //to create 26 buttons
        for(int i = 0; i<btn.length; i++) {

            btn[i] = new Button(this, null, R.attr.buttonStyleSmall); //Buttonsytlesmall so that it fits the screen
            btn[i].setText(""+(char)('A'+i)); //need to set back to char, as +i will set it back to integer. "" to set this to a String so it is sync to setText
            btn[i].setTag(""+(char)('A'+i));
            btn[i].setOnClickListener(new View.OnClickListener() {
                @SuppressLint("ResourceAsColor")
                @Override
                public void onClick(View v) {

                    strGuess += v.getTag(); //Get letter that the player guessed and keep adding on to strGuess
                    v.setEnabled(false); //disable pressed button since the player already press
                    v.setBackgroundColor(android.R.color.black);

                    //Check for error guess. If the letter is not inside the strSecret, it will return less than 0
                    if (strSecret.indexOf(v.getTag().toString())<0){

                        intError++; //your error is added
                        int livesRemaining = 8;
                        livesRemaining -= intError; // Countdown based on errors recorded
                        textViewScore.setText(String.valueOf(livesRemaining));
                        imageSwitcher.setImageResource(img[intError]); //set the img no. to follow the error
                    }

                    else {

                        numCorr++;
                    }

                    boolean playerWin = true;
                    //Display all correct guesses
                    strText = ""; //reset the display

                    for (int i = 0 ; i<strSecret.length();i++){

                        char ch = strSecret.charAt(i); // get each character from strSecret

                        //To check if this letter can be found in strGuess
                        if(strGuess.indexOf(ch)>=0){
                            //found
                            strText += ch;

                        }

                        else{

                            //Not found
                            strText += "-";
                            playerWin=false;
                        }

                    }

                    textView.setText(strText);

                    if (playerWin) {

                        if (numCorr == strSecret.length()) {

                            //Toast.makeText(getApplicationContext(), "You won", Toast.LENGTH_LONG).show();

                            //let user know they have won, ask if they want to play again
                            AlertDialog.Builder winBuild = new AlertDialog.Builder(GameActivity.this);
                            winBuild.setTitle("Amazing! You save Batman!");

                            winBuild.setMessage("The Hangman\'s favourite fruit is:\n\n" + strSecret);

                            winBuild.setPositiveButton("Play Again",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            GameActivity.this.startNewGame();

                                        }
                                    });

                            winBuild.setNegativeButton("Exit",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            GameActivity.this.finish();

                                        }
                                    });
                            winBuild.show();

                        }

                    } else if (livesRemaining > 0) {

                        //still have lives remaining. do nothing
                    }

                    else {

                        //Toast.makeText(getApplicationContext(), "You Lost",Toast.LENGTH_LONG).show();

                        // Display Alert Dialog
                        AlertDialog.Builder loseBuild = new AlertDialog.Builder(GameActivity.this);
                        loseBuild.setTitle("Batman got executed!");
                        loseBuild.setMessage("You lose!\n\nThe answer was:\n\n"+ strSecret);
                        loseBuild.setPositiveButton("Play Again",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        GameActivity.this.startNewGame();
                                    }});

                        loseBuild.setNegativeButton("Exit",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        GameActivity.this.finish();
                                    }});

                        loseBuild.show();

                    }

                }

            });

            g.addView(btn[i]);

        }

    }



    private void getSecretWord() {

        int index = random.nextInt(strWords.length);
        strSecret = strWords[index];

        for(int i=0; i<strSecret.length(); i++) {

            strText += "-"; //to create multiple - for the unknown word

        }

        textView.setText(strText);


    }

    private void setupImageSwitcher() {

        //https://www.tutorialspoint.com/android/android_imageswitcher.htm
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

            @Override
            public View makeView() {

                ImageView imageView = new ImageView(getApplicationContext());

                imageView.setImageResource(R.drawable.img0);

                return imageView;
            }
        });

        Animation aniOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

        Animation aniIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);

        imageSwitcher.setOutAnimation(aniOut);

        imageSwitcher.setOutAnimation(aniIn);
    }

    //show help information
    public void showHelp(){
        AlertDialog.Builder helpBuild = new AlertDialog.Builder(this);
        helpBuild.setTitle("Help");
        helpBuild.setMessage("Whisper the password (Hangman's favourite fruit) to save Batman\n\n"
                + "You only have 8 tries!");
        helpBuild.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        helpAlert.dismiss();
                    }});
        helpAlert = helpBuild.create();
        helpBuild.show();
    }

}
-3
DWing 25 मई 2018, 15:34

1 उत्तर

सबसे बढ़िया उत्तर

हर बार जब आप startNewGame को कॉल करते हैं तो आपकी समस्या होती है:

  • एक नया टेक्स्ट सेट करने के बजाय strText के मौजूदा मान में एक नया टेक्स्ट जोड़ें
  • btn सरणी में नए बटन जोड़ें (पुराने को हटाए बिना)

इसलिए आपको यह सुनिश्चित करने की ज़रूरत है कि startNewGame पिछले गेम के लिए बनाई गई हर चीज़ से छुटकारा पाएं और जिसकी अब आपको आवश्यकता नहीं है।

संपादित करें

एक दृष्टिकोण यह हो सकता है कि एक 'गेम' ऑब्जेक्ट हो जो गतिविधि में रखने के बजाय सब कुछ (strSecret, strGuess,...) संग्रहीत करता हो। हर बार जब startNewGame को कॉल किया जाता है, तो आप myGame = new Game(); करते हैं, ताकि आप सुनिश्चित हो जाएं कि पिछला सारा डेटा चला गया है।

2 संपादित करें

एक त्वरित फिक्स है:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    //Retrieve the reference
    imageSwitcher = findViewById(R.id.imageSwitcher);
    textView = findViewById(R.id.textView);
    textViewScore = findViewById(R.id.textViewScore);
    textViewScore.setText(String.valueOf(livesRemaining));


    // ** QUICK FIX **
    btn = new Button[26];
    strText = "";

    setupImageSwitcher();
    setup26Buttons();
    getSecretWord();
}

संपादित करें ३ (काम करने का बेहतर उदाहरण)

मुख्य गतिविधि

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.Random;

public class GameActivity extends AppCompatActivity {

    // Constants

    //Images for the hangman
    @DrawableRes
    private final static int img[] = {R.drawable.img0, // Img0 is when the player is dead
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5,
            R.drawable.img6,
            R.drawable.img7,
            R.drawable.img8};

    private final static String strWords[] = {"APPLE", "ORANGE", "BANANA"};
    private final static Random random = new Random(); //Random generator

    // To reference the components
    private ImageSwitcher imageSwitcher;
    private TextView textView;
    private TextView textViewScore;

    // Other private fields
    private Game mGame;

    public void startNewGame() {
        mGame = new Game(img.length - 1); // The max lives is the size of the array - 1
        setup26Buttons();
        initSecretWord();
        imageSwitcher.reset();
        imageSwitcher.setImageResource(img[mGame.livesRemaining]);
        textViewScore.setText(String.valueOf(mGame.livesRemaining));
        textView.setText(mGame.hiddenWord);
    }

    //To create help icon at top right
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //To create help icon at top right
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_help:
                showHelpDialog();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        //Retrieve the reference
        imageSwitcher = findViewById(R.id.imageSwitcher);
        textView = findViewById(R.id.textView);
        textViewScore = findViewById(R.id.textViewScore);

        // Init the view components
        initImageSwitcher();

        // Start the game
        startNewGame();
    }

    private void setup26Buttons() {

        final GridLayout g = findViewById(R.id.gridLayout);
        g.removeAllViews(); // clear the children views (if exist)

        //to create 26 buttons
        final int maxLetters = 26;
        Button btn;
        for (int i = 0; i < maxLetters; i++) {

            btn = new Button(this, null, R.attr.buttonStyleSmall); //Buttonsytlesmall so that it fits the screen
            btn.setText(Character.toString((char) ('A' + i))); // Set the letter
            btn.setOnClickListener(listener);

            g.addView(btn);
        }
    }

    private final View.OnClickListener listener = new View.OnClickListener() {
        @SuppressLint("ResourceAsColor")
        @Override
        public void onClick(View v) {

            String guess = (((Button) v).getText()).toString();
            v.setEnabled(false); //disable pressed button since the player already press
            v.setBackgroundColor(android.R.color.black);

            // Check if the current letter is in the secret word
            if (!mGame.secretWord.contains(guess)) {
                mGame.livesRemaining--; // Remove a live
                imageSwitcher.setImageResource(img[mGame.livesRemaining]); //set the img no. to follow the error
                textViewScore.setText(String.valueOf(mGame.livesRemaining));

                if (mGame.livesRemaining == 0) {
                    showEndGameDialog("Batman got executed!", "You lose!\n\nThe answer was:\n\n" + mGame.secretWord);
                }
            } else {
                mGame.correctGuess += guess; // Add the letter to the ones correctly guessed
                //Display all correct guesses
                final StringBuilder sb = new StringBuilder();
                final char[] chars = mGame.secretWord.toCharArray();
                for (int i = 0; i < mGame.secretWord.length(); i++) {
                    //To check if this letter can be found in correctGuess
                    sb.append((mGame.correctGuess.contains(Character.toString(chars[i])))
                            ? chars[i] //found
                            : "-");
                }
                mGame.hiddenWord = sb.toString();
                textView.setText(mGame.hiddenWord);

                // If all the unique letters in the word have been found, it's a win
                if (mGame.correctGuess.length() == mGame.maxLetters) {
                    showEndGameDialog("Amazing! You save Batman!", "The Hangman\'s favourite fruit is:\n\n" + mGame.secretWord);
                }
            }
        }
    };

    private void showEndGameDialog(String title, String message) {
        new AlertDialog.Builder(GameActivity.this)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton("Play Again",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                GameActivity.this.startNewGame();
                            }
                        })
                .setNegativeButton("Exit",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                GameActivity.this.finish();
                            }
                        })
                .show();
    }

    private void initSecretWord() {

        int index = random.nextInt(strWords.length);
        String secretWord = strWords[index];
        // Replace all the letters by '-' ('\p{L}' matches any letter)
        String hiddenWord = secretWord.replaceAll("\\p{L}", "-");

        mGame.maxLetters = countUniqueLettersInWord(secretWord);
        mGame.secretWord = secretWord;
        mGame.hiddenWord = hiddenWord;
    }

    /**
     * Calculate the number of unique letter in the word
     * E.g BANANA has 3 unique letters (B,A,N)
     *
     * @param word The word to count the unique letter in
     * @return The total of unique letters
     */
    private static int countUniqueLettersInWord(final String word) {
        char[] chars = word.toCharArray();
        int count = 0;
        char curr;
        boolean contains;
        for (int i = 0; i < chars.length; i++) {
            curr = chars[i];
            // Check if the same letter exists in the visited letters (from pos 0 to current letter)
            contains = false;
            for (int j = 0; j < i; j++) {
                if (curr == chars[j]) {
                    contains = true;
                    break; // Don't need to continue if the letter already exists.
                }
            }
            if (!contains)
                count++;
        }

        return count;
    }

    private void initImageSwitcher() {

        //https://www.tutorialspoint.com/android/android_imageswitcher.htm
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return new ImageView(getApplicationContext());
            }
        });

        Animation aniOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
        Animation aniIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);

        imageSwitcher.setOutAnimation(aniOut);
        imageSwitcher.setOutAnimation(aniIn);
    }

    //show help information
    public void showHelpDialog() {
        new AlertDialog.Builder(this)
                .setTitle("Help")
                .setMessage("Whisper the password (Hangman's favourite fruit) to save Batman\n\n"
                        + "You only have 8 tries!")
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        })
                .show();
    }
}

खेल वर्ग

public class Game {

    String secretWord, correctGuess, hiddenWord;

    //Lives remaining by player
    int livesRemaining;

    public int maxLetters;

    public Game(int lives) {
        secretWord = "";
        correctGuess = "";
        hiddenWord = "";
        livesRemaining = lives;
    }
}
0
Eselfar 25 मई 2018, 18:43