Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.AndroidPack;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.widget.TextView;
- public abstract class AbstractHangman extends Activity {
- /**
- * ArrayList which contains any characters that have been used, whether
- * they are in the secret word, or not. All characters in this ArrayList
- * should be uppercase.
- */
- protected ArrayList<Character> charactersUsed = new ArrayList<Character>();
- /**
- * String which contains the word which the player should guess.
- */
- protected String wordToGuess = "";
- /**
- * Amount of tries remaining before the game ends.
- */
- protected int triesRemaining = 0;
- // References to the via XML generated textviews:
- private TextView secretWordTextView = null;
- private TextView countDownTextView = null;
- private TextView charsUsedTextView = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- // Propagate the event to the superclass:
- super.onCreate(savedInstanceState);
- // Show the main activity:
- setContentView(R.layout.main);
- // Link the via XML generated textviews to some private variables:
- secretWordTextView = (TextView) findViewById(R.id.hiddenWord);
- countDownTextView = (TextView) findViewById(R.id.countDown);
- charsUsedTextView = (TextView) findViewById(R.id.guessedLetters);
- // Everything is setup, lets start a new game!
- restart();
- }
- /**
- * Restart the game with a new random word. Any characters used are cleared and
- * the countdown has been reset to the default value.
- */
- public void restart() {
- // Reset the countdown timer
- triesRemaining = 10;
- // Pull a random word from a predefined collection:
- wordToGuess = randomWord();
- // Wipe any characters that have already been used:
- charactersUsed.clear();
- // Update all textviews:
- updateSecretWord();
- updateCharactersUsed();
- updateCountDown();
- }
- /**
- * Update the via XML generated textview which shows the secret word. Characters
- * that have not yet been guessed are replaced with an asterisk (*) and are thus
- * hidden.
- */
- protected void updateSecretWord() {
- String buffer = "";
- // Iterate over every single character in the secret (wordToGuess) word:
- for(int i = 0; i < wordToGuess.length();i++){
- // If the character has already been 'used' by the player,
- // show the actual character. If not, show an asterisk (*).
- if(charactersUsed.contains(Character.toUpperCase(wordToGuess.charAt(i)))) {
- buffer += wordToGuess.charAt(i);
- } else {
- buffer += "*";
- }
- }
- // Update the actual textview:
- secretWordTextView.setText(buffer);
- }
- /**
- * Update the via XML generated textview which shows the characters already used.
- */
- protected void updateCharactersUsed() {
- String buffer = "";
- // Build a string from all characters that have already been used by the player:
- for(char character : charactersUsed) {
- buffer += character;
- }
- // Update the actual textview with the newly created string:
- charsUsedTextView.setText(buffer);
- }
- /**
- * Update the via XML generated textview so show the current countdown value.
- */
- protected void updateCountDown() {
- // Update the actual textview with the amount of tries remaining:
- countDownTextView.setText(String.valueOf(triesRemaining));
- }
- /**
- * Used to generate select a random word from a predefined collection.
- *
- * @return Return a random item from the predefined collection of words.
- */
- protected String randomWord() {
- // Bunch of random words to choose from:
- String[] wordList = {"Android", "Arcade", "Restart", "Dolphin"};
- return wordList[new java.util.Random().nextInt(wordList.length)];
- }
- @Override
- public abstract boolean onKeyDown(int keyCode, KeyEvent event);
- }
Advertisement
Add Comment
Please, Sign In to add comment