#include <LiquidCrystal.h>
#include <stdlib.h> // For random number generation
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
// --- Game Variables ---
int playerGold = 100;
int playerFood = 50;
int playerArmy = 10;
int enemyArmy = 20;
int day = 1;
int location = 1; // 1: Forest, 2: Mountains, 3: Plains
// --- Function to display text on LCD with scrolling ---
void displayText(String text) {
lcd.clear();
for (int i = 0; i < text.length(); i++) {
lcd.scrollDisplayLeft();
lcd.print(text.charAt(i));
delay(300);
}
delay(1000);
}
// --- Function to get button input ---
int getButtonInput() {
if (digitalRead(button1) == LOW) {
return 1;
}
if (digitalRead(button2) == LOW) {
return 2;
}
if (digitalRead(button3) == LOW) {
return 3;
}
if (digitalRead(button4) == LOW) {
return 4;
}
return 0;
}
// --- Function to display game stats ---
void displayStats() {
lcd.clear();
lcd.print("Day: ");
lcd.print(day);
lcd.setCursor(0, 1);
lcd.print("Gold: ");
lcd.print(playerGold);
delay(2000);
lcd.clear();
lcd.print("Food: ");
lcd.print(playerFood);
lcd.setCursor(0, 1);
lcd.print("Army: ");
lcd.print(playerArmy);
delay(2000);
}
// --- Function to handle location-specific choices ---
void handleLocationChoice(int choice) {
switch (location) {
case 1: // Forest
switch (choice) {
case 1: // Gather Wood
playerGold += 5;
displayText("Gathered wood!");
break;
case 2: // Hunt Animals
playerFood += 10;
displayText("Hunted animals!");
break;
case 3: // Explore Deeper
// Trigger random encounter or event
displayText("Exploring deeper...");
break;
case 4: // Return to Camp
location = 0; // Return to main camp
displayText("Returned to camp.");
break;
}
break;
case 2: // Mountains
// Add choices specific to Mountains
break;
case 3: // Plains
// Add choices specific to Plains
break;
}
}
// --- Mini-game: Guess the Number ---
void playGuessTheNumber() {
int randomNumber = random(1, 11); // Generate random number between 1 and 10
int guess = 0;
int attempts = 3;
displayText("Guess the number (1-10):");
while (guess != randomNumber && attempts > 0) {
int buttonPressed = getButtonInput();
if (buttonPressed >= 1 && buttonPressed <= 10) {
guess = buttonPressed;
attempts--;
if (guess < randomNumber) {
displayText("Too low!");
} else if (guess > randomNumber) {
displayText("Too high!");
}
}
}
if (guess == randomNumber) {
displayText("You win! +20 Gold");
playerGold += 20;
} else {
displayText("You lose!");
}
}
// --- Function to handle player choices ---
void handleChoice(int choice) {
switch (choice) {
case 1: // Gather Resources
if (location == 0) { // In main camp
playerGold += 10;
playerFood += 5;
displayText("Gathered resources!");
} else { // In a specific location
handleLocationChoice(1);
}
break;
case 2: // Train Troops
if (playerGold >= 20 && playerFood >= 10) {
playerGold -= 20;
playerFood -= 10;
playerArmy += 5;
displayText("Trained new troops!");
} else {
displayText("Not enough resources!");
}
break;
case 3: // Attack Enemy or Explore
if (location == 0) { // In main camp
if (playerArmy > enemyArmy) {
displayText("Victory!");
enemyArmy = 0;
} else if (playerArmy < enemyArmy) {
displayText("Defeat!");
playerArmy = 0;
} else {
displayText("Draw!");
playerArmy /= 2;
enemyArmy /= 2;
}
} else { // In a specific location
handleLocationChoice(3);
}
break;
case 4: // Do Nothing or Return to Camp
if (location == 0) { // In main camp
displayText("Did nothing...");
} else { // In a specific location
handleLocationChoice(4);
}
break;
case 5: // Play Mini-game (only available in main camp)
if (location == 0) {
playGuessTheNumber();
}
break;
case 6: // Travel to Forest
if (location == 0) {
location = 1;
displayText("Traveled to the forest.");
}
break;
case 7: // Travel to Mountains
if (location == 0) {
location = 2;
displayText("Traveled to the mountains.");
}
break;
case 8: // Travel to Plains
if (location == 0) {
location = 3;
displayText("Traveled to the plains.");
}
break;
}
}
// --- Function to display location-specific choices ---
void displayLocationChoices() {
switch (location) {
case 1: // Forest
lcd.clear();
lcd.print("1: Gather Wood");
lcd.setCursor(0, 1);
lcd.print("2: Hunt Animals");
delay(2000);
lcd.clear();
lcd.print("3: Explore Deeper");
lcd.setCursor(0, 1);
lcd.print("4: Return to Camp");
break;
case 2: // Mountains
// Add choices specific to Mountains
break;
case 3: // Plains
// Add choices specific to Plains
break;
}
}
// --- Function to display main camp choices ---
void displayMainCampChoices() {
lcd.clear();
lcd.print("1: Gather");
lcd.setCursor(0, 1);
lcd.print("2: Train");
delay(2000);
lcd.clear();
lcd.print("3: Attack");
lcd.setCursor(0, 1);
lcd.print("4: Nothing");
delay(2000);
lcd.clear();
lcd.print("5: Mini-game");
lcd.setCursor(0, 1);
lcd.print("6: Forest");
delay(2000);
lcd.clear();
lcd.print("7: Mountains");
lcd.setCursor(0, 1);
lcd.print("8: Plains");
}
void setup() {
// Initialize LCD and buttons
lcd.begin(16, 2);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
// Display intro message
displayText("Welcome to the game!");
randomSeed(analogRead(0)); // Seed the random number generator
}
void loop() {
// Display game stats
displayStats();
// Display choices based on location
if (location == 0) { // Main camp
displayMainCampChoices();
} else { // Specific location
displayLocationChoices();
}
// Get player input
int choice = 0;
while (choice == 0) {
choice = getButtonInput();
}
// Handle player choice
handleChoice(choice);
// Increase day counter
day++;
// Check for game over
if (playerArmy <= 0) {
displayText("Game Over!");
while (true) {} // Game Over loop
}
// Enemy AI (simple example)
if (day % 3 == 0) {
enemyArmy += 5;
displayText("Enemy reinforced!");
}
// Add more game logic and content here...
}