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