示例#1
0
// Solution for the puzzle by Backtracking
void solveSudoku(int row, int column) {
    if (row == boardSize) { // Means that all cells are filled
        solutionPrint();
    }
    if (table[row][column] != 0) { // If a cell is not a zero, it means we have to move to the next cell
        nextCell(row, column);
    }else {
        for (int counter = 1; counter <= boardSize; counter++) { // Checks if numbers 1-9 can be put on a cell
            if ((rowCheck(row, counter) == true) && (columnCheck(column, counter) == true) && (boxCheck(row, column, counter) == true)) { // If looks promising
                table[row][column] = counter; // Make tentative assignment
                nextCell(row, column);
            }
        }
        table[row][column] = 0; // If doesn't lead to a solution, reset to 0. This triggers the backtracking
        // Note this backtracking step, a very important step**
        // We come at this position, this step, this line when we have already checked all possible values at 
        // sudoku[i][j] and we couldn't find the solution
        // Put any value does not solves our board implies that we must have made wrong choice earlier
        // so we make this sudoku[i][j] again a vacant cell and try to correct our previous guesses/choices.
    }
}
示例#2
0
文件: main.c 项目: pjztam/TetrisGBA
//main
int main() {
  REG_DISPCNT = MODE_3 | BG2_ENABLE;

  enum GBAState state = START;
  enum GBAState prevState = state;

  while(1) {
		waitForVblank();
		switch(state) {
		case START:
			drawImage3(0,0,SPLASH_WIDTH,SPLASH_HEIGHT,splash);
			prevState = START;
			state = NODRAW;
			break;
		case NODRAW:

			if (prevState == START) {
				if (KEY_DOWN_NOW(BUTTON_START)) {
					state = GAME;
				}
				if (KEY_DOWN_NOW(BUTTON_A)) {
					state = HELP;
				}
			}
			if (prevState == HELP) {
				if (KEY_DOWN_NOW(BUTTON_SELECT)) {
					state = START;
				}
			}
			if (prevState == GAMEOVER) {
				if (KEY_DOWN_NOW(BUTTON_SELECT)) {
					state = START;
				}
			}

			break;
		case GAME:
			drawImage3(0,0,GAME2_WIDTH,GAME2_HEIGHT,game2);

			char stuff[4] = "000\0";
		  stuff[0] = 48 + (clearedRows/100)%10;
		  stuff[1] = 48 + (clearedRows/10)%10;
		  stuff[2] = 48 + clearedRows%10;
		  drawImagePartial(25, 180, 20, 30, GAME2_WIDTH, game2);
		  drawString(25,180,stuff, WHITE);

      block curr = randomBlock();
			block next = randomBlock();
      drawBlock(curr);
			int button = 0;
      while(1) {
				delay(20);
        if (KEY_DOWN_NOW(BUTTON_SELECT)) {
					prevState = GAME;
					state = START;
          break;
        }

        block old = curr;
        tick++;
        if (collisionDetectBottom(curr) == 1) {
          rowCheck(curr);
					curr = next;
					next = randomBlock();
					if(collisionDetect(curr) == 1) {
						prevState = GAME;
						state = GAMEOVER;
						break;
					}
        } else {
          if(KEY_DOWN_NOW(BUTTON_LEFT) && collisionDetectLeft(curr) == 0){
              curr.x--;
          } else if(KEY_DOWN_NOW(BUTTON_RIGHT) && collisionDetectRight(curr) == 0){
              curr.x++;
          } else if(KEY_DOWN_NOW(BUTTON_DOWN) && collisionDetectBottom(curr) == 0){
              curr.y--;
          } else if(button == 0 && KEY_DOWN_NOW(BUTTON_UP) && rotationDetect(curr) == 0){
							button = 1;
              rotateBlock(&curr);
          } else if (tick > 20) {
            tick = 0;
            curr.y--;
          } else if (!KEY_DOWN_NOW(BUTTON_DOWN)){
						//drawString(25,70,"stuff stuff", WHITE);
						button = 0;
					}
          waitForVblank();
          eraseBlock(old);
          drawBlock(curr);
        }
      }
      clearBoard();
      drawRect(4,4,70,10, BLACK);
			break;
		case GAMEOVER:
      drawImage3(0,0,END_WIDTH,END_HEIGHT,end);
			prevState = GAMEOVER;
			state = NODRAW;
			break;
		case HELP:
			drawImage3(0,0,HELP_WIDTH,HELP_HEIGHT,help);
			prevState = HELP;
			state = NODRAW;
			break;
		}
	}

  return 0;
}