Created
April 6, 2015 19:43
-
-
Save greevex/4e64c58298f757c50b85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BUTTON PINS | |
const int buttonPin_1 = 2; | |
const int buttonPin_2 = 3; | |
const int buttonPin_3 = 4; | |
const int buttonPin_4 = 5; | |
const int buttonPin_reset = 5; | |
// GAME OPTIONS | |
const int defaultValue = 16; | |
const int minValue = 5; | |
const int maxValue = 60; | |
const int winValue = 49; | |
// GAME STATES | |
const int gameState_new = 2; | |
const int gameState_playing = 3; | |
const int gameState_win = 4; | |
// CURRENT GAME STATE | |
int gameState = gameState_new; | |
// BUTTON PINS LIST | |
int buttonPins[] = { | |
buttonPin_1, | |
buttonPin_2, | |
buttonPin_3, | |
buttonPin_4, | |
buttonPin_reset | |
}; | |
// BUTTON STATES LIST | |
int buttonStates[] = { | |
LOW, | |
LOW, | |
LOW, | |
LOW, | |
LOW | |
}; | |
// BUTTON FLAGS LIST (`HIGH` IF WAS PRESSED) | |
int buttonValues[] = { | |
LOW, | |
LOW, | |
LOW, | |
LOW, | |
LOW | |
}; | |
// BUTTON MODIFIERS LIST (HOW BUTTON MODIFIES CURRENT GAME VALUE) | |
int buttonModifier[] = { | |
+9, | |
-9, | |
+6, | |
-6, | |
0 // because reset button | |
} | |
// CURRENT GAME VALUE | |
int currentValue = 16; | |
// ARDUINO SYSTEM METHOD TO INITIALIZE PINS | |
void setup() { | |
// BUTTON PINS INITIALIZE | |
for(int i = 0; i < sizeof(buttonPins); i++) { | |
pinMode(buttonPins[i], INPUT); | |
} | |
} | |
// Read buttons state by pins, calculate that button was pressed | |
void updateStates() { | |
int currentState; | |
for(int i = 0; i < sizeof(buttonPins); i++) { | |
currentState = digitalRead(buttonPins[i]); | |
if(buttonStates[i] != HIGH) { | |
buttonStates[i] = currentState; | |
} else if(currentState == LOW) { | |
buttonStates[i] = LOW; | |
buttonValues[i] = HIGH; | |
} | |
} | |
} | |
// Checking number by game rules | |
void checkNumber() { | |
if(currentValue < minValue || currentValue > maxValue) { | |
currentValue = defaultValue; | |
} else if(currentValue = winValue) { | |
gameState = gameState_win; | |
} | |
} | |
// Get button state, if was pressed it's returns HIGH and reseting state | |
int getButtonState(i) { | |
int result = buttonValues[i]; | |
if(result == HIGH) { | |
buttonValues[i] = LOW; | |
} | |
return result; | |
} | |
// Update states of all entities | |
void process() { | |
// update buttons state | |
updateStates(); | |
// checking buttons values: value is HIGH if button was pressed | |
for(int i = 0; i < sizeof(buttonPins); i++) { | |
if(getButtonState(i) == HIGH) { | |
// means that button was pressed | |
if(buttonPins[i] == buttonPin_reset) { | |
// if reset button was pressed, resetting game value & set game state to `new` | |
currentValue = defaultValue; | |
gameState = gameState_new; | |
} else { | |
// if one of 4 game buttons pressed updating game value and setting gameState to `playing` | |
currentValue += buttonModifier[i]; | |
gameState = gameState_playing; | |
} | |
} | |
} | |
} | |
// Main loop | |
void loop() { | |
// Updating buttons and game states | |
process(); | |
// Checking game state | |
if(gameState == gameState__playing) { | |
// Game value was updated, need to check it | |
checkNumber(); | |
} | |
// checking game state after number check | |
if(gameState == gameState_win) { | |
//@todo exit, player win | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment