/* Generates a random "number" such as 1 <= "number" < "top". * Returns it by reference in the "number" parameter. */ void PrimeGenerator::makeRandom(BigInt &number, const BigInt &top) { //randomly select the number of digits for the random number unsigned long int newDigitCount = (rand() % top.digitCount) + 1; makeRandom(number, newDigitCount); //make sure the number is < top and not zero while (number >= top || number.EqualsZero()) makeRandom(number, newDigitCount); //make sure the leading digit is not zero while (number.digits[number.digitCount - 1] == 0) number.digitCount--; }
int main(void) { uint8_t i, j, randomNumber, tempNumber, buttonCount, n = 0; uint8_t portState[3] = {0xDF,0xBF,0x7F}; uint8_t inputState[3] = {0x04,0x08,0x10}; uint8_t* arr; uint8_t* backup_arr; //Источник энтропии - счетчик задержки нажатия клавиш uint16_t delayCounter = eeprom_read_word((uint16_t*)1); DDRC = 0xFF; DDRB = 0xFF; DDRD = 0xE3; PORTD = 0xFF; initUART(); while (1) { arr = (uint8_t *)malloc((n + 1) * sizeof(uint8_t)); if (n > 0) { memcpy(arr, backup_arr, n); free(backup_arr); } tempNumber = eeprom_read_byte((uint8_t*)0); srand(delayCounter); tempNumber ^= (makeRandom(1000)%255); eeprom_write_byte((uint8_t*)0, (uint8_t)(tempNumber % 8)); randomNumber = eeprom_read_byte((uint8_t*)0); arr[n] = toDisplayNum(randomNumber); for (i = 0; i < n + 1; i++) { comboPort(arr[i]); _delay_ms(DELAY); comboPort(0x00); _delay_ms(DELAY); } buttonCount = 0; while(buttonCount < n + 1) { for(i = 0; i < 3; i++) { PORTD = portState[i]; for(j = 0; j < 3; j++) { if(!(PIND & inputState[j])) { while((PIND & inputState[j]) != inputState[j]); if(display[i][j] == arr[buttonCount]){ comboPort(display[i][j]); buttonCount++; } else { sendStr("Game over! Your score: "); sendNum(n); sendByte(13); //Мигаем GG while(1) { comboPort(0xC2); _delay_ms(DELAY); comboPort(0x00); _delay_ms(DELAY); } } } delayCounter++; } } } backup_arr = (uint8_t*)malloc((n + 1) * sizeof(uint8_t)); memcpy(backup_arr, arr, n + 1); free(arr); _delay_ms(DELAY); comboPort(0x00); _delay_ms(DELAY * 2); eeprom_write_word((uint16_t*)1, (uint16_t)(delayCounter)); n++; } }
void PuzzleWorldShow::step(float seconds) { //a state machine must be implemented if (state == EGoingCenter1) { //find out if the first object has reached some point, say 400/400 and make set the next state if (allObjects.at(allObjects.size() * 95 / 100)->reachedDestination()) { state = ESolve1; solveAll(); } } else if (state == ESolve1) { if(allObjects.at(allObjects.size() * 95 / 100)->reachedDestination()) { state = EImplode1; gather(Coordinate(rand() % 800, rand() % 800)); } } else if (state == EImplode1) { if(allObjects[allObjects.size() / 2] ->reachedDestination()) { state = ESolve2; solveAll(); } } else if (state == ESolve2) { if(allObjects[allObjects.size() / 2]->reachedDestination()) { state = EHalfImplode1; gather(Coordinate(rand() % 800, rand() % 800)); } } else if (state == EHalfImplode1) { //parts of the code won't survive simple changes, like the sizes if ( allObjects[allObjects.size() / 2 + 20] -> reachedDestination() ) { state = EMoveTogether1; randomTogether(); timer.start(); } } else if (state == EMoveTogether1) { if (timer.elapsed() >= 5 * secondDivisions) { state = EShakeTogether1; randomTogether(); timer.restart(); } } else if (state == EShakeTogether1) { randomTogether(); if(timer.elapsed() >= 5 * secondDivisions) { state = EMix1; makeRandom(); } } else if (state == EMix1) { if(allObjects.first()->reachedDestination()) { state = ESolve3; solveAll(); } } else if (state == ESolve3) { if (allObjects[allObjects.size() / 2]->reachedDestination()) { state = EExplode1; random(); timer.restart(); } } else if (state == EExplode1) { if (timer.elapsed() >= 5 * secondDivisions) { randomTogether(); if (allObjects.first()->getSpeedModule() > 20) { makeRandom(); state = EMix2; } } } else if (state == EMix2) { makeRandom(); if(timer.elapsed() >= 5 * secondDivisions) { state = ESolve4; solveAll(); } } else if (state == ESolve4) { if(allObjects.first()->reachedDestination()) { state = EExplode2; timer.restart(); } } else if (state == EExplode2) { random(); if (timer.elapsed() >= 5 * secondDivisions) { state = ESolve5; randomTogether(); solveAll(); } } else if (state == ESolve5) { //this time check that the image is really composed bool composed = true; int i = 0; while(composed && i < allObjects.size()) { if (! allObjects.at(i)->reachedDestination()) composed = false; i++; } if (composed) { state = ESlowlyDecompose1; slowlyDecomposeCounter = 0; } //check for full composition only the first time else if (episode > 0) { if (allObjects.first()->reachedDestination()) { //stop the objects from coming back, if an objects did not reach its destination it won't at all for(int i = 0; i < allObjects.size(); i++) if (! allObjects.at(i)->reachedDestination()) allObjects.at(i)->unsetDestination(); state = ESlowlyDecompose1; slowlyDecomposeCounter = 0; } } } else if (state == ESlowlyDecompose1) { if (slowlyDecomposeCounter < allObjects.size()) { //percents here int nrTake; if (slowlyDecomposeCounter < 5.0 / 100.0 * allObjects.size()) nrTake = nrCol / 2; else if (slowlyDecomposeCounter < 10.0 / 100.0 * allObjects.size()) nrTake = 1; else if (slowlyDecomposeCounter < 98.0/100.0 * allObjects.size()) nrTake = nrCol / 2; else nrTake = 1; //take care here, the program can crash if it does not check for limits for (int i = 0; i < nrTake; i++) { allObjects[slowlyDecomposeCounter]->setSpeed(Speed(-50, -50)); slowlyDecomposeCounter++; } } else { state = EImplode2; gather(Coordinate(rand() % 800, rand() % 800)); episode++; } } else if(state == EImplode2) { if (allObjects.first()->reachedDestination()) { state = EExplode3; random(); timer.restart(); } } else if (state == EExplode3) { if (timer.elapsed() >= 10 * secondDivisions) { timer.restart(); state = EFollowing1; Speed x(rand() % 1000 + 300, rand() % 1000 + 300); allObjects.first()->setSpeed(x); for (int i = 1; i < allObjects.size(); i++) { allObjects.at(i)->setSpeed(x); allObjects.at(i)->setSpeedModule( x.speedModule() / i * 50 + 30 ); allObjects.at(i)->setDestination(allObjects.at(i-1)); } } } else if(state == EFollowing1) { if (timer.elapsed() >= 15 * secondDivisions) { state = EGoingCenter1; gather(Coordinate(rand() % 800, rand() % 800)); } } else { qDebug() << "PuzzleWorldShow, no such state" << state; state = EGoingCenter1; } World::step(seconds); }
int main(int argc, char *argv[]) { GraphType graphType; edgefn ef; opts.pfx = ""; opts.name = ""; opts.cnt = 1; graphType = init(argc, argv, &opts); if (opts.directed) { fprintf(opts.outfile, "digraph %s{\n", opts.name); ef = dirfn; } else { fprintf(opts.outfile, "graph %s{\n", opts.name); ef = undirfn; } switch (graphType) { case grid: makeSquareGrid(opts.graphSize1, opts.graphSize2, opts.foldVal, opts.isPartial, ef); break; case circle: makeCircle(opts.graphSize1, ef); break; case path: makePath(opts.graphSize1, ef); break; case tree: if (opts.graphSize2 == 2) makeBinaryTree(opts.graphSize1, ef); else makeTree(opts.graphSize1, opts.graphSize2, ef); break; case trimesh: makeTriMesh(opts.graphSize1, ef); break; case ball: makeBall(opts.graphSize1, opts.graphSize2, ef); break; case torus: if ((opts.parm1 == 0) && (opts.parm2 == 0)) makeTorus(opts.graphSize1, opts.graphSize2, ef); else makeTwistedTorus(opts.graphSize1, opts.graphSize2, opts.parm1, opts.parm2, ef); break; case cylinder: makeCylinder(opts.graphSize1, opts.graphSize2, ef); break; case mobius: makeMobius(opts.graphSize1, opts.graphSize2, ef); break; case sierpinski: makeSierpinski(opts.graphSize1, ef); break; case complete: makeComplete(opts.graphSize1, ef); break; case randomg: makeRandom (opts.graphSize1, opts.graphSize2, ef); break; case randomt: { int i; treegen_t* tg = makeTreeGen (opts.graphSize1); for (i = 1; i <= opts.cnt; i++) { makeRandomTree (tg, ef); if (i != opts.cnt) closeOpen (); } freeTreeGen (tg); } makeRandom (opts.graphSize1, opts.graphSize2, ef); break; case completeb: makeCompleteB(opts.graphSize1, opts.graphSize2, ef); break; case hypercube: makeHypercube(opts.graphSize1, ef); break; case star: makeStar(opts.graphSize1, ef); break; case wheel: makeWheel(opts.graphSize1, ef); break; default: /* can't happen */ break; } fprintf(opts.outfile, "}\n"); exit(0); }