Skip to content

Commit ff59914

Browse files
committed
Second Commit.
1 parent 28daa1d commit ff59914

File tree

12 files changed

+296
-80
lines changed

12 files changed

+296
-80
lines changed

app.iml

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant_run_split_apk_resources" />
104104
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javac" />
105105
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
106-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_jar" />
107106
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifest-checker" />
108107
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_assets" />
109108
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'com.android.application'
33
android {
44
compileSdkVersion 28
55
defaultConfig {
6-
applicationId "com.example.natashagoel.snowman"
6+
applicationId "com.example.snowman"
77
minSdkVersion 15
88
targetSdkVersion 28
99
versionCode 1

src/androidTest/java/com/example/natashagoel/snowman/ExampleInstrumentedTest.java src/androidTest/java/com/example/natashagoel/com/ExampleInstrumentedTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.natashagoel.snowman;
1+
package com.example.natashagoel.com;
22

33
import android.content.Context;
44
import android.support.test.InstrumentationRegistry;

src/main/AndroidManifest.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.example.natashagoel.snowman">
3+
package="com.example.natashagoel.com">
44

55
<uses-permission android:name="android.permission.INTERNET" />
66

@@ -11,13 +11,18 @@
1111
android:roundIcon="@mipmap/ic_launcher_round"
1212
android:supportsRtl="true"
1313
android:theme="@style/AppTheme">
14-
<activity android:name=".MainActivity">
14+
15+
<activity android:name=".HomeActivity"
16+
android:theme="@style/AppTheme.NoActionBar">
1517
<intent-filter>
1618
<action android:name="android.intent.action.MAIN" />
17-
1819
<category android:name="android.intent.category.LAUNCHER" />
1920
</intent-filter>
2021
</activity>
22+
23+
<activity android:name=".MainActivity" >
24+
</activity>
25+
2126
</application>
2227

2328
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
}

src/main/java/com/example/natashagoel/snowman/RetrieveTask.java src/main/java/com/example/natashagoel/com/RetrieveTask.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
package com.example.natashagoel.snowman;
1+
package com.example.natashagoel.com;
22

33
import android.os.AsyncTask;
44

5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
59
import java.io.BufferedReader;
610
import java.io.InputStreamReader;
711
import java.net.URL;
12+
import java.util.ArrayList;
813

914
import javax.net.ssl.HttpsURLConnection;
1015

1116
public class RetrieveTask extends AsyncTask<String, Integer, String> {
1217
String data = "";
18+
final int minWordLength = 4;
19+
final int maxWordLength = 12;
1320
@Override
1421
protected String doInBackground(String... params) {
1522
final String app_id = "88ef67b7";
@@ -39,11 +46,29 @@ protected String doInBackground(String... params) {
3946
}
4047
}
4148

42-
4349
@Override
4450
protected void onPostExecute(String s) {
4551
super.onPostExecute(s);
4652

47-
MainActivity.data.setText(this.data);
53+
MainActivity.dictionary = parseJSON(this.data);
54+
}
55+
56+
private ArrayList<String> parseJSON(String input) {
57+
ArrayList<String> listWords = new ArrayList<>();
58+
try {
59+
JSONObject js = new JSONObject(input);
60+
JSONArray results = js.getJSONArray("results");
61+
for (int i = 0; i < results.length(); i++) {
62+
JSONObject jObj = (JSONObject) results.get(i);
63+
String word = jObj.getString("word");
64+
boolean hasDigit = word.matches(".*\\d+.*");
65+
if (minWordLength <= word.length() && word.length() <= maxWordLength && !hasDigit && word.indexOf("-") == -1 && word.indexOf(" ") == -1) {
66+
listWords.add(word);
67+
}
68+
}
69+
} catch (JSONException e) {
70+
e.printStackTrace();
71+
}
72+
return listWords;
4873
}
4974
}

src/main/java/com/example/natashagoel/snowman/MainActivity.java

-39
This file was deleted.

0 commit comments

Comments
 (0)