Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Krishna Lalith
Published © LGPL

Date Game

Two players take turns increasing either the month or the number, but not both. The player to reach December 31 wins the game.

BeginnerShowcase (no instructions)2 hours534
Date Game

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Switch Actuator, Head for spring return push-button
Switch Actuator, Head for spring return push-button
×1
7 Segment LED Display, Red
7 Segment LED Display, Red
×1

Story

Read more

Schematics

Date Game

Code

Date Game

Arduino
// THE GAME IS PLAYED BY USER Vs CPU

//    RULES OF THE GAME

// 1. EACH TURN A CHANCE TO CHANGE EITHER MONTH OR DATE BUT NOT "BOTH".

// 2. WHO EVER REACHES THE FINAL DESTINATION (i.e, DEC 31) IN A CALENDER WILL WON THE GAME.

// 3. THERE IS NO WAY TO GO BACK FROM A GIVEN POSITION (since always TIME moves in
// forward or positive direction).

// 4. A LEAP YEAR IS CONSIDERED AS STANDARD REFERANCE (SO FEB HAS 29 DAYS).

// -----------------------------------------------------------------------

#include <Arduino.h>
#include <TM1637Display.h>

#define CLK 2
#define DIO 3

TM1637Display display(CLK, DIO);

void setup() {
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  digitalWrite(8, LOW);
  display.setBrightness(2);
  Serial.begin(9600);
}

int month = 0, date = 0, numb, b5, b6, b7, i, flag = 0;

int calendar[12][31] =  {
  {1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, //01-JAN
  {1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 2, 2}, //02-FEB
  {0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1}, //03-MAR
  {1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 2}, //04-APR
  {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0}, //05-MAY
  {1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 2}, //06-JUN
  {0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1}, //07-JUL
  {1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1}, //08-AUG
  {0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 2}, //09-SEP
  {1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1}, //10-OCT
  {1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 2}, //11-NOV
  {1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0}  //12-DEC
};

void loop() {
  numb = 100 * (month + 1) + date + 1;
  //display.showNumberDec(numb, true);
  display.showNumberDecEx(numb, 64, true); //with colon enabled
  delay(100);

  //User's Turn
  b5 = digitalRead(5);
  b6 = digitalRead(6);

  if (b5 == 1) {
    for (i = month + 1; i < 12; i++) {
      if (calendar[i][date] != 2) {
        month = i;
        //flag = 1;
        break;
      }
    }
  }

  if (b6 == 1) {
    if (date + 1 < 31) {
      if (calendar[month][date + 1] != 2) {
        date = date + 1;
        //flag = 1;
      }
    }
  }


  //Arduino's Turn
  b7 = digitalRead(7);
  if (b7 == 1) {
    delay(3000);
    flag = 1;
    b5 = 0; b6 = 0;
    if (flag == 1) {
      for (i = 30; i > date; i--) {
        if (calendar[month][i] == 0) {
          date = i;
          flag = 0;
          break;
        }
      }
    }

    if (flag == 1) {
      for (i = month; i < 12; i++) {
        if (calendar[i][date] == 0) {
          month = i;
          flag = 0;
          break;
        }
      }
    }

  }


  //checking for winner
  if (month == 11 && date == 30) {
    digitalWrite(8, HIGH);
  }

}

Credits

Krishna Lalith
11 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.