Gerard-Meier

HangDroid

Mar 10th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. package org.AndroidPack;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.view.KeyEvent;
  8. import android.widget.TextView;
  9.  
  10. public abstract class AbstractHangman extends Activity {
  11.    
  12.     /**
  13.      * ArrayList which contains any characters that have been used, whether
  14.      * they are in the secret word, or not. All characters in this ArrayList
  15.      * should be uppercase.
  16.      */
  17.     protected ArrayList<Character> charactersUsed = new ArrayList<Character>();
  18.    
  19.     /**
  20.      * String which contains the word which the player should guess.
  21.      */
  22.     protected String wordToGuess        = "";
  23.    
  24.     /**
  25.      * Amount of tries remaining before the game ends.
  26.      */
  27.     protected int triesRemaining        = 0;
  28.    
  29.     // References to the via XML generated textviews:
  30.     private TextView secretWordTextView = null;
  31.     private TextView countDownTextView  = null;
  32.     private TextView charsUsedTextView  = null;
  33.    
  34.     @Override
  35.     public void onCreate(Bundle savedInstanceState) {
  36.         // Propagate the event to the superclass:
  37.         super.onCreate(savedInstanceState);
  38.        
  39.         // Show the main activity:
  40.         setContentView(R.layout.main);
  41.        
  42.         // Link the via XML generated textviews to some private variables:
  43.         secretWordTextView  = (TextView) findViewById(R.id.hiddenWord);
  44.         countDownTextView   = (TextView) findViewById(R.id.countDown);
  45.         charsUsedTextView   = (TextView) findViewById(R.id.guessedLetters);
  46.        
  47.         // Everything is setup, lets start a new game!
  48.         restart();
  49.     }
  50.    
  51.     /**
  52.      * Restart the game with a new random word. Any characters used are cleared and
  53.      * the countdown has been reset to the default value.
  54.      */
  55.     public void restart() {
  56.         // Reset the countdown timer
  57.         triesRemaining  = 10;
  58.         // Pull a random word from a predefined collection:
  59.         wordToGuess     = randomWord();
  60.        
  61.         // Wipe any characters that have already been used:
  62.         charactersUsed.clear();
  63.        
  64.         // Update all textviews:
  65.         updateSecretWord();
  66.         updateCharactersUsed();
  67.         updateCountDown();
  68.     }
  69.    
  70.     /**
  71.      * Update the via XML generated textview which shows the secret word. Characters
  72.      * that have not yet been guessed are replaced with an asterisk (*) and are thus
  73.      * hidden.
  74.      */
  75.     protected void updateSecretWord() {
  76.         String buffer = "";
  77.        
  78.         // Iterate over every single character in the secret (wordToGuess) word:
  79.         for(int i = 0; i < wordToGuess.length();i++){
  80.             // If the character has already been 'used' by the player,
  81.             // show the actual character. If not, show an asterisk (*).
  82.             if(charactersUsed.contains(Character.toUpperCase(wordToGuess.charAt(i)))) {
  83.                 buffer += wordToGuess.charAt(i);
  84.             } else {
  85.                 buffer += "*";
  86.             }
  87.         }
  88.        
  89.         // Update the actual textview:
  90.         secretWordTextView.setText(buffer);
  91.     }
  92.    
  93.     /**
  94.      * Update the via XML generated textview which shows the characters already used.
  95.      */
  96.     protected void updateCharactersUsed() {
  97.         String buffer = "";
  98.        
  99.         // Build a string from all characters that have already been used by the player:
  100.         for(char character : charactersUsed) {
  101.             buffer += character;
  102.         }
  103.        
  104.         // Update the actual textview with the newly created string:
  105.         charsUsedTextView.setText(buffer);
  106.     }
  107.    
  108.     /**
  109.     * Update the via XML generated textview so show the current countdown value.
  110.     */
  111.     protected void updateCountDown() {
  112.         // Update the actual textview with the amount of tries remaining:
  113.         countDownTextView.setText(String.valueOf(triesRemaining));
  114.     }
  115.    
  116.     /**
  117.      * Used to generate select a random word from a predefined collection.
  118.      *
  119.      * @return Return a random item from the predefined collection of words.
  120.      */
  121.     protected String randomWord() {
  122.         // Bunch of random words to choose from:
  123.         String[] wordList = {"Android", "Arcade", "Restart", "Dolphin"};
  124.         return wordList[new java.util.Random().nextInt(wordList.length)];
  125.     }
  126.  
  127.     @Override
  128.     public abstract boolean onKeyDown(int keyCode, KeyEvent event);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment