Krishna Lalith
Published © LGPL

Fibonacci Nim

Fibonacci nim is played by two players, who alternate removing coins or other counters from a pile of coins.

BeginnerShowcase (no instructions)2 hours864
Fibonacci Nim

Things used in this project

Story

Read more

Schematics

Fibonacci_Nim

Code

Fibonacci_Nim

Arduino
// Fibonacci nim is played by two players, who alternate removing coins or other counters from a pile of coins.
// On the first move, a player is not allowed to take all of the coins, and on each subsequent move,
// the number of coins removed can be any number that is at most twice the previous move.

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

LedControl lc = LedControl(12, 10, 11, 0);
TM1637Display display(2, 3);

int NIM[8][8], balls, numb = 0, counter, moved, stateval, b5, b6, b7, i, j;
int Fib[9] = {1, 2, 3, 5, 8, 13, 21, 34, 55}, basefib[9];

void setup() {
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);

  digitalWrite(8, LOW);
  display.setBrightness(2);

  Serial.begin(9600);
  lc.shutdown(0, false);      //The MAX72XX is in power-saving mode on startup
  lc.setIntensity(0, 1);      // Set the brightness to maximum value
  lc.clearDisplay(0);         // and clear the display
  randomSeed(analogRead(0));

  balls = random(25, 55);
  moved = balls - 1;

  stateval = 1;
  counter = 0;
  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      NIM[i][j] = stateval;
      counter = counter + 1;
      if (NIM[i][j] == 1) {
        lc.setLed(0, i, j , true);
      }
      if (NIM[i][j] == 0) {
        lc.setLed(0, i, j , false);
      }
      if (counter == balls) {
        stateval = 0;
      }
    }
  }

  numb = 1;
  display.showNumberDec(numb, true);
}

void loop() {
  //User's Turn
  b5 = digitalRead(5);
  b6 = digitalRead(6);
  b7 = digitalRead(7);

  if (b5 == 1) {
    if (numb < moved) {
      numb = numb + 1;
      display.showNumberDec(numb, true);
    }
  }

  if (b6 == 1) {
    if (numb > 0) {
      numb = numb - 1;
      display.showNumberDec(numb, true);
    }
  }

  if (b7 == 1) {
    moved = numb;
    balls = balls - numb;

    stateval = 1;
    counter = 0;
    for (i = 0; i < 8; i++) {
      for (j = 0; j < 8; j++) {
        if (counter == balls) {
          stateval = 0;
        }
        NIM[i][j] = stateval;
        counter = counter + 1;
        if (NIM[i][j] == 1) {
          lc.setLed(0, i, j , true);
        }
        if (NIM[i][j] == 0) {
          lc.setLed(0, i, j , false);
        }
      }
    }

    delay(2000);

    //Arduino's Turn
    numb = balls;
    for (i = 8; i >= 0; i--) {
      basefib[i] = 0;
      if (Fib[i] <= numb) {
        numb = numb - Fib[i];
        basefib[i] = 1;
      }
    }

    for (i = 0; i <= 8; i++) {
      if (2 * moved >= Fib[i] && basefib[i] == 1) {
        numb = Fib[i];
        break;
      }
    }

    display.showNumberDec(numb, true);
    moved = 2 * numb;
    balls = balls - numb;

    stateval = 1;
    counter = 0;
    for (i = 0; i < 8; i++) {
      for (j = 0; j < 8; j++) {
        if (counter == balls) {
          stateval = 0;
        }
        NIM[i][j] = stateval;
        counter = counter + 1;
        if (NIM[i][j] == 1) {
          lc.setLed(0, i, j , true);
        }
        if (NIM[i][j] == 0) {
          lc.setLed(0, i, j , false);
        }
      }

    }
  }

  delay(100);


  //checking for winner
  if (balls == 0) {
    digitalWrite(8, HIGH);
  }

}

Credits

Krishna Lalith
11 projects • 2 followers

Comments