Created
January 24, 2018 14:27
-
-
Save stigok/c30a9807b4318e2d4f8bc5fc7d59d7f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const int SECOND = 1000; | |
const int MINUTE = 60 * SECOND; | |
const int RESOLUTION = 1 * SECOND; | |
//const int EXTRA_TURN_TIME = SECOND; | |
const int GREEN = 0; | |
const int RED = 1; | |
const int DRY_RUN = 1; // Comment this whole line to enable gpio pins | |
void pinMode(int pin, int mode) { | |
// noop | |
} | |
// Pin assignment | |
BTN_PLAYER_0 = 10 | |
LED_0_GREEN = 13 | |
LED_0_RED = 14 | |
BTN_PLAYER_1 = 11 | |
LED_1_GREEN = 15 | |
LED_1_RED = 16 | |
BTN_MODE_SELECT = 12 | |
// Pin setuo | |
pinMode(BTN_PLAYER_0, INPUT) | |
pinMode(LED_0_GREEN, OUTPUT) | |
pinMode(LED_0_RED, OUTPUT) | |
pinMode(BTN_PLAYER_1, INPUT) | |
pinMode(LED_1_GREEN, OUTPUT) | |
pinMode(LED_1_RED, OUTPUT) | |
pinMode(BTN_MODE_SELECT, INPUT) | |
// Setup pins | |
const int GAME_TIME = 5 * MINUTE | |
int activePlayer = 0; | |
int timeLeft[] = { GAME_TIME, GAME_TIME }; | |
boolean gameStarted = 0; | |
void playerLed(int player, int color, int state) { | |
digitalWrite((player * 2) + color, state); | |
} | |
void turnComplete() { | |
activePlayer ^= 1; | |
} | |
// Runs forever | |
void loop() { | |
if (gameStarted) { | |
// Game over ? | |
if (timeLeft[activePlayer] <= 0) { | |
gameStarted = false; | |
int winner = activePlayer ^ 1; | |
int loser = activePlayer; | |
playerLed(winner, RED, LOW); | |
playerLed(loser, GREEN, LOW); | |
// Visualize winner and loser | |
while (true) { | |
playerLed(winner, GREEN, HIGH); | |
playerLed(loser, RED, HIGH); | |
delay(0.5 * SECOND); | |
playerLed(winner, GREEN, LOW); | |
playerLed(loser, RED, LOW); | |
delay(0.5 * SECOND); | |
} | |
} | |
// Countdown! | |
timeLeft[activePlayer] -= RESOLUTION; | |
delay(RESOLUTION); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment