#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int buttonPin1 = 6;
const int buttonPin2 = 5;
const int buttonPin3 = 4;
uint8_t dot[] = {
0b00000,
0b00100,
0b00010,
0b11111,
0b00010,
0b00100,
0b00000,
0b00000
};
uint8_t galochka[] = {
0b00000,
0b00000,
0b00000,
0b00001,
0b00010,
0b10100,
0b01000,
0b00000
};
int pot = 10;
int air = 0;
int you = 0;
int chouse[3] = {1, 2, 3};
int scrlmn = 1;
int scrlmngal = 1;
int aig = 0;
bool checkgalochka = false;
int szmn = 3;
int szai = 3;
void setup() {
lcd.begin(20, 4);
lcd.createChar(1, dot);
lcd.createChar(2, galochka);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
lcd.setCursor(0, 1);
lcd.print("Pot:");
lcd.setCursor(0, 2);
lcd.print("AIr:");
lcd.setCursor(0, 3);
lcd.print("You:");
lcd.setCursor(4, 1);
lcd.print(pot);
lcd.print("gems");
lcd.setCursor(4, 2);
lcd.print(air);
lcd.print("gems");
lcd.setCursor(4, 3);
lcd.print(you);
lcd.print("gems");
menu();
lcd.setCursor(3,0);
lcd.print("<10 GEMS game>");
lcd.setCursor(13, scrlmn);
lcd.write(byte(1));
if (digitalRead(buttonPin3) == LOW) {
szmnchange();
}
if (checkgalochka) {
lcd.setCursor(14, scrlmngal);
lcd.write(byte(2));
}
if (digitalRead(buttonPin2) == LOW) {
checkgalochka = true;
scrlmngal = scrlmn;
lcd.clear();
delay(200);
}
if (digitalRead(buttonPin1) == LOW && checkgalochka) {
you += scrlmngal;
pot -= scrlmngal;
checkgalochka = false;
scrlmn = 1;
lcd.clear();
if (pot <= 2) {
szmn = pot;
}
win();
AIc();
}
}
void menu() {
if (szmn == 3) {
lcd.setCursor(15, 1);
lcd.print(chouse[0]);
lcd.print("gems");
lcd.setCursor(15, 2);
lcd.print(chouse[1]);
lcd.print("gems");
lcd.setCursor(15, 3);
lcd.print(chouse[2]);
lcd.print("gems");
} else if (szmn == 2) {
lcd.setCursor(15, 1);
lcd.print(chouse[0]);
lcd.print("gems");
lcd.setCursor(15, 2);
lcd.print(chouse[1]);
lcd.print("gems");
}
}
void AIc() {
if (pot <= 2) {
szai = pot;
}
aig = random(1, szai);
Serial.print("AI chosen ");
Serial.println(aig);
air += aig;
pot -= aig;
if (pot <= 2) {
szmn = pot;
}
if (szmn <= 1) {
lose();
} else {
delay(200);
}
}
void szmnchange() {
if (szmn == 3) {
if (scrlmn == 1 ) {
scrlmn += 1;
} else if (scrlmn == 2) {
scrlmn += 1;
} else if (scrlmn == 3) {
scrlmn -= 2;
};
}
if (szmn == 2) {
scrlmn = (scrlmn == 1) ? 2 : 1;
}
lcd.clear();
delay(200);
}
void win() {
if (szmn == 1) {
lcd.clear();
air += 1;
lcd.setCursor(5, 0);
lcd.print("YOU WIN!");
lcd.setCursor(0, 2);
if (you > air) {
lcd.print("You scored more gems");
} else if (you < air) {
lcd.print("AI scored more gems");
} else {
lcd.print("Have the same count");
}
delay(2000);
reset();
}
}
void lose() {
lcd.clear();
you += 1;
lcd.setCursor(5, 0);
lcd.print("YOU LOSE!");
lcd.setCursor(0, 2);
if (you > air) {
lcd.print("You scored more gems");
} else if (you < air) {
lcd.print("AI scored more gems");
} else {
lcd.print("Have the same count");
}
delay(2000);
reset();
}
void reset() {
pot = 10;
air = 0;
you = 0;
scrlmn = 1;
scrlmngal = 1;
aig = 0;
checkgalochka = false;
szmn = 3;
szai = 3;
lcd.clear();
}