#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
const int JOY_X = 35, JOY_Y = 34, JOY_SEL = 27;
const bool FLIP_X = true; // set true if left/right feel reversed
const bool FLIP_Y = true; // set true if up/down feel reversed
// ---------- layout ----------
const int CW = 40, CH = 32; // card size
const int COL_STEP = 45, X0 = 2; // column spacing / left margin
const int TOP_Y = 2; // top row (stock, waste, foundations)
const int TAB_Y = 40; // first tableau card
const int GAP_DOWN = 4; // spacing between face-down cards
const int GAP_UP = 11; // spacing between face-up cards
const uint16_t FELT = 0x03E0;
const uint16_t SLOT = 0x07E0;
const char *RANKS[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
// Card = 0..51. rank = c % 13 (0=Ace..12=King), suit = c / 13 (0=Spade 1=Heart 2=Diamond 3=Club)
// Built-in font glyphs: 6=spade 3=heart 4=diamond 5=club
const uint8_t SUIT_CH[4] = {6, 3, 4, 5};
// ---------- game state ----------
uint8_t deck[52];
uint8_t tab[7][20];
uint8_t tabCount[7];
uint8_t tabUp[7]; // index of first face-up card in each column
uint8_t stock[24], stockCount;
uint8_t waste[24], wasteCount;
uint8_t found[4]; // cards on each foundation (by suit)
// cursor: row 0 = top row (cols 0,1,3..6), row 1 = tableau (cols 0..6)
int row = 0, col = 0, curIdx = 0;
// holding a card/stack
bool holding = false;
uint8_t srcType = 0; // 0 = waste, 1 = tableau, 2 = foundation
uint8_t srcCol = 0;
int srcIdx = 0;
int holdCount = 0;
bool won = false;
bool dirtyTop[7], dirtyTab[7];
enum Dir { D_NONE, D_LEFT, D_RIGHT, D_UP, D_DOWN };
// ---------- helpers ----------
bool isRed(uint8_t c) { int s = c / 13; return s == 1 || s == 2; }
int colX(int c) { return X0 + c * COL_STEP; }
int tabCardY(int c, int i) {
int y = TAB_Y;
for (int j = 0; j < i; j++) y += (j < tabUp[c]) ? GAP_DOWN : GAP_UP;
return y;
}
void markCursor() {
if (row == 0) dirtyTop[col] = true;
else dirtyTab[col] = true;
}
void markSrc() {
if (srcType == 0) dirtyTop[1] = true;
else if (srcType == 2) dirtyTop[3 + srcCol] = true;
else dirtyTab[srcCol] = true;
}
void setIdxToLast() {
curIdx = tabCount[col] > 0 ? tabCount[col] - 1 : 0;
}
// ---------- setup of a game ----------
void shuffleDeck() {
for (int i = 0; i < 52; i++) deck[i] = i;
for (int i = 51; i > 0; i--) {
int j = random(i + 1);
uint8_t t = deck[i]; deck[i] = deck[j]; deck[j] = t;
}
}
void deal() {
int n = 0;
for (int c = 0; c < 7; c++) {
tabCount[c] = c + 1;
tabUp[c] = c;
for (int r = 0; r <= c; r++) tab[c][r] = deck[n++];
}
stockCount = 0;
while (n < 52) stock[stockCount++] = deck[n++];
wasteCount = 0;
for (int i = 0; i < 4; i++) found[i] = 0;
}
void newGame() {
shuffleDeck();
deal();
holding = false;
won = false;
row = 0; col = 0; curIdx = 0;
tft.fillScreen(FELT);
for (int i = 0; i < 7; i++) { dirtyTop[i] = true; dirtyTab[i] = true; }
}
// ---------- drawing ----------
void outline(int x, int y, int w, int h, uint16_t color) {
tft.drawRoundRect(x - 2, y - 2, w + 4, h + 4, 4, color);
tft.drawRoundRect(x - 1, y - 1, w + 2, h + 2, 3, color);
}
void drawEmpty(int x, int y) {
tft.drawRoundRect(x, y, CW, CH, 3, SLOT);
}
// big = card is fully visible, so also draw a large suit symbol
void drawCard(int x, int y, uint8_t c, bool faceUp, bool big) {
if (!faceUp) {
tft.fillRoundRect(x, y, CW, CH, 3, ILI9341_BLUE);
tft.drawRoundRect(x, y, CW, CH, 3, ILI9341_WHITE);
tft.drawRect(x + 4, y + 4, CW - 8, CH - 8, 0x7BEF);
return;
}
uint16_t ink = isRed(c) ? ILI9341_RED : ILI9341_BLACK;
tft.fillRoundRect(x, y, CW, CH, 3, ILI9341_WHITE);
tft.drawRoundRect(x, y, CW, CH, 3, ILI9341_BLACK);
tft.setTextColor(ink);
tft.setTextSize(1);
tft.setCursor(x + 3, y + 3);
tft.print(RANKS[c % 13]);
tft.write(SUIT_CH[c / 13]);
if (big) {
tft.setTextSize(2);
tft.setCursor(x + 14, y + 14);
tft.write(SUIT_CH[c / 13]);
}
}
void drawTopSlot(int s) {
int x = colX(s), y = TOP_Y;
tft.fillRect(s * COL_STEP, 0, COL_STEP, TAB_Y - 3, FELT);
if (s == 0) {
if (stockCount > 0) drawCard(x, y, 0, false, false);
else {
drawEmpty(x, y);
tft.setTextColor(SLOT);
tft.setTextSize(2);
tft.setCursor(x + 14, y + 8);
tft.print("O");
}
} else if (s == 1) {
if (wasteCount > 0) drawCard(x, y, waste[wasteCount - 1], true, true);
else drawEmpty(x, y);
} else if (s >= 3) {
int su = s - 3;
if (found[su] > 0) drawCard(x, y, su * 13 + found[su] - 1, true, true);
else {
drawEmpty(x, y);
tft.setTextColor(SLOT);
tft.setTextSize(2);
tft.setCursor(x + 14, y + 8);
tft.write(SUIT_CH[su]);
}
}
if (holding && srcType == 0 && s == 1) outline(x, y, CW, CH, ILI9341_CYAN);
if (holding && srcType == 2 && s == 3 + srcCol) outline(x, y, CW, CH, ILI9341_CYAN);
if (row == 0 && col == s) outline(x, y, CW, CH, ILI9341_YELLOW);
}
void drawTabCol(int c) {
int x = colX(c);
tft.fillRect(c * COL_STEP, TAB_Y - 3, COL_STEP, 240 - (TAB_Y - 3), FELT);
if (tabCount[c] == 0) drawEmpty(x, TAB_Y);
int y = TAB_Y;
for (int i = 0; i < tabCount[c]; i++) {
bool up = i >= tabUp[c];
bool last = (i == tabCount[c] - 1);
drawCard(x, y, tab[c][i], up, last);
if (!last) y += up ? GAP_UP : GAP_DOWN;
}
// y is now the top of the last card (or TAB_Y if the column is empty)
int endY = y + CH;
if (holding && srcType == 1 && srcCol == c) {
int sy = tabCardY(c, srcIdx);
outline(x, sy, CW, endY - sy, ILI9341_CYAN);
}
if (row == 1 && col == c) {
if (holding || tabCount[c] == 0) {
outline(x, y, CW, CH, ILI9341_YELLOW);
} else {
int sy = tabCardY(c, curIdx);
outline(x, sy, CW, endY - sy, ILI9341_YELLOW);
}
}
}
void drawWin() {
tft.fillRoundRect(60, 90, 200, 60, 6, ILI9341_BLACK);
tft.drawRoundRect(60, 90, 200, 60, 6, ILI9341_YELLOW);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(3);
tft.setCursor(88, 100);
tft.print("YOU WIN!");
tft.setTextSize(1);
tft.setCursor(106, 132);
tft.print("Click for new game");
}
void render() {
bool drew = false;
for (int s = 0; s < 7; s++) {
if (dirtyTop[s]) { drawTopSlot(s); dirtyTop[s] = false; drew = true; }
}
for (int c = 0; c < 7; c++) {
if (dirtyTab[c]) { drawTabCol(c); dirtyTab[c] = false; drew = true; }
}
if (won && drew) drawWin();
}
void flashInvalid() {
if (row == 0) {
outline(colX(col), TOP_Y, CW, CH, ILI9341_RED);
} else {
int last = tabCount[col] > 0 ? tabCount[col] - 1 : 0;
outline(colX(col), tabCardY(col, last), CW, CH, ILI9341_RED);
}
delay(250);
markCursor();
}
// ---------- input ----------
Dir readStick() {
static bool held = false;
int x = analogRead(JOY_X);
int y = analogRead(JOY_Y);
if (FLIP_X) x = 4095 - x;
if (FLIP_Y) y = 4095 - y;
Dir d = D_NONE;
if (x < 1000) d = D_LEFT;
else if (x > 3000) d = D_RIGHT;
else if (y < 1000) d = D_UP;
else if (y > 3000) d = D_DOWN;
if (d == D_NONE) { held = false; return D_NONE; }
if (held) return D_NONE;
held = true;
return d;
}
// 0 = nothing, 1 = short click, 2 = long press (released)
int readSel() {
static bool down = false;
static uint32_t t0 = 0;
bool now = (digitalRead(JOY_SEL) == LOW);
int ev = 0;
if (now && !down) { down = true; t0 = millis(); }
else if (!now && down) {
down = false;
ev = (millis() - t0 > 1500) ? 2 : 1;
}
return ev;
}
// ---------- game logic ----------
void handleMove(Dir d) {
markCursor();
switch (d) {
case D_LEFT:
col = (col + 6) % 7;
if (row == 0 && col == 2) col = 1;
setIdxToLast();
break;
case D_RIGHT:
col = (col + 1) % 7;
if (row == 0 && col == 2) col = 3;
setIdxToLast();
break;
case D_UP:
if (row == 1) {
if (!holding && tabCount[col] > 0 && curIdx > tabUp[col]) {
curIdx--;
} else {
row = 0;
if (col == 2) col = 1;
}
}
break;
case D_DOWN:
if (row == 0) {
row = 1;
setIdxToLast();
} else if (!holding && curIdx < (int)tabCount[col] - 1) {
curIdx++;
}
break;
default: break;
}
markCursor();
}
uint8_t heldCard(int k) {
if (srcType == 0) return waste[wasteCount - 1];
if (srcType == 2) return srcCol * 13 + found[srcCol] - 1;
return tab[srcCol][srcIdx + k];
}
void fixTab(int c) {
if (tabCount[c] == 0) tabUp[c] = 0;
else if (tabUp[c] >= tabCount[c]) tabUp[c] = tabCount[c] - 1; // flip the new top card
}
void removeHeld() {
if (srcType == 0) wasteCount--;
else if (srcType == 2) found[srcCol]--;
else { tabCount[srcCol] -= holdCount; fixTab(srcCol); }
}
void checkWin() {
for (int i = 0; i < 4; i++) if (found[i] < 13) return;
won = true;
dirtyTop[2] = true; // forces a render pass, which draws the win box
}
bool tryDrop() {
uint8_t first = heldCard(0);
if (row == 1) {
int d = col;
if (tabCount[d] > 0) {
uint8_t top = tab[d][tabCount[d] - 1];
if (isRed(top) == isRed(first)) return false; // must alternate color
if ((int)(first % 13) != (int)(top % 13) - 1) return false; // one rank lower
}
uint8_t buf[20];
int n = holdCount;
for (int k = 0; k < n; k++) buf[k] = heldCard(k);
removeHeld();
markSrc();
if (tabCount[d] == 0) tabUp[d] = 0;
for (int k = 0; k < n; k++) tab[d][tabCount[d]++] = buf[k];
dirtyTab[d] = true;
holding = false;
curIdx = tabCount[d] - 1;
} else if (col >= 3) {
if (holdCount != 1) return false; // one card at a time
int s = first / 13;
if (found[s] != first % 13) return false; // must be the next rank of its suit
removeHeld();
markSrc();
found[s]++;
holding = false;
markCursor();
col = 3 + s; // cursor follows the card to its pile
markCursor();
} else {
return false;
}
checkWin();
return true;
}
void handleSelect() {
if (won) { newGame(); return; }
if (!holding) {
if (row == 0) {
if (col == 0) {
if (stockCount > 0) {
waste[wasteCount++] = stock[--stockCount];
} else {
while (wasteCount > 0) stock[stockCount++] = waste[--wasteCount];
}
dirtyTop[0] = true;
dirtyTop[1] = true;
} else if (col == 1 && wasteCount > 0) {
holding = true; srcType = 0; holdCount = 1;
dirtyTop[1] = true;
} else if (col >= 3 && found[col - 3] > 0) {
holding = true; srcType = 2; srcCol = col - 3; holdCount = 1;
dirtyTop[col] = true;
}
} else if (tabCount[col] > 0) {
holding = true; srcType = 1; srcCol = col; srcIdx = curIdx;
holdCount = tabCount[col] - curIdx;
dirtyTab[col] = true;
}
return;
}
// holding something: cancel if clicking where it came from
bool cancel =
(srcType == 1 && row == 1 && col == srcCol) ||
(srcType == 0 && row == 0 && col == 1) ||
(srcType == 2 && row == 0 && col >= 3);
if (cancel) {
holding = false;
markSrc();
markCursor();
return;
}
if (!tryDrop()) flashInvalid();
}
// ---------- Arduino ----------
void setup() {
Serial.begin(115200);
pinMode(JOY_SEL, INPUT_PULLUP);
randomSeed(esp_random());
tft.begin();
tft.setRotation(1); // landscape 320x240
newGame();
}
void loop() {
Dir d = readStick();
if (d != D_NONE) handleMove(d);
int s = readSel();
if (s == 2) newGame();
else if (s == 1) handleSelect();
render();
delay(10);
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4