@@ -1,30 +1,24 @@
package app .a3_in_1_game ;
import android .app .AlertDialog ;
import android .content .Context ;
import android .content .DialogInterface ;
import android .content .SharedPreferences ;
import android .content .res .Resources ;
import android .graphics .Color ;
import android .support .v7 .app .AppCompatActivity ;
import android .os .Bundle ;
import android .app .Activity ;
import android .app .AlertDialog ;
import android .support .v4 .app .NavUtils ;
import android .support .v7 .app .AppCompatActivity ;
import android .view .Gravity ;
import android .view .Menu ;
import android .view .MenuItem ;
import android .content .DialogInterface ;
import android .content .Intent ;
import android .view .View ;
import android .view .View .OnClickListener ;
import android .view .ViewGroup ;
import android .widget .Button ;
import android .widget .GridView ;
import android .widget .ImageView ;
import android .widget .LinearLayout ;
import android .widget .TextView ;
import android .widget .Toast ;
import com .android .volley .Request ;
import com .android .volley .RequestQueue ;
import com .android .volley .Response ;
import com .android .volley .VolleyError ;
import com .android .volley .toolbox .JsonObjectRequest ;
@@ -34,19 +28,24 @@
import org .json .JSONObject ;
import java .io .BufferedReader ;
import java .io .FileNotFoundException ;
import java .io .FileReader ;
import java .io .IOException ;
import java .io .InputStream ;
import java .io .InputStreamReader ;
import java .util .Random ;
import static app .a3_in_1_game .Hangman .getRandomWord ;
import static app .a3_in_1_game .Hangman .currentWord ;
import static app .a3_in_1_game .Tic_Tac_Toe_Activity .textView ;
public class Hangman_Activity extends AppCompatActivity {
private static int MAX_WORD_SIZE = 50 ;
private static int MAX_NUM_WORDS_MULTIPLAYER = 20 ;
private static long startTime ; //stores the start time for multiplayer timing
private static int numWordsGuessed = 0 ; //score for multiplayer games
private static int totalNumWrongGuesses = 0 ; //tiebreaker for multiplayer
public InputStream inputStream ;
public String [] wordsList = new String [MAX_NUM_WORDS_MULTIPLAYER ];
public boolean multiplayer = MySingleton .tic_tac_toe_multiplayer ;
int score = 0 ;
RequestQueue requestQueue ;
private String [] words ;
private Random rand ;
private String currWord = "" ;
@@ -64,41 +63,38 @@ public class Hangman_Activity extends AppCompatActivity {
//number correctly guessed
private int numCorr ;
private LetterAdapter ltrAdapt ;
int score = 0 ;
private String winner ;
private Thread thread ;
private String url = MySingleton .url ;
private Context context ;
private boolean run ;
private String host = "Chris" ; //TODO change this in main activity later
private String user = "chris" ; //TODO change this in main activity later
private String host = MySingleton . tic_tac_toe_host ;
private String user ;
private int numErrors ;
private boolean multiplayer = true ; //TODO change this in main activity later
private static int MAX_WORD_SIZE = 50 ;
private static int MAX_NUM_WORDS_MULTIPLAYER = 20 ;
public String [] wordsList = new String [MAX_NUM_WORDS_MULTIPLAYER ];
private static long startTime ; //stores the start time for multiplayer timing
private static int numWordsGuessed = 0 ; //score for multiplayer games
private static int totalNumWrongGuesses = 0 ; //tiebreaker for multiplayer
private TextView textView ;
private SharedPreferences sharedPref ;
public Hangman_Activity () throws IOException {
}
public static double getElapsedTime () {
long now = System .currentTimeMillis ();
return (now - startTime ) / 1000.0 ;
}
@ Override
protected void onCreate (Bundle savedInstanceState ) {
super .onCreate (savedInstanceState );
setContentView (R .layout .activity_hangman_ );
Resources res = getResources ();
words = res .getStringArray (R .array .words );
sharedPref = getSharedPreferences ("myPref" , Context .MODE_PRIVATE );
user = sharedPref .getString ("user" , "" );
wordLayout = (LinearLayout ) findViewById (R .id .word );
letters = (GridView ) findViewById (R .id .letters );
requestQueue = MySingleton .getInstance (this .getApplicationContext ()).getRequestQueue ();
bodyParts = new ImageView [numParts ];
bodyParts [0 ] = (ImageView ) findViewById (R .id .head );
@@ -118,7 +114,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
protected void update () {
thread = new Thread (new Runnable () {
@ Override
@@ -187,7 +182,7 @@ public void onErrorResponse(VolleyError error) {
MySingleton .getInstance (context ).addToRequestQueue (stringRequest );
}
protected void getWordsList (){
protected void getWordsList () {
String req = url + "/hangman_words_list/" + host ;
System .err .println (req );
JsonObjectRequest jsonObjectRequest =
@@ -196,7 +191,7 @@ protected void getWordsList(){
public void onResponse (JSONObject response ) {
try {
System .err .println ("RESPONSE: " + response .toString ());
wordsList = (String [])response .get ("words" );
wordsList = (String []) response .get ("words" );
} catch (JSONException e ) {
e .printStackTrace ();
}
@@ -209,8 +204,7 @@ public void onErrorResponse(VolleyError error) {
});
jsonObjectRequest .setTag (this );
MySingleton .getInstance (context ).addToRequestQueue (jsonObjectRequest );
}
}
public void getRandomWord () throws IOException {
inputStream = getAssets ().open ("HangmanWordList.txt" );
@@ -232,7 +226,7 @@ public void getRandomWord() throws IOException {
//extracts a random line as the word
Random r = new Random ();
int randomLine = r .nextInt (numLinesInFile - 0 ) + 0 ;
int randomLine = r .nextInt (numLinesInFile ) ;
String trmp = "" ;
for (int i = 0 ; i < randomLine ; i ++) {
@@ -246,52 +240,16 @@ public void getRandomWord() throws IOException {
System .out .println (text );
}
public void playGame () {
if (!multiplayer ) {
try {
getRandomWord ();
} catch (IOException e ) {
e .printStackTrace ();
}
currPart = 0 ;
numCorr = 0 ;
charViews = new TextView [currWord .length ()];
wordLayout .removeAllViews ();
//do currWord = ... here
for (int c = 0 ; c < currWord .length (); c ++) {
charViews [c ] = new TextView (this );
charViews [c ].setText ("" + currWord .charAt (c ));
charViews [c ].setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT ));
charViews [c ].setGravity (Gravity .CENTER );
charViews [c ].setTextColor (Color .WHITE );
charViews [c ].setBackgroundResource (R .drawable .letter_bg );
//add to layout
wordLayout .addView (charViews [c ]);
}
ltrAdapt = new LetterAdapter (this );
letters .setAdapter (ltrAdapt );
for (int p = 0 ; p < numParts ; p ++) {
bodyParts [p ].setVisibility (View .INVISIBLE );
}
}else {
//multiplayer
int ROUND_TIME_SECS = 120 ; //2 minutes
getWordsList (); //populates global var wordsList
startTime = System .currentTimeMillis ();
int indexInWordsList = 0 ; //count of where we are in the words list
while (getElapsedTime () < ROUND_TIME_SECS ) {
//update a text field with the remainder of the time:
//ROUND_TIME_SECS - getElapsedTime());
if (!multiplayer ) {
try {
getRandomWord ();
} catch (IOException e ) {
e .printStackTrace ();
}
currPart = 0 ;
numCorr = 0 ;
while (wordsList [0 ] == null ){} //wait until request works
charViews = new TextView [wordsList [indexInWordsList ++].length ()];
charViews = new TextView [currWord .length ()];
wordLayout .removeAllViews ();
//do currWord = ... here
for (int c = 0 ; c < currWord .length (); c ++) {
@@ -304,21 +262,52 @@ public void playGame() {
charViews [c ].setBackgroundResource (R .drawable .letter_bg );
//add to layout
wordLayout .addView (charViews [c ]);
}
ltrAdapt = new LetterAdapter (this );
letters .setAdapter (ltrAdapt );
for (int p = 0 ; p < numParts ; p ++) {
bodyParts [p ].setVisibility (View .INVISIBLE );
}
}
//timer is done
post (numWordsGuessed , totalNumWrongGuesses ); //tell the server how you did
}
}
public static double getElapsedTime () {
long now = System .currentTimeMillis ();
return (now - startTime ) / 1000.0 ;
} else {
//multiplayer
int ROUND_TIME_SECS = 15 ; //2 minutes
getWordsList (); //populates global var wordsList
startTime = System .currentTimeMillis ();
int indexInWordsList = 0 ; //count of where we are in the words list
while (getElapsedTime () < ROUND_TIME_SECS ) {
//update a text field with the remainder of the time:
//ROUND_TIME_SECS - getElapsedTime());
currPart = 0 ;
numCorr = 0 ;
while (wordsList [0 ] == null ) {
} //wait until request works
charViews = new TextView [wordsList [indexInWordsList ++].length ()];
wordLayout .removeAllViews ();
//do currWord = ... here
for (int c = 0 ; c < currWord .length (); c ++) {
charViews [c ] = new TextView (this );
charViews [c ].setText ("" + currWord .charAt (c ));
charViews [c ].setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT ));
charViews [c ].setGravity (Gravity .CENTER );
charViews [c ].setTextColor (Color .WHITE );
charViews [c ].setBackgroundResource (R .drawable .letter_bg );
//add to layout
wordLayout .addView (charViews [c ]);
}
ltrAdapt = new LetterAdapter (this );
letters .setAdapter (ltrAdapt );
for (int p = 0 ; p < numParts ; p ++) {
bodyParts [p ].setVisibility (View .INVISIBLE );
}
}
//timer is done
post (numWordsGuessed , totalNumWrongGuesses ); //tell the server how you did
}
}
public void letterPressed (View view ) {
@@ -335,10 +324,10 @@ public void letterPressed(View view) {
guessedRight = true ;
}
}
if (guessedRight == true ) {
if (guessedRight ) {
if (numCorr == currWord .length ()) {
//user has won
if (!multiplayer ) {
if (!multiplayer ) {
// Disable Buttons
disableBtns ();
@@ -363,7 +352,7 @@ public void onClick(DialogInterface dialog, int id) {
});
winBuild .show ();
}else {
} else {
//multiplayer
//user got a word
numWordsGuessed ++;
@@ -378,12 +367,12 @@ public void onClick(DialogInterface dialog, int id) {
//some guesses left
bodyParts [currPart ].setVisibility (View .VISIBLE );
currPart ++;
if (multiplayer ){
if (multiplayer ) {
totalNumWrongGuesses ++;
}
} else {
if (!multiplayer ) {
if (!multiplayer ) {
//user has lost
disableBtns ();
@@ -406,7 +395,7 @@ public void onClick(DialogInterface dialog, int id) {
}
});
loseBuild .show ();
}else {
} else {
//multiplayer
//user has lost