static void search(gamestate_t *gs, alist_t *moveStack) { if (gamestate_pegs_remaining(gs) == 1) { // printf("Found a winning sequence. Final state:\n"); // gamestate_print(gs); alist_add(solutions, alist_new_copy(moveStack)); gamesPlayed++; return; } alist_t *legalMoves = gamestate_legal_moves(gs); if (alist_is_empty(legalMoves)) { gamesPlayed++; return; } for (int i = 0; i < legalMoves->size; i++) { move_t *m = alist_get(legalMoves, i); gamestate_t *nextState = gamestate_apply_move(gs, m); alist_add(moveStack, m); search(nextState, moveStack); alist_remove_last(moveStack); } }
/* Frees AdbGuest instance created with _adb_guest_new routine. */ static void _adb_guest_free(AdbGuest* adb_guest) { if (adb_guest != NULL) { /* At this poin the guest must not be in any of the lists. */ assert(alist_is_empty(&adb_guest->list_entry)); AFREE(adb_guest); } }
void adb_server_on_guest_closed(void* opaque) { AdbGuest* const adb_guest = (AdbGuest*)opaque; AdbHost* const adb_host = adb_guest->adb_host; /* Remove the guest from the list */ if (!alist_is_empty(&adb_guest->list_entry)) { alist_remove(&adb_guest->list_entry); } /* Disassociate the host. */ if (adb_host != NULL) { if (!alist_is_empty(&adb_host->list_entry)) { alist_remove(&adb_host->list_entry); } _adb_host_free(adb_host); } _adb_guest_free(adb_guest); }
/* Frees AdbHost instance created with _adb_host_new routine. */ static void _adb_host_free(AdbHost* adb_host) { if (adb_host != NULL) { /* At this point it must not be listed anywhere. */ assert(alist_is_empty(&adb_host->list_entry)); /* Close the host socket. */ if (adb_host->host_so >= 0) { loopIo_done(adb_host->io); socket_close(adb_host->host_so); } /* Free pending data buffers. */ if (adb_host->pending_data != NULL) { free(adb_host->pending_data); } if (adb_host->pending_send_buffer != NULL) { free(adb_host->pending_send_buffer); } AFREE(adb_host); } }