|
| 1 | +package com.example.natashagoel.com; |
| 2 | + |
| 3 | +import android.content.res.TypedArray; |
| 4 | +import android.support.v7.app.AppCompatActivity; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.View; |
| 8 | +import android.widget.Button; |
| 9 | +import android.widget.EditText; |
| 10 | +import android.widget.ImageView; |
| 11 | +import android.widget.TextView; |
| 12 | +import android.widget.Toast; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Random; |
| 16 | + |
| 17 | +public class MainActivity extends AppCompatActivity { |
| 18 | + |
| 19 | + private TextView header; |
| 20 | + private TextView displayWord; |
| 21 | + private TypedArray listImages; |
| 22 | + private ImageView image; |
| 23 | + private EditText guessEdit; |
| 24 | + private Button guess; |
| 25 | + private TextView amtGuesses; |
| 26 | + private TextView pastGuesses; |
| 27 | + private Button newGame; |
| 28 | + private static TextView data; |
| 29 | + protected static ArrayList<String> dictionary; |
| 30 | + private String randWord; |
| 31 | + private int numOfGuesses = 8; |
| 32 | + |
| 33 | + public String incorrectGuessedletters; |
| 34 | + public String displayOfCorrectLetters; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void onCreate(Bundle savedInstanceState) { |
| 38 | + super.onCreate(savedInstanceState); |
| 39 | + setContentView(R.layout.activity_main); |
| 40 | + |
| 41 | + new RetrieveTask().execute(wordList()); |
| 42 | + |
| 43 | + listImages = getResources().obtainTypedArray(R.array.listOfImages); |
| 44 | + header = (TextView) findViewById(R.id.tvHeader); |
| 45 | + displayWord = (TextView) findViewById(R.id.tvLinefGuesses); |
| 46 | + image = (ImageView) findViewById(R.id.imageView); |
| 47 | + guessEdit = (EditText) findViewById(R.id.etInputGuess); |
| 48 | + guess = (Button) findViewById(R.id.bGuess); |
| 49 | + amtGuesses = (TextView) findViewById(R.id.tvAmtOfGuesses); |
| 50 | + pastGuesses = (TextView) findViewById(R.id.tvListOfPastGuesses); |
| 51 | + newGame = (Button) findViewById(R.id.bNewGame); |
| 52 | + //randWord = generateRandWord(dictionary).toUpperCase(); |
| 53 | + try { |
| 54 | + newGame.setOnClickListener(new View.OnClickListener() { |
| 55 | + @Override |
| 56 | + public void onClick(View v) { |
| 57 | + newGame( ); |
| 58 | + } |
| 59 | + }); |
| 60 | + } catch(Exception e) { |
| 61 | + Log.d("HOI", "HELLLLLOOOO -- " + e.getMessage()); |
| 62 | + } |
| 63 | + |
| 64 | + //newGame(null); |
| 65 | + } |
| 66 | + |
| 67 | + public void newGame() { |
| 68 | + header.setText("Let's Play Hangman!"); |
| 69 | + findViewById(R.id.etInputGuess).setVisibility(View.VISIBLE); |
| 70 | + numOfGuesses = 8; |
| 71 | + amtGuesses.setText("Number of Guesses left: " + numOfGuesses); |
| 72 | + pastGuesses.setText(""); |
| 73 | + image.setImageResource(listImages.getResourceId(numOfGuesses, 0)); |
| 74 | + randWord = generateRandWord(dictionary).toUpperCase(); |
| 75 | + displayWord.setText(new String(new char[randWord.length()]).replace("\0", "_ ")); |
| 76 | + } |
| 77 | + |
| 78 | + public void guess(View view) { |
| 79 | + String guess = guessEdit.getText().toString().toUpperCase(); |
| 80 | + |
| 81 | + guessEdit.setText(""); |
| 82 | + |
| 83 | + if (guess.length() > 1 || !guess.matches("[a-zA-Z]+")) { |
| 84 | + Toast.makeText(MainActivity.this, "You must input a single alphabetic character!", Toast.LENGTH_SHORT).show(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + String wrongGuessesTillNow = pastGuesses.getText().toString(); |
| 89 | + String guessesTillNow = displayWord.getText().toString(); |
| 90 | + String newGuessesSoFar = ""; |
| 91 | + |
| 92 | + if (guessesTillNow.indexOf(guess) != -1 || wrongGuessesTillNow.indexOf(guess) != -1) { |
| 93 | + Toast.makeText(MainActivity.this, "You've inputted this letter already!", Toast.LENGTH_SHORT).show(); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + Boolean correctGuess = randWord.contains(guess); |
| 98 | + |
| 99 | + if (correctGuess) { |
| 100 | + for (int i = 0; i < randWord.length(); i++) { |
| 101 | + if (guessesTillNow.contains(randWord.substring(i, i+1)) || randWord.charAt(i) == guess.charAt(0)) { //if letter already guessed and displayed or if current guess is this letter |
| 102 | + newGuessesSoFar += randWord.charAt(i) + " "; |
| 103 | + } else { |
| 104 | + newGuessesSoFar += "_ "; |
| 105 | + } |
| 106 | + } |
| 107 | + displayWord.setText(newGuessesSoFar.toUpperCase()); |
| 108 | + Toast.makeText(MainActivity.this, "Nice guess!", Toast.LENGTH_SHORT).show(); |
| 109 | + } else { |
| 110 | + numOfGuesses--; |
| 111 | + amtGuesses.setText("You have " + numOfGuesses + " incorrect guesses remaining."); |
| 112 | + image.setImageResource(listImages.getResourceId(numOfGuesses, 0)); |
| 113 | + |
| 114 | + String guessedSoFar = pastGuesses.getText().toString().toUpperCase(); |
| 115 | + pastGuesses.setText(guessedSoFar + " " + guess.toUpperCase()); |
| 116 | + Toast.makeText(MainActivity.this, "Not right.", Toast.LENGTH_SHORT).show(); |
| 117 | + } |
| 118 | + |
| 119 | + newGuessesSoFar = newGuessesSoFar.replaceAll("\\s",""); |
| 120 | + |
| 121 | + if (numOfGuesses == 0) { |
| 122 | + displayWord.setText(randWord.toUpperCase()); |
| 123 | + header.setText("You lost!"); |
| 124 | + findViewById(R.id.etInputGuess).setVisibility(View.INVISIBLE); |
| 125 | + } else if (randWord.toUpperCase().equals(newGuessesSoFar.toUpperCase())) { |
| 126 | + displayWord.setText(randWord.toUpperCase()); |
| 127 | + header.setText("You won!"); |
| 128 | + findViewById(R.id.etInputGuess).setVisibility(View.INVISIBLE); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private String wordList() { |
| 133 | + final String language = "en"; |
| 134 | + final String filters = "domains=Computing"; |
| 135 | + return "https://od-api.oxforddictionaries.com:443/api/v1/wordlist/" + language + "/" + filters; |
| 136 | + } |
| 137 | + |
| 138 | + private String generateRandWord(ArrayList<String> list) { |
| 139 | + Random random = new Random(); |
| 140 | + int randIndex = random.nextInt(list.size()); |
| 141 | + return list.get(randIndex); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | +// click = (Button) findViewById(R.id.button); |
| 146 | + |
| 147 | +// click.setOnClickListener(new View.OnClickListener() { |
| 148 | +// @Override |
| 149 | +// public void onClick(View v) { |
| 150 | +// try { |
| 151 | +// randomWord = generateRandWord(list); |
| 152 | +// } catch (Exception e) { |
| 153 | +// randomWord = e.getMessage(); |
| 154 | +// } |
| 155 | +// data.setText(randomWord); |
| 156 | +// } |
| 157 | +// }); |
| 158 | + |
| 159 | +} |
0 commit comments