aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com')
-rw-r--r--app/src/main/java/com/jopek/pai_fillo_nai/Bot.java3
-rw-r--r--app/src/main/java/com/jopek/pai_fillo_nai/OfflineGameActivity.java5
-rw-r--r--app/src/main/java/com/jopek/pai_fillo_nai/OnlineGameActivity.java23
3 files changed, 20 insertions, 11 deletions
diff --git a/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java b/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
index 8f59d2b..d96367d 100644
--- a/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
+++ b/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
@@ -34,6 +34,8 @@ public class Bot {
static int minimax(int depth, Boolean isMax) {
int score = goodOrBad();
+ if (score == 10 && depth < 2)
+ return score * 100_000;
if (score == 10)
return score;
@@ -61,6 +63,7 @@ public class Bot {
if (board[i] == -1) {
board[i] = opponent;
best = Math.min(best, minimax(depth + 1, !isMax));
+
board[i] = -1;
}
}
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 e7a3210..cfc23f6 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
@@ -5,6 +5,7 @@ import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
+import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
@@ -141,6 +142,7 @@ public class OfflineGameActivity extends AppCompatActivity {
}
protected void onSquareClick(TextView square, int id, boolean force) {
+ Log.d(TAG, "onSquareClick: " + botTurn + " " + force);
if (botTurn && !force) return;
if (gameBoard[id] != -1) return;
gameBoard[id] = whoseTurn;
@@ -170,7 +172,8 @@ public class OfflineGameActivity extends AppCompatActivity {
int chosenSquare = -1;
chosenSquare = Bot.getBestMove(gameBoard.clone(), playerIs == 0 ? 1 : 0, playerIs);
onSquareClick(squares[chosenSquare], chosenSquare, true);
- botTurn = false;
+ if (!checkWon())
+ botTurn = false;
}
protected boolean checkWon() {
diff --git a/app/src/main/java/com/jopek/pai_fillo_nai/OnlineGameActivity.java b/app/src/main/java/com/jopek/pai_fillo_nai/OnlineGameActivity.java
index 461c1b1..7c76cec 100644
--- a/app/src/main/java/com/jopek/pai_fillo_nai/OnlineGameActivity.java
+++ b/app/src/main/java/com/jopek/pai_fillo_nai/OnlineGameActivity.java
@@ -38,7 +38,7 @@ public class OnlineGameActivity extends AppCompatActivity {
List<Long> gameBoard = new ArrayList<>(
Arrays.asList(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L)
);
- List<ImageView> squares = new ArrayList<>();
+ List<TextView> squares = new ArrayList<>();
DatabaseReference rooms;
DatabaseReference room;
// FinishGameAlert
@@ -50,7 +50,7 @@ public class OnlineGameActivity extends AppCompatActivity {
String TAG = "maks";
ImageView p0Img;
//ImageView p1Img;
- ImageView square0, square1, square2, square3, square4, square5, square6, square7, square8;
+ TextView square0, square1, square2, square3, square4, square5, square6, square7, square8;
TextView tvName;
@RequiresApi(api = Build.VERSION_CODES.N)
@@ -77,9 +77,9 @@ public class OnlineGameActivity extends AppCompatActivity {
square8 = findViewById(R.id.square8);
squares.addAll(Arrays.asList(square0, square1, square2, square3, square4, square5, square6, square7, square8));
int i = 0;
- for (ImageView square : squares) {
+ for (TextView square : squares) {
int finalI = i;
- square.setOnClickListener(view -> onSquareClick((ImageView) view, finalI));
+ square.setOnClickListener(view -> onSquareClick((TextView) view, finalI));
i++;
}
rooms = FirebaseDatabase.getInstance("https://pai-fillo-nai-default-rtdb.europe-west1.firebasedatabase.app/").getReference("rooms");
@@ -254,13 +254,16 @@ public class OnlineGameActivity extends AppCompatActivity {
protected void playTurn() {
int i = 0;
- for (ImageView square : squares) {
+ for (TextView square : squares) {
if (gameBoard.get(i) == -1) {
- square.setImageResource(R.mipmap.emptiness_foreground);
+// square.setImageResource(R.mipmap.emptiness_foreground);
+ square.setText("");
} else if (gameBoard.get(i) == 0) {
- square.setImageResource(R.mipmap.bg_o_foreground);
+// square.setImageResource(R.mipmap.bg_o_foreground);
+ square.setText("O");
} else if (gameBoard.get(i) == 1) {
- square.setImageResource(R.mipmap.bg_x_foreground);
+// square.setImageResource(R.mipmap.bg_x_foreground);
+ square.setText("X");
}
i++;
}
@@ -277,10 +280,10 @@ public class OnlineGameActivity extends AppCompatActivity {
}
@RequiresApi(api = Build.VERSION_CODES.N)
- protected void onSquareClick(ImageView square, int id) {
+ protected void onSquareClick(TextView square, int id) {
if (gameBoard.get(id) != -1 || !myTurn) return;
gameBoard.set(id, whoseTurn);
- square.setImageResource(whoseTurn == 0 ? R.mipmap.bg_o_foreground : R.mipmap.bg_x_foreground);
+ square.setText(whoseTurn == 0 ? "O" : "X");
if (checkWon()) {
endGame(whoseTurn);
return;