Tic Tac Toe Code

// Written by Yemoni Huguely

//LED tic tac toe code that allows lights to light up on a 3x3 matrix corresponding to a keypad matrix.

#include <Keypad.h>

const byte rows = 4; //four rows

const byte cols = 4; //three columns

char keys[rows][cols] = {

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}

};

byte rowPins[rows] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad

byte colPins[cols] = {2, 3, 4, 5}; //connect to the column pinouts of the keypad

Keypad k1 = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

 

 

 

int data = 10;

int clock = 11;

int le = 12;

int tally = 0;

 

int player;

 

byte gameboard1 = B00000000;

byte gameboard2 = B00000000;

byte gameboard3 = B00000000;

byte gameboard4 = B00000000;

 

void setup()

{

  Serial.begin(9600);

  k1.setDebounceTime(250);

  pinMode(data, OUTPUT);

  pinMode(clock, OUTPUT);

  pinMode(le, OUTPUT);

}

 

void loop() {

  char key = k1.getKey();

  char keysPressed[16] = {key};

 

  if (key != NO_KEY){

    if (key == '1') {

        changeColor(1, player);

        Serial.println(key);

    }

    else if (key == '2') {

      changeColor(2, player);

      Serial.println(key);

    }

    else if (key == '3') {

      changeColor(3, player);

      Serial.println(key);

    }

    else if (key == '4') {

      changeColor(4, player);

      Serial.println(key);

    }

    else if (key == '5') {

      changeColor(5, player);

      Serial.println(key);

    }

    else if (key == '6') {

      changeColor(6, player);

      Serial.println(key);

    }

    else if (key == '7') {

      changeColor(7, player);

      Serial.println(key);

    }

    else if (key == '8') {

      changeColor(8, player);

      Serial.println(key);

    }

    else if (key == '9') {

      changeColor(9, player);

      Serial.println(key);

    }

    else {

      Serial.println(key);

    }

 

    tally = tally + 1;

    // allows for the switching of the colors(players) back and forth

    for(int i =1; i<tally; i++){

      if (i%2 == 0){

        player = 1;

      }

      else if (i%2 == 1){

        player =2;

      }

    }

 

 

 

  }

 

  digitalWrite(le, LOW);

  shiftOut(data, clock, MSBFIRST, gameboard4);

  shiftOut(data, clock, MSBFIRST, gameboard3);

  shiftOut(data, clock, MSBFIRST, gameboard2);

  shiftOut(data, clock, MSBFIRST, gameboard1);

  digitalWrite(le, HIGH);

  delay(1);

}

 

// Changes the players color and sets every key position with player 1 and player 2 color.

void changeColor(int num, int playerNum){

  if(playerNum == 1) {

    if( num == 1){

     gameboard1 ^= B00000100;

    }

    else if (num == 2) {

      gameboard4 ^= B00001000;

    }

    else if (num == 3) {

      gameboard3 ^= B00000100;

    }

     else if (num == 4) {

      gameboard1 ^= B00100000;

    }

     else if (num == 5) {

      gameboard2 ^= B01000000;

    }

     else if (num == 6) {

      gameboard3 ^= B00100000;

    }

     else if (num == 7) {

      gameboard2 ^= B00000001;

    }

     else if (num == 8) {

      gameboard2 ^= B00001000;

    }

     else if (num == 9) {

      gameboard4 ^= B00000001;

    }

  }

  else {

    if( num == 1){

     gameboard1 ^= B00000001;

    }

    else if (num == 2) {

       gameboard4 ^= B00000010;

    }

    else if (num == 3) {

       gameboard3 ^= B00000001;

    }

    else if (num == 4) {

       gameboard1 ^= B00001000;

    }

    else if (num == 5) {

       gameboard2 ^= B00010000;

    }

    else if (num == 6) {

       gameboard3 ^= B00001000;

    }

    else if (num == 7) {

       gameboard1 ^= B01000000;

    }

    else if (num == 8) {

       gameboard2 ^= B00000010;

    }

    else if (num == 9) {

       gameboard3 ^= B01000000;

    }

 

  }

  /*

  if(player == 1){

    player = 1;

 

 

  }

  else {

    player = 1;

  }*/

}

 

void clearBoard(){

 gameboard1 = B00000000;

 gameboard2 = B00000000;

 gameboard3 = B00000000;

 gameboard4 = B00000000;

}

 

Return