// TrollTron.cpp : définit le point d'entrée pour l'application console. #include #include #include #include "MainWindow.h" #include "KeyboardController.h" #include "AIController.h" int main(int argc, char **argv) { srand((unsigned) time(NULL)); // This will define the grid Game game(100, 100); // init GLUT and create window MainWindow& mainWindow = MainWindow::getInstance(); mainWindow.init(&argc, argv); const KeyCode keys[] = {KEY_LEFT, KEY_RIGHT, KEY_A, KEY_E, KEY_I, KEY_P, KEY_C, KEY_B}; const PlayerNumber maxNumHumanPlayers = 4; PlayerNumber currNumHumanPlayers = 0; PlayerNumber currNumPlayers = 0; Player *pPlayer; /* * Usage (regex style) : trolltron(.exe)? (player|easy|medium|hard)+ (gameWidth game height)? (windowWidht windowHeight)? * 4 human players max. 1 player min (human or not) * if undefined, gameWidth gameHeight = 100 100, windowWidth windowHeight = 320 320 */ // If this bool goes to true, then the parameters are messed up //We don't start the game and display the usage bool argFail = false; // The higher the easier int botInhibitDelay; int botRandomTurn; // Start at 1 because 0 is the path/name int argNum=1; // We need this to see if we have either 0, 2 or 4 numbers passed and where to store them char pairOfValuesDefined = 0; // Default values uint gameWidth = 100; uint gameHeight = 100; int windowWidth = 320; int windowHeight = 320; // Go through all parameters while(argNum < argc) { // If it's not a number if (atoi(argv[argNum]) == 0) { // If we started to inputing numbers, we shouldn't input players anymore if (pairOfValuesDefined > 0) { argFail = true; } // If it's "player" if (! strcmp(argv[argNum], "player")) { if(currNumHumanPlayers < maxNumHumanPlayers) { // Add him pPlayer = game.addPlayer(); // And create its controller pPlayer->setController(mainWindow.generateNewKeyboardController(pPlayer, keys[currNumHumanPlayers*2], keys[currNumHumanPlayers*2+1])); // Count 1 more (human) player currNumPlayers ++; currNumHumanPlayers ++; } } else { bool isABot = false; // If it's "easy" if(! strcmp(argv[argNum], "easy")) { isABot = true; botInhibitDelay = 10; botRandomTurn = 10; } else if(! strcmp(argv[argNum], "medium")) { isABot = true; botInhibitDelay = 3; botRandomTurn = 6; } else if(! strcmp(argv[argNum], "hard")) { isABot = true; botInhibitDelay = 1; botRandomTurn = 3; } else argFail = true; if(isABot) { // Count 1 more (non human) player currNumPlayers ++; pPlayer = game.addPlayer(); // And instanciate its AI pPlayer->setController(new AIController(pPlayer, game.getField(), botInhibitDelay, botRandomTurn)); } } } // Else if it IS a number else { // Numbers are only valid as pairs if (atoi(argv[argNum]) > 0 && argNum < argc -1 && atoi(argv[argNum+1]) > 0) { // Depending on if it's... switch(pairOfValuesDefined) { //The first pair : game dimensions case 0: pairOfValuesDefined ++; gameWidth = atoi(argv[argNum]); gameHeight = atoi(argv[argNum+1]); // It's a pair so we take 2 args at once argNum ++; break; //The 2nd pair : window dimensions case 1: pairOfValuesDefined ++; windowWidth = atoi(argv[argNum]); windowHeight = atoi(argv[argNum+1]); argNum ++; break; // Another pair : user mistake default: argFail = true; } } // If we input an odd number of numbers else { argFail = true; } } argNum++; if(argFail) { // Stop looping, we lost already break; } } // If too many or too few players; if(currNumHumanPlayers > maxNumHumanPlayers || currNumPlayers < 1) { argFail = true; } if(argFail) { std::cout << "Usage (regex style) : trolltron(.exe)? (player|easy|medium|hard)+ (gameWidth gameHeight)? (windowWidht windowHeight)?" << std::endl; std::cout << "4 human players max. 1 player min (human or not)" << std::endl; std::cout << "if undefined, gameWidth gameHeight = 100 100, windowWidth windowHeight = 320 320" << std::endl; exit(1); } // This will define the size and position, in pixel, of the window, originally. It can be moved and resized. mainWindow.create("Troll Tron", 100, 100, windowWidth, windowWidth); game.setSize(gameWidth, gameHeight); mainWindow.setField(&(game.getField())); mainWindow.setGame(&game); game.start(); mainWindow.startLoop(); return 0; }