[fix] Skip win animation if AI vs Player wins. Logical error in timong loops.
This commit is contained in:
+16
-11
@@ -58,11 +58,11 @@ void renderBoard();
|
|||||||
int getFirstEmptyRow(int col);
|
int getFirstEmptyRow(int col);
|
||||||
bool isBoardFull();
|
bool isBoardFull();
|
||||||
int8_t scanBoard();
|
int8_t scanBoard();
|
||||||
void updateThinkingVisuals(int8_t p, int8_t col);
|
void updateThinkingVisuals(int8_t playerColor, int8_t column);
|
||||||
void animateDrop(int col, int player);
|
void animateDrop(int col, int player);
|
||||||
void moveDiscToCol(int startCol, int targetCol, int player, int speed);
|
void moveDiscToCol(int startCol, int targetCol, int player, int speed);
|
||||||
int minimax(int depth, int alpha, int beta, bool isMax, int8_t aiP, int8_t huP, int8_t rootCol);
|
int minimax(int depth, int alpha, int beta, bool isMax, int8_t aiPlayer, int8_t humanPlayer, int8_t rootCol);
|
||||||
void performAiMove(int8_t aiP);
|
void performAiMove(int8_t aiPlayer);
|
||||||
void showMenu();
|
void showMenu();
|
||||||
int getDynamicPly();
|
int getDynamicPly();
|
||||||
|
|
||||||
@@ -212,7 +212,6 @@ int8_t scanBoard()
|
|||||||
return (int8_t)0;
|
return (int8_t)0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Horizontal
|
|
||||||
for (int row = 0; row < 6; row++)
|
for (int row = 0; row < 6; row++)
|
||||||
for (int col = 0; col < 4; col++)
|
for (int col = 0; col < 4; col++)
|
||||||
{
|
{
|
||||||
@@ -220,7 +219,6 @@ int8_t scanBoard()
|
|||||||
if (result)
|
if (result)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
// Vertical
|
|
||||||
for (int row = 0; row < 3; row++)
|
for (int row = 0; row < 3; row++)
|
||||||
for (int col = 0; col < 7; col++)
|
for (int col = 0; col < 7; col++)
|
||||||
{
|
{
|
||||||
@@ -228,7 +226,6 @@ int8_t scanBoard()
|
|||||||
if (result)
|
if (result)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
// Diagonal Up
|
|
||||||
for (int row = 0; row < 3; row++)
|
for (int row = 0; row < 3; row++)
|
||||||
for (int col = 0; col < 4; col++)
|
for (int col = 0; col < 4; col++)
|
||||||
{
|
{
|
||||||
@@ -236,7 +233,6 @@ int8_t scanBoard()
|
|||||||
if (result)
|
if (result)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
// Diagonal Down
|
|
||||||
for (int row = 3; row < 6; row++)
|
for (int row = 3; row < 6; row++)
|
||||||
for (int col = 0; col < 4; col++)
|
for (int col = 0; col < 4; col++)
|
||||||
{
|
{
|
||||||
@@ -254,6 +250,7 @@ int minimax(int depth, int alpha, int beta, bool isMax, int8_t aiPlayer, int8_t
|
|||||||
else
|
else
|
||||||
yield();
|
yield();
|
||||||
|
|
||||||
|
// Check winner via temporary scan (logic check only)
|
||||||
int8_t winner = scanBoard();
|
int8_t winner = scanBoard();
|
||||||
if (winner == aiPlayer)
|
if (winner == aiPlayer)
|
||||||
return 1000 + depth;
|
return 1000 + depth;
|
||||||
@@ -297,6 +294,7 @@ void performAiMove(int8_t aiPlayer)
|
|||||||
int originalPly = current_look_ahead;
|
int originalPly = current_look_ahead;
|
||||||
current_look_ahead = (gameState == DEMO) ? demoPly : getDynamicPly();
|
current_look_ahead = (gameState == DEMO) ? demoPly : getDynamicPly();
|
||||||
|
|
||||||
|
// Instant win/block logic
|
||||||
for (int column = 0; column < COLS; column++)
|
for (int column = 0; column < COLS; column++)
|
||||||
{
|
{
|
||||||
int row = getFirstEmptyRow(column);
|
int row = getFirstEmptyRow(column);
|
||||||
@@ -319,6 +317,7 @@ void performAiMove(int8_t aiPlayer)
|
|||||||
board[column][row] = 0;
|
board[column][row] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int column : {3, 2, 4, 1, 5, 0, 6})
|
for (int column : {3, 2, 4, 1, 5, 0, 6})
|
||||||
{
|
{
|
||||||
int row = getFirstEmptyRow(column);
|
int row = getFirstEmptyRow(column);
|
||||||
@@ -334,12 +333,14 @@ void performAiMove(int8_t aiPlayer)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((gameState == DEMO || blunder_enabled) && random(100) < 20)
|
if ((gameState == DEMO || blunder_enabled) && random(100) < 20)
|
||||||
{
|
{
|
||||||
int randomColumn = random(0, 7);
|
int randomColumn = random(0, 7);
|
||||||
if (getFirstEmptyRow(randomColumn) != -1)
|
if (getFirstEmptyRow(randomColumn) != -1)
|
||||||
bestCol = randomColumn;
|
bestCol = randomColumn;
|
||||||
}
|
}
|
||||||
|
|
||||||
finalizeMove:
|
finalizeMove:
|
||||||
current_look_ahead = originalPly;
|
current_look_ahead = originalPly;
|
||||||
moveDiscToCol(activeCol, bestCol, aiPlayer, 100);
|
moveDiscToCol(activeCol, bestCol, aiPlayer, 100);
|
||||||
@@ -458,6 +459,7 @@ void loop()
|
|||||||
long newPos = myEnc.read() / SENSITIVITY;
|
long newPos = myEnc.read() / SENSITIVITY;
|
||||||
bool pressed = (digitalRead(ENC_SW) == LOW);
|
bool pressed = (digitalRead(ENC_SW) == LOW);
|
||||||
|
|
||||||
|
// Activity check
|
||||||
if (newPos != oldEncPos || (pressed && (millis() - lastActivityTime > 500)))
|
if (newPos != oldEncPos || (pressed && (millis() - lastActivityTime > 500)))
|
||||||
{
|
{
|
||||||
if (gameState >= 2 || gameState == DEMO)
|
if (gameState >= 2 || gameState == DEMO)
|
||||||
@@ -519,12 +521,14 @@ void loop()
|
|||||||
{
|
{
|
||||||
activeCol = (newPos % 7 + 7) % 7;
|
activeCol = (newPos % 7 + 7) % 7;
|
||||||
oldEncPos = newPos;
|
oldEncPos = newPos;
|
||||||
|
lastActivityTime = millis();
|
||||||
}
|
}
|
||||||
renderBoard();
|
renderBoard();
|
||||||
leds[getIdx(activeCol, 0)] = (currentPlayer == 1) ? CRGB::Yellow : CRGB::Red;
|
leds[getIdx(activeCol, 0)] = (currentPlayer == 1) ? CRGB::Yellow : CRGB::Red;
|
||||||
FastLED.show();
|
FastLED.show();
|
||||||
if (pressed)
|
if (pressed)
|
||||||
{
|
{
|
||||||
|
lastActivityTime = millis();
|
||||||
int row = getFirstEmptyRow(activeCol);
|
int row = getFirstEmptyRow(activeCol);
|
||||||
if (row != -1)
|
if (row != -1)
|
||||||
{
|
{
|
||||||
@@ -544,8 +548,9 @@ void loop()
|
|||||||
{
|
{
|
||||||
if (menuMode < 2)
|
if (menuMode < 2)
|
||||||
{
|
{
|
||||||
int8_t aiPlayer = (menuMode == 0) ? 2 : 1;
|
int8_t aiP = (menuMode == 0) ? 2 : 1;
|
||||||
performAiMove(aiPlayer);
|
performAiMove(aiP);
|
||||||
|
lastActivityTime = millis(); // Reset after AI thinking
|
||||||
winnerPlayer = scanBoard();
|
winnerPlayer = scanBoard();
|
||||||
if (winnerPlayer != 0)
|
if (winnerPlayer != 0)
|
||||||
{
|
{
|
||||||
@@ -590,7 +595,7 @@ void loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ // FINISHED state
|
{ // FINISHED state (WIN/DRAW)
|
||||||
static uint32_t lastFlash = 0;
|
static uint32_t lastFlash = 0;
|
||||||
static bool toggle = true;
|
static bool toggle = true;
|
||||||
if (millis() - lastFlash > 300)
|
if (millis() - lastFlash > 300)
|
||||||
@@ -624,7 +629,7 @@ void loop()
|
|||||||
memset(board, 0, sizeof(board));
|
memset(board, 0, sizeof(board));
|
||||||
gameState = DEMO;
|
gameState = DEMO;
|
||||||
demoResetTimer = 0;
|
demoResetTimer = 0;
|
||||||
demoPly = random(2, 5);
|
demoPly = random(3, 7);
|
||||||
}
|
}
|
||||||
if (pressed)
|
if (pressed)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user