//déplacement selon l'etat de mouvement courant void Bee::move(sf::Time dt) { switch(moveMode_) { case MoveMode::Rest: { changeEnergy(-restloss_); break; } case MoveMode::Random: { randomMove(dt); changeEnergy(-moveloss_); break; } case MoveMode::Targeted: { targetMove(dt,target_); changeEnergy(-moveloss_); break; } } }
int main(void) { printf("Running...\n"); Patterns patterns; // patterns.init(patternsFile3x3, patternsFile5x5); Board* b = new Board(); b->init(); UCTNode::rootState = b; UCTNode *node = new UCTNode(1, -1, nullptr); GameResult r; while (!b->isGameOver(&r)) { switch (b->turn) { case Black: randomMove(&node, b); break; case White: computerMove(&node, b, 0, 500, nullptr); // &patterns); break; case Empty: case OutOfBounds: default: assert(false); } } delete node; delete b; }
virtual void update() override { if (m_state == Idle) { if (m_game->random(100) < 50) { return; } if (m_game->random(100) < 25) { m_state = RunToCabbage; return; } randomMove(); } if (m_state == RunToCabbage) { if (m_game->random(100) < 10) { m_state = Idle; return; } auto cabbage = m_game->getNearest<Cabbage>(this); if (!cabbage) { m_state = Idle; return; } if (auto blockingObject = moveTo(cabbage->m_x, cabbage->m_y)) { if (blockingObject->is<Player>()) { m_state = RunAway; return; } if (blockingObject->is<Fence>()) { m_state = Idle; return; } } } if (m_state == RunAway) { auto blockingObject = move(DirDown); if (blockingObject) m_state = Idle; } }
std::vector<std::vector< int> > Player::possibleMoves() { // if(x>=width) // std::cout<<x<<" in possibleMoves() "<<digit<<" "<<std::endl; std::vector<std::vector< int> > result; if(y==0)//top { //if(debug) //std::cout<<"topMoves\n"; result = topMoves(); } else if (y==29)//bottom { //if(debug) //std::cout<<"bottommoves\n"; result = bottomMoves(); } else//somewhere in the middle { //std::cout<<"middlemoves\n"; result = middleMoves(); } if(result.empty()) result.push_back(randomMove()); return result; }
void Puzzle::scramble(kj::Random<int>& random) { for(int i = 0; i < 1000; i++) randomMove(random); for(int i = 0; i < d_rows; i++) up(); for(int i = 0; i < d_columns; i++) left(); }
unsigned int MCBoard::randomGame() { setPosition(start_pos); // incomplete. need turn. (arg ?) for (unsigned int i = 0; i < max_game_length; i++) { Move m = randomMove(); if (m.empty()) return turn; // winner move(m); } return 2; // no win }
int GameScene::act() { leadAct(lead, end, restFlag); moveableGroup->routeReset(); ////////////////////////////////////////////////////////////////////////// if (xFlag) enemy = y_1; else enemy = x_1; randomMove(enemy); ////////////////////////////////////////////////////////////////////////// return NOEVENT; }
Move Board::randomMove() const { // Accurate random move //vector<Move> moves = validMoves(); //return moves[entropy(moves.size())]; // This one is fast, but biased: BoardMask pp = playerPieces(); do { BoardPoint p = pp.randomPoint(); Move m = randomMove(p); if(m.isValid()) return m; pp.clear(p); } while(pp); return Move(); }
void MCBoard::genetrateRandomPosition(const unsigned int n_moves) { unsigned int num_moves = n_moves ? n_moves : rand() % max_game_length; move_history.clear(); for (unsigned int i = 0; i < num_moves; i++) { Move m = randomMove(); if (m.empty()) { std::cerr << toString(1) << " - game over, sorry." << std::endl; return; } moveRecorded(m); } print(1); /* int v = Position::evaluatePosition(); if (v > 1000 || v < -1000) std::cerr << toString(1) << " " << v << std::endl; else std::cout << toString(1) << " " << v << std::endl; */ }
Point Opponent::play(const Board& board) { if (m_difficulty == Trivial) { return randomMove(board); } else { int maxDepth = int(m_difficulty); Candidate best; QElapsedTimer timer; timer.start(); for (int depth = 1; depth <= maxDepth; ++depth) { std::pair<Candidate, bool> result = bestMove(board, depth, O, timer); qDebug() << "Depth" << depth << "of" << maxDepth << "took" << timer.elapsed() << "ms, timed out: " << result.second; if (result.second) { qDebug() << "Search timed out at depth" << depth << "of" << maxDepth; break; } best = result.first; } return best.move; } }
void Noble::run() { if(!ran) { if(isEnemyInRange("Villager")) { attack(); } else { if(name=="king" || !moveTowardEnemy()) randomMove(); for(int i=0;i<castle->repaint;i++) { castle->OnPaint(); } ran=true; } } else ran=false; }
void mode1Loop(Input *pIn,GameOptions* pGameOptions,Screen* pScreen, GameState* pGameState){ MusicManager musicManager; audioInitialization(&musicManager); Mix_PlayMusic(musicManager.music[musicManager.currentMusic], -1); //Jouer infiniment la musique TTFManager ttfManager; ttfInitialization(pScreen,&ttfManager); initPauseText(pScreen, &ttfManager); Character character; characterInitialization(&character,pScreen); Enemies enemies; enemiesInitialization(&enemies,pScreen); Unit seed; initSeed(&seed,pScreen); randomMove(&seed); Collision collision; TimeManager timeManager; timeManager.debutTicks = (long) SDL_GetTicks(); timeManager.playingTime=0; long frame = 0; long frameTime = 0; long delay = 0; int score = 0; Texte scoreTexte = {0}; scoreTexte.rec.x=865; scoreTexte.rec.y=580; scoreTexte.rec.w=0; scoreTexte.rec.h=30; pGameState->lost = 0; //On set la taille du BG du mode pGameOptions->BGRec.x = 0; pGameOptions->BGRec.y = 0; pGameOptions->BGRec.w = PLAYING_AREA_WIDTH; pGameOptions->BGRec.h = PLAYING_AREA_HEIGHT; updateTTFManager(pScreen,&ttfManager,&timeManager,&enemies); updateSeedScore(pScreen, &scoreTexte, score, &ttfManager); while(!pGameState->menu && !pIn->quit && !pGameState->lost){ frameTime = (long) SDL_GetTicks(); frame++; updateInput(pIn); checkCollision(&character,&enemies,&collision,pGameState); moveEnemies(&enemies); moveCharacter(pIn,&collision,&character); //On ajoute un ennemi régulièrement if (frame%240 == 0){ addOneEnemy(&enemies); } //On met à jours l'afficheur du temps de jeu mais pas trop souvent if(frame%20 == 0){ updateTTFManager(pScreen,&ttfManager,&timeManager,&enemies); } //Est ce que le joueur à trouver une seed ? if(checkUnitCollisionWIthCharacter(&seed, &character)){ score ++; randomMove(&seed); updateSeedScore(pScreen, &scoreTexte, score, &ttfManager); } //On affiche l'ensemble SDL_RenderClear(pScreen->renderer); updateScreen(&character,&enemies,pScreen,&ttfManager,pGameOptions); SDL_RenderCopy(pScreen->renderer,seed.pTexture, NULL, &seed.rec); SDL_RenderCopy(pScreen->renderer,scoreTexte.pTexture, NULL, &scoreTexte.rec); SDL_RenderPresent(pScreen->renderer); //Si on vérifie si le joueur veux mettre en pause(P) ou quitter (Q) if(pIn->keys[SDL_SCANCODE_P]){ pGameState->pause=1; pIn->keys[SDL_SCANCODE_P]=0; } if(pIn->keys[SDL_SCANCODE_Q]){ pGameState->menu=1; } //boucle de pause if(pGameState->pause){ //il faut arrêter replacer le compteur de temps après la pause long pauseDebut = (long) SDL_GetTicks(); //on print dans le renderer ce qu'on veut afficher pendant la pause updateScreen(&character,&enemies,pScreen,&ttfManager,pGameOptions); SDL_RenderCopy(pScreen->renderer, ttfManager.pauseTextL1, NULL, &ttfManager.pauseTextL1Rec); SDL_RenderCopy(pScreen->renderer, ttfManager.pauseTextL2, NULL, &ttfManager.pauseTextL2Rec); SDL_RenderCopy(pScreen->renderer, ttfManager.pauseTextL3, NULL, &ttfManager.pauseTextL3Rec); SDL_RenderCopy(pScreen->renderer,seed.pTexture, NULL, &seed.rec); pauseLoop(pGameState,pIn,pScreen); timeManager.debutTicks += (long) SDL_GetTicks() - pauseDebut; } //Code pour attendre une durée de frame fixe delay = pScreen->frameDuration - (long) SDL_GetTicks() + frameTime; if (delay > 0) { SDL_Delay(delay); } } //Si le joueur sort du jeu car il a perdu la partie courante if(pGameState->lost){ pGameState->waiting=1; //Petit son de défaite Mix_PauseMusic(); Mix_PlayChannel(1, musicManager.sound[0], 0); //On print l'affichage de fin de partie updateScreen(&character,&enemies,pScreen,&ttfManager,pGameOptions); SDL_RenderCopy(pScreen->renderer, ttfManager.playAgain, NULL, &ttfManager.playAgainRec); SDL_RenderCopy(pScreen->renderer, ttfManager.BAM, NULL, &ttfManager.BAMRec); SDL_RenderCopy(pScreen->renderer,seed.pTexture, NULL, &seed.rec); //On attend que l'utilisateur décide de rejouer ou non endGameLoop(pIn,pGameState,pScreen); } }
void Puzzle::randomMove() { kj::Random<int> random; randomMove(random); }
void Solution::mutation(){ randomMove(); }
void Board::iterateBoard() { int x_pos, y_pos; // now start at from the beginning // and keep iterating over the element till you find // nth element...or reach the end of vector. for (int i = 0; i < this->ANTS_LIVE; i++) { EnumBoard stateAnt = this->board[liveAntsInBoard[i].x_pos][liveAntsInBoard[i].y_pos].state; bool holdItem = stateAnt == ANT_CARRYING ? true : false; if (stateAnt == ANT_CARRYING) { if (dropOrNot(liveAntsInBoard[i])) { this->board[liveAntsInBoard[i].x_pos][liveAntsInBoard[i].y_pos].state = ANT_DEAD; stateAnt = ANT_NONCARRING; //now is not carrying holdItem = false; } else { this->board[liveAntsInBoard[i].x_pos][liveAntsInBoard[i].y_pos].state = FREE; holdItem = true; } } else { bool isPick = pickOrNot(liveAntsInBoard[i]); if (stateAnt == ANT_DEAD_AND_NONCARRYING && isPick) { holdItem = true; this->board[liveAntsInBoard[i].x_pos][liveAntsInBoard[i].y_pos].state = FREE; stateAnt = ANT_CARRYING; //now is carrying } else if (stateAnt == ANT_DEAD_AND_NONCARRYING && !isPick) { this->board[liveAntsInBoard[i].x_pos][liveAntsInBoard[i].y_pos].state = ANT_DEAD; holdItem = false; } else { this->board[liveAntsInBoard[i].x_pos][liveAntsInBoard[i].y_pos].state = FREE; holdItem = false; } } //walk ant //find new position to go EnumBoard stateInNewCell; if (stateAnt == ANT_CARRYING) {// || stateAnt == ANT_DEAD_AND_NONCARRYING) { do { x_pos = randomMove(liveAntsInBoard[i].x_pos); y_pos = randomMove(liveAntsInBoard[i].y_pos); stateInNewCell = this->board[x_pos][y_pos].state; } while (stateInNewCell != FREE); } else { do { x_pos = randomMove(liveAntsInBoard[i].x_pos); y_pos = randomMove(liveAntsInBoard[i].y_pos); stateInNewCell = this->board[x_pos][y_pos].state; } while (stateInNewCell != FREE && stateInNewCell != ANT_DEAD); } //walk to new liveAntsInBoard[i].x_pos = x_pos; liveAntsInBoard[i].y_pos = y_pos; //update new if (holdItem) { this->board[x_pos][y_pos].state = ANT_CARRYING; } else { if (stateInNewCell == ANT_DEAD) { this->board[x_pos][y_pos].state = ANT_DEAD_AND_NONCARRYING; } else { this->board[x_pos][y_pos].state = ANT_NONCARRING; } } } }
int main(int argc, char *argv[]) { int pos_x = 100; int pos_y = 100; const int res_x = 640; const int res_y = 480; int scale = 1; setup *app = sdl_setup("Movement", pos_x, pos_y, res_x, res_y); if (TTF_Init() != 0) { logSDLError("TTF_Init"); cleanup("rw", app->renderer, app->window); free(app); SDL_Quit(); return -1; } SDL_Rect container; container.x = 20; container.y = 20; container.w = res_x - 2 * container.x; container.h = res_y - 2 * container.y; SDL_Rect box; box.w = scale * container.x; box.h = scale * container.y; box.x = container.x;//res_x / 2 - box.w / 2; box.y = res_y / 2 - box.h / 2; SDL_Rect goal; goal.w = box.w; goal.h = box.h; goal.x = res_x - container.x; goal.y = res_y / 2 - goal.h / 2; SDL_Rect winner; winner.h = container.h - 20; winner.w = container.w - 20; winner.x = container.x + 10; winner.y = container.y + 10; int size = 26; SDL_Rect *army = addEnemy(size); // SDL_Rect *army2 = addEnemy(size); bool quit = false; bool won = false; SDL_Event e; while (!quit) { while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { quit = true; break; } if (e.type == SDL_KEYDOWN) { switch(e.key.keysym.sym) { case SDLK_ESCAPE: quit = true; break; case SDLK_UP: box.y -= box.h; break; case SDLK_DOWN: box.y += box.h; break; case SDLK_LEFT: box.x -= box.w; break; case SDLK_RIGHT: box.x += box.w; break; default: break; } } } if (!won) { randomMove(army, size, 1); // randomMove(army2, size, 3); if (contains(&container, &box) || enemyCollision(&box, army, size))// || enemyCollision(&box, army2, size)) { printf("Collision\n"); if (collision(&goal, &box)) { won = true; printf("WINNER!\n"); } box.x = container.x;//res_x / 2 - box.w / 2; box.y = res_y / 2 - box.h / 2; } renderHard(app->renderer, &container, &box, &goal, army, NULL, size); SDL_Delay(2); } if (won) { // SDL_Delay(100); SDL_SetRenderDrawColor(app->renderer, 0xff, 0xff, 0xff, 0xff); SDL_RenderFillRect(app->renderer, &winner); const string f_name = "fonts/Gidole-Regular.ttf"; SDL_Color f_color = {25, 25, 25, 25}; SDL_Texture *slogan = renderText(app->renderer, "You have WON the game! BANZAI!!!", f_name, 30, f_color); if (slogan == NULL) { cleanup("rw", app->renderer, app->window); free(army); free(app); TTF_Quit(); SDL_Quit(); logSDLError("renderText"); return -2; } int slgn_h, slgn_w; SDL_QueryTexture(slogan, NULL, NULL, &slgn_w, &slgn_h); int x = res_x / 2 - slgn_w / 2; int y = res_y / 2 - slgn_h / 2; renderTexture(app->renderer, slogan, x, y, NULL); SDL_RenderPresent(app->renderer); } } cleanup("rw", app->renderer, app->window); free(army); free(app); SDL_Quit(); return 0; }