void GameApplication::gameLoop(){ loadStatsFile(); musicVolumeControl(); while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_BACKSPACE) { if (board.isStarted() && !endOfTheGame()) { undo(); } } } for (int i = 0; i < NUMBER_OF_BUTTON; ++i) { if (lockButtons) { temp[2] = button[2].handleEvent(e); temp[3] = button[3].handleEvent(e); } else { temp[i] = button[i].handleEvent(e); } } for (int i = 0; i < NUMBER_OF_TILE * 4; i++) { choice = tile[i].handleEvent(e, coordI, coordJ); if (choice == true) { mark++; if (!board.pile[coordI][coordJ].empty()) { takePair(); break; } else { choice = false; } } } } logicButtons(temp); logicBoard(); drowGame(board); } }
void GameApplication::logicBoard() { eraseTileMap(); unblockedBoard(); if (endOfTheGame() && board.isStarted()) { youWin = true; winGames++; allGames++; award(calculateAward(calculate())); saveStatsFile(allGames, winGames); board.stop(); timer.stop(); winAnimation.startAnimation(50, 50); } if (availableMatches() == 0 && board.isStarted()) { youLose = true; allGames++; saveStatsFile(allGames, winGames); board.stop(); timer.stop(); } }
/** @brief The game loop function * @param p_structCommon : Struct with all program informations */ void playGame(structProgramInfo* p_structCommon) { unsigned char l_cKey; char* l_sTopText; unsigned int l_iWatchdog; unsigned int l_iCursorX; unsigned int l_iCursorY; unsigned int l_iMovement; /* store the wanted move, if impossible this variable */ unsigned int l_iCurrentSocketIndex; /* allow the program to go back */ int l_iNewCoordinates; char l_bForceRedraw; l_bForceRedraw = TRUE; l_cKey = 0; l_iNewCoordinates = -1; l_iMovement = 0; l_iCursorX = 1; l_iCursorY = 1; p_structCommon->iLastXUsed = l_iCursorX; p_structCommon->iLastYUsed = l_iCursorY; l_iCurrentSocketIndex = 0; p_structCommon->iOffsetX = (p_structCommon->iCol / 2) - (p_structCommon->iSizeX / 2); p_structCommon->iOffsetY = (p_structCommon->iRow / 2) - (p_structCommon->iSizeY / 2); l_sTopText = (char*)malloc((p_structCommon->iCol + 1) * sizeof(char)); if(l_sTopText == NULL) exit(-ENOMEM); p_structCommon->iCurrentUserColor = enumRouge; /* Main user, or server always red. If multiplayer and client, it receive another color suring connection */ p_structCommon->cUserMove = 0; /* Init the game, screen stuff etc... */ gameInit(p_structCommon); do { snprintf( l_sTopText, p_structCommon->iCol, "Your turn to play [ ] | Connected [ ] | Current port [%d] | Nickname %s", p_structCommon->iTcpPort, p_structCommon->sUserName); if(p_structCommon->bMyTurnToPlay == TRUE) { l_sTopText[19] = 'X'; } else { l_sTopText[19] = ' '; } if(p_structCommon->bMutexInitialized == TRUE) { l_sTopText[35] = 'X'; } else { l_sTopText[35] = ' '; } if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} usleep(TIME_BETWEEN_TWO_REQUEST); initBar(); topText(l_sTopText); /* Display wursor each time */ displayCursor(l_iCursorX, l_iCursorY, p_structCommon->iOffsetX, p_structCommon->iOffsetY, FALSE, p_structCommon->cGrid); pointCounting(p_structCommon->cGrid, p_structCommon->iPoints, p_structCommon->iSizeX, p_structCommon->iSizeY); displayRanking(p_structCommon->iPoints, p_structCommon->iCol, p_structCommon->iRow); topText(l_sTopText); refresh(); usleep(TIME_BETWEEN_TWO_REQUEST); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} l_cKey = getch(); /* In order to allow an alone player to play alone */ if(p_structCommon->bMutexInitialized == FALSE) { p_structCommon->bMyTurnToPlay = TRUE; } switch(l_cKey) { case 'D': { /* LEFT */ l_iCursorX = (l_iCursorX < 1) ? p_structCommon->iSizeX - 1 : l_iCursorX - 1; p_structCommon->cUserMove = 'd'; l_iMovement = DIRECTION_LEFT; break; } case 'C': { /* RIGHT */ l_iCursorX = (l_iCursorX > p_structCommon->iSizeX - 2) ? 0 : l_iCursorX + 1; p_structCommon->cUserMove = 'c'; l_iMovement = DIRECTION_RIGHT; break; } case 'A': { /* UP */ l_iCursorY = (l_iCursorY < 1) ? p_structCommon->iSizeY - 1 : l_iCursorY - 1; p_structCommon->cUserMove = 'a'; l_iMovement = DIRECTION_UP; break; } case 'B': { /* DOWN */ l_iCursorY = (l_iCursorY > p_structCommon->iSizeY - 2) ? 0 : l_iCursorY + 1; p_structCommon->cUserMove = 'b'; l_iMovement = DIRECTION_DOWN; break; } case ':': { /* Command mode */ l_bForceRedraw = TRUE; logBar(p_structCommon, CLEAN_L2, ""); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} logBar(p_structCommon, DISPLAY, ""); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} /* Get command from the user, the command set by the user will be saved in p_structCommon->sUserCommand */ userCommandGetter(p_structCommon); /* Analyse user command */ l_iNewCoordinates = userCommandExecute(p_structCommon); if(l_iNewCoordinates > - 1) { if(l_iNewCoordinates > - 1 && l_iNewCoordinates < (signed)p_structCommon->iSizeX) { /* belongs to the interval [0 ; p_structCommon->iSizeX[ ==> redefine X coordinate */ l_iCursorX = l_iNewCoordinates; l_bForceRedraw = FALSE; } else if(l_iNewCoordinates >= (signed)p_structCommon->iSizeX && l_iNewCoordinates < (signed)p_structCommon->iSizeX + (signed)p_structCommon->iSizeY) { /* belongs to the interval [p_structCommon->iSizeX ; p_structCommon->iSizeX + p_structCommon->iSizeY[ ==> redefine Y coordinate */ l_iCursorY = l_iNewCoordinates - (p_structCommon->iSizeX); l_bForceRedraw = FALSE; } } if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} displayCursor(l_iCursorX, l_iCursorY, p_structCommon->iOffsetX, p_structCommon->iOffsetY, l_bForceRedraw, p_structCommon->cGrid); refresh(); /* Clean the screen */ logBar(p_structCommon, DISPLAY, ""); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} break; } case 'q': case 'Q': { p_structCommon->bNetworkDisconnectionRequiered = TRUE; /* Started threads have to down this flag -- bMutexInitialized means we have at least one thread started */ while(p_structCommon->bNetworkDisconnectionRequiered == TRUE && p_structCommon->bMutexInitialized == TRUE) { usleep(TIME_BETWEEN_TWO_REQUEST); for(l_iCurrentSocketIndex = 0; l_iCurrentSocketIndex < MAX_CONNECTED_CLIENTS ; l_iCurrentSocketIndex++) { if(p_structCommon->iClientsSockets[l_iCurrentSocketIndex] != 0) { break; } } if(l_iCurrentSocketIndex >= MAX_CONNECTED_CLIENTS - 1) { break; } } break; /* Normally, network.c have clean the mutex at the end of all connexions */ if(p_structCommon->bMutexInitialized == TRUE) { pthread_mutex_destroy(p_structCommon->pthreadMutex); p_structCommon->bMutexInitialized = FALSE; } } case ' ': { if(p_structCommon->bMyTurnToPlay != TRUE) { break; } /* When the user drop a rock */ p_structCommon->cUserMove = 'r'; p_structCommon->iLastXUsed = l_iCursorX; p_structCommon->iLastYUsed = l_iCursorY; /* Put the color information in the matrix */ p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] = p_structCommon->iCurrentUserColor; /* Put the text information in the matrix */ p_structCommon->cGrid[TEXT_MATRIX][l_iCursorY][l_iCursorX] = ' '; /* declare this point to be synchronized with all clients */ p_structCommon->cGrid[SYNC_MATRIX][l_iCursorY][l_iCursorX] = POINT_TO_SYNC; /* Draw the block of the current user (the other blocks are draw by another function) */ if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} drawElement(l_iCursorX + p_structCommon->iOffsetX, l_iCursorY + p_structCommon->iOffsetY, p_structCommon->cGrid[TEXT_MATRIX][l_iCursorY][l_iCursorX], p_structCommon->iCurrentUserColor); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} /* Check neighborhood - If there is two contigous blocks of the player's color that means there is maybee a loop */ loopCompletion(l_iCursorX, l_iCursorY, p_structCommon->iCurrentUserColor, p_structCommon); /* Reset this player turn */ p_structCommon->bMyTurnToPlay = FALSE; break; } default: { /* Else, do nothing */ break; } } /* Check code, in order to forbid access to already reserved boxes */ if((unsigned int)p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] != enumNoir) { /* So there is something here and it is not me */ switch(l_iMovement) { case DIRECTION_UP: l_iCursorY++; break; case DIRECTION_DOWN: l_iCursorY--; break; case DIRECTION_LEFT: l_iCursorX++; break; case DIRECTION_RIGHT: l_iCursorX--; break; default: /* Who, error ! */ perror("Unknown code"); break; } /* If we go out of the screen by the upper or wester side put it in 0:0 and let the next block check if this block is empty or not */ if(l_iCursorY >= p_structCommon->iSizeY || l_iCursorX >= p_structCommon->iSizeX) { l_iCursorX = 0; l_iCursorY = 0; } /* If with bad luck you go over the grid */ l_iWatchdog = 0; while((unsigned int)p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] != enumNoir) { /* Find random coordinate with the simplest way */ l_iCursorX = rand() % p_structCommon->iSizeX; l_iCursorY = rand() % p_structCommon->iSizeY; l_iWatchdog++; if(l_iWatchdog > (p_structCommon->iSizeX * p_structCommon->iSizeY) / 2) { if(isTheGridFull(p_structCommon) == TRUE) { endOfTheGame(p_structCommon); return; } } } } }while((l_cKey != 'q') && (l_cKey != 'Q')); /* until q/Q pressed */ free(l_sTopText); }
int main(int argc, char* args[]) { bool temp[NUMBER_OF_BUTTON] = { false }; bool quit = false; SDL_Event e; if (!init()) { printf("Failed to initialize!\n"); } else { if (!loadMedia()) { printf("Failed to load media!\n"); } else { loadStatsFile(); musicVolumeControl(); while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } if(e.type == SDL_KEYDOWN) { if(e.key.keysym.sym == SDLK_BACKSPACE) { if(board.isStarted() && !endOfTheGame()) { undo(); } } } for (int i = 0; i < NUMBER_OF_BUTTON; ++i) { if(lockButtons) { temp[2] = gButton[2].handleEvent(e); temp[3] = gButton[3].handleEvent(e); } else { temp[i] = gButton[i].handleEvent(e); } } for (int i = 0; i < NUMBER_OF_TILE * 4; i++) { choice = gTile[i].handleEvent(e, coordI, coordJ); if (choice == true) { mark++; if (!board.pile[coordI][coordJ].empty()) { takePair(); break; } else { choice = false; } } } } logicButtons(temp); logicBoard(); drowGame(board); } } } close(); return 0; }