Skip to content

Commit

Permalink
Uttt pass and play (#334)
Browse files Browse the repository at this point in the history
#328 Add UTTT pass-and-play
  • Loading branch information
AEFeinstein authored Dec 2, 2024
1 parent 8ef3de5 commit 36b5b6f
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 34 deletions.
Binary file modified assets/ultimateTTT/sq_bl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/ultimateTTT/sq_bs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/ultimateTTT/sq_rl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/ultimateTTT/sq_rs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 29 additions & 16 deletions main/modes/games/ultimateTTT/ultimateTTT.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ static void tttMsgRxCb(p2pInfo* p2p, const uint8_t* payload, uint8_t len);
//==============================================================================

// It's good practice to declare immutable strings as const so they get placed in ROM, not RAM
static const char tttName[] = "Ultimate TTT";
static const char tttMultiStr[] = "Wireless Connect";
static const char tttMultiShortStr[] = "Connect";
static const char tttSingleStr[] = "Single Player";
static const char tttDiffEasyStr[] = "Easy";
static const char tttDiffMediumStr[] = "Medium";
static const char tttDiffHardStr[] = "Hard";
static const char tttMarkerSelStr[] = "Marker Select";
static const char tttHowToStr[] = "How To Play";
static const char tttResultStr[] = "Result";
static const char tttRecordsStr[] = "Records";
static const char tttExit[] = "Exit";
static const char tttName[] = "Ultimate TTT";
static const char tttMultiStr[] = "Wireless Connect";
static const char tttPassAndPlayStr[] = "Pass and Play";
static const char tttMultiShortStr[] = "Connect";
static const char tttSingleStr[] = "Single Player";
static const char tttDiffEasyStr[] = "Easy";
static const char tttDiffMediumStr[] = "Medium";
static const char tttDiffHardStr[] = "Hard";
static const char tttMarkerSelStr[] = "Marker Select";
static const char tttHowToStr[] = "How To Play";
static const char tttResultStr[] = "Result";
static const char tttRecordsStr[] = "Records";
static const char tttExit[] = "Exit";

// NVS keys
const char tttWinKey[] = "ttt_win";
Expand Down Expand Up @@ -124,6 +125,7 @@ static void tttEnterMode(void)
// Initialize the main menu
ttt->menu = initMenu(tttName, tttMenuCb);
addSingleItemToMenu(ttt->menu, tttMultiStr);
addSingleItemToMenu(ttt->menu, tttPassAndPlayStr);

ttt->menu = startSubMenu(ttt->menu, tttSingleStr);
addSingleItemToMenu(ttt->menu, tttDiffEasyStr);
Expand Down Expand Up @@ -338,29 +340,40 @@ static void tttMenuCb(const char* label, bool selected, uint32_t value)
{
if (tttMultiStr == label)
{
ttt->game.singlePlayer = false;
ttt->game.singleSystem = false;
ttt->game.passAndPlay = false;
// Show connection UI
tttShowUi(TUI_CONNECTING);
// Start multiplayer
p2pStartConnection(&ttt->game.p2p);
}
else if (tttPassAndPlayStr == label)
{
ttt->game.singleSystem = true;
ttt->game.passAndPlay = true;
tttBeginGame(ttt);
tttShowUi(TUI_GAME);
}
else if (tttDiffEasyStr == label)
{
ttt->game.singlePlayer = true;
ttt->game.singleSystem = true;
ttt->game.passAndPlay = false;
ttt->game.cpu.difficulty = TDIFF_EASY;
tttBeginGame(ttt);
tttShowUi(TUI_GAME);
}
else if (tttDiffMediumStr == label)
{
ttt->game.singlePlayer = true;
ttt->game.singleSystem = true;
ttt->game.passAndPlay = false;
ttt->game.cpu.difficulty = TDIFF_MEDIUM;
tttBeginGame(ttt);
tttShowUi(TUI_GAME);
}
else if (tttDiffHardStr == label)
{
ttt->game.singlePlayer = true;
ttt->game.singleSystem = true;
ttt->game.passAndPlay = false;
ttt->game.cpu.difficulty = TDIFF_HARD;
tttBeginGame(ttt);
tttShowUi(TUI_GAME);
Expand Down
3 changes: 2 additions & 1 deletion main/modes/games/ultimateTTT/ultimateTTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ typedef struct

typedef struct
{
bool singlePlayer;
bool singleSystem;
bool passAndPlay;
playOrder_t singlePlayerPlayOrder;
p2pInfo p2p;
tttGameState_t state;
Expand Down
64 changes: 48 additions & 16 deletions main/modes/games/ultimateTTT/ultimateTTTgame.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void tttBeginGame(ultimateTTT_t* ttt)
tttShowUi(TUI_GAME);

// Randomly determine play order for single player
if (ttt->game.singlePlayer)
if (ttt->game.singleSystem)
{
ttt->game.singlePlayerPlayOrder = (esp_random() % 2) ? GOING_FIRST : GOING_SECOND;
}
Expand All @@ -108,12 +108,15 @@ void tttBeginGame(ultimateTTT_t* ttt)
// Send it to the second player
tttSendMarker(ttt, ttt->game.p1MarkerIdx);

if (ttt->game.singlePlayer)
if (ttt->game.singleSystem)
{
ttt->game.state = TGS_PLACING_MARKER;
ttt->game.cpu.state = TCPU_INACTIVE;
ttt->game.state = TGS_PLACING_MARKER;
if (!ttt->game.passAndPlay)
{
ttt->game.cpu.state = TCPU_INACTIVE;
}

// Randomize CPU marker to be not the players
// Randomize markers to be not match
ttt->game.p2MarkerIdx = esp_random() % ttt->numUnlockedMarkers;
// While the markers match
while (ttt->game.p1MarkerIdx == ttt->game.p2MarkerIdx)
Expand All @@ -123,12 +126,19 @@ void tttBeginGame(ultimateTTT_t* ttt)
}
}
}
else if (ttt->game.singlePlayer)
else if (ttt->game.singleSystem)
{
ttt->game.state = TGS_WAITING;
ttt->game.cpu.state = TCPU_THINKING;
if (ttt->game.passAndPlay)
{
ttt->game.state = TGS_PLACING_MARKER;
}
else
{
ttt->game.state = TGS_WAITING;
ttt->game.cpu.state = TCPU_THINKING;
}

// Randomize CPU marker to be not the players
// Randomize markers to be not match
ttt->game.p1MarkerIdx = esp_random() % ttt->numUnlockedMarkers;
// While the markers match
while (ttt->game.p1MarkerIdx == ttt->game.p2MarkerIdx)
Expand Down Expand Up @@ -528,10 +538,13 @@ void tttReceiveCursor(ultimateTTT_t* ttt, const tttMsgMoveCursor_t* msg)
*/
void tttSendPlacedMarker(ultimateTTT_t* ttt)
{
if (ttt->game.singlePlayer)
if (ttt->game.singleSystem)
{
ttt->game.state = TGS_WAITING;
ttt->game.cpu.state = TCPU_THINKING;
ttt->game.state = TGS_WAITING;
if (!ttt->game.passAndPlay)
{
ttt->game.cpu.state = TCPU_THINKING;
}
}
else if (ttt->game.p2p.cnc.isConnected)
{
Expand Down Expand Up @@ -712,7 +725,7 @@ static void tttPlaceMarker(ultimateTTT_t* ttt, const vec_t* subgame, const vec_t
ttt->lastResult = TTR_DRAW;
}

if (!ttt->game.singlePlayer)
if (!ttt->game.singleSystem)
{
// Stop p2p
p2pDeinit(&ttt->game.p2p);
Expand Down Expand Up @@ -845,7 +858,7 @@ tttPlayer_t tttCheckSubgameWinner(tttSubgame_t* subgame)
*/
static playOrder_t tttGetPlayOrder(ultimateTTT_t* ttt)
{
if (ttt->game.singlePlayer)
if (ttt->game.singleSystem)
{
return ttt->game.singlePlayerPlayOrder;
}
Expand Down Expand Up @@ -1002,9 +1015,28 @@ void tttDrawGame(ultimateTTT_t* ttt)
}
}

if (ttt->game.singlePlayer && ttt->game.state == TGS_WAITING)
if (ttt->game.singleSystem && ttt->game.state == TGS_WAITING)
{
tttCpuNextMove(ttt);
if (!ttt->game.passAndPlay)
{
// Let CPU make the next move
tttCpuNextMove(ttt);
}
else
{
// Flip the active player
if (GOING_FIRST == ttt->game.singlePlayerPlayOrder)
{
ttt->game.singlePlayerPlayOrder = GOING_SECOND;
}
else
{
ttt->game.singlePlayerPlayOrder = GOING_FIRST;
}

// Let the active player place a marker
ttt->game.state = TGS_PLACING_MARKER;
}
}
}

Expand Down
18 changes: 17 additions & 1 deletion main/modes/games/ultimateTTT/ultimateTTTresult.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// Variables
//==============================================================================

static const char redStr[] = "Red wins!";
static const char blueStr[] = "Blue wins!";
static const char winStr[] = "A winner is you!";
static const char lossStr[] = "You lost :(";
static const char drawStr[] = "It is a draw.";
Expand Down Expand Up @@ -60,7 +62,21 @@ void tttDrawResult(ultimateTTT_t* ttt, int64_t elapsedUs)
{
case TTR_WIN:
{
resultStr = winStr;
if (ttt->game.singleSystem)
{
if (GOING_FIRST == ttt->game.singlePlayerPlayOrder)
{
resultStr = redStr;
}
else
{
resultStr = blueStr;
}
}
else
{
resultStr = winStr;
}
break;
}
case TTR_LOSE:
Expand Down

0 comments on commit 36b5b6f

Please sign in to comment.