diff --git a/.idea/gradle.xml b/.idea/gradle.xml index fc132b2..d390b65 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -6,7 +6,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981..1a3eaff 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bdf7566..3437db9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,9 +16,7 @@ android:name=".MainActivity" android:configChanges="orientation" android:launchMode="singleInstance" - android:screenOrientation="portrait"> - - + android:screenOrientation="portrait"> + + diff --git a/app/src/main/java/com/example/haresh/tic_tac_toe_app/CanvasViewDouble2P.java b/app/src/main/java/com/example/haresh/tic_tac_toe_app/CanvasViewDouble2P.java index c30669e..7f9c66b 100644 --- a/app/src/main/java/com/example/haresh/tic_tac_toe_app/CanvasViewDouble2P.java +++ b/app/src/main/java/com/example/haresh/tic_tac_toe_app/CanvasViewDouble2P.java @@ -145,6 +145,50 @@ public boolean onTouchEvent(MotionEvent event) { status = status.concat(";"+turn); byte[] ByteArray = status.getBytes(); connectedThread.write(ByteArray); + if(turn == 1){ + two_player_names.p1Name = TwoDevice2P_names.MyName.trim().toUpperCase(); + two_player_names.p2Name = TwoDevice2P_names.OpponentName.trim().toUpperCase(); + String tmp = dbh.checkUser(two_player_names.p1Name); + if (tmp.equals("FOUND")) { + //Toast.makeText(two_player_names.this,"found 1",Toast.LENGTH_SHORT).show(); + } else { + scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p1Name, 0); + //Toast.makeText(two_player_names.this,"adding 1",Toast.LENGTH_SHORT).show(); + dbh.addScore(sb); + } + + String tmp2 = dbh.checkUser(two_player_names.p2Name); + if (tmp2.equals("FOUND")) { + //Toast.makeText(two_player_names.this,"found 2",Toast.LENGTH_SHORT).show(); + + } else { + scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p2Name, 0); + //Toast.makeText(two_player_names.this,"adding 2",Toast.LENGTH_SHORT).show(); + dbh.addScore(sb); + } + } + else if(turn == 2){ + two_player_names.p2Name = TwoDevice2P_names.MyName.trim().toUpperCase(); + two_player_names.p1Name = TwoDevice2P_names.OpponentName.trim().toUpperCase(); + String tmp = dbh.checkUser(two_player_names.p1Name); + if (tmp.equals("FOUND")) { + //Toast.makeText(two_player_names.this,"found 1",Toast.LENGTH_SHORT).show(); + } else { + scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p1Name, 0); + //Toast.makeText(two_player_names.this,"adding 1",Toast.LENGTH_SHORT).show(); + dbh.addScore(sb); + } + + String tmp2 = dbh.checkUser(two_player_names.p2Name); + if (tmp2.equals("FOUND")) { + //Toast.makeText(two_player_names.this,"found 2",Toast.LENGTH_SHORT).show(); + + } else { + scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p2Name, 0); + //Toast.makeText(two_player_names.this,"adding 2",Toast.LENGTH_SHORT).show(); + dbh.addScore(sb); + } + } touchEnabled = false; } @@ -178,7 +222,8 @@ public void onClick(DialogInterface dialog, int which) { case DialogInterface.BUTTON_POSITIVE: { try { - connectedThread=null; + connectedThread.cancel(); + connectedThread = null; bluetoothSocket.close(); } catch (Exception e) { Log.d(TAG, "exception " + e.getMessage()); @@ -198,7 +243,7 @@ public void onClick(DialogInterface dialog, int which) { } public void updateWin(int i){ - /*if(i==1){ + if(i==1){ String tmp="FOUND"; try{ tmp=dbh.checkUser(two_player_names.p1Name); @@ -235,7 +280,7 @@ else if (i==2){ scoreboard sb=new scoreboard(dbh.getPlayerCount(),two_player_names.p2Name,1); dbh.addScore(sb); } - }*/ + } } public void check() @@ -400,6 +445,7 @@ public class ConnectedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; + private int cnt = 0; public ConnectedThread(BluetoothSocket socket) { Log.d(TAG, "create ConnectedThread"); @@ -415,40 +461,72 @@ public ConnectedThread(BluetoothSocket socket) { } mmInStream = tmpIn; mmOutStream = tmpOut; + } public void run() { Log.i(TAG, "BEGIN mConnectedThread"); + if(cnt == 0){ + try{ + byte[] ByteArray = TwoDevice2P_names.MyName.getBytes(); + connectedThread.write(ByteArray); + cnt++; + } + catch(Exception e){ + Log.d(TAG, e.getMessage()); + } + } byte[] buffer = new byte[1024]; int bytes; // Keep listening to the InputStream while connected while (true) { try { + Log.i(TAG, "BEGIN Listening"); // Read from the InputStream String readMessage = ""; bytes = mmInStream.read(buffer); readMessage = new String(buffer, 0, bytes); - // Send the obtained bytes to the UI Activity - Log.i(TAG, "Listening : " + readMessage); - a[0][0] = (int)(readMessage.charAt(0)-48); - a[0][1] = (int)(readMessage.charAt(1)-48); - a[0][2] = (int)(readMessage.charAt(2)-48); - a[1][0] = (int)(readMessage.charAt(3)-48); - a[1][1] = (int)(readMessage.charAt(4)-48); - a[1][2] = (int)(readMessage.charAt(5)-48); - a[2][0] = (int)(readMessage.charAt(6)-48); - a[2][1] = (int)(readMessage.charAt(7)-48); - a[2][2] = (int)(readMessage.charAt(8)-48); - turn = (int)(readMessage.charAt(10)-48); - String str= ""+a[0][0]+a[0][1]+a[0][2]+a[1][0]+a[1][1]+a[1][2]+a[2][0]+a[2][1]+a[2][2]+";"+turn; - - Log.i(TAG, "GOT : " + str); - - touchEnabled = true; - - if(!oncewin && !oncedrawen){ - postInvalidate(); - check(); + if(readMessage.contains(";")){ + // Send the obtained bytes to the UI Activity + Log.i(TAG, "Listening : " + readMessage); + a[0][0] = (int)(readMessage.charAt(0)-48); + a[0][1] = (int)(readMessage.charAt(1)-48); + a[0][2] = (int)(readMessage.charAt(2)-48); + a[1][0] = (int)(readMessage.charAt(3)-48); + a[1][1] = (int)(readMessage.charAt(4)-48); + a[1][2] = (int)(readMessage.charAt(5)-48); + a[2][0] = (int)(readMessage.charAt(6)-48); + a[2][1] = (int)(readMessage.charAt(7)-48); + a[2][2] = (int)(readMessage.charAt(8)-48); + turn = (int)(readMessage.charAt(10)-48); + String str= ""+a[0][0]+a[0][1]+a[0][2]+a[1][0]+a[1][1]+a[1][2]+a[2][0]+a[2][1]+a[2][2]+";"+turn; + + Log.i(TAG, "GOT : " + str); + + touchEnabled = true; + + if(!oncewin && !oncedrawen){ + postInvalidate(); + check(); + } + } + else{ + try{ + Log.i(TAG,"Hello"); + TwoDevice2P_names.OpponentName = readMessage; + Log.i(TAG,TwoDevice2P_names.MyName + " vs "+TwoDevice2P_names.OpponentName); + TwoDevice2P.act_2p.runOnUiThread( + new Runnable() { + @Override + public void run() { + Toast.makeText(TwoDevice2P.act_2p,TwoDevice2P_names.MyName + " vs "+TwoDevice2P_names.OpponentName,Toast.LENGTH_SHORT).show(); + } + } + ); + } + catch(Exception e){ + Log.d(TAG,e.getMessage()); + } } } catch (Exception e) { //Log.e(TAG, "disconnected", e); diff --git a/app/src/main/java/com/example/haresh/tic_tac_toe_app/TwoDevice2P_names.java b/app/src/main/java/com/example/haresh/tic_tac_toe_app/TwoDevice2P_names.java new file mode 100644 index 0000000..811c2c0 --- /dev/null +++ b/app/src/main/java/com/example/haresh/tic_tac_toe_app/TwoDevice2P_names.java @@ -0,0 +1,39 @@ +package com.example.haresh.tic_tac_toe_app; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; + +public class TwoDevice2P_names extends AppCompatActivity { + + public static String MyName; + public static String OpponentName; + Button btn; + EditText name; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_two_device2_p_names); + btn = (Button) findViewById(R.id.btn_find); + name = (EditText) findViewById(R.id.myName); + name.setText(""); + btn.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + MyName = name.getText().toString(); + if(MyName.trim().equals("")){ + name.setError("Enter Name"); + } + else{ + Intent intent = new Intent(TwoDevice2P_names.this,BluetoothActivity.class); + startActivity(intent); + } + } + } + ); + } +} diff --git a/app/src/main/java/com/example/haresh/tic_tac_toe_app/two_player_selector.java b/app/src/main/java/com/example/haresh/tic_tac_toe_app/two_player_selector.java index 8b32ae0..1047745 100644 --- a/app/src/main/java/com/example/haresh/tic_tac_toe_app/two_player_selector.java +++ b/app/src/main/java/com/example/haresh/tic_tac_toe_app/two_player_selector.java @@ -28,7 +28,7 @@ public void onClick(View v) { new View.OnClickListener() { @Override public void onClick(View v) { - Intent intent=new Intent(two_player_selector.this,BluetoothActivity.class); + Intent intent=new Intent(two_player_selector.this,TwoDevice2P_names.class); startActivity(intent); } } diff --git a/app/src/main/res/layout/activity_two_device2_p_names.xml b/app/src/main/res/layout/activity_two_device2_p_names.xml new file mode 100644 index 0000000..c9b7f04 --- /dev/null +++ b/app/src/main/res/layout/activity_two_device2_p_names.xml @@ -0,0 +1,27 @@ + + + + + +