aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java')
-rw-r--r--app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java177
1 files changed, 137 insertions, 40 deletions
diff --git a/app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java b/app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java
index 2b1d918..e7a3210 100644
--- a/app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java
+++ b/app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java
@@ -4,8 +4,7 @@ import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
-import android.os.PersistableBundle;
-import android.util.Log;
+import android.os.Handler;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
@@ -14,19 +13,22 @@ import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
+import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
public class OfflineGameActivity extends AppCompatActivity {
+ final String TAG = "maks";
// 0player - O, 1player - X
int whoseTurn = ThreadLocalRandom.current().nextInt(0, 1 + 1);
int[] gameBoard = new int[]{-1, -1, -1, -1, -1, -1, -1, -1, -1};
-
ImageView p0Img;
ImageView p1Img;
-
TextView square0, square1, square2, square3, square4, square5, square6, square7, square8;
-
- final String TAG = "maks";
+ TextView[] squares;
+ TextView tvP0, tvP1;
+ boolean bot = false;
+ int playerIs = -1;
+ boolean botTurn = false;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
@@ -36,6 +38,8 @@ public class OfflineGameActivity extends AppCompatActivity {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_offline_game);
+ boolean playBotTurn = false;
+
square0 = findViewById(R.id.square0);
square1 = findViewById(R.id.square1);
square2 = findViewById(R.id.square2);
@@ -45,17 +49,57 @@ public class OfflineGameActivity extends AppCompatActivity {
square6 = findViewById(R.id.square6);
square7 = findViewById(R.id.square7);
square8 = findViewById(R.id.square8);
+ squares = new TextView[]{square0, square1, square2, square3, square4, square5, square6, square7, square8};
+
+ tvP0 = findViewById(R.id.tvP0);
+ tvP1 = findViewById(R.id.tvP1);
- Log.d(TAG, "onCreate: "+ savedInstanceState);
- if(savedInstanceState != null) {
+ if (savedInstanceState == null) {
+ new AlertDialog.Builder(this)
+ .setTitle("How do you want to play?")
+ .setMessage("Do you want to play with a bot or a friend?")
+ .setCancelable(false)
+ .setPositiveButton("Bot", (dialog, which) -> {
+ bot = true;
+ playerIs = ThreadLocalRandom.current().nextInt(0, 1 + 1);
+ whoseTurn = 0;
+ if (playerIs == 0) {
+ tvP0.setText("Player (O)");
+ tvP1.setText("Bot (X)");
+ } else {
+ tvP0.setText("Bot (O)");
+ tvP1.setText("Player (X)");
+ botTurn = true;
+ botTurn();
+ }
+ })
+ .setNegativeButton("Friend", (dialog, which) -> {
+ })
+ .setIcon(R.drawable.ic_baseline_gamepad_24)
+ .show();
+ } else {
int[] maybeGameBoard = savedInstanceState.getIntArray("gameBoard");
int maybeWhoseTurn = savedInstanceState.getInt("whoseTurn");
- if(maybeGameBoard != null) {
+ boolean maybeBot = savedInstanceState.getBoolean("bot");
+ int maybePlayerIs = savedInstanceState.getInt("playerIs");
+ if (maybeGameBoard != null) {
gameBoard = maybeGameBoard;
whoseTurn = maybeWhoseTurn;
+ bot = maybeBot;
+ playerIs = maybePlayerIs;
+ if (bot) {
+ if (playerIs == 0) {
+ tvP0.setText("Player (O)");
+ tvP1.setText("Bot (X)");
+ } else {
+ tvP0.setText("Bot (O)");
+ tvP1.setText("Player (X)");
+ playBotTurn = whoseTurn != playerIs;
+// botTurn = true;
+// botTurn();
+ }
+ }
}
- Log.d(TAG, "onCreate: "+ gameBoard);
- Log.d(TAG, "onCreate: "+ whoseTurn);
}
p0Img = findViewById(R.id.p0Img);
@@ -73,13 +117,18 @@ public class OfflineGameActivity extends AppCompatActivity {
for (TextView square :
new TextView[]{square0, square1, square2, square3, square4, square5, square6, square7, square8}) {
int finalI = i;
- square.setOnClickListener(view -> onSquareClick((TextView) view, finalI));
- if(gameBoard[i] == -1)
+ square.setOnClickListener(view -> onSquareClick((TextView) view, finalI, false));
+ if (gameBoard[i] == -1)
square.setText("");
else
square.setText(gameBoard[i] == 0 ? "O" : "X");
i++;
}
+
+ if (playBotTurn || (bot && playerIs == 1 && Arrays.stream(gameBoard).filter(c -> c == -1).count() == 9)) {
+ botTurn = true;
+ botTurn();
+ }
}
@Override
@@ -87,10 +136,12 @@ public class OfflineGameActivity extends AppCompatActivity {
super.onSaveInstanceState(outState);
outState.putIntArray("gameBoard", gameBoard);
outState.putInt("whoseTurn", whoseTurn);
+ outState.putBoolean("bot", bot);
+ outState.putInt("playerIs", playerIs);
}
- @RequiresApi(api = Build.VERSION_CODES.N)
- protected void onSquareClick(TextView square, int id) {
+ protected void onSquareClick(TextView square, int id, boolean force) {
+ if (botTurn && !force) return;
if (gameBoard[id] != -1) return;
gameBoard[id] = whoseTurn;
square.setText(whoseTurn == 0 ? "O" : "X");
@@ -101,7 +152,7 @@ public class OfflineGameActivity extends AppCompatActivity {
endGame(-1);
return;
}
- whoseTurn = whoseTurn == 0 ? 1 : 0;
+ whoseTurn = whoseTurn == 0 ? 1 : 0;
if (whoseTurn == 0) {
p0Img.setImageResource(R.mipmap.bg_you_in_army_foreground);
p1Img.setImageResource(R.mipmap.bg_no_no_foreground);
@@ -109,6 +160,17 @@ public class OfflineGameActivity extends AppCompatActivity {
p0Img.setImageResource(R.mipmap.bg_no_no_foreground);
p1Img.setImageResource(R.mipmap.bg_you_in_army_foreground);
}
+ if (bot && whoseTurn != playerIs) {
+ botTurn();
+ }
+ }
+
+ protected void botTurn() {
+ botTurn = true;
+ int chosenSquare = -1;
+ chosenSquare = Bot.getBestMove(gameBoard.clone(), playerIs == 0 ? 1 : 0, playerIs);
+ onSquareClick(squares[chosenSquare], chosenSquare, true);
+ botTurn = false;
}
protected boolean checkWon() {
@@ -117,7 +179,6 @@ public class OfflineGameActivity extends AppCompatActivity {
chkTriEqOnGB(0, 4, 8) || chkTriEqOnGB(2, 4, 6);
}
- @RequiresApi(api = Build.VERSION_CODES.N)
protected boolean checkDraw() {
return gameBoard[0] != -1 &&
gameBoard[1] != -1 &&
@@ -131,29 +192,65 @@ public class OfflineGameActivity extends AppCompatActivity {
}
protected void endGame(int whoWon) {
- new AlertDialog.Builder(this)
- .setTitle(whoWon == -1 ? "DRAW" : "Player " + (whoWon + 1) + " WON")
- .setMessage(whoWon == -1 ? "Maybe next time" : "Congratulations to Player " + (whoWon + 1))
- .setCancelable(false)
- .setPositiveButton("Play again", (dialog, which) -> {
- gameBoard = new int[]{-1, -1, -1, -1, -1, -1, -1, -1, -1};
- square0.setText("");
- square1.setText("");
- square2.setText("");
- square3.setText("");
- square4.setText("");
- square5.setText("");
- square6.setText("");
- square7.setText("");
- square8.setText("");
- whoseTurn = ThreadLocalRandom.current().nextInt(0, 1 + 1);
- })
- .setNegativeButton("Go to menu", (dialog, which) -> {
- Intent intent = new Intent(OfflineGameActivity.this, MainActivity.class);
- startActivity(intent);
- })
- .setIcon(android.R.drawable.ic_dialog_alert)
- .show();
+ botTurn = true;
+ int delay = bot ? 2000 : 0;
+ if (bot && whoWon == playerIs) delay = 0;
+ Handler handler = new Handler();
+ handler.postDelayed(() -> {
+ String title = "";
+ if (bot) {
+ if (whoWon == -1) title = "Draw, but you couldn't do better";
+ else title = whoWon == playerIs ? "You were cheating" : "Bot won, no surprise here";
+ } else title = whoWon == -1 ? "DRAW" : "Player " + (whoWon + 1) + " WON";
+
+ String msg = "";
+ if (bot) {
+ if (whoWon == -1) msg = "Maybe next time you'll loose";
+ else msg = whoWon == playerIs ? "Cheeeeeeeeater" : "You didn't have any chance";
+ } else
+ msg = whoWon == -1 ? "Maybe next time" : "Congratulations to Player " + (whoWon + 1);
+
+ new AlertDialog.Builder(this)
+ .setTitle(title)
+ .setMessage(msg)
+ .setCancelable(false)
+ .setPositiveButton("Play again", (dialog, which) -> {
+ botTurn = false;
+ gameBoard = new int[]{-1, -1, -1, -1, -1, -1, -1, -1, -1};
+ square0.setText("");
+ square1.setText("");
+ square2.setText("");
+ square3.setText("");
+ square4.setText("");
+ square5.setText("");
+ square6.setText("");
+ square7.setText("");
+ square8.setText("");
+ if (!bot)
+ whoseTurn = ThreadLocalRandom.current().nextInt(0, 1 + 1);
+ else {
+ playerIs = ThreadLocalRandom.current().nextInt(0, 1 + 1);
+ if (playerIs == 0) {
+ tvP0.setText("Player (O)");
+ tvP1.setText("Bot (X)");
+ } else {
+ tvP0.setText("Bot (O)");
+ tvP1.setText("Player (X)");
+ }
+ if (whoseTurn != playerIs) {
+ botTurn = true;
+ botTurn();
+ }
+ }
+ })
+ .setNegativeButton("Go to menu", (dialog, which) -> {
+ Intent intent = new Intent(OfflineGameActivity.this, MainActivity.class);
+ startActivity(intent);
+ })
+ .setIcon(android.R.drawable.ic_dialog_alert)
+ .show();
+ }, delay);
+
}
// CheckTripleEqualityOnGameBoard