void GuiManager::launchGameRunner(const char *runnerName) { loadStages(); loadShips(); char **stageNames = newMatchDialog_->getStageNames(); char **shipNames = newMatchDialog_->getShipNames(); GuiPrintHandler *printHandler = new GuiPrintHandler(0, runnerConsole_, menuBarMaker_); gameRunner_ = new GuiGameRunner(printHandler, stageNames, numStages_, shipNames, numShips_, zipper_, resourcePath().c_str(), new RunnerFormRedrawer(this)); runnerDialog_->Hide(); nextWindow_ = 0; runnerRunning_ = true; runnerConsole_->clear(); runnerConsole_->Show(); runnerConsole_->Raise(); gameRunner_->run(runnerName); runnerRunning_ = false; GuiGameRunner *oldRunner = gameRunner_; gameRunner_ = 0; delete oldRunner; delete printHandler; for (int x = 0; x < numStages_; x++) { delete stageNames[x]; } delete stageNames; for (int x = 0; x < numShips_; x++) { delete shipNames[x]; } delete shipNames; switch (nextWindow_) { case NEXT_NEW_MATCH: showNewMatchDialog(); break; case NEXT_PACKAGE_SHIP: showPackageShipDialog(); break; case NEXT_PACKAGE_STAGE: showPackageStageDialog(); break; case NEXT_GAME_RUNNER: showGameRunnerDialog(); break; case NEXT_RESUME_MATCH: resumeMatch(); break; default: break; } }
void initGame(char board[], char mask[]) { int k; char carrier[] = "[CCCCC=>", destroyer[] = "[DDD=>", sub[] = "[SS=>", frigate[] = "[F>"; for (k = 0; k<SIZE; k++) { board[k] = '~'; mask[k] = 0; } srand(time(NULL)); loadShips(mask, carrier); loadShips(mask, destroyer); loadShips(mask, sub); loadShips(mask, frigate); }
/******************************************************************************* * function playBattleship (void) * * This is the main function for the game, responsible for managing all * other functions necessary for the game to work */ void playBattleship ( int numMissiles ) { srand( time( NULL ) ); // Randomizing radomic randomizer char board [391]; char mask [391]; char previousMove[1024] = "-"; // Previous move (miss/hit on [coord]) char nextMove[3]; // Coordinates typed by the user int nextMoveCoordinates; // Numerical coordinates int validCoordinates = 0; // Controls if the coordinate is valid or not int missilesFired = 0; int missilesRemaining = numMissiles; int score = 0; // Filling the board with water and loading the ships // You can customize this with ships of different designs initGame ( board, mask ); loadShips( board, "XXXXXXX" ); loadShips( board, "XXXXXX" ); loadShips( board, "XXXXX" ); loadShips( board, "XXXX" ); int c = 0; // Printing the board again for every move while the player still has // missiles and hasn't destroyed all the ships do { for ( c = 0; c < 80; c++ ) printf("\n"); // Clearing the screen // Drawing the board //drawGame (board, missilesFired, missilesRemaining, score, previousMove); // For debug only drawGame (mask, missilesFired, missilesRemaining, score, previousMove); // Asking for valid coordinates printf("\nEnter the target coordinates : "); do { validCoordinates = 0; scanf( "%s", nextMove ); // Replacing non-valid coordinates with "?" if ( (nextMove[0] < '0' || nextMove[0] > '9') && (nextMove[0] < 'A' || nextMove[0] > 'F') && (nextMove[0] < 'a' || nextMove[0] > 'f') ) nextMove[0] = '?'; if ( (nextMove[1] < 'A' || nextMove[1] > 'Z') && (nextMove[1] < 'a' || nextMove[1] > 'z') ) nextMove[1] = '?'; // Checking if the coordinates are valid if ( (nextMoveCoordinates = checkMove (nextMove)) == -1 ) { printf("Sorry... '%c%c' is not a valid coordinate... Try again: ", nextMove[0], nextMove[1]); } else validCoordinates = 1; while ( getchar() != '\n' ); // Cleaning the buffer } while ( validCoordinates == 0 ); // Coordinates received, updating the board updateData (board, mask, &missilesFired, &missilesRemaining, &score, previousMove, nextMove); } while ( missilesRemaining > 0 && score < 110 ); for ( c = 0; c < 80; c++ ) printf("\n"); // Clearing the screen if ( score < 110 ) { drawGame (board, missilesFired, missilesRemaining, score, previousMove); printf("\n\n+---------------< Sorry, you lost the war ... >---------------+\n\n"); } else { drawGame (mask, missilesFired, missilesRemaining, score, previousMove); printf("\n\n+----------------< HOORAY !! >----------------+\n"); printf("+-----------< You sunk all the ships, YOU WON ! >-----------+\n\n"); } }
GuiManager::GuiManager(GuiListener *listener) { listener_ = listener; newMatchDialog_ = 0; srand((unsigned int) time(NULL)); reloadBaseDirs(); window_ = 0; previewWindow_ = 0; zipper_ = new GuiZipper(); fileManager_ = new FileManager(zipper_); gameRunner_ = 0; menuBarMaker_ = new MenuBarMaker(); packagingConsole_ = new OutputConsole("Packaging Details", CONSOLE_PLAIN, menuBarMaker_); errorConsole_ = new OutputConsole("Error Console", CONSOLE_PLAIN, menuBarMaker_); runnerConsole_ = new OutputConsole("Game Runner Console", CONSOLE_RUNNER, menuBarMaker_); runnerConsoleListener_ = new RunnerConsoleListener(this); runnerConsole_->setListener(runnerConsoleListener_); packagingConsole_->SetPosition(wxPoint(150, 100)); newMatchListener_ = new MatchStarter(this); newMatchDialog_ = new NewMatchDialog(newMatchListener_, menuBarMaker_); resultsDialog_ = 0; shipPackager_ = new ShipPackager(this, fileManager_, packagingConsole_); packageShipDialog_ = new PackageShipDialog(shipPackager_, menuBarMaker_); stagePackager_ = new StagePackager(this, fileManager_, packagingConsole_); packageStageDialog_ = new PackageStageDialog(stagePackager_, menuBarMaker_); runnerLauncher_ = new RunnerLauncher(this, fileManager_, runnerConsole_); runnerDialog_ = new RunnerDialog(runnerLauncher_, menuBarMaker_); loadStages(); loadShips(); loadRunners(); guiPrintHandler_ = 0; stageConsole_ = 0; teamConsoles_ = 0; stagePreview_ = new StagePreview(menuBarMaker_); stagePreview_->setListener(new PreviewInputListener(this)); gfxManager_ = new GfxManager(resourcePath(), true); viewListener_ = new ViewListener(this); gfxManager_->setListener(viewListener_); packageReporter_ = new PackageReporter(packagingConsole_); fileManager_->setListener(packageReporter_); newMatchDialog_->Show(); newMatchDialog_->SetFocus(); engine_ = 0; gfxHandler_ = 0; currentStagePath_ = 0; currentTeamPaths_ = 0; currentNumTeams_ = 0; interrupted_ = false; paused_ = false; restarting_ = false; quitting_ = false; showedResults_ = false; runnerRunning_ = false; nextWindow_ = 0; tpsFactor_ = 0.5; nextDrawTime_ = 1; numStages_ = numShips_ = numRunners_ = 0; #ifdef __WINDOWS__ windowIcon_.loadFromFile(resourcePath() + BBICON_32); #elif defined(__WXGTK__) windowIcon_.loadFromFile(resourcePath() + BBICON_128); #endif }