This commit is contained in:
Hymmel 2026-02-10 15:09:35 +01:00
parent 9f946701c8
commit c10eb85957
9 changed files with 47 additions and 3 deletions

View file

@ -437,6 +437,16 @@ public class Main extends JFrame {
}
sb.append("\n");
}
// Threat Detection
char opponent = (mySymbol == 'X') ? 'O' : 'X';
String threats = findThreats(opponent);
if (!threats.isEmpty()) {
sb.append("\n!!! CRITICAL WARNING !!!\n");
sb.append(threats);
sb.append("You MUST block this threat immediately or you will lose.\n");
}
sb.append(
"\nYour goal is to WIN. If you cannot win immediately, BLOCK the opponent. If neither, play optimally to force a draw.\n");
sb.append("Analyze the board carefully. Don't make random moves.\n");
@ -444,6 +454,38 @@ public class Main extends JFrame {
return sb.toString();
}
private String findThreats(char opponent) {
int[][] wins = {
{ 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, // rows
{ 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, // cols
{ 0, 4, 8 }, { 2, 4, 6 } // diags
};
StringBuilder threatMsg = new StringBuilder();
for (int[] w : wins) {
// Check if 2 cells are opponent and 1 is empty
int opCount = 0;
int emptyCount = 0;
int emptyIndex = -1;
for (int idx : w) {
if (boardState[idx] == opponent)
opCount++;
else if (boardState[idx] == ' ') {
emptyCount++;
emptyIndex = idx;
}
}
if (opCount == 2 && emptyCount == 1) {
int r = emptyIndex / 3;
int c = emptyIndex % 3;
threatMsg.append("Opponent '").append(opponent).append("' is about to win at row ").append(r)
.append(", col ").append(c).append(".\n");
}
}
return threatMsg.toString();
}
private String callOpenRouter(String prompt) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
String jsonBody = "{"

View file

@ -1,5 +1,5 @@
#Generated by Maven
#Tue Feb 10 14:58:36 CET 2026
#Tue Feb 10 15:06:20 CET 2026
artifactId=ai-client
groupId=com.lona.tictactoe
version=1.0-SNAPSHOT

View file

@ -161,7 +161,8 @@ input[type="text"]:focus {
.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-template-rows: repeat(3, minmax(0, 1fr));
gap: 10px;
aspect-ratio: 1;
margin: 0 auto;

View file

@ -161,7 +161,8 @@ input[type="text"]:focus {
.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-template-rows: repeat(3, minmax(0, 1fr));
gap: 10px;
aspect-ratio: 1;
margin: 0 auto;