Last active
February 7, 2018 21:53
-
-
Save jbobrow/1f3b7eac709d5c4616b6b36861c9482f to your computer and use it in GitHub Desktop.
Not sure why this is failing. Should only light up green when in a configuration of 3 all sharing sides.
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
| /* | |
| * Test of ready state for Mortals | |
| */ | |
| enum State { | |
| DEAD, | |
| ALIVE, | |
| ENGUARDE, // I am ready to attack! | |
| ATTACKING, // Short window when I have already come across my first victim and started attacking | |
| INJURED, | |
| READY, | |
| TEAM_A_START, | |
| TEAM_B_START, | |
| }; | |
| void setup() { | |
| // put your setup code here, to run once: | |
| setValueSentOnAllFaces(DEAD); | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| if(isBlinkInReadyConfiguration()) { | |
| setColor(GREEN); | |
| } | |
| else { | |
| setColor(RED); | |
| } | |
| } | |
| /* | |
| * Determine if we are in the READY Mode | |
| */ | |
| bool isBlinkInReadyConfiguration() { | |
| // first count neighbors, if we have more or less than 2, then we are not in the ready mode | |
| byte numNeighbors = 0; | |
| FOREACH_FACE(f) { | |
| if(!isValueReceivedOnFaceExpired(f)) { | |
| numNeighbors++; | |
| } | |
| } | |
| if(numNeighbors != 2) { | |
| return false; | |
| } | |
| // we have 2 neighbors, let's make sure they are dead or ready | |
| FOREACH_FACE(f) { | |
| if(!isValueReceivedOnFaceExpired(f)) { | |
| byte neighborMode = getLastValueReceivedOnFace(f); | |
| if(neighborMode != DEAD && neighborMode != READY) { | |
| return false; | |
| } | |
| } | |
| } | |
| // great we have 2 neighbors that are either dead or ready | |
| // let's check to see if they are adjacent to one another | |
| byte NO_FACE = 255; | |
| byte firstNeighborFace = NO_FACE; | |
| byte secondNeighborFace = NO_FACE; | |
| FOREACH_FACE(f) { | |
| if(!isValueReceivedOnFaceExpired(f)) { | |
| if(firstNeighborFace == NO_FACE) { | |
| firstNeighborFace = f; | |
| }else { | |
| secondNeighborFace = f; | |
| } | |
| } | |
| } | |
| // now that we have the two faces, are they adjacent? | |
| if( (firstNeighborFace == 0 && secondNeighborFace == 1 ) || | |
| (firstNeighborFace == 1 && secondNeighborFace == 2 ) || | |
| (firstNeighborFace == 2 && secondNeighborFace == 3 ) || | |
| (firstNeighborFace == 3 && secondNeighborFace == 4 ) || | |
| (firstNeighborFace == 4 && secondNeighborFace == 5 ) || | |
| (firstNeighborFace == 0 && secondNeighborFace == 5 ) ) | |
| { | |
| return true; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment