#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Keypad.h>
// Piny wyświetlacza TFT ILI9341
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Konfiguracja klawiatury numerycznej 4x4
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {1, 2, 3, 4};
byte colPins[COLS] = {5, 6, 7, 8};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Zmienne gry
int playerScores[4] = {501, 501, 501, 501};
int currentPlayer = 0;
int throwsLeft = 3;
int roundTotal = 0; // Suma punktów w kolejce
bool gameOver = false;
int previousScores[3] = {0, 0, 0}; // Przechowywanie punktów w kolejce
int currentThrowIndex = 0; // Indeks rzutu w kolejce
String input = "";
// Funkcja wyświetlania wyników
void updateScreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(4);
tft.setTextSize(2);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(10, 10);
tft.println("Scores:");
for (int i = 0; i < 4; i++) {
tft.setCursor(10, 50 + i * 30);
tft.print("Player ");
tft.print(i + 1);
tft.print(": ");
tft.print(playerScores[i]);
}
tft.setCursor(10, 180);
tft.setTextColor(ILI9341_WHITE);
tft.print("Player ");
tft.print(currentPlayer + 1);
tft.println("'s turn");
tft.print("Throws left: ");
tft.print(throwsLeft);
tft.setCursor(10, 210);
tft.print("Round total: ");
tft.println(roundTotal);
// Wyświetl aktualnie wpisywaną liczbę
tft.setCursor(10, 240);
tft.print("Input: ");
tft.println(input);
}
// Funkcja przetwarzania wpisu
int processThrow(String input) {
if (input.startsWith("D")) {
return input.substring(1).toInt() * 2; // Double
} else if (input.startsWith("C")) {
return input.substring(1).toInt() * 3; // Triple
} else {
return input.toInt(); // Single
}
}
// Cofnięcie ostatniego rzutu
void undoLastThrow() {
if (currentThrowIndex > 0) {
currentThrowIndex--;
int undoPoints = previousScores[currentThrowIndex];
playerScores[currentPlayer] += undoPoints;
roundTotal -= undoPoints;
throwsLeft++;
updateScreen();
}
}
// Cofnięcie całej kolejki
void undoRound() {
for (int i = 0; i < currentThrowIndex; i++) {
playerScores[currentPlayer] += previousScores[i];
roundTotal -= previousScores[i];
}
throwsLeft = 3;
currentThrowIndex = 0;
updateScreen();
}
// Sprawdzenie zakończenia gry
bool checkGameOver(int score, String input) {
return (score == 0 && input.startsWith("D"));
}
// Funkcja obsługi kolejki gracza
void handlePlayerTurn() {
char key = keypad.getKey();
if (key) {
if (key == '*') { // Zakończenie kolejki
if (--throwsLeft == 0) {
throwsLeft = 3;
roundTotal = 0;
currentThrowIndex = 0;
currentPlayer = (currentPlayer + 1) % 4;
}
input = "";
updateScreen();
} else if (key == '#') { // Wprowadzenie rzutu
int points = processThrow(input);
playerScores[currentPlayer] -= points;
roundTotal += points;
previousScores[currentThrowIndex++] = points;
// Sprawdzenie zakończenia gry
if (checkGameOver(playerScores[currentPlayer], input)) {
gameOver = true;
tft.fillScreen(ILI9341_GREEN);
tft.setTextSize(2);
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.println("Game Over!");
tft.setCursor(10, 50);
tft.print("Winner: Player ");
tft.println(currentPlayer + 1);
return;
}
input = "";
throwsLeft--;
updateScreen();
} else if (key == 'B') { // Cofnięcie ostatniego rzutu
undoLastThrow();
} else if (key == 'A') { // Cofnięcie całej kolejki
undoRound();
} else {
input += key; // Wpisz kolejną cyfrę
}
}
}
// Inicjalizacja programu
void setup() {
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.println("Dart Game: 501");
delay(2000);
updateScreen();
}
// Pętla główna
void loop() {
if (!gameOver) {
handlePlayerTurn();
}
}