void GameLogicProcessor::FrameStart() { // LOG(EngineLog, BE_LOG_VERBOSE) << "GameLogicProcessor::FrameStart\n"; // Initialize all components not yet initialized for (ComponentHandle hdl : m_notInitialized) { auto& pieces = gameLogicHolder->getComponent(hdl)->m_gamelogics; for (size_t i = 0; i < pieces.size(); ++i) { GameLogic* logic = pieces[i]; GameLogic::RunEvents ev = logic->getRunEvents(); if (ev & GameLogic::RunEvents::EFrameStart){ m_onFrameStart.emplace_back(logic); } if (ev & GameLogic::RunEvents::EFrameMiddle){ m_onFrameMiddle.emplace_back(logic); } if (ev & GameLogic::RunEvents::EFrameEnd){ m_onFrameEnd.emplace_back(logic); } if (!logic->init()){ LOG(EngineLog, BE_LOG_ERROR) << "GameLogicProcessor: GameLogicComponent " << hdl << " logic " << i << " failed to Init! "; } } } m_notInitialized.clear(); // Run FrameStart on all components for (GameLogic* l : m_onFrameStart){ l->frameStart(); } }
int Engine::Draw(Context& context) { // Create a graphics and set it to our system Graphics* graph = GetSystem<Graphics>(SystemType::Sys_Graphics); if (graph == nullptr) return false; // Create the game logic and set it to our system GameLogic* logic = GetSystem<GameLogic>(SystemType::Sys_Logic); if (logic == nullptr) return false; // Begin the Draw graph->BeginDraw(); // Draw the game logic logic->Draw(context); Rect2D r(100, 100, 200, 200); RENDERER->DrawRect(r, 2.f); // End the Draw graph->EndDraw(); return true; }
void Player::hit(float damage) { _health -= damage; if (_health <= 0) { std::vector<std::string> wrecks; wrecks.push_back("player_destroyed_0.PNG"); wrecks.push_back("player_destroyed_1.PNG"); wrecks.push_back("player_destroyed_2.PNG"); wrecks.push_back("player_destroyed_3.PNG"); DestroyedShip::create(this->getPosition(), wrecks); this->setScale(0); GameLogic* logic = GameManager::getInstance()->getGameLogic(); _lifes--; this->stopAllActions(); _isFiring = false; CCDirector::getInstance()->getScheduler()->unschedule(schedule_selector(Player::shoot), this); if (_lifes > 0) { logic->setGameState(GameState::PLAYER_SPAWN); } else { logic->setGameState(GameState::GAME_OVER); } } }
bool CBarrierGame::Link(Vertex avPath[MAX_VERTEX_NUM], int &nVexNum) { //m_ptSelSec,m_ptFirstSecÊÇ·ñÁ¬Í¨ if (m_ptSelFirst.row == m_ptSelSec.row && m_ptSelFirst.col == m_ptSelSec.col) { return false; } int nInfo1 = m_graph.GetVertex(m_ptSelFirst.row * MAX_PIC_NUM + m_ptSelFirst.col); int nInfo2 = m_graph.GetVertex(m_ptSelSec.row * MAX_PIC_NUM + m_ptSelSec.col); int info1 = m_ptSelFirst.row * MAX_PIC_NUM + m_ptSelFirst.col; int info2 = m_ptSelSec.row * MAX_PIC_NUM + m_ptSelSec.col; if (nInfo1 != nInfo2 || nInfo1 == BLANK || nInfo2 == BLANK) { return false; } GameLogic gameLogic; if (gameLogic.IsLink(m_graph, m_ptSelFirst, m_ptSelSec)) { m_nGrade += 10; gameLogic.Clear(m_graph, m_ptSelFirst, m_ptSelSec); nVexNum = gameLogic.GetVexPath(avPath); return true; } return false; }
int main() { GameLogic logic; logic.init(); return logic.exec(); }
void World::StartGame( const GameSettings& _Settings ) { // Init game m_spGame = GamePtr( new Game() ); GameLogic logic; logic.Setup( m_spDatabase, m_spGame ); logic.StartGame( _Settings ); }
void World::Update( Float32 _f32TimeDifference ) { if ( m_spGame != NULL ) { GameLogic logic; logic.Setup( m_spDatabase, m_spGame ); logic.UpdateGame( _f32TimeDifference ); } }
int CBarrierGame::IsWin(int nTime) { GameLogic logic; if (nTime <= 0) { m_graph.ClearGraph(); return GAME_LOSE; } if (logic.IsBlank(m_graph)) { m_graph.ClearGraph(); return GAME_NEXT; } return GAME_PALY; }
/** * @brief GameInteligence::Hard Metoda definující nejtěžší úroven herní inteligence. * @param GameDat Herní data * @return Vraci herni data na které byla aplikována herní inteligence pro provedení tahu. */ GameData *GameInteligence::Hard(GameData *GameDat) { struct ScoreArray Score[200]; int pointer=0; int actual=GameDat->Game.Actual; int last= GameDat->Game.Last; GameData *TmpGame = new GameData(); TmpGame=GameDat; for(int i=0;i<GameDat->Game.GridSize;i++){ for(int j=0;j<GameDat->Game.GridSize;j++){ GameLogic *Logic = new GameLogic(); if(TmpGame->Game.History[TmpGame->Game.Actual].Grid[i][j]==0){ if(Logic->SolveMove(TmpGame->Game.History[TmpGame->Game.Actual].Grid,i,j,TmpGame->Game.GridSize,TmpGame->Game.ActivePlayer)){ TmpGame = Logic->Move(TmpGame,i,j); Score[pointer].i=i; Score[pointer].j=j; int points=0; for(int x=0;x<GameDat->Game.GridSize;x++){ for(int y=0;y<GameDat->Game.GridSize;y++){ if((TmpGame->Game.History[TmpGame->Game.Actual].Grid[x][y])==2){ points++; } } } Score[pointer].diffscore=points-(GameDat->Game.Player2Score); points=0; TmpGame->Game.Actual=actual; TmpGame->Game.Last=last; TmpGame=GameDat; pointer++; } } } } int maxpointer=pointer; int max=Score[0].diffscore; int pointerOfMax=0; for(pointer=1;pointer<maxpointer;pointer++){ if(Score[pointer].diffscore>max){ max=Score[pointer].diffscore; pointerOfMax=pointer; } } GameLogic *Logic = new GameLogic(); GameDat->Game.Actual=actual; GameDat->Game.Last=last; return Logic->Move(GameDat,Score[pointerOfMax].i,Score[pointerOfMax].j); }
bool Board::searchForWinner(GameLogic& gameLogic, Directions direction, Board& board, char gamePiece) { if (!gameLogic.isWinnerFound()) { int i = 0; for (int row = 1; row < Board::ROWS - 1; row++) { for (int col = 1; col < Board::COLUMNS - 1; col++) { while (board.getPosition(row, col) == gamePiece && !gameLogic.isWinnerFound()) { i++; if (direction == Directions::Horizontal) { row++; } if (direction == Directions::Vertical) { col++; } if (direction == Directions::LeftDiagonal) { row++; col--; } if (direction == Directions::RightDiagonal) { row++; col++; } if (i == GameLogic::WINNING_ROW) { return true; } } i = 0; } } } //If winner has already been found else if (gameLogic.isWinnerFound()) { return true; } return false; }
int main() { //TODO: faire un systeme de "logs" GameApp app; GameLogic game; app.initialize(); game.initialize(); app.startLoop(); game.terminate(); app.terminate(); return 0; }
void AIController::update(uint64_t diff, const GameLogic& state, int player_id) { m_changes[m_changeset] = NO_CHANGE; m_changeset ^= 1; const Player* my_player = state.get_player(player_id); if (my_player == NULL) { return; } if (my_player->is_frozen()) { return; } if (!my_player->is_grabbing_obstacle()) { m_time_of_grab = 0; } else if (m_time_of_grab == 0) { m_time_of_grab = get_ticks(); } if (m_ai != NULL) { m_ai->set_logic(&state); m_ai->set_own_player(my_player); // Update the AI state: m_ai->update(state, diff); // Determine new desired aim. m_wanted_aim = m_ai->find_desired_aim(); m_aim_reason = m_ai->get_aim_reason(); // Determine whether the AI switched weapons. if (m_current_weapon != m_ai->get_curr_weapon()) { m_changes[m_changeset ^ 1] |= CHANGE_WEAPON; m_current_weapon = m_ai->get_curr_weapon(); } } // Turn gun towards wanted aim. float aimdiff = update_gun(); if (aimdiff <= AIM_TOLERANCE) { if (m_aim_reason == AI::FIRE) { m_changes[m_changeset ^ 1] |= FIRE_WEAPON; m_changes[m_changeset ^ 1] |= STOP_JUMPING; m_ai->randomize_aim_inaccuracy(); } else if (m_aim_reason == AI::JUMP && my_player->is_grabbing_obstacle() && get_ticks() - m_time_of_grab > MIN_GRAB_TIME) { m_changes[m_changeset ^ 1] |= JUMPING; } else { m_changes[m_changeset ^ 1] |= STOP_JUMPING; } } else { m_changes[m_changeset ^ 1] |= STOP_JUMPING; } }
void GameLogicProcessor::onMessage(const MsgComponentDestroyed<GameLogicComponent>& msg) { GameLogicComponent* comp = gameLogicHolder->getComponent(msg.component); auto& pieces = comp->m_gamelogics; for (size_t i = 0; i < pieces.size(); ++i) { GameLogic* logic = pieces[i]; GameLogic::RunEvents ev = logic->getRunEvents(); if (ev & GameLogic::RunEvents::EFrameStart) { removeFrom(logic, m_onFrameStart); } if (ev & GameLogic::RunEvents::EFrameMiddle) { removeFrom(logic, m_onFrameMiddle); } if (ev & GameLogic::RunEvents::EFrameEnd) { removeFrom(logic, m_onFrameEnd); } logic->end(); } }
//TODO:NOTE: add AI with different play styles and such. Can analyse outcomes int main(){ GameLogic newGame; while(newGame.checkPlayerChipCount() > 0 && newGame.getPlayAgain() == true){ newGame.gameSetup(); newGame.playerLogic(); std::cout<<"Dealer hand:"<<std::endl; newGame.dealerLogic(); std::cout<<"\n \n \n \n"<<std::endl; newGame.checkWins(); //std::cout<<"\n \n \n \n \n"<<std::endl; //TODO:change this to display more relevant info std::cout<<"\n"<<std::endl; newGame.playAgainPrompt(); std::cout<<"\n \n \n \n"<<std::endl; } return 0; }
bool SystemClass::Initialize() { // test basic lua int screenWidth, screenHeight; bool result; // Initialize the width and height of the screen to zero before sending the variables into the function. screenWidth = 0; screenHeight = 0; // Initialize the windows api. InitializeWindows(screenWidth, screenHeight); // Create the input object. This object will be used to handle reading the keyboard input from the user. m_Input = new InputClass; if(!m_Input) { return false; } // Initialize the input object. result = m_Input->Initialize(m_hinstance, m_hwnd, screenWidth, screenHeight); if(!result) { MessageBox(m_hwnd, L"Could not initialize the input object.", L"Error", MB_OK); return false; } // Create the sound object. m_Sound = AudioClass::GetInstance(); if(!m_Sound) { return false; } m_Sound->Initialize(m_hwnd); m_timer = new RSTimer(); if (!m_timer) { return false; } m_timer->SetGameSpeed(60); // Create the graphics object. This object will handle rendering all the graphics for this application. m_Graphics = new GraphicsClass; if(!m_Graphics) { return false; } // Initialize the graphics object. result = m_Graphics->Initialize(screenWidth, screenHeight, m_hwnd); if(!result) { return false; } GameLogic* gl = GameLogic::GetInstance(); gl->InitInputMgr(m_Input); return true; }
int main() { sf::RenderWindow window(sf::VideoMode(1260, 860), "Tetris AI"); #pragma region set cmd size COORD c; c.X = 200; c.Y = 1000; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), c); #pragma endregion sf::Clock clock; // starts the clock sf::Clock frameRateClock; bool pause = false; GameLogic* gameLogic = new GameLogic(); AI* ai = new AI(gameLogic); gameLogic->setAI(ai); View* view = new View(&window); view->setGameLogic(gameLogic); view->setAI(ai); #pragma region audio sf::Music music; if (!music.openFromFile("Sound/tetris_theme.wav")) printf("ERROR while loading tetris_theme"); music.play(); music.setVolume(10); music.setLoop(true); #pragma endregion while (window.isOpen()) { #pragma region SFML event stuff + keyboard / mouse events sf::Event event; while (window.pollEvent(event) ) { if (event.type == sf::Event::Closed) window.close(); if (event.key.code == sf::Keyboard::Escape) window.close(); if (event.type == sf::Event::MouseButtonPressed && !view->isGamePlayChosen()) if (event.mouseButton.button == sf::Mouse::Left) view->choseGamePlay(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y); if (event.type == sf::Event::MouseMoved && !view->isGamePlayChosen()) view->highlightButtons(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y); if (event.type == sf::Event::KeyPressed && !gameLogic->getGameOver() && gameLogic->getSinglePlayer()) { if (event.key.code == sf::Keyboard::Right) gameLogic->moveRight(); //drawCurrentTetro(); if (event.key.code == sf::Keyboard::Left) gameLogic->moveLeft(); //drawCurrentTetro(); // hard drop if (event.key.code == sf::Keyboard::Down) gameLogic->automaticDrop(); // firm drop if (event.key.code == sf::Keyboard::Space || event.key.code == sf::Keyboard::Return) gameLogic->firmDrop(); if (event.key.code == sf::Keyboard::F) gameLogic->rotate(1); // // 1 = 90°, 3 = 270° / -90° if (event.key.code == sf::Keyboard::D) gameLogic->rotate(3); // // 1 = 90°, 3 = 270° / -90° } if (event.type == sf::Event::KeyReleased && !gameLogic->getGameOver() && !pause) if (event.key.code == sf::Keyboard::P) pause = true; } #pragma endregion // currently disabled #pragma region framerate //sf::Time frameRateTime = frameRateClock.getElapsedTime(); //float framerate = 1.0f / frameRateTime.asSeconds(); //printf("frameRate: %f \n", framerate ); //frameRateClock.restart().asSeconds(); #pragma endregion window.clear(); if (view->isGamePlayChosen()) { if (!(gameLogic->getGameOver())) { view->drawBackground(); #pragma region trigger automaticDrop sf::Time elapsed = clock.getElapsedTime(); if (!pause) { if (elapsed.asSeconds() >= 0.1f) { if (gameLogic->getGeneticAlgorithmComputing()) { clock.restart(); // returns true, if tetromino has landed. this also triggers spawning new tetrominos within gamelogic.cpp if (gameLogic->dropAI()) { // once the tetromino has landed, starting computation for next tetromino ai->makeDecision(false); } } else if (gameLogic->getFinishedAIPlays()) { clock.restart(); // returns true, if tetromino has landed. this also triggers spawning new tetrominos within gamelogic.cpp if (gameLogic->automaticDrop()) { // once the tetromino has landed, starting computation for next tetromino ai->makeDecision(false); } } } if (elapsed.asSeconds() >= 1.f - 0.05f * gameLogic->getLevel()) { if (gameLogic->getSinglePlayer()) { clock.restart(); gameLogic->automaticDrop(); } } } else if (pause) { //drawPauseScreen(); } #pragma endregion view->drawCurrentTetromino(); view->drawNextTetro(); view->drawLandedArray(); view->drawInterface(); } else view->drawGameOver(); } else view->drawGamePlaySelection(); window.display(); } /*delete ai; delete gameLogic; delete view;*/ return 0; }
void CBarrierGame::StartGame() { GameLogic gameLogic; gameLogic.InitMap(m_graph); m_test.output(m_graph); }
int main(int argc, char* args[]) { //Start up SDL and create window int screenmode = 0; if (argc > 1) { screenmode = std::stoi(args[1]); } if (!init(screenmode)) { printf("Failed to initialize!\n"); } else { Menu menu = Menu(); //Load media if (!menu.init()) { printf("Failed to load media!\n"); } else { //Main loop flag bool quit = false; int state = STATE_MENU; int mode = M_SINGLEPLAYER; rngGfx.seed(time(NULL)); glLoadIdentity(); glTranslated(-1, 1, 0); glScaled(2.0 / SCREEN_WIDTH, -2.0 / SCREEN_HEIGHT, 1.0); const Uint8 *keyboard = SDL_GetKeyboardState(NULL); //Event handler SDL_Event e; frame = 0; unsigned int endTime = SDL_GetTicks(); GameLogic g; //While application is running while (!quit) { endTime = endTime + 17; frame++; if (frame % 3 == 0) endTime--; //Handle events on queue SDL_Scancode keydown = SDL_SCANCODE_UNKNOWN; while (SDL_PollEvent(&e) != 0) { //User requests quit if (e.type == SDL_QUIT) { quit = true; } if (e.type == SDL_MOUSEMOTION) { //Get the mouse offsets mouseX = e.motion.x; mouseY = e.motion.y; if (screenmode == 1 || screenmode == 3) { mouseX /= 1.5; mouseY /= 1.5; } } if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) { mouseX = e.button.x; mouseY = e.button.y; if (e.button.button == SDL_BUTTON_LEFT) clicked = (e.button.state == SDL_PRESSED); if (screenmode == 1 || screenmode == 3) { mouseX /= 1.5; mouseY /= 1.5; } } if (e.type == SDL_KEYDOWN) { keydown = e.key.keysym.scancode; } } if (keyboard[SDL_SCANCODE_ESCAPE]) { quit = true; } //Clear screen glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); switch (state) { case STATE_MENU: g = GameLogic(); g.write_other_players = SDL_CreateMutex(); mode = menu.step( mouseX, mouseY, clicked, keyboard, keydown);//returns mode if changed, 0 if no change if (mode != 0) { std::cout << mode; state = (mode == M_SERVER) ? STATE_WAIT_FOR_CLIENT : STATE_BEGINGAME; } menu.draw( mouseX, mouseY); break; case STATE_WAIT_FOR_CLIENT: uint32_t rng_seed; IPaddress ip; glColor3f(1.0f, .4f, .9f); menu.draw_text(80, 300, "Waiting for client"); menu.draw_text(180, 400, "to connect"); if (server_begin(&rng_seed, &ip)) { state = STATE_BEGINGAME; g.addOtherPlayer(100, 100, 0, ip); rngGame.seed(rng_seed); SDL_CreateThread(receive_packets, "Network", &g); } break; case STATE_BEGINGAME: if (mode == M_CLIENT) { //Client uint32_t rng_seed = time(NULL); g.addOtherPlayer(100, 100, 0, client_begin(menu.tmpip, rng_seed)); rngGame.seed(rng_seed); SDL_CreateThread(receive_packets, "Network", &g); } if (mode == M_SINGLEPLAYER) { rngGame.seed(time(NULL)); } g.rings.init(); state = STATE_GAMEPLAY;//don't break, continue directly to gameplay case STATE_GAMEPLAY: g.step(keyboard); g.draw(); if (g.rings.thisRing >= 20) { g.haveWon = g.haveWon || ((g.multiplayer && g.opponent_rings < 20) || !g.multiplayer); glColor3f(1.0f, .4f, .9f); if (g.haveWon) menu.draw_text(10, 10, "You won"); else menu.draw_text(10, 10, "You lost"); menu.draw_text(10, 110, "Time taken:"); menu.draw_text(10 + chrw * 11, 110, const_cast<char *>((std::to_string(g.timeFlying)).c_str())); menu.draw_text(10, 200, "Press Enter to return"); menu.draw_text(10, 300, "to menu"); if (keydown == SDL_SCANCODE_RETURN) { state = STATE_MENU; if (mode == M_CLIENT || mode == M_SERVER) server_stop(); } } else { glColor3f(0.0f, 1.0f, 0.0f); if (g.rings.thisRing >= 10) menu.draw_text(10, 10, const_cast<char *>((std::to_string(g.rings.thisRing)).c_str())); else menu.draw_text(10 + chrw, 10, const_cast<char *>((std::to_string(g.rings.thisRing)).c_str())); menu.draw_text(10 + 2 * chrw, 10, "/20"); glColor3f(1.0f, 0.0f, 0.0f); if (g.multiplayer) { if (g.opponent_rings >= 10) menu.draw_text(10, 30, const_cast<char *>((std::to_string(g.opponent_rings)).c_str())); else menu.draw_text(10 + chrw, 30, const_cast<char *>((std::to_string(g.opponent_rings)).c_str())); menu.draw_text(10 + 2 * chrw, 30, "/20"); } } break; } menu.drawGrid(); //Update screen SDL_GL_SwapWindow(gWindow); if (SDL_GetTicks() < endTime) { //Sleep the remaining frame time SDL_Delay(endTime-SDL_GetTicks()); } else { //std::cout << SDL_GetTicks()-endTime << "\n"; } } } } //Free resources and close SDL close(); return 0; }
void BeginSimulation(void) { char str[32]; int brokenTime, FixedTime, TechCount, RunTime; float DiscardDataTime; HWND progressbar = GetDlgItem(HDialog,IDC_PROGRESS1); //Accept User Input GetDlgItemText(HDialog, IDC_EDIT1, str, 32); brokenTime = atoi(str); if(brokenTime == NULL) brokenTime = 8; GetDlgItemText(HDialog, IDC_EDIT2, str, 32); FixedTime = atoi(str); if(FixedTime == NULL) FixedTime = 30; GetDlgItemText(HDialog, IDC_EDIT4, str, 32); TechCount = atoi(str); if(TechCount == NULL) TechCount = 10; GetDlgItemText(HDialog, IDC_EDIT5, str, 32); RunTime = atoi(str); if(RunTime == NULL || RunTime <= 2) { RunTime = 2; SetDlgItemText(HDialog, IDC_EDIT5,(LPCSTR)"2"); } DiscardDataTime = 1; DiscardDataTime *= 120000; bool exit = false; RECT rect; GameLogic GL; RECT rect2; int wdRect; GetClientRect(HOutput, &rect); wdRect = rect.right - rect.left; rect2.left = -wdRect / 2; rect2.right = wdRect / 2; wdRect = rect.bottom - rect.top; rect2.bottom = -(wdRect/2); rect2.top = -rect2.bottom; RAND->Set((float)time(NULL)); GL.Init(5,10,4,2,3,1,2); float temp = 0; SendMessage(progressbar,PBM_SETRANGE,0,MAKELPARAM(0,100)); SendMessage(progressbar,PBM_SETSTEP,(WPARAM) 1, 0); SendMessage(progressbar,PBM_SETBARCOLOR,(WPARAM) 0,MAKELPARAM(0,RGB(255,0,0))); //SendDlgItemMessage(progressbar,IDC_PROGRESS1,PBM_SETRANGE,0,100); //SendDlgItemMessage(progressbar,IDC_PROGRESS1,PBM_SETBARCOLOR,0,RGB(0,255,0)); SendDlgItemMessage(progressbar,IDC_PROGRESS1,PBM_SETSTATE,PBST_NORMAL,0); SendDlgItemMessage(progressbar,IDC_PROGRESS1,PBM_SETMARQUEE,1,0); //SendDlgItemMessage(progressbar,IDC_PROGRESS1,PBM_SETSTEP,10,0); //SendMessage(progressbar,PBM_STEPIT,0,0); MS->ResetClock(); int temp1; //11 hours a day 7 days a week for 30 days in minutes int MaxRunTime = 138600; float HasDiscarded = false; while(MS->GetClock() < 138600) { if(!HasDiscarded && MS->GetClock() >= DiscardDataTime) { //GD->Reset(); HasDiscarded = true; } if((int)temp < (int)((MS->GetClock() / 138600)*100.0f)) { SendMessage(progressbar,PBM_STEPIT,0,0); temp = (float)MS->GetClock() / 138600 * 100.0f; } GL.Update(.016f); GL.Draw(); temp1 = (int)MS->GetClock(); } //AM->Complete(); OM->Clear(); AM->Clear(); }