mirror of
https://github.com/Aviortheking/Hangman.git
synced 2024-12-19 18:20:54 +00:00
Updating time
This commit is contained in:
parent
0ae21f293f
commit
e5b5fb6363
@ -1,7 +1,9 @@
|
||||
package net.DeltaWings.Android.Hangman;
|
||||
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
@ -15,13 +17,17 @@ import android.widget.TextView;
|
||||
import net.DeltaWings.Android.Hangman.Util.ConnectionUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class GameActivity extends AppCompatActivity {
|
||||
|
||||
private AlertDialog.Builder builder;
|
||||
private ConnectionUtil co;
|
||||
private TextView textView;
|
||||
private List<String> letters = new ArrayList<>();
|
||||
private Context context = this;
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
@ -41,6 +47,7 @@ public class GameActivity extends AppCompatActivity {
|
||||
MainActivity.setTheme(this);
|
||||
setContentView(R.layout.game_activity);
|
||||
|
||||
textView = findViewById(R.id.textView2);
|
||||
ProgressBar progressBar = findViewById(R.id.progressBar);
|
||||
EditText input = findViewById(R.id.input);
|
||||
|
||||
@ -89,19 +96,65 @@ public class GameActivity extends AppCompatActivity {
|
||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||
EditText txt = ((EditText) v);
|
||||
String text = txt.getText().toString();
|
||||
if(text.length() == 1) {
|
||||
log("Letter : " + text);
|
||||
|
||||
|
||||
if(text.length() == 1) { //is letter
|
||||
if(letters.contains(text)) {
|
||||
log("Letter : " + text + "Already in");
|
||||
} else {
|
||||
log("Sending letter " + text);
|
||||
log("Sending letter : \"" + text +"\"");
|
||||
|
||||
//preparing datas
|
||||
HashMap<String, String> temp = new HashMap<>();
|
||||
temp.put("query", "letter");
|
||||
temp.put("letter", text);
|
||||
co.sendData(temp);
|
||||
|
||||
//get result
|
||||
HashMap<String, String> result = co.getDatas();
|
||||
if(Objects.equals(result.get("status"), "won")) { //check if player won
|
||||
//Game Won
|
||||
|
||||
DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
switch (which){
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
//Close Connection
|
||||
Intent intent = getIntent();
|
||||
overridePendingTransition(0, 0);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
|
||||
finish();
|
||||
|
||||
overridePendingTransition(0, 0);
|
||||
startActivity(intent);
|
||||
break;
|
||||
default:
|
||||
finish();
|
||||
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
new AlertDialog.Builder(context)
|
||||
.setMessage("You won!")
|
||||
.setPositiveButton("Restart", clickListener)
|
||||
.setNegativeButton("Quit", clickListener)
|
||||
.show();
|
||||
}
|
||||
//Send Letter
|
||||
txt.setText("", TextView.BufferType.EDITABLE);
|
||||
}
|
||||
} else if(text.length() > 1) {
|
||||
log("Word : " + text);
|
||||
//Send Letter
|
||||
HashMap<String, String> temp = new HashMap<>();
|
||||
temp.put("query", "word");
|
||||
temp.put("letter", text);
|
||||
co.sendData(temp);
|
||||
|
||||
txt.setText("", TextView.BufferType.EDITABLE);
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -114,7 +167,7 @@ public class GameActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
//Return Builder
|
||||
builder = new AlertDialog.Builder(this);
|
||||
builder = new AlertDialog.Builder(context);
|
||||
|
||||
builder.setMessage("Do you really want to quit your game ?")
|
||||
.setPositiveButton("Yes", dialogClickListener)
|
||||
|
@ -1,8 +1,8 @@
|
||||
package net.DeltaWings.Android.Hangman.Util;
|
||||
|
||||
import java.util.List;
|
||||
import net.DeltaWings.Android.Hangman.MainActivity;
|
||||
import net.DeltaWings.Android.Hangman.Util.GameUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ConnectionUtil {
|
||||
|
||||
@ -36,7 +36,7 @@ public class ConnectionUtil {
|
||||
|
||||
}
|
||||
|
||||
public boolean sendData(List<Object> query) {
|
||||
public boolean sendData(HashMap<String, String> query) {
|
||||
if(single) return gameUtil.datasReader(query);
|
||||
else {
|
||||
//return state
|
||||
@ -44,7 +44,7 @@ public class ConnectionUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
public HashMap<String, String> getDatas() {
|
||||
if(single) {
|
||||
return gameUtil.datasSender();
|
||||
} else {
|
||||
@ -53,3 +53,4 @@ public class ConnectionUtil {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,25 +1,76 @@
|
||||
package net.DeltaWings.Android.Hangman.Util;
|
||||
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import net.DeltaWings.Android.Hangman.MainActivity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
public class GameUtil {
|
||||
|
||||
private String word = null;
|
||||
private List<String> letters = new ArrayList<>();
|
||||
private List<Object> datas = new ArrayList<>();
|
||||
private String word = "example";
|
||||
private ArrayList<String> res = new ArrayList<>();
|
||||
private ArrayList<String> letters = new ArrayList<>();
|
||||
private HashMap<String, String> returning = new HashMap<>();
|
||||
private String tag = "GameUtil";
|
||||
|
||||
public GameUtil() {
|
||||
|
||||
//Generate Word
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
res.add("_");
|
||||
}
|
||||
Log.v(tag, res.toString());
|
||||
}
|
||||
|
||||
public boolean datasReader(List<Object> datas) {
|
||||
public boolean datasReader(HashMap<String, String> datas) {
|
||||
returning.clear();
|
||||
String query = datas.get("query");
|
||||
if(Objects.equals(query, "letter")) {
|
||||
Log.v(tag, "1");
|
||||
Log.v(tag, "1");
|
||||
String letter = datas.get("letter");
|
||||
if(word.contains(letter)) {
|
||||
Log.v(tag, "2");
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
if(Objects.equals(String.valueOf(word.charAt(i)), letter)) {
|
||||
res.set(i, letter);
|
||||
}
|
||||
}
|
||||
if(!res.contains("_")) {
|
||||
//winner
|
||||
returning.put("status", "won");
|
||||
}
|
||||
}
|
||||
returning.put("newWord", res.toString());
|
||||
} else if (Objects.equals(query, "word")) {
|
||||
if(Objects.equals(datas.get("word"), word)) {
|
||||
returning.put("status", "won");
|
||||
|
||||
}
|
||||
}
|
||||
Log.v(tag, returning.toString());
|
||||
Toast.makeText(MainActivity.getInstance(), returning.get("newWord"), Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Object> datasSender() {
|
||||
|
||||
return null;
|
||||
public HashMap<String, String> datasSender() {
|
||||
return returning;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Trames sended to app :
|
||||
status = status de la game (won, Integer of tries left, lost)
|
||||
lettersUsed = String of letters used (,)
|
||||
newWord = _________
|
||||
|
||||
|
||||
Trames sended from app:
|
||||
word = word || letter = letter
|
||||
userId = userId
|
||||
username = username
|
||||
ip = ip
|
||||
*/
|
@ -74,7 +74,6 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:fontFamily="sans-serif-condensed"
|
||||
android:text="________________"
|
||||
android:windowSoftInputMode="adjustResize|adjustPan"
|
||||
android:textAlignment="center"
|
||||
android:textSize="36sp"
|
||||
android:textStyle="bold"/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user