@@ -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

Large diffs are not rendered by default.

@@ -11,18 +11,20 @@
*/

public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
static final String url = "http://10.0.2.2:8080";
//static final String url = "https://server-3in1.herokuapp.com";
// static final String url = "http://10.0.2.2:8080";
// static final String url = "http://192.168.0.20:8080";
static final String url = "https://server-3in1.herokuapp.com";

static String tic_tac_toe_host;
static boolean tic_tac_toe_multiplayer;
static String connect_4_host;
static boolean connect_4_multiplayer;

static String hangman_host;
static boolean hangman_multiplayer;

private static MySingleton mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;


private MySingleton(Context context) {
mCtx = context;

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -9,5 +9,7 @@
<string name="body">The Body</string>
<string name="arm">An Arm</string>
<string name="leg">A Leg</string>
<string name="restart">Restart</string>
<string name="forfeit">Forfeit?</string>

</resources>
@@ -10,6 +10,6 @@
"license": "ISC",
"dependencies": {
"express": "^4.15.3",
"mysql": "^2.13.0"
"ip": "^1.1.5"
}
}
@@ -6,8 +6,6 @@ var port = process.env.PORT || 8080;

//var pool = mysql.createPool(process.env.JAWSDB_URL);

var connect_4_games = {};

function checkPlayer(user, game) {
if (game.host == user || game.client == user) {
return true;
@@ -28,10 +26,52 @@ app.get("/", function (req, res) {
res.send("Hello World!");
});

var users = [];

app.get("/set_username/:user", function(req, res) {
// Check if new username is unique
var user = req.params.user;
if (users.indexOf(user) > -1) {
console.log("Username not unique");
res.send("Username not unique");
}
else {
users.push(user);
console.log(users);
res.send("Username added!");
}
});

app.get("/set_and_delete_username/:old-:user", function(req, res) {
// Delete old username
var old = req.params.old;
var index = users.indexOf(old);
if (index > -1) {
users.splice(index, 1);
}

// Check if new username is unique
var user = req.params.user;
if (users.indexOf(user) > -1) {
console.log("Username not unique");
res.send("Username not unique");
}
else {
users.push(user);
console.log(users);
res.send("Username added!");
}
});

var connect_4_games = {};

app.get("/connect_4_host/:host", function (req, res) {
var host = req.params.host;
console.log("New Connect 4: " + host);
res.send("Game created!");
if (host in connect_4_games) {
delete connect_4_games[host];
}
game = {
host: host
};
@@ -44,11 +84,18 @@ app.get("/connect_4_join/:host/:client", function (req, res) {
var client = req.params.client;
console.log("Connect 4: " + host);
console.log("Being joined by " + client);
if (host in connect_4_games) {
if (host in connect_4_games) {
var turn;
if (Math.random() >= 0.5) {
turn = host;
}
else {
turn = client;
}
var game = connect_4_games[host];
res.send("Game joined!");
game.client = client;
game.turn = game.host;
game.turn = turn;
game.col = "-1";
console.log(game);
}
@@ -118,6 +165,9 @@ app.get("/tic_tac_toe_host/:host", function (req, res) {
var host = req.params.host;
console.log("New Tic Tac Toe: " + host);
res.send("Game created!");
if (host in tic_tac_toe_games) {
delete tic_tac_toe_games[host];
}
game = {
host: host
};
@@ -130,11 +180,18 @@ app.get("/tic_tac_toe_join/:host/:client", function (req, res) {
var client = req.params.client;
console.log("Tic Tac Toe: " + host);
console.log("Being joined by " + client);
if (host in tic_tac_toe_games) {
if (host in tic_tac_toe_games) {
var turn;
if (Math.random() >= 0.5) {
turn = host;
}
else {
turn = client;
}
var game = tic_tac_toe_games[host];
res.send("Game joined!");
game.client = client;
game.turn = game.host;
game.turn = turn;
game.col = "-1";
game.row = "-1";
console.log(game);
@@ -210,6 +267,9 @@ app.get("/hangman_host/:host", function (req, res) {
var host = req.params.host;
console.log("New Hangman: " + host);
res.send("Game created!");
if (host in hangman_games) {
delete hangman_games[host];
}
game = {
host: host,
words: getWords()

This file was deleted.

This file was deleted.