#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <OneButton.h>
#define button1 2
#define button2 3
#define button3 4
OneButton onea(button1, true);
OneButton twoa(button2,true);
OneButton threea(button3, true);
boolean gameon = false;
boolean playerturn = false;
boolean freakgame = false; //NIM doesnt win
boolean startup = true;
boolean twoplayer = false;
boolean playerone = true;
int one=0;
int two= 0;
int three = 0;
int left = 12;
int nim = 0;
int player = 0;
int x = 0;
#define OLED_WIDTH 128
#define OLED_HEIGHT 32
long time = 0;
#define OLED_ADDR 0x3C
int textsize = 1;
Adafruit_SSD1306 lcd(OLED_WIDTH, OLED_HEIGHT);
void setup() {
Wire.begin();
onea.attachClick (numberone);
twoa.attachClick (numbertwo);
threea.attachClick (numberthree);
lcd.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
lcd.clearDisplay();
lcd.setTextSize(2);
lcd.setTextColor(WHITE);
lcd.setCursor(25, 0);
lcd.println("Welcome");
lcd.setTextSize(2);
lcd.setTextColor(WHITE);
lcd.setCursor(30, 17);
lcd.println("To NIM");
lcd.display();
delay(1500);
lcd.setTextSize(1);
lcd.clearDisplay();
lcd.display();
lcd.setCursor(0, 0);
}
void loop() {
while (startup == true && gameon == false) {
lcd.setCursor(0, 0);
lcd.println("How many players?");
lcd.print("1 or 2");
lcd.display();
one = two = three = 0;
onea.tick();
twoa.tick();
threea.tick();
playerturn=false;
freakgame = false;
if (two == 2)
{ lcd.clearDisplay();
lcd.println("Two");
lcd.display();
gameon = true;
playerone = true;
left = 12;
delay(1000);
lcd.clearDisplay();
startup = false;
twoplayer = true;
}
if (one == 1) {
lcd.clearDisplay();
lcd.print("One");
left = 13;
gameon = false;
delay(1000);
lcd.clearDisplay();
startup = false;
twoplayer = false;
}
}
while (gameon == false && startup == false && twoplayer ==false) {
lcd.setCursor(0, 0);
lcd.println("Who is 1st?");
lcd.print("1 4 me 2 4 u");
lcd.display();
one = two = three = 0;
onea.tick();
twoa.tick();
threea.tick();
if (two == 2)
{ lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("You First");
lcd.display();
gameon = true;
playerturn = true;
left = 12;
delay(1000);
lcd.clearDisplay();
}
if (one == 1)
{ lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("Me First");
lcd.display();
left = 13;
gameon = true;
playerturn = false;
delay(1000);
lcd.clearDisplay();
}
if (three == 3) {
freakgame = true;
left = 12;
gameon = true;
playerturn = random (0,100)%2;
delay(1000);
}
}
while (gameon == true && playerturn == false && freakgame == false && twoplayer == false) {
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.println("My turn, I take");
nim = left % 4;
if (nim == 0) {
nim = 1;
}
lcd.print (nim);
lcd.display();
delay (1000);
left = left - nim;
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.println("Coins left");
lcd.print(left);
lcd.display();
delay (1500);
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("Your Turn");
lcd.display();
playerturn = true;
if (left <= 0) {
lcd.clearDisplay();
lcd.setCursor(25, 0);
lcd.println("GAME OVER");
lcd.setCursor(30, 17);
lcd.print("I win");
lcd.display();
delay(1000);
lcd.clearDisplay();
lcd.setCursor(0,0);
gameon = false;
startup = true;
}
}
while (gameon == true && playerturn == false && freakgame == true) {
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.println("My turn, I take");
nim = random(0, 4);
if (nim > left) {
nim--;
}
if (nim == 0) {
nim = 1;
}
lcd.print (nim);
lcd.display();
delay (1000);
left = left - nim;
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.println("Coins left");
lcd.print(left);
lcd.display();
delay (1500);
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("Your Turn");
lcd.display();
playerturn = true;
if (left <= 0) {
lcd.clearDisplay();
lcd.setCursor(25, 0);
lcd.println("GAME OVER");
lcd.setCursor(30, 17);
lcd.print("I win");
lcd.display();
delay(1000);
lcd.clearDisplay();
gameon = false;
startup = true;
}
}
while (gameon == true && playerturn == true) {
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("Coins Left = ");
lcd.println(left);
lcd.println("Your go, you take");
lcd.display();
one = two = three = 0;
onea.tick();
twoa.tick();
threea.tick();
player = one+two+three;
if(one> 0 || two >0 || three>0){
lcd.print (player);
lcd.display();
delay (1000);
left = left - player;
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.println("Coins left");
lcd.print(left);
lcd.display();
delay (1500);
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("My Turn");
lcd.display();
playerturn = false;
if (left <= 0) {
lcd.clearDisplay();
lcd.setCursor(25, 0);
lcd.println("GAME OVER");
lcd.setCursor(30, 17);
lcd.print("You win");
lcd.display();
delay(1000);
lcd.clearDisplay();
gameon = false;
startup = true;
}
}}
while (gameon == true && twoplayer == true) {
if (playerone == true) {
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("Coins Left = ");
lcd.println(left);
lcd.println("Player 1 takes");
lcd.display();
}
else {
lcd.clearDisplay();
lcd.setCursor(0,0);
lcd.print("Coins Left = ");
lcd.println(left);
lcd.println("Player 2 takes");
lcd.display();
}
one = two = three = 0;
onea.tick();
twoa.tick();
threea.tick();
player = one+two+three;
if(one> 0 || two >0 || three>0){
lcd.print (player);
lcd.display();
delay (1000);
left = left - player;
lcd.clearDisplay();
lcd.println ("Coins left");
lcd.print(left);
lcd.display();
delay (1500);
lcd.clearDisplay();
if (left <= 0) {
lcd.clearDisplay();
lcd.setCursor(4, 0);
lcd.println("GAME OVER");
if (playerone == true) {
lcd.print("Player 1 wins");
}
else {
lcd.print("Player 2 wins");
}
lcd.display();
delay(1000);
lcd.clearDisplay();
startup = true;
gameon = false;
}
playerone = !playerone;
lcd.clearDisplay();
}}
}
void numberone(){
one =1;
two = 0;
three = 0;
}
void numbertwo(){
one =0;
two = 2;
three = 0;
}
void numberthree(){
one =0;
two = 0;
three = 3;
}