void arkanoid::Game::update_all(){ int r = update_scene(); if(r == 2){ end_actual_game(); new_game(); } else if(r == 1){ ending_message(); g_input = 27; } else { g_input = 0; get_input(); ticks++; } }
// The game loop(tm) where all the fun happens void repl() { // Set the current room to the starting room, which we define to be // the first room. struct room *current_room = &rooms_list[0]; // Allocate an array to hold the list of visited functions struct room **visited = malloc(sizeof(struct room*) * NUM_ROOMS); unsigned int visited_index = 0; unsigned int visited_cap = NUM_ROOMS; // Create a buffer for holding the name the user will type. Obviously // shouldn't be longer than the longest room's name char buffer[NAME_BUFFER_LEN]; // The number of steps taken so far unsigned int num_steps = 0; while (true) { // I've written a fair bit of C and so far I have listened to // Dijkstra's advice on gotos. // relevant XKCD: https://www.xkcd.com/292/ Top: // If we're at the end, say so and return if (current_room->type == END_ROOM) { ending_message(num_steps, visited, visited_index); free(visited); return; } // Print all those UGLY MESSAGES! printf("\nCURRENT LOCATION: %s\n", current_room->name); print_all_connections(current_room); printf("WHERE TO? >"); char *ret = fgets(buffer, NAME_BUFFER_LEN, stdin); assert(ret != NULL); // Strip the newline from the end. In other languages this // would be buffer.comp buffer[strlen(buffer)-1] = '\0'; // Compare the user's input with all of the connections for (int i = 0; i < current_room->num_conns; i++) { // If a connection matches the user's input if (strncmp(buffer, current_room->connections[i]->name, NAME_BUFFER_LEN) == 0) { // update the current room current_room = current_room->connections[i]; // If we have reached the capacity of the // array of visited rooms reallocate if (visited_index >= visited_cap) { visited_cap += NUM_ROOMS; visited = realloc(visited, visited_cap * sizeof(struct room*) ); assert(visited != NULL); } // push the room onto the list of visited rooms visited[visited_index] = current_room; visited_index++; num_steps++; // Loop again goto Top; } } // If none of the user's input didn't match any of the // connections print a message and continue. printf("\nHUH? I DON’T UNDERSTAND THAT ROOM. TRY AGAIN.\n"); } }