void Tela::changeLevel() { if(changedLevel && user->getPontuacao() > 1000 && user->getPontuacao() < 3000) { //if(changedLevel && user->getPontuacao() > 100 && user->getPontuacao() < 150) { Para testes level = 2; fillMatriz(5); changedLevel = false; showLevelUp(); showGame(); } else if(!changedLevel && user->getPontuacao() >= 3000 && user->getPontuacao() < 6000) { //} else if(!changedLevel && user->getPontuacao() >= 150 && user->getPontuacao() < 200) { Para testes level = 3; fillMatriz(6); changedLevel = true; showLevelUp(); showGame(); } else if(changedLevel && user->getPontuacao() >= 6000 && user->getPontuacao() < 10000) { //} else if(changedLevel && user->getPontuacao() >= 250 && user->getPontuacao() < 300) { Para testes level = 4; fillMatriz(7); changedLevel = false; showLevelUp(); showGame(); } else if(!changedLevel && user->getPontuacao() >= 10000) { // esse é o zerar do jogo //} else if(!changedLevel && user->getPontuacao() >= 300) { Para testes /* SHOW GAME OVER */ level = 5; } }
/* gets a game where the next player is the AI. show loading Image until the computer makes it's move and then make the move on board PRECONDITON: DeleteWidgets(&(screen->widgetChildren)) was made throws : malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError() */ void GUImakeAITurn(){ move m; mainGame->isLoadingAIMove=1; showGame(); if(standart_fail){return;} m=get_AI_Move(mainGame->gameInfo); if(standart_fail){return;} mainGame->isCheckMate=applyMove(mainGame->gameInfo,&m); if(standart_fail){return;} DeleteWidgets(&screen->widgetChildren); mainGame->isLoadingAIMove=0; showGame(); }
/* * handle clicking on the board * throws calloc errors, malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), * SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError() */ void handleBoardSelection(Widget *widget, int xClicked, int yClicked) { int row = 0, col = 0; move moveToBeApplied={0}; if(mainGame->isCheckMate==2) return; yClicked-=20; xClicked-=20; row = BOARD_SIZE - ( yClicked / PIECE_SIZE ) - 1; col = ( xClicked / PIECE_SIZE ); if(mainGame->pieceWasClicked){ moveToBeApplied.row = mainGame->clickedPieceRow; moveToBeApplied.col = mainGame->clickedPieceCol; moveToBeApplied.newRow = row; moveToBeApplied.newCol = col; moveToBeApplied.promoteTo = mainGame->promoteTo; graphicMoveToCastle(&moveToBeApplied); if(isMoveLegal(mainGame->gameInfo,&moveToBeApplied)){ if(standart_fail) return; mainGame->isCheckMate=applyMove(mainGame->gameInfo, &moveToBeApplied); if(standart_fail) return; } mainGame->pieceWasClicked = 0; /* need to reset the flag saying if we clicked */ mainGame->promoteTo=0; /* reseting the option to promote */ DeleteWidgets(&(screen->widgetChildren)); /*PvsAI and AI Turn*/ if( (mainGame->isCheckMate!=2) && (isAITurn(mainGame->gameInfo)) ) GUImakeAITurn(); else showGame(); } else { /*we might be clicking on a piece for movement and its with our color (player color) */ if( occupier_color(row, col , mainGame->gameInfo->boardPointer) == mainGame->gameInfo->nextTurnPlayerColor ) { mainGame->pieceWasClicked = 1;/* set the flag saying "we clicked!" */ mainGame->clickedPieceCol = col;/* save coordinates of the click */ mainGame->clickedPieceRow = row; if(isPawnBeforePromotionMarked()){ if(standart_fail) return; mainGame->promoteTo=getQueenChar(mainGame->gameInfo->nextTurnPlayerColor); } /*color the appropriate square - do a label on the label place a button - we put the piece */ DeleteWidgets(&(screen->widgetChildren)); showGame(); } } }
int main(int argc, const char * argv[]){ sudokuGrid sample; readGame(sample); showGame(sample); printf("\n\n\n\n"); if(hasSolution(sample)){ showGame(sample); } getch(); return 0; }
/* * throws : malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), * SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError() */ void updatePromotionToKnight(Widget* widget, int optionNum, int y ) { int player=mainGame->gameInfo->nextTurnPlayerColor; DeleteWidgets(&(screen->widgetChildren)); mainGame->promoteTo=getKnightChar(player); showGame(); }
/*Recives a widget which is a button (namely a slot) and according to that buttton constructs the name of its co-relating file and saves the game's info in a file: chess(file number).xml throws : malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError() */ void SaveSlotClicked(Widget *widget, int x, int y) { int slotNumber = 0; char *fileName=getFileName(widget->source, &slotNumber); if(standart_fail) return; (*mainGame->saveGameFunc)(mainGame->gameInfo, fileName); free(fileName); file_slots[slotNumber-1].isFree = false; DeleteWidgets(&screen->widgetChildren); showGame(); }
/***************************************************************** * 函数名称:execGame * 描述说明:运行连连看游戏 * 输入参数:p_gameUi:游戏体指针,blockNum:当前关数 * 输出参数:1:重新开始当前关2:返回3:闯关成功4:超时5:死局 *****************************************************************/ int execGame(gameUi_t *p_gameUi, int blockNum) { int ret = 0; struct LcdDot touchXY; gameExeFlag = 1; restTime = 75; noConnectTime = 0; initGame(p_gameUi, blockNum); showGame(p_gameUi); while(1){ while(GetTouch(&touchXY) == 0); if(restTime <= 0){ gameExeFlag = 0; return 4; } if(noConnectTime > 3){ if(haveConnect(&p_gameUi->m_mat) == 0){ gameExeFlag = 0; return 5; }else{ noConnectTime = 0; } } ret = doGame(&p_gameUi->m_gameMng, &p_gameUi->m_mat, &touchXY); if(ret != 0){//按在矩阵内 if(ret == 2){//点击第二次 if(seccess(&p_gameUi->m_gameMng)){ gameExeFlag = 0; return 3; }else{ //gameDelay(1000); //showGame(p_gameUi); } } }else{//按在矩阵外 //showGame(p_gameUi); ret = getMenu(&p_gameUi->m_ui, &touchXY); if(ret > 0){ gameExeFlag = 0; return ret; } } } return 0; }
/* SHOWS THE GAME, IF COMPUTER STARTS FIRST IT WILL MAKE ITS TURN AND WILL BE SHOWN. THIS FUNCTION DEALS WITH AI STARTING FIRST AS WHITE throws : malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError() */ void handleChessGameStart(Widget* widget, int x, int y) { copyChessGame(mainGame->gameInfo,mainGame->startingGameSettings); /*save settings */ mainGame->pieceWasClicked = 0; mainGame->promoteTo=0; if((!canPlayerMove(mainGame->gameInfo))&&(!standart_fail)){ /*check for check/mate */ mainGame->isCheckMate=2; } else if((!standart_fail)&&(isKingThreatened(mainGame->gameInfo->boardPointer,mainGame->gameInfo->nextTurnPlayerColor))&&(!standart_fail)){ mainGame->isCheckMate=1; } else mainGame->isCheckMate=0; if(standart_fail) return; DeleteWidgets(&(screen->widgetChildren)); if( (mainGame->gameInfo->gameMode==2) && (mainGame->gameInfo->userColor!=mainGame->gameInfo->nextTurnPlayerColor) ) GUImakeAITurn(); else showGame(); }
extern void showMainMenu(MI0283QT9 lcd, MENUOBJECTS obj, MY_USART serial){ lcd.fillScreen(BACKGROUND); while(1){ char title[] = "RaceGame"; uint8_t lengthOfString = (strlen(title) * 26); uint8_t x = (240 - lengthOfString); lcd.drawText(x, 15, title, OBJECTCOLOR, BACKGROUND, 3); if(obj.drawButton(lcd, "Singleplayer", 20, 75, 200, 40)){ showGame(lcd, obj, serial); } if(obj.drawButton(lcd, "Settings", 20, 135, 200, 40)){ showSettingMenu(lcd,obj,serial); } if(obj.drawButton(lcd, "Highscore", 20, 195, 200, 40)){ showHighscoreMenu(lcd, obj, serial); } } }
bool Tela::playGame() { Ponto p1 = {-1, -1}, p2 = {-1, -1}, pDica = {-1, -1}; bool quit = false, execute = true; showGame(); if(testMove() == false) quit = true; float frame = 0; Uint32 start = SDL_GetTicks(); SDL_Rect rect0[20], rect1[20], rect2[20], rect3[20], rect4[20], rect5[20], rect6[20]; setrects(rect0, 60, 0); setrects(rect1, 62, 70); setrects(rect2, 58, 140); setrects(rect3, 58, 210); setrects(rect4, 60, 280); setrects(rect5, 64, 350); setrects(rect6, 64, 420); while( execute ) { changeLevel(); if( level == 5 ) { execute = false; return execute; } if( SDL_PollEvent( &event ) ) { Ponto tmp; //Botão esquerdo do mouse pressionado if( event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) { tmp.x = event.button.x; tmp.y = event.button.y; if((tmp.x >= 135 && tmp.x <= 250) && (tmp.y >= 480 && tmp.y <= 595)) { undoPlay(); SDL_Flip( screen ); } else if((tmp.x >= 45 && tmp.x <= 160) && (tmp.y >= 395 && tmp.y <= 505)){ pDica = getDica(); apply_surface(pDica.x, pDica.y, gems_dica, screen); SDL_UpdateRect(screen, matriz[pDica.x][pDica.y].celula.x, matriz[pDica.x][pDica.y].celula.y, matriz[pDica.x][pDica.y].celula.w, matriz[pDica.x][pDica.y].celula.h); } else if((tmp.x >= 30 && tmp.x <= 115) && (tmp.y >= 525 && tmp.y <= 605)){ if(showConfScreen()) { quit = true; return quit; } else { showGame(); } } else if((tmp.x >= 175 && tmp.x <= 265) && (tmp.y >= 380 && tmp.y <= 465)){ if(showInstrucoes()) { quit = true; return quit; } else { showGame(); } } else if((tmp.x >= MAT_INITIAL_POINT_X && tmp.x <= MAT_INITIAL_POINT_X + 480) && (tmp.y >= MAT_INITIAL_POINT_Y && tmp.y <= MAT_INITIAL_POINT_Y + 480)) { if(pDica.x >= 0) { apply_surface(pDica.x, pDica.y, gems, screen); SDL_UpdateRect(screen, matriz[pDica.x][pDica.y].celula.x, matriz[pDica.x][pDica.y].celula.y, matriz[pDica.x][pDica.y].celula.w, matriz[pDica.x][pDica.y].celula.h); } tmp.x = (tmp.x - MAT_INITIAL_POINT_X) / 60; tmp.y = (tmp.y - MAT_INITIAL_POINT_Y) / 60; if(p1.x < 0) { p1.x = tmp.y; p1.y = tmp.x; //Destaca a nova joia selecionada } else if(p2.x < 0) { if(saoAdjacentes(p1.x, p1.y, tmp.y, tmp.x)) { p2.x = tmp.y; p2.y = tmp.x; } else { //Ignora selecao //Tira o destaque da joia que tinha sido selecionada apply_surface(p1.x, p1.y, gems, screen); SDL_UpdateRect(screen, matriz[p1.x][p1.y].celula.x, matriz[p1.x][p1.y].celula.y, matriz[p1.x][p1.y].celula.w, matriz[p1.x][p1.y].celula.h); p1.x = tmp.y; p1.y = tmp.x; } } } //Se o usuario fechar a janela } else if( (event.type == SDL_QUIT) ) { //Encerra o programa quit = true; return quit; //Se o usuario teclar ESC } else if ( (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) ) { level = 5; /* Show Game Over */ } else if( (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE) ) { if(audio == true) { //Pausa a musica Mix_PauseMusic(); audio = false; } else { //Toca a musica novamente Mix_ResumeMusic(); audio = true; } } } SDL_Rect offset; offset.x = matriz[p1.x][p1.y].celula.x; offset.y = matriz[p1.x][p1.y].celula.y; switch(getElement(p1.x, p1.y)) { case 1: SDL_BlitSurface( gems, &rect0[static_cast<int>(frame)], screen, &offset ); break; case 2: SDL_BlitSurface( gems, &rect1[static_cast<int>(frame)], screen, &offset ); break; case 3: SDL_BlitSurface( gems, &rect2[static_cast<int>(frame)], screen, &offset ); break; case 4: SDL_BlitSurface( gems, &rect3[static_cast<int>(frame)], screen, &offset ); break; case 5: SDL_BlitSurface( gems, &rect4[static_cast<int>(frame)], screen, &offset ); break; case 6: SDL_BlitSurface( gems, &rect5[static_cast<int>(frame)], screen, &offset ); break; case 7: SDL_BlitSurface( gems, &rect6[static_cast<int>(frame)], screen, &offset ); break; } SDL_UpdateRect(screen, matriz[p1.x][p1.y].celula.x, matriz[p1.x][p1.y].celula.y, matriz[p1.x][p1.y].celula.w, matriz[p1.x][p1.y].celula.h); //Velocidade de transição entre frames frame += 0.5; if(frame > 7) { frame = 0; } if(1000/FPS > SDL_GetTicks()-start) { SDL_Delay(1000/FPS-(SDL_GetTicks()-start)); } if((p1.x >= 0) && (p2.x >= 0)) { checkSwitch(p1.x, p1.y, p2.x, p2.y); contrastItem(p1.x, p1.y, matriz[p1.x][p1.y].elemento); apply_surface(p1.x, p1.y, gems, screen); SDL_UpdateRect(screen, matriz[p1.x][p1.y].celula.x, matriz[p1.x][p1.y].celula.y, matriz[p1.x][p1.y].celula.w, matriz[p1.x][p1.y].celula.h); p1.x = -1; p1.y = -1; p2.x = -1; p2.y = -1; if(testMove() == false) level = 5; } } return execute; }
int main(int argc, char *argv[]) { char **playersNameList; int totalPlayersNumber; int turn, i; char buffer[BUFF_SIZE]; FILE *configFile; /* legge gli argomenti */ char **name1, **name2; if (argc < 4) { fprintf(stderr, "ERROR: Wrong number of arguments. \n USAGE: %s\n", USAGE); exit(EXIT_FAILURE); } playersNameList = argv + 3; totalPlayersNumber = argc - 3; /* controlla se ci sono due giocatori con lo stesso nome */ for (name1 = playersNameList; *name1; name1++) for (name2 = name1 + 1; *name2; name2++) if (strcmp(*name1, *name2) == 0) { fprintf(stderr, "ERROR: found two player with the" "same name \"%s\"\n", *name1); exit(EXIT_FAILURE); } initIoInterface(argv[2]); /* crea e inizializza le strutture dati per i giocatori */ initPlayersManager(totalPlayersNumber); for (; *playersNameList; playersNameList++) addPlayer(*playersNameList); initBoard(); /* * legge il file di configurazione secondo il formato: * numero_casella:descrizione della prova\n * e aggiunge le descrizioni al tabellone */ if ((configFile = fopen(argv[1], "r")) == NULL) { printErr("ERROR: error while opening configuration file\n"); exit(EXIT_FAILURE); } while (fgets(buffer, BUFF_SIZE, configFile)) { char *description; int boxNumber; /* legge il numero di casella */ if ((boxNumber = atoi(buffer)) <= 0) { printErr("ERROR:invalid box num(\"%s\") in" " configuration file\n", buffer); exit(EXIT_FAILURE); } /* aggiunge una nuova casella con la relativa descrizione */ if ((description = strchr(buffer, ':')) == NULL) { printErr("ERROR: missing ':' in configuration file\n"); exit(EXIT_FAILURE); } addBox(boxNumber, description + 1); } if (getTotalBoxesNumber() == 0) { printErr("ERROR: invalid configuration file\n"); exit(EXIT_FAILURE); } fclose(configFile); printBoard(); showGame(); /* avvia la simulazione del gioco */ srand(time(NULL)); for (turn = 0; !allPlayersDone(); turn++) { if (!nextStep()) return EXIT_SUCCESS; printMessage("\n**************************************\n"); printMessage("turno %d", turn + 1); printMessage("\n**************************************\n"); showGame(); /* * per ogni giocatore G che non ha terminato il gioco: * 1. se G e' fermo per un turno cambia il suo stato in * modo che al turno successivo venga rimesso in gioco * 2. altrimenti viene lanciato il dado, mosso il giocatore * e visualizzata la sua prova */ while (nextPlayer()) { int state = getPlayerState(); if (state == ACTIVE || state == TEST_PASSED || state == TO_BE_ACTIVATED) { if (state != ACTIVE) setPlayerState(ACTIVE, 0); movePlayer((rand() % 6) + 1); if (getPlayerBox() > getTotalBoxesNumber()) setPlayerState(DONE, turn); else printMessage("player %s: \"%s\"\n", getPlayerName(), getDescription(getPlayerBox())); } else if (state == OUT_OF_TURN) setPlayerState(TO_BE_ACTIVATED, 0); } showGame(); /* * Legge e registra l'esito di tutte le prove sostenute nel * turno corrente dai giocatori */ for (i = getActivePlayersNumber(); i > 0; i--) { int playerNumber; bool result; do { result = askPlayerResult(&playerNumber); if (playerNumber > totalPlayersNumber) printErr("WARNING: player number %d out of " "bounds [1; %d]\n", playerNumber, totalPlayersNumber); else { setCurrentPlayer(playerNumber); if (getPlayerState() != ACTIVE) printErr("WARNING: player number %d not " "valid because player:" "\n\t-won" "\n\t-is out of turn" "\n\t-already passed the test\n", playerNumber); } } while (playerNumber > totalPlayersNumber || getPlayerState() != ACTIVE); if (result) setPlayerState(TEST_PASSED, 0); else setPlayerState(OUT_OF_TURN, 0); } } printScore(); closeIoInterface(); return EXIT_SUCCESS; }
void Menu::parseCommand(string cmd) { if (cmd == "help") showHelp(); else if (cmd == "add_player") addPlayer(); else if (cmd == "del_player") delPlayer(); else if (cmd == "modify_player") modifyPlayer(); else if (cmd == "show_player") showPlayer(); else if (cmd == "show_all_players") showAllPlayers(); else if (cmd == "search_player") searchPlayer(); else if (cmd == "add_game") addGame(); else if (cmd == "del_game") delGame(); else if (cmd == "modify_game") modifyGame(); else if (cmd == "set_game_res") setGameResult(); else if (cmd == "show_game") showGame(); else if (cmd == "show_all_games") showAllGames(); else if (cmd == "sort_games_by_date") sortGamesByDate(); else if (cmd == "sort_games_by_res") sortGamesByResult(); else if (cmd == "search_game_by_date") searchGameByDate(); else if (cmd == "search_game_by_opponent") searchGameByOpponent(); else if (cmd == "add_stadium") addStadium(); else if (cmd == "del_stadium") delStadium(); else if (cmd == "modify_stadium") modifyStadium(); else if (cmd == "show_stadium") showStadium(); else if (cmd == "show_all_stadiums") showAllStadiums(); else if (cmd == "search_stadium") searchStadium(); else if (cmd == "save_db") saveDbToFile(); else if (cmd == "load_db") loadDbFromFile(); else if (cmd == "") {} else if (cmd == "exit") exit(0); else error(ERR_INVALID_CMD); }