// Method for moving brick down, // detects if new bricks should be spawned, also detects end of game bool TetrisEngine:: moveDown() { bool canContinue = true; for (int i= 0; i < BRICKCOUNT; i++) { // maximum allowed number for tile table is numRows * 10 - 1 // if it exceeds brick should be stopped // or if its colliding with another brick if ((_curBrick->brickPosition[i] + _numCols > _numRows * 10 - 1) || (checkCollision(_curBrick->brickPosition[i] + _numCols)) ) { canContinue = false; break; } } if (canContinue) { for (int i= 0; i < BRICKCOUNT; i++) { // going one row down by adding columns count _tiles[_curBrick->brickPosition[i]]->setUnactive(); _curBrick->brickPosition[i] = _curBrick->brickPosition[i] + _numCols; } // highlighting new tiles after changing all to avoid double highlight on one tile for (int i= 0; i < BRICKCOUNT; i++) { _tiles[_curBrick->brickPosition[i]]->setActive(resolveBrickColor(_curBrick->color)); } } else { qDebug() << "Collision detected, end of life of brick"; if (spawnBrick()) { setEndOfGame(true); } // after spawnBrick, so old brick tiles will be unactive checkForLine(); } return canContinue; }
void doShortCourse(int state) { int nextState = 0; switch (state) { case 0: { //move 2 metres to the wall forwards(20); while (!wallFound(1)) { } nextState = 1; break; } case 1: { //found a wall follow it setSensorSide(1);//follow left wall while (wallFound(1)) { correctForwardMotion(); } nextState = 2; break; } case 2: { //wall ended find a line forwards(20); while (!checkForLine()) { } nextState = 3; break; } case 3: { //line found follow it lineFollow(); nextState = 4; break; } case 4: { // docked please stop. while(wallFound(3)) { } stopLineFollow(); nextState = 7; break; } } if (nextState != 7){ doShortCourse(nextState); } }
void doLongCourse(int state) { int nextState = 0; switch (state) { case 0: { //move 2 metres to the wall forwards(20); while (!wallFound(1)) { } nextState = 1; break; } case 1: { //found a wall follow it setSensorSide(1);//follow left wall while (wallFound(1)) { correctForwardMotion(); } nextState = 2; break; } case 2: { //turn away from the wall and run to the right hand side right(); delay(150); nextState = 3; break; } case 3:{ forwards(20); while(!wallFound(2)) { } nextState = 4; break; } case 4 :{ setSensorSide(2); while(wallFound(2)){ correctForwardMotion(); } nextState = 5; break; } case 5: { //wall ended find a line forwards(20); while (!checkForLine()) { } nextState = 6; break; } case 6: { //line found follow it lineFollow(); nextState = 7; break; } case 7: { // docked please stop. while(wallFound(3)) { } stopLineFollow(); nextState = 8; break; } } if (nextState != 8){ doLongCourse(nextState); } }
void doTheDemo() { cmdDoPlay(">cc"); _DBG_("State 0"); forwards(20); cmdDoPlay(">dd"); _DBG_("State 1"); while (sensorSide != 1) { //Forwards until we find a left wall checkForWall(); _DBD(sensorSide);_DBG_(" sens"); } cmdDoPlay(">ee"); _DBG_("State 2"); //Follow wall until it's ended int wallState = -1; while (wallState != 3 && wallState != 0) { wallState = checkForWall(); correctForwardMotion(); } if (courseType == 0) { cmdDoPlay(">aa"); _DBG_("State 5"); while(!checkForLine()) { } followLine(); while(!checkForNoLine()) { } brake(); } else { //Bear right to head for other wall cmdDoPlay(">ff"); _DBG_("State 3"); right(); delay(1000); _DBG_("State 3.1"); forwards(20); //Wait until right wall is trackable while (sensorSide != 2) { checkForWall(); _DBD(sensorSide);_DBG_(" sens"); } //Track right wall cmdDoPlay(">gg"); _DBG_("State 4"); wallState = -1; while (wallState != 4 && wallState != 0) { wallState = checkForWall(); correctForwardMotion(); } //Find the line cmdDoPlay(">aa"); _DBG_("State 5"); forwards(20); while(!checkForLine()) { } followLine(); while(!checkForNoLine()) { } brake(); } }