Krishna Lalith
Published © LGPL

Othello

Othello or Reversi Game on Arduino.

IntermediateShowcase (no instructions)1 hour248
Othello

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Analog joystick (Generic)
×1
SparkFun 7-Segment Serial Display - Red
SparkFun 7-Segment Serial Display - Red
×1
RGB LED Pixel Matrix, NeoPixel NeoMatrix
RGB LED Pixel Matrix, NeoPixel NeoMatrix
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Code

Othello or Reversi Game

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

int UD = 0, LR = 0, BT = 0, nLED, xdot, ydot, retval;
int OTH[8][8], BKP[8][8], i, j, k, Turn = 0, rScore, gScore, count;
boolean flag = false;

#define PIN 6      // Output pin for led strip
#define NUMPIXELS 64 // Number of LED Diodes
#define SPEED 250

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define CLK 10
#define DIO 11

TM1637Display display(CLK, DIO);

void BlowLED() {
  rScore = 0;
  gScore = 0;
  flag = true;

  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      nLED = 8 * i + j;
      if (OTH[i][j] == 0) { //RED
        strip.setPixelColor(nLED, strip.Color(255, 0, 0));
        rScore = rScore + 1;
      }
      if (OTH[i][j] == 1) { //GREEN
        strip.setPixelColor(nLED, strip.Color(0, 255, 0));
        gScore = gScore + 1;
      }
      if (OTH[i][j] == 2) { //NO COLOR
        strip.setPixelColor(nLED, strip.Color(0, 0, 0));
        flag = false;
      }
    }
  }
  strip.show();

  //Display SCORES on TM1637
  display.showNumberDecEx(rScore * 100 + gScore, 64, true);

  //Full Board Case
  if (flag == true) {
    strip.setBrightness(50);
    strip.show();
    delay(SPEED);
    strip.setBrightness(100);
    strip.show();
    delay(SPEED);
    strip.setBrightness(200);
    strip.show();
  }
}

int valid()
{
  for (i = 0; i < 8; i++)
    for (j = 0; j < 8; j++)
      BKP[i][j] = OTH[i][j];

  retval = 0;

  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      if (OTH[i][j] == Turn ) {

        //Serial.println();
        //Serial.print(i);
        //Serial.println(j);
        //Serial.print(xdot);
        //Serial.println(ydot);
        //Serial.println();
        //delay(10 * SPEED);

        if (abs(j - ydot) == abs(i - xdot)) { //DIAGONAL
          if (i < xdot && j < ydot) {
            count = 0;
            for (k = i + 1; k < xdot; k++) {
              if (OTH[k][j + k - i] == 1 - Turn) count += 1;
            }
            if (count + 1 == xdot - i && count > 0) {
              for (k = i + 1; k < xdot; k++) {
                BKP[k][j + k - i] =  Turn;
              }
              retval = 1;
            }
          }

          if (i > xdot && j < ydot) {
            count = 0;
            for (k = xdot + 1; k < i; k++) {
              if (OTH[k][j + k - xdot] == 1 - Turn) count += 1;
            }
            if (count + 1 == i - xdot && count > 0) {
              for (k = xdot + 1; k < i; k++) {
                BKP[k][j + k - xdot] = Turn;
              }
              retval = 1;
            }
          }

          if (i < xdot && j > ydot) {
            count = 0;
            for (k = i + 1; k < xdot; k++) {
              if (OTH[k][ydot + k - i] == 1 - Turn) count += 1;
            }
            if (count + 1 == xdot - i  && count > 0) {
              for (k = i + 1; k < xdot; k++) {
                BKP[k][ydot + k - i] =  Turn;
              }
              retval = 1;
            }
          }

          if (i > xdot && j > ydot) {
            count = 0;
            for (k = xdot + 1; k < i; k++) {
              if (OTH[k][ydot + k - xdot] == 1 - Turn) count += 1;
            }
            if (count + 1 == i - xdot && count > 0) {
              for (k = xdot + 1; k < i; k++) {
                BKP[k][ydot + k - xdot] = Turn;
              }
              retval = 1;
            }
          }
        }
        /////////////////////////////////////////////////////////////////////////////////////
        if (j == ydot) { //same COLUMN
          if (i < xdot) {
            count = 0;
            for (k = i + 1; k < xdot; k++) {
              if (OTH[k][ydot] == 1 - Turn) count += 1;
            }
            if (count + 1 == xdot - i  && count > 0) {
              for (k = i + 1; k < xdot; k++) {
                BKP[k][ydot] =  Turn;
              }
              retval = 1;
            }
          }

          if (i > xdot) {
            count = 0;
            for (k = xdot + 1; k < i; k++) {
              if (OTH[k][ydot] == 1 - Turn) count += 1;
            }
            if (count + 1 == i - xdot && count > 0) {
              for (k = xdot + 1; k < i; k++) {
                BKP[k][ydot] = Turn;
              }
              retval = 1;
            }
          }
        }
        /////////////////////////////////////////////////////////////////////////////////////
        if (i == xdot) { //same ROW
          if (j < ydot) {
            count = 0;
            for (k = j + 1; k < ydot; k++) {
              if (OTH[xdot][k] == 1 - Turn) count += 1;
            }
            if (count + 1 == ydot - j && count > 0) {
              for (k = j + 1; k < ydot; k++) {
                BKP[xdot][k] =  Turn;
              }
              retval = 1;
            }
          }

          if (j > ydot) {
            count = 0;
            for (k = ydot + 1; k < j; k++) {
              if (OTH[xdot][k] == 1 - Turn) count += 1;
            }
            if (count + 1 == j - ydot && count > 0) {
              for (k = ydot + 1; k < j; k++) {
                BKP[xdot][k] =  Turn;
              }
              retval = 1;
            }
          }
        }

      }
    }
  }


  for (i = 0; i < 8; i++)
    for (j = 0; j < 8; j++)
      OTH[i][j] = BKP[i][j];

  return retval;
}

void setup()
{
  Serial.begin(9600); //Sets the baud for serial data transmission
  strip.begin();
  strip.setBrightness(25);
  strip.show(); // Initialize all pixels to 'off'
  display.setBrightness(2);

  for (i = 0; i < 8; i++)
    for (j = 0; j < 8; j++)
      OTH[i][j] = 2; //No Color

  OTH[3][3] = 1;
  OTH[3][4] = 0;
  OTH[4][3] = 0;
  OTH[4][4] = 1;

  BlowLED();
}

void loop() {

  UD = analogRead(A0);
  LR = analogRead(A1);
  BT = analogRead(A2);

  xdot = map(LR, 1021, 0, 7, 0); //This maps the values//
  ydot = map(UD, 1021, 0, 7, 0);

  nLED = 8 * xdot + ydot;

  //Serial.print(xdot);
  //Serial.println(ydot);

  //strip.setPixelColor(nLED, strip.Color(128, 128, 255)); //Yellow
  strip.setPixelColor(nLED, strip.Color(255 * (1 - Turn), 255 * Turn, 0));
  strip.show();

  if (BT == 0 && OTH[xdot][ydot] == 2) {
    if (valid() == 1) {
      OTH[xdot][ydot] = Turn;
      Turn = 1 - Turn;
    }
  }

  delay(SPEED);
  BlowLED();
}

Credits

Krishna Lalith

Krishna Lalith

11 projects • 2 followers

Comments