//------------------------- CreatePathBFS -------------------------------- // // uses BFS to find a path between the start and target cells. // Stores the path as a series of node indexes in m_Path. //------------------------------------------------------------------------ void BattleMap::createPathBFS() { //set current algorithm m_CurrentAlgorithm = AT_BFS; //clear any existing path m_Path.clear(); m_SubTree.clear(); //create and start a timer PrecisionTimer timer; timer.Start(); //do the search Graph_SearchBFS<NavGraph> BFS(*m_pGraph, m_iSourceCell, m_iTargetCell); //record the time taken m_dTimeTaken = timer.TimeElapsed(); //now grab the path (if one has been found) if (BFS.Found()) { m_Path = BFS.GetPathToTarget(); } m_SubTree = BFS.GetSearchTree(); m_dCostToTarget = 0.0; }
//--------------------------- CreatePathAStar --------------------------- //------------------------------------------------------------------------ void Pathfinder::CreatePathAStar() { //set current algorithm m_CurrentAlgorithm = search_astar; //create and start a timer PrecisionTimer timer; timer.Start(); //create a couple of typedefs so the code will sit comfortably on the page typedef Graph_SearchAStar<NavGraph, Heuristic_Euclid> AStarSearch; //create an instance of the A* search using the Euclidean heuristic AStarSearch AStar(*m_pGraph, m_iSourceCell, m_iTargetCell); //record the time taken m_dTimeTaken = timer.TimeElapsed(); m_Path = AStar.GetPathToTarget(); m_SubTree = AStar.GetSPT(); m_dCostToTarget = AStar.GetCostToTarget(); }
//-------------------------- CreatePathDijkstra -------------------------- // // creates a path from m_iSourceCell to m_iTargetCell using Dijkstra's algorithm //------------------------------------------------------------------------ void BattleMap::createPathDijkstra() { //set current algorithm m_CurrentAlgorithm = AT_DIJKSTRA; //create and start a timer PrecisionTimer timer; timer.Start(); Graph_SearchDijkstra<NavGraph> djk(*m_pGraph, m_iSourceCell, m_iTargetCell); //record the time taken m_dTimeTaken = timer.TimeElapsed(); m_Path = djk.GetPathToTarget(); m_SubTree = djk.GetSPT(); m_dCostToTarget = djk.GetCostToTarget(); }
//-------------------------------- WinMain ------------------------------- // // The entry point of the windows program //------------------------------------------------------------------------ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) { //handle to our window HWND hWnd; //our window class structure WNDCLASSEX winclass; // first fill in the window class stucture winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hInstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = NULL; winclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1); winclass.lpszClassName = g_szWindowClassName; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //register the window class if (!RegisterClassEx(&winclass)) { MessageBox(NULL, "Registration Failed!", "Error", 0); //exit the application return 0; } //create the window and assign its ID to hwnd hWnd = CreateWindowEx (NULL, // extended style g_szWindowClassName, // window class name g_szApplicationName, // window caption WS_OVERLAPPED | WS_VISIBLE | WS_CAPTION | WS_SYSMENU, GetSystemMetrics(SM_CXSCREEN)/2 - constWindowWidth/2, GetSystemMetrics(SM_CYSCREEN)/2 - constWindowHeight/2, constWindowWidth, // initial x size constWindowHeight, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters //make sure the window creation has gone OK if(!hWnd) { MessageBox(NULL, "CreateWindowEx Failed!", "Error!", 0); } //make the window visible ShowWindow (hWnd, iCmdShow); UpdateWindow (hWnd); // Enter the message loop bool bDone = false; //create a timer PrecisionTimer timer; timer.SmoothUpdatesOn(); //start the timer timer.Start(); MSG msg; while(!bDone) { while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if( msg.message == WM_QUIT) { //stop loop if it's a quit message bDone = true; } else { TranslateMessage( &msg ); DispatchMessage( &msg ); } } if (msg.message != WM_QUIT ) { //update g_GameWorld->Update(timer.TimeElapsed()); //render RedrawWindow(hWnd, false); Sleep(2); } }//end while delete g_GameWorld; UnregisterClass( g_szWindowClassName, winclass.hInstance ); return msg.wParam; }
//-------------------------------- WinMain ------------------------------- // // The entry point of the windows program //------------------------------------------------------------------------ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) { //handle to our window HWND hWnd; //our window class structure WNDCLASSEX winclass; // first fill in the window class stucture winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hInstance; winclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = NULL; winclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1); winclass.lpszClassName = g_szWindowClassName; winclass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); //register the window class if (!RegisterClassEx(&winclass)) { MessageBox(NULL, "Registration Failed!", "Error", 0); //exit the application return 0; } //create the window and assign its ID to hwnd hWnd = CreateWindowEx (NULL, // extended style g_szWindowClassName, // window class name g_szApplicationName, // window caption WS_OVERLAPPED | WS_VISIBLE | WS_CAPTION | WS_SYSMENU, GetSystemMetrics(SM_CXSCREEN)/2 - WindowWidth/2, GetSystemMetrics(SM_CYSCREEN)/2 - WindowHeight/2, WindowWidth, // initial x size WindowHeight, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters //make sure the window creation has gone OK if(!hWnd) { MessageBox(NULL, "CreateWindowEx Failed!", "Error!", 0); } //start the timer timer.Start(); MSG msg; //enter the message loop bool bDone = false; while(!bDone) { while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if( msg.message == WM_QUIT ) { // Stop loop if it's a quit message bDone = true; } else { TranslateMessage( &msg ); DispatchMessage( &msg ); } } if (timer.ReadyForNextFrame() && msg.message != WM_QUIT) { //update game states g_SoccerPitch->Update(); //render RedrawWindow(hWnd, true); Sleep(2); } if (g_SoccerPitch->IsTimeUp(Prm.GameTime)) bDone = true; }//end while char* buff; buff = new char[150]; int blueScore = g_SoccerPitch->m_pRedGoal->NumGoalsScored(); int redScore = g_SoccerPitch->m_pBlueGoal->NumGoalsScored(); LONGLONG redTime = g_SoccerPitch->m_redTotalTime.QuadPart; LONGLONG blueTime = g_SoccerPitch->m_blueTotalTime.QuadPart; LARGE_INTEGER freq; freq.QuadPart = 0I64; QueryPerformanceFrequency (&freq); double redAdjScore = redScore; double blueAdjScore = blueScore; if (redTime > blueTime) { if (redScore >= blueScore) { blueAdjScore = adjustedScore (redTime, blueTime, blueScore); } } else if (blueTime > redTime) { if (blueScore >= redScore) { redAdjScore = adjustedScore (blueTime, redTime, redScore); } } double fRedTime = (redTime * 1.0) / freq.QuadPart; double fBlueTime = (blueTime * 1.0) / freq.QuadPart; sprintf(buff, "Game Over\nRed: %i (%2.3f) = %2.2f \nBlue: %i (%2.3f) = %2.2f", redScore, fRedTime, redAdjScore, blueScore, fBlueTime, blueAdjScore); //sprintf(buff, "Game Over\nRed: %i = %2.2f \nBlue: %i = %2.2f", // redScore, redAdjScore, blueScore, blueAdjScore); debug_con << buff << "\n"; MessageBox(NULL, buff, "Final Result", 0); delete g_SoccerPitch; UnregisterClass( g_szWindowClassName, winclass.hInstance ); return msg.wParam; }