This commit is contained in:
Florian Bouillon 2018-04-10 18:01:31 +02:00
parent a56725c310
commit 2156de4939
8 changed files with 194 additions and 9 deletions

View File

@ -13,7 +13,9 @@ import android.view.animation.DecelerateInterpolator;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import net.DeltaWings.Android.Hangman.Util.Command;
import net.DeltaWings.Android.Hangman.Util.ConnectionUtil;
import java.util.ArrayList;
@ -96,7 +98,7 @@ public class GameActivity extends AppCompatActivity {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
EditText txt = ((EditText) v);
String text = txt.getText().toString();
String text = txt.getText().toString().toLowerCase();
if(text.length() == 1) { //is letter
@ -113,9 +115,12 @@ public class GameActivity extends AppCompatActivity {
//get result
HashMap<String, String> result = co.getDatas();
Toast.makeText(MainActivity.getInstance(), result.toString(), Toast.LENGTH_SHORT).show();
word.setText(result.get("newWord"));
new Command().execute("AFFICHER|"+result.get("newWord"));
if(Objects.equals(result.get("status"), "won")) { //check if player won
//Game Won
new Command().execute("AFFICHER|Tu as gagné");
DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@Override

View File

@ -15,8 +15,11 @@ import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import net.DeltaWings.Android.Hangman.Util.Command;
import net.DeltaWings.Android.Hangman.settings.SettingActivity;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
public static MainActivity instance;
@ -63,6 +66,7 @@ public class MainActivity extends AppCompatActivity {
findViewById(R.id.singleplayerButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Command().execute("AFFICHER|test");
Toast.makeText(instance, getPref("setting_theme", getApplicationContext()), Toast.LENGTH_LONG).show();
}
});

View File

@ -0,0 +1,112 @@
package net.DeltaWings.Android.Hangman.Util;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import android.os.AsyncTask;
import java.net.UnknownHostException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class Command extends AsyncTask<String, Integer, Long> {
protected Long doInBackground(String... s) {
Command c = new Command();
try {
c.envoyer_commande(s[0]);
}
catch ( UnknownHostException e ) {
System.out.println("\nDEBUG:\texception UnknownHostException");
System.out.println( e.getMessage() );
} catch (IOException e){
System.out.println("\nDEBUG:\texception IOException");
System.out.println( e.getMessage() );
} catch (ClassNotFoundException e) {
System.out.println("\nDEBUG:\texception ClassNotFoundException");
System.out.println( e.getMessage() );
} catch (InterruptedException e) {
System.out.println("\nDEBUG:\texception InterruptedException");
System.out.println( e.getMessage() );
}
publishProgress((int) (50));
long l = 0;
return l;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
}
//----------------------------------------------------------------------------------------------
public void envoyer_commande( String c ) throws IOException, ClassNotFoundException, InterruptedException {
System.out.println("\nDEBUG:\tenvoyer_commande");
String message_recu;
byte[] ipAddr = new byte[]{(byte)192,(byte)168,(byte)1,(byte)103};
InetAddress host = InetAddress.getByAddress(ipAddr);
int port = Integer.parseInt("53000");
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
//establish socket connection to server
socket = new Socket("192.168.1.2", 53000);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
//dataInputStream = new DataInputStream(socket.getInputStream());
System.out.println("\nDEBUG:\tConnexion ok");
System.out.println("\nDEBUG:\tdebut envoi");
dataOutputStream.writeBytes(c);
System.out.println("\nDEBUG:\tfinenvoi");
System.out.println("\nDEBUG:\tdebut reception");
//message_recu = dataInputStream.readLine();
//System.out.println("\nDEBUG:\tmessage recu = " + message_recu);
System.out.println("\nDEBUG:\tfin réception");
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("\nDEBUG:\tFin envoyer_commande");
}
}

View File

@ -37,9 +37,12 @@ public class ConnectionUtil {
}
public boolean sendData(HashMap<String, String> query) {
if(single) return gameUtil.datasReader(query);
if(single) {
System.out.println(query);
return gameUtil.datasReader(query);
}
else {
//return state
System.out.println(query);
}
return false;
}

View File

@ -6,9 +6,18 @@ import android.widget.Toast;
import net.DeltaWings.Android.Hangman.MainActivity;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class GameUtil {
@ -20,9 +29,36 @@ public class GameUtil {
public GameUtil() {
//Generate Word
JSONObject obj = null;
String s = "https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json";
try{
URL url = new URL(s);
// read from the URL
Scanner scan = new Scanner(url.openStream());
StringBuilder str = new StringBuilder();
while (scan.hasNext()) str.append(scan.nextLine());
scan.close();
obj = new JSONObject(str.toString());
int pageName = obj.getJSONObject("pageInfo").length();
Log.v("NTM", pageName +"");
Toast.makeText(MainActivity.getInstance(), pageName, Toast.LENGTH_LONG).show();
}catch (Exception e) {
e.printStackTrace();
}
word = generateRandomWords();
for (int i = 0; i < word.length(); i++) {
res.add("_");
}
Log.v(tag, res.toString());
}
@ -55,13 +91,27 @@ public class GameUtil {
}
}
Log.v(tag, returning.toString());
Toast.makeText(MainActivity.getInstance(), returning.get("newWord"), Toast.LENGTH_SHORT).show();
//Toast.makeText(MainActivity.getInstance(), returning.get("newWord"), Toast.LENGTH_SHORT).show();
return true;
}
public HashMap<String, String> datasSender() {
return returning;
}
public static String generateRandomWords()
{
String randomStrings;
Random random = new Random();
char[] word = new char[random.nextInt(8)+3]; // words of length 3 through 10. (1 and 2 letter words are boring.)
for(int j = 0; j < word.length; j++)
{
word[j] = (char)('a' + random.nextInt(26));
}
randomStrings = new String(word);
return randomStrings;
}
}
/*

View File

@ -40,6 +40,7 @@
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:layout_weight="1"
android:textColor="?android:textColorPrimaryInverse"
android:background="@drawable/button"
android:text="@string/singleplayer"
android:textAppearance="@style/TextAppearance.AppCompat"
@ -56,6 +57,7 @@
android:layout_marginTop="64dp"
android:layout_weight="1"
android:background="@drawable/button"
android:textColor="?android:textColorPrimaryInverse"
android:text="@string/multiplayer"
android:textAppearance="@style/TextAppearance.AppCompat"
app:layout_constraintTop_toBottomOf="@+id/singleplayerButton"

View File

@ -3,9 +3,11 @@
<item
android:id="@+id/navigation_options"
android:title="@string/action_settings"/>
android:title="@string/action_settings"
android:background="?android:attr/textColorPrimary"/>
<item
android:id="@+id/navigation_about"
android:title="@string/about"/>
android:title="@string/about"
android:background="?android:attr/textColorPrimary"/>
</menu>

View File

@ -48,11 +48,18 @@
<item name="android:colorAccent">@color/Indigo500</item>
<item name="android:colorBackground">@color/Grey50</item>
<item name="android:colorForeground">@color/Grey900</item>
<item name="android:textColorPrimary">@color/White</item>
<item name="android:textColorPrimaryInverse">@color/Black</item>
<item name="android:textColorPrimary">@color/Black</item>
<item name="android:textColorPrimaryInverse">@color/White</item>
<item name="android:textColorSecondary">@color/Grey900</item>
<item name="android:textColorSecondaryInverse">@color/White</item>
<item name="android:navigationBarColor">@color/Blue500</item>
<item name="colorPrimary">@color/Blue500</item>
<item name="colorPrimaryDark">@color/Blue900</item>
<item name="colorAccent">@color/Indigo500</item>
<item name="android:statusBarColor">@color/Blue900</item>
<item name="android:windowBackground">@color/White</item>
<item name="android:textColor">@color/Black</item>
</style>