Hello guys, thanks to some help I'v managed to get somewhat of a game working and now I'm trying to implement a scoring system that adds points until the user fails which then will display the total score on serial monitor. I'm trying to think of a scoring system but I'm not sure how, can anyone help me put a scoring system in this code? if so thank you very much
I'v been trying to work on this for awhile and my idea is this:
State score(){
if (pressed = correct;)
Serial.println("Score = +10");(
{
This is what my game code looks like:
#include <SM.h>
SM game(gameStart);
const int noControls = 4;
const byte led[noControls] = {9, 10, 11, 12};
const byte btn[noControls] = {2, 3, 5, 7};//pin 0 and 1 are for serial
const byte startBtn = 6;
const unsigned long limit = 120;//reaction time limit
byte currentCtrl;
enum btnState {noPress, correct, fault};
void setup(){
Serial.begin(115200);
for(byte i = 0; i<noControls; i++) pinMode(led[i], OUTPUT);
randomSeed(analogRead(0));
}//setup()
void loop(){
EXEC(game);
}//loop()
State gameStart(){
if (not)(!digitalRead(startBtn)){
currentCtrl = random(noControls);//determine control number
ledCtrl(1<<currentCtrl);//light correct led
Serial.print("Started with:");
Serial.println(currentCtrl);
game.Set(randomLed);//change state
}//if(stertBtn)
for(byte i = 0; i<noControls; i++) if(!digitalRead(btn[i])){//check if user cheats
ledCtrl(15);//all leds on
Serial.println("Cheat!");
game.Set(miss);//change state
break;//no need to check further
}//if(cheat)
}//gameStart()
State randomLed(){
// if(game.Timeout(limit)){//check for Timeout
// ledCtrl(15);//all leds on
// Serial.println("Timeout");
// game.Set(miss);//change state
// }//if(Timeout)
btnState pressed = noPress;
for(byte i = 0; i<noControls; i++){//scan all buttons
if(!digitalRead(btn[i])){//check individual button
if(i == currentCtrl){//check validity of btn
pressed = correct;//we have a hit
}else{//incorrect btn
pressed = fault;//false press
break;//abort scan
}//()
}//if(btn[i])
}//for(i)
switch(pressed){//act on press
case correct:
currentCtrl = 1;//reuse currentCtrl for display
ledCtrl(currentCtrl);
Serial.println("Hit");
game.Set(success);//change state
break;//case correct
case fault:
ledCtrl(15);//all leds on
Serial.println("Wrong button");
game.Set(miss);//change state
break;//case fault
}//switch(pressed)
}//randomLed()
State miss(){
if(game.Timeout(1000)){
ledCtrl(0);//all leds off
Serial.println("Back to start from miss");
game.Set(gameStart);
}//()
}//miss()
State success(){
if(game.Timeout(300)){//wait 300ms
currentCtrl <<= 1;//shift 1 bit
if(currentCtrl<(1<<noControls)){//more ledsd to show?
ledCtrl(currentCtrl);
game.Set(success);
}else{//back to start
ledCtrl(0);//all leds off
Serial.println("back to start from success");
game.Set(gameStart);//change state
}
}//if(wait 300 ms)()
}//success()
inline void ledCtrl(byte ctrl){
for(byte i = 0; i<noControls; i++) digitalWrite(led[i], !!(ctrl&(1<<i)));
}//ledCtrl()