0

8x8 BiColor Matrix: Tic Tac Toe Game + Joystick
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

8x8 BiColor Matrix: Tic Tac Toe Game + Joystick

by farous97 on Tue Dec 05, 2017 5:52 pm

Hello! So this project uses the Arduino UNO/RedBoard from Sparksfun, 8x8 Bicolor LED Matrix from Adafruit, and a 2-Axis Thumb Joystick.

I'm almost done making the game but I have little experience with coding and the issue I am facing right is whenever I hover over a taken spot on the matrix with the joystick, the LED turns off. So I want add a condition that will make the LED skip over occupied areas. I have an idea of maybe using an array but I have no clue how to actually do it using the Adafruit library. I think adding the win condition of the game should use the same idea but I'll leave that after solving this problem. Any help is very appreciated!

Edit: I updated the code and now player1 can go over its own LED (Green) and p2 can go over its own LED (Red) with no problems. But How do I make them skip over occupied areas?

Shown below is the program, libraries, and the parts that I used. I also have some comments that could help.

http://www.kr4.us/sparkfun-redboard-pro ... duino.html
https://learn.adafruit.com/adafruit-led ... 8x8-matrix
http://www.learningaboutelectronics.com ... ystick.php


https://github.com/adafruit/Adafruit-GFX-Library
https://github.com/adafruit/Adafruit_LED_Backpack

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"

#define ROWS 8
#define COLS 8
#define PLAYER_1 LED_GREEN
#define PLAYER_2 LED_RED

Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();

/************* Variables ************/
//Binary map for board
static const uint8_t PROGMEM
  board_bmp[] = {
    B00100100,
    B00100100,
    B11111111,
    B00100100,
    B00100100,
    B11111111,
    B00100100,
    B00100100 };
   
int buttonState = HIGH;
int X=6;
int Y=6;
bool player1 = true;
bool victory = false;
bool filled = true;
bool p1[8][8]={false};
bool p2[8][8]={false};

//Joystick Pins
const int SW_pin = 2;
const int X_pin = 0;
const int Y_pin = 1;


/********* Setup **********/
void setup()
{
  Serial.begin(9600);
  matrix.begin(0x70);

  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
 

  //Make Game board
  matrix.setBrightness(5);
  matrix.drawBitmap(0, 0, board_bmp, ROWS, COLS, LED_ORANGE);
  matrix.writeDisplay();
}

/******** Loop *******/
void loop() {

while(!victory) {

/********* Player 1's turn ********/
    while(player1) {
      Serial.print("X Axis:");
      Serial.print(X);
      Serial.print("\n");
      Serial.print("Y Axis:");
      Serial.print(Y);
      Serial.print("\n\n");
     
      matrix.drawRect(X,Y, 2,2, PLAYER_1);
      matrix.writeDisplay();
      delay(300);
     
      //Go Left
      if((analogRead(X_pin) < 500) && (X < 6))
      {
        if (p1[X][Y]==true){
          X=X+3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          X=X+3;
          delay(300);
        }
      }
     
      //Go Right
      else if((analogRead(X_pin) > 600) && (X > 0) )
      {
        if (p1[X][Y]==true){
          X=X-3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          X=X-3;
          delay(300);
        }
      }
      //Go Up
      else if((analogRead(Y_pin) < 500) && (Y < 6) )
      {
        if (p1[X][Y]==true){
          Y=Y+3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          Y=Y+3;
          delay(300);
        }
      }
      //Go Down
      else if((analogRead(Y_pin) > 600) && (Y > 0))
      {
        if (p1[X][Y]==true){
          Y=Y-3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          Y=Y-3;
          delay(300);
        }
      }
     
      //Click button for player 1
      buttonState = digitalRead(SW_pin);
      if(buttonState==LOW)
      {
        Serial.print("Button Click \n");
        Serial.print("X Axis:");
        Serial.print(X);
        Serial.print("\n");
        Serial.print("Y Axis:");
        Serial.print(Y);
        Serial.print("\n\n");
        filled=true;
        p1[X][Y]=true;
        matrix.drawRect(X,Y, 2,2, PLAYER_1);
        matrix.writeDisplay();
        delay(300);
        X=6;
        Y=6;
        player1 = false;
      }
     }//End of player 1

/*********** Player 2's turn ******/
  while(!player1) {
      matrix.drawRect(X,Y, 2,2, PLAYER_2);
      matrix.writeDisplay();
      delay(300);
     
      //Go Left
      if((analogRead(X_pin) < 500) && (X < 6))
      {
        if (p2[X][Y]==true){
          X=X+3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          X=X+3;
          delay(300);
        }
      }
     
      //Go Right
      else if((analogRead(X_pin) > 600) && (X > 0) )
      {
        if (p2[X][Y]==true){
          X=X-3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          X=X-3;
          delay(300);
        }
      }
      //Go Up
      else if((analogRead(Y_pin) < 500) && (Y < 6) )
      {
        if (p2[X][Y]==true){
          Y=Y+3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          Y=Y+3;
          delay(300);
        }
      }
      //Go Down
      else if((analogRead(Y_pin) > 600) && (Y > 0))
      {
        if (p2[X][Y]==true){
          Y=Y-3;
        }
        else{
          matrix.drawRect(X,Y, 2,2, LED_OFF);
          matrix.writeDisplay();
          Y=Y-3;
          delay(300);
        }
      }
     
      //Click button for player 2
      buttonState = digitalRead(SW_pin);
      if(buttonState==LOW)
      {
        Serial.print("Button Click \n");
        Serial.print("X Axis:");
        Serial.print(X);
        Serial.print("\n");
        Serial.print("Y Axis:");
        Serial.print(Y);
        Serial.print("\n\n");
        p2[X][Y]=true;
        matrix.drawRect(X,Y, 2,2, PLAYER_2);
        matrix.writeDisplay();
        delay(300);
        X=6;
        Y=6;
        player1 = true;
      }
     
    }//End of player 2
   
  }//End of victory
 
}//End of loop


farous97
 
Posts: 1
Joined: Mon Dec 04, 2017 2:31 pm

Please be positive and constructive with your questions and comments.