Code game(SDL_Renderer *renderer, CommandTable *table) { char name[MAXIMUM_PLAYER_NAME_SIZE]; Player player; Game game; Code code; code = read_player_name(name, MAXIMUM_PLAYER_NAME_SIZE, renderer); if (code == CODE_QUIT || code == CODE_CLOSE) { return code; } player = create_player(name, table); game = create_game(&player); code = run_game(&game, renderer); destroy_game(&game); return code; }
void run_game(){ char buf[40]; int x,y; int flags; int result=1; int index; int theme; while(1){ // World map gen_areamap(&x,&y); theme=get_random(0,NUM_MAP_THEME); while(1){ // Area map area_menu(&x,&y); flags=get_area_map(x,y); if(flags&AMAP_ENCOUNTER_BIT){ result=encounter(theme); if(!result){ destroy_game(); return; } } if(flags&AMAP_TREASURE_BIT){ index=spawn_item_by_price(50,500); add_item(index); sprintf(buf,"Obtained %s!",eq_name(index)); print_message(buf); } if(flags&AMAP_EXIT_BIT) break; explore_areamap(x,y); } world_menu(); } }
Game* end_of_game_action(Game* game) { short gameType; /* If the game has not reached the end, there is no action to realize */ if(!end_of_game(game)) return NULL; /* Else, informs players the game is over */ printf("Plus aucun coup possible : fin du jeu\n"); /* If the black player has more pawn, he wins */ if(game->oth->piecesNb[BLACK] > (game->oth->piecesNb[WHITE])) printf("\n%s a gagné !\n", game->p1.player_name); /* Else, if the white one has more pawn, he wins */ else if(game->oth->piecesNb[BLACK] < (game->oth->piecesNb[WHITE])) printf("%s a gagné !\n", game->p2.player_name); /* Else, the two players are dead heat */ else printf("%s et %s sont ex-aequo !\n", game->p1.player_name, game->p2.player_name); printf("Score : %d - %d\n", game->oth->piecesNb[BLACK], game->oth->piecesNb[WHITE]); /* Asking the players if they want to save the game */ ask_save_game(game); /* Freeing the game */ destroy_game(game); game = NULL; /* Asking the players if they want to play another time */ if(ask_new_game() == YES) { /* If so, initializing the game, the players' names ans the board */ game = init_game(); /* Choosing the game type (H vs H or H vs C) */ gameType = choose_game_type(); /* If the game type is Human vs Human */ if(gameType == H_VS_H) { /* Asking for the two players' names */ enter_players_names(game); } /* If the game type is Human vs Computer */ else if(gameType == H_VS_C) { /* Choosing the color of the human player, and asking for his name */ gameType = choose_player_color(); enter_one_name(game, gameType); /* Giving the computer the other player and giving it a name */ give_computer_name(game, (gameType+1)%2); } else if(gameType == C_VS_C) { /* Giving the computer the other player and giving it a name */ give_computer_name(game, 0); give_computer_name(game, 1); } clear_screen(); display_othellier(game); printf("Au tour de %s de jouer :\n", game->turn==BLACK?game->p1.player_name:game->p2.player_name); game = play_ai(game); } /* Otherwise, clearing the screen and returning NULL */ else clear_screen(); return game; }
int main(int argc, char* argv[]) { struct game *game = create_game(); apply_sc(0, &zero_value, game); switch_turn(game); apply_sc(100, &help_value, game); switch_turn(game); apply_cs(&K_value, 100, game); switch_turn(game); apply_cs(&S_value, 100, game); switch_turn(game); apply_sc(100, &get_value, game); switch_turn(game); apply_sc(100, &zero_value, game); switch_turn(game); apply_cs(&put_value, 0, game); switch_turn(game); apply_sc(0, &zero_value, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&K_value, 100, game); switch_turn(game); apply_cs(&S_value, 100, game); switch_turn(game); apply_sc(100, &get_value, game); switch_turn(game); apply_sc(100, &zero_value, game); switch_turn(game); apply_cs(&put_value, 0, game); switch_turn(game); apply_sc(0, &zero_value, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); // 8192 apply_cs(&K_value, 100, game); switch_turn(game); apply_cs(&S_value, 100, game); switch_turn(game); apply_sc(100, &get_value, game); switch_turn(game); apply_sc(100, &zero_value, game); switch_turn(game); print_game(game); apply_cs(&put_value, 0, game); switch_turn(game); apply_sc(0, &zero_value, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_sc(100, &attack_value, game); switch_turn(game); apply_cs(&K_value, 100, game); switch_turn(game); apply_cs(&S_value, 100, game); switch_turn(game); apply_sc(100, &get_value, game); switch_turn(game); apply_sc(100, &zero_value, game); switch_turn(game); apply_cs(&put_value, 0, game); switch_turn(game); apply_sc(0, &zero_value, game); switch_turn(game); apply_cs(&K_value, 100, game); switch_turn(game); apply_cs(&S_value, 100, game); switch_turn(game); apply_sc(100, &get_value, game); switch_turn(game); apply_sc(100, &zero_value, game); switch_turn(game); apply_cs(&put_value, 0, game); switch_turn(game); apply_sc(0, &zero_value, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); // 16384 apply_cs(&K_value, 100, game); switch_turn(game); apply_cs(&S_value, 100, game); switch_turn(game); apply_sc(100, &get_value, game); switch_turn(game); apply_sc(100, &zero_value, game); print_game(game); apply_cs(&put_value, 0, game); switch_turn(game); apply_sc(0, &zero_value, game); switch_turn(game); apply_cs(©_value, 0, game); switch_turn(game); print_game(game); apply_cs(&put_value, 0, game); switch_turn(game); apply_sc(0, &zero_value, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&dbl_value, 0, game); switch_turn(game); apply_cs(&succ_value, 0, game); switch_turn(game); apply_cs(&revive_value, 0, game); switch_turn(game); print_game(game); destroy_game(game); return 0; }
int main(int argc, char *argv[]) { Game *game = create_game(ROWS, COLS); if (argc==3 && !strcmp(argv[1], "display")) { char *p = argv[2]; char *err; while (*p) { int col = *p - '0'; if (col>=0 && col<COLS) { if (can_play(game, col)) { play(game, col); if (victory(game, col)) printf("victory!\n"); } else { err = "illegal move"; goto err_handler; } } else { int i; err = "unknown move syntax"; err_handler: fprintf(stderr, "error: %s.\n%s\n", err, argv[2]); for (i=0; i<p-argv[2]; i++) fprintf(stderr, " "); fprintf(stderr, "^\n"); return 2; } p++; } print_board(game); } else if ((argc==2 || argc==3) && !strcmp(argv[1], "play")) { int j; if (argc==3 && !strcmp(argv[2], "red")) goto red; while (1) { print_board(game); do { printf("Your move: "); if (scanf("%d", &j) != 1) { printf("\n"); goto out; } } while (!can_play(game, j)); play(game, j); if (victory(game, j)) { printf("You've won!\n"); break; } else { int ret = negamax(game, DEFEAT, VICTORY, 12, &j); red: if (ret == CUT) printf("I've got no idea what to do...\n" "I'm gonna try %d.\n", j); else printf("I'll play %d (score %d)\n", j, ret); play(game, j); if (victory(game, j)) { printf("You've lost!\n"); break; } } } print_board(game); } else { fprintf(stderr, "usage:\t%s display moves\n", argv[0]); fprintf(stderr, "\t%s play [yellow|red]\n", argv[0]); fprintf(stderr, "try:\t%s display 01221\n", argv[0]); fprintf(stderr, "\t%s play\n", argv[0]); return 1; } out: destroy_game(game); return 0; }