Cool Math Game

Compete against your friends, learn math, and have fun!

AdvancedFull instructions provided16 hours777
Cool Math Game

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
You need the box from this too!
×1
Breadboard (generic)
Breadboard (generic)
×3
Display Driver, LCD 4 Digits
Display Driver, LCD 4 Digits
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Pushbutton Switch, Pushbutton
Pushbutton Switch, Pushbutton
×9
ESD Tape, 1 "
ESD Tape, 1 "
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

img_2231_wO6P2fe1Uz.jpg

Code

Cool Math Game Code

C/C++
Upload this code onto the launchpad.
#include "TM1637.h"
#define CLKa 27//pins definitions for TM1637 and can be changed to other ports
#define DIOa 28
TM1637 playerA(CLKa,DIOa);
#define CLKb 36//pins definitions for TM1637 and can be changed to other ports
#define DIOb 35
TM1637 playerB(CLKb,DIOb);

// Variable Declerations
int disp = 0;
const int buttonPinB = 17;
int buttonStateB = 0;
const int buttonPinA = 31;
int buttonStateA = 0;
const int buttonReset = 33;
int buttonStateReset = 0;
boolean gameOn = false;
boolean questionchanged = 1;
int randNumber;
int num1;
int num2;
int answer;
int j = 1;
int thsB = 0; 
int thsA = 0;
int hunB = 0;
int hunA = 0; 
int tenA = 0; 
int tenB = 0;
int oneA = 0;
int oneB = 0;

void setup()
{

  // Set pin modes
  pinMode(23, INPUT_PULLUP);
  pinMode(24, INPUT_PULLUP);
  pinMode(25, INPUT_PULLUP);
  pinMode(26, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);

  // Set display, recet pins
  playerB.init();
  playerB.set(4);
  playerA.init();
  playerA.set(4);
  pinMode(buttonPinB,INPUT_PULLUP);
  pinMode(buttonPinA,INPUT_PULLUP);
  pinMode(buttonReset,INPUT_PULLUP);
  
//from Bobby's code
  Serial.begin(9600);
  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
  pinMode(buttonReset, INPUT_PULLUP);


}


void loop()
{
  // Start the game
  buttonStateReset = digitalRead(buttonReset);
  if (buttonStateReset == LOW){
    gameOn = true;
  }
  
  //Game set up, generates random number and starts the game
  buttonStateReset = digitalRead(buttonReset);
  if(questionchanged == 1 && buttonStateReset == 0){
    randNumber = random(3);
    if(randNumber == 0){
      num1 = random(1, 5000);
      num2 = random(1, 5000);
      Serial.print(num1);
      Serial.print('+');
      Serial.print(num2);
      answer = num1 + num2;  
  }
  else if(randNumber == 1){
    num1 = random(1, 9999);
    num2 = random(1, 9999);
      if(num1 > num2){
        Serial.print(num1);
        Serial.print('-');
        Serial.print(num2);
        answer = num1 - num2;
      }
      else{
        Serial.print(num2);
        Serial.print('-');
        Serial.print(num1);
        answer = num2 - num1;
      }
    }
    else{
      num1 = random(1, 100);
      num2 = random(1, 100);
      Serial.print(num1);
      Serial.print('x');
      Serial.print(num2);
      answer = num1 * num2;
    }
    while(j < 1000){
      j = j + 1;
    }
    Serial.println();
    questionchanged = 0;
}
  if(questionchanged == 0 && buttonStateReset == 1){
    questionchanged = 1;
}

    boolean player1_changed[] = {true, true, true, true};
    boolean player2_changed[] = {true, true, true, true};

    int player1_counts[] = {0, 0, 0, 0};
    int player2_counts[] = {0, 0, 0, 0};

  
  delay(150);

  // Updates the buttons and display the numbers
  while(gameOn)
  {
 int pin;
  for(pin = 23; pin < 27; pin = pin + 1 ){
    
    int buttonState = digitalRead(pin);

    if(buttonState == 0 && player1_changed[pin-23] == true){
      player1_changed[pin-23] = false;
      player1_counts[pin-23] += 1;
      
    }
    if(buttonState == 1){
      player1_changed[pin-23] = true;
    }
    
  }

  for(pin = 11; pin < 14; pin = pin + 1 ){
    
    int buttonState = digitalRead(pin);

    if(buttonState == 0 && player2_changed[14 - pin] == true){
      player2_changed[14 - pin] = false;
      player2_counts[14 - pin] += 1;
      
    }
    if(buttonState == 1){
      player2_changed[14 - pin] = true;
    }
    
  }

  int buttonState = digitalRead(18);

    if(buttonState == 0 && player2_changed[0] == true){
      player2_changed[0] = false;
      player2_counts[0] += 1;
      
    }
    if(buttonState == 1){
      player2_changed[0] = true;
    }

  oneA = player1_counts[3] % 10;
  tenA = player1_counts[2] % 10;
  hunA = player1_counts[1] % 10;
  thsA = player1_counts[0] % 10;

  oneB = player2_counts[3] % 10;
  tenB = player2_counts[2] % 10;
  hunB = player2_counts[1] % 10;
  thsB = player2_counts[0] % 10;




    
  buttonStateB = digitalRead(buttonPinB);
  buttonStateA = digitalRead(buttonPinA);

  playerB.display(0,thsB);
  playerB.display(1,hunB);
  playerB.display(2,tenB);
  playerB.display(3,oneB);
  playerA.display(0,thsA);
  playerA.display(1,hunA);
  playerA.display(2,tenA);
  playerA.display(3,oneA);
    
  if (buttonStateB == LOW) {
    gameOn = false;
    int playerBAns = (1000*thsB) + (100*hunB) + (10*tenB) + oneB;
    if(playerBAns == answer){
      Serial.print("Player B wins!!!!"); 
    }else
    {
      Serial.print("Player A wins!!!!");
    }
    Serial.println();
  } 
    
  if (buttonStateA == LOW) {
    gameOn = false;
    int playerAAns = (1000*thsA) + (100*hunA) + (10*tenA) + oneA;
    if(playerAAns != answer){
      Serial.print("Player B wins!!!!"); 
    }else
    {
      Serial.print("Player A wins!!!!");
    }
    Serial.println();
  } 
    
    
    
  }
  
}

Credits

Joshua Engels
1 project • 1 follower
BenZaltsman
1 project • 1 follower
I love Ray Simar AND Joe Young
Elliot Butterworth
0 projects • 1 follower
Yong Shin
0 projects • 1 follower
Robert Walsh
0 projects • 0 followers

Comments