int main(int argc, char *argv[]){ if(argc != 2) usage(argv[0]); int level = atoi(argv[1]); if(level <= 0) usage(argv[0]); game g = init_game(level); char buf[3][100]; int piece_num; dir d; int distance; int back_code; while(!game_over_hr(g)){ bool good = false; while(!good){ //Loop for the break of cancel instruction while(!good){ system("clear"); print_game(g); printf("Move the pieces for free the piece 0 to the exit:\n"); printf("Write exit for quit the game or cancel for restart the current move.\n"); printf("Total move: %d\n",game_nb_moves(g)); //First loop to take the number piece that you want to move back_code = take_piece_num(g, buf[0], &(piece_num)); if(back_code == 0) break; if(back_code == -1) return EXIT_SUCCESS; //Second loop to take the direction where you want to move back_code = take_direction(g, piece_num, buf[1], &(d)); if(back_code == 0) break; if(back_code == -1) return EXIT_SUCCESS; //Third loop to take the number of case that need for the move back_code = take_number_case(g, piece_num, d, buf[2], &(distance)); if(back_code == 0) break; if(back_code == -1) return EXIT_SUCCESS; good = true; } } } system("clear"); print_game(g); printf("CONGRATULATION\nYou won in %d moves\n", game_nb_moves(g)); delete_game(g); return EXIT_SUCCESS; }
void display_score(cgame newGame) { clear(); int row, col; getmaxyx(stdscr, row, col); mvprintw(row / 2, col / 2 - 20, "You win with : %d moves !", game_nb_moves(newGame)); mvprintw(row / 2 + 1, col / 2 - 20, "Press any key to continue"); refresh(); getch(); }
void show_grid(game g){ printf("\n"); for (int ord=game_height(g)-1;ord>=0;ord--){ for (int abs=0;abs<game_width(g);abs++){ if (game_square_piece(g, abs, ord)== -1) printf(" · "); else printf("%2d ", game_square_piece(g, abs, ord)); } printf("\n\n\n"); } printf("\nMoves : %d\n----------\n\n", game_nb_moves(g)); }
int main(int argc, char *argv[]){ set_up_pieces(); game g = new_game_hr(NB_PIECES, pieces); char buf[3][100]; int piece_num; dir d; int distance; while(!game_over_hr(g)){ bool good = false; while(!good){ while(!good){ set_up_board(g); print_game(g); printf("Move the pieces for free the piece 0 to the exit:\n"); printf("Total move: %d\n",game_nb_moves(g)); while(!good){ printf("What piece do you want to move?\n"); read(0, buf[0], sizeof(char)*100); if(strcmp(buf[0], "cancel") == 10) break; if(strcmp(buf[0], "exit") == 10) return EXIT_SUCCESS; if(buf[0][0]<48 || buf[0][0]>=48+NB_PIECES || buf[0][1] != 10) printf("Write a number between 0 and %d\tor write cancel or exit.\n",NB_PIECES-1); else{ piece_num = atoi(buf[0]); good = true; } } if(!good) break; good = false; while(!good){ printf("In what direction?\n"); read(0, buf[1], sizeof(char)*100); if(strcmp(buf[1], "cancel") == 10) break; if(strcmp(buf[1], "exit") == 10) return EXIT_SUCCESS; if(!is_dir_option(buf[1])) printf("Write one of those direction: up, down, right, left\tor write cancel or exit.\n"); else{ for(int i=0; i<4; ++i){ if(strcmp(buf[1], direction[i].dir_name) == 10) d = direction[i].dir_option; } if(!good_direction(g, piece_num, d)){ if(is_horizontal(game_piece(g, piece_num))) printf("The piece %d cannot move vertycaly\n", piece_num); else printf("The piece %d cannot move horizontaly\n", piece_num); }else good = true; } } if(!good) break; good = false; while(!good){ printf("How many case?\n"); read(0, buf[2], sizeof(char)*100); if(strcmp(buf[2], "cancel") == 10) break; if(strcmp(buf[2], "exit") == 10) return EXIT_SUCCESS; if(buf[2][0]<48 || buf[2][0]>=48+SIZE_ARRAY || buf[2][1] != 10) printf("Write a number between 0 and %d\tor write cancel or exit.\n",SIZE_ARRAY-1); else{ distance = atoi(buf[2]); good = play_move(g, piece_num, d, distance); if(!good) printf("The piece %d cannot move to that case.\n", piece_num); } } } } } set_up_board(g); print_game(g); printf("CONGRETULATION\nYou won in %d moves\n", game_nb_moves(g)); return EXIT_SUCCESS; }
void SDL_Display(game g, SDL_Renderer* rdr, TTF_Font* font, int popup){ SDL_Rect rect; SDL_Color fontColor = {255, 255, 255}; rect.w = (SCREEN_Y/game_height(g)); rect.h = (SCREEN_Y/game_height(g)); SDL_SetRenderDrawColor(rdr, 192,192,192, 255); SDL_RenderClear(rdr); SDL_SetRenderDrawColor(rdr, 0,255,127, 255); if(rules==0){//pour la Sortie des piece de Rush hour rect.y = (SCREEN_Y/game_height(g))*2; rect.x = (SCREEN_Y/game_height(g))*game_width(g); rect.h = (SCREEN_Y/game_height(g)); rect.w = 10; SDL_RenderFillRect(rdr, &rect); SDL_SetRenderDrawColor(rdr, 105,105,105, 255); rect.y = 0; rect.x = (SCREEN_Y/game_height(g))*game_width(g); rect.w = 10; rect.h = ((SCREEN_Y/game_height(g))*2); SDL_RenderFillRect(rdr, &rect); rect.y = ((SCREEN_Y/game_height(g))*3); rect.x = (SCREEN_Y/game_height(g))*game_width(g); rect.w = 10; rect.h = ((SCREEN_Y/game_height(g))*3); SDL_RenderFillRect(rdr, &rect); rect.y = (SCREEN_Y); rect.x = 0; rect.w = SCREEN_X ; rect.h = 10; SDL_RenderFillRect(rdr, &rect); } else if(rules==1){ //pour la Sortie des piece de Ane rouge rect.y = SCREEN_Y; rect.x = (SCREEN_Y/game_height(g)); rect.h = 10; rect.w = (SCREEN_Y/game_height(g))*2; SDL_RenderFillRect(rdr, &rect); SDL_SetRenderDrawColor(rdr, 105,105,105, 255); rect.y = 0; rect.x = (SCREEN_Y/game_height(g))*game_width(g); rect.w = 10; rect.h = SCREEN_Y; SDL_RenderFillRect(rdr, &rect); rect.y = SCREEN_Y; rect.x = 0; rect.w = (SCREEN_Y/game_height(g)); rect.h = 10; SDL_RenderFillRect(rdr, &rect); rect.y = SCREEN_Y; rect.x = ((SCREEN_Y/game_height(g))*3); rect.w = ((SCREEN_Y/game_height(g))*6); rect.h = 10; SDL_RenderFillRect(rdr, &rect); } //Affichage des pièces for(int i = 0; i < game_nb_pieces(g); ++i){ SDL_SetRenderDrawColor(rdr, color[i][0],color[i][1],color[i][2], 255); rect.x = get_x(game_piece(g,i))*(SCREEN_Y/game_height(g)); rect.y = (SCREEN_Y - (get_y(game_piece(g,i))*(SCREEN_Y/game_height(g)))); rect.w = get_width(game_piece(g,i))*(SCREEN_Y/game_height(g)); rect.h = get_height(game_piece(g,i))*(SCREEN_Y/game_height(g)); rect.y -= rect.h; SDL_RenderFillRect(rdr, &rect); } //Affichage du nombre de mouvements char disp_moves[20]; //contiendra l'affichage du nombre de mouvements char end_msg[30]; //contiendra le message de fin de partie sprintf(disp_moves," Mouvements : %d ",game_nb_moves(g));// Convertie le nb_mouv en caractère drawText(rdr, font, fontColor,SCREEN_X-250 ,SCREEN_Y/2-SCREEN_Y/6 -200 ,disp_moves); //affichage nombre de mouvements //Affichage des règles drawText(rdr, font, fontColor,(SCREEN_Y/game_height(g))*game_width(g)+120 ,SCREEN_Y-40 ,"R : Relancer"); drawText(rdr, font, fontColor,SCREEN_X-120 ,SCREEN_Y-40 ,"Q : Quitter"); if (popup != 0){ SDL_SetRenderDrawColor(rdr, 50, 50, 50, 170); rect.x = 0; rect.y = SCREEN_Y/4; rect.w = SCREEN_X; rect.h = SCREEN_Y/2; SDL_RenderFillRect(rdr, &rect); if (popup == 1){ drawText(rdr, font, fontColor,SCREEN_X/2 ,SCREEN_Y/2-SCREEN_Y/8 ,"Voulez-vous lancer une nouvelle partie ?"); } else if (popup == 2){ drawText(rdr, font, fontColor,SCREEN_X/2 ,SCREEN_Y/2-SCREEN_Y/8 ,"Voulez-vous quitter le jeu en cours ?"); } else if (popup == 3){ sprintf(end_msg,"Partie finie en %d mouvements",game_nb_moves(g)); drawText(rdr, font, fontColor,SCREEN_X/2, SCREEN_Y/2-SCREEN_Y/8, end_msg); drawText(rdr, font, fontColor,SCREEN_X/2, SCREEN_Y/2-SCREEN_Y/17, "Voulez vous rejouer ?"); } drawText(rdr, font, fontColor,SCREEN_X/6 ,SCREEN_Y/2+SCREEN_Y/8 ,"Echap : Non"); drawText(rdr, font, fontColor,SCREEN_X-SCREEN_X/6 ,SCREEN_Y/2+SCREEN_Y/8 ,"Entree : Oui"); } SDL_RenderPresent(rdr); SDL_Delay(50); }
void draw_game(game newGame, int starty, int startx, int MAXROW, int MAXCOL, WINDOW **grid, WINDOW **car, WINDOW **score, int choosenCar, char *message, bool gameOverRh) { *grid = create_newgrid(starty, startx, MAXROW, MAXCOL, SIZE, gameOverRh); // Create new grid wmove(*grid, 0, 0); //Create each car and highlight the selected one for (int i = 0; i < game_nb_pieces(newGame); i++) { if (choosenCar == i) { car[i] = create_newcar(game_piece(newGame, i), i, true, SIZE, MAXROW); // Create new car and highlight it continue; } car[i] = create_newcar(game_piece(newGame, i), i, false, SIZE, MAXROW); // Create new car } wmove(*grid, 0, 0); //Create score on right of the grid *score = create_newscore(SIZE * MAXROW + 1, SIZE*MAXCOL, starty, startx + SIZE * MAXCOL * 2 + 1, game_nb_moves(newGame), message); wmove(*grid, 0, 0); }