#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd_1(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
int Seconds = 0;
int TimeSinceLastUpdate = 0;
int Difficulty = 0;
int Input1 = 0;
int Input2 = 0;
int Input3 = 0;
int Input4 = 0;
bool Win = false;
bool Lose = false;
bool GameActive = false;
int Turn = 0;
const int numWires = 4;
int WirePins[numWires] = {13, 12, 11, 10};
int Wire1;
int Wire2;
int Wire3;
int Wire4;
void shuffleArray(int array[], int n) {
// Implement a simple shuffle algorithm
for (int i = n - 1; i > 0; i--) {
int j = random(i + 1); // Pick a random index from 0 to i
// Swap array[i] with array[j]
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
void setup() {
Serial.begin(9600);
lcd_1.init(); // Use begin() instead of init()
lcd_1.backlight();
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(13, INPUT);
pinMode(12, INPUT);
pinMode(11, INPUT);
pinMode(10, INPUT);
Turn = 0;
GameActive = false;
lcd_1.setCursor(0, 0);
lcd_1.print("Select");
lcd_1.setCursor(0, 1);
lcd_1.print("Difficulty");
}
void StartGame()
{
// Initialize random number generator
randomSeed(analogRead(0));
// Shuffle the array and assign shuffled values to the variables
shuffleArray(WirePins, numWires);
Wire1 = WirePins[0];
Wire2 = WirePins[1];
Wire3 = WirePins[2];
Wire4 = WirePins[3];
Serial.println(Wire1);
Serial.println(Wire2);
Serial.println(Wire3);
Serial.println(Wire4);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Bomb has been");
lcd_1.setCursor(0, 1);
lcd_1.print("planted");
digitalWrite(7, LOW);
digitalWrite(6 , LOW);
digitalWrite(5 , LOW);
digitalWrite(4 , LOW);
delay(2000); // Wait for 2000 milliseconds
digitalWrite(Wire1 - 6, HIGH);
delay(500); // Wait for 500 milliseconds
digitalWrite(Wire1 - 6, LOW);
digitalWrite(Wire2 - 6, HIGH);
delay(500); // Wait for 500 milliseconds
digitalWrite(Wire2 - 6, LOW);
digitalWrite(Wire3 - 6, HIGH);
delay(500); // Wait for 500 milliseconds
digitalWrite(Wire3 - 6, LOW);
delay(1000); // Wait for 1000 milliseconds
lcd_1.clear();
lcd_1.print("Detonation in:");
GameActive = true;
Turn = 1;
}
void loop() {
Input1 = digitalRead(Wire1);
Input2 = digitalRead(Wire2);
Input3= digitalRead(Wire3);
Input4= digitalRead(Wire4);
lcdUpdate();
TimeSinceLastUpdate += 10;
if(digitalRead(A0) == HIGH && !GameActive)
{
tone(3, 520, 500);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Easy");
lcd_1.setCursor(0, 1);
lcd_1.print("difficulty");
delay(2000);
Seconds = 10;
StartGame();
}
if(digitalRead(A1) == HIGH && !GameActive)
{
tone(3, 520, 500);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Medium");
lcd_1.setCursor(0, 1);
lcd_1.print("difficulty");
delay(2000);
Seconds = 7;
StartGame();
}
if(digitalRead(A2) == HIGH && !GameActive)
{
tone(3, 520, 500);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Hard");
lcd_1.setCursor(0, 1);
lcd_1.print("difficulty");
delay(2000);
Seconds = 3;
StartGame();
}
if (Turn == 1) {
if (Input1 == LOW) {
Turn += 1;
}
if (Input2 == LOW || Input3 == LOW || Input4 == LOW) {
Lose = true;
}
}
if (Turn == 2) {
if (Input2 == LOW) {
Turn += 1;
}
if (Input3 == LOW || Input4 == LOW) {
Lose = true;
}
}
if (Turn == 3) {
if (Input3 == LOW) {
Turn += 1;
}
if (Input4 == LOW) {
Lose = true;
}
}
if (Turn == 4) {
Win = true;
}
if (Seconds == -2 && !Lose) {
Lose = true;
}
if (Lose && GameActive) {
GameActive = false;
Serial.println("Terrorists win");
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Terrorists");
lcd_1.setCursor(0, 1);
lcd_1.print("Win");
}
if (Win && GameActive) {
GameActive = false;
Serial.println("Counter Terrorists win");
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Counter");
lcd_1.setCursor(0, 1);
lcd_1.print("Terrorists Win");
}
delay(10);
}
void lcdUpdate() {
if (TimeSinceLastUpdate < 1000) {
return;
}
TimeSinceLastUpdate = 0;
if (GameActive) {
if (Seconds < 10) {
lcd_1.setCursor(0, 1);
lcd_1.print("0");
lcd_1.setCursor(1, 1);
lcd_1.print(Seconds);
} else {
lcd_1.setCursor(0, 1);
lcd_1.print(Seconds);
}
Seconds -= 1;
tone(3, 16744, 100); // play tone 120 (C10 = 16744 Hz)
Serial.println("Red: " + String(digitalRead(13)));
Serial.println("Yellow: " + String(digitalRead(12)));
Serial.println("Green: " + String(digitalRead(11)));
Serial.println("Blue: " + String(digitalRead(10)));
Serial.println("Turn: " + String(Turn));
}
}