Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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;
- }
Add Comment
Please, Sign In to add comment