void test_get_move() { freopen("test/fixtures/test_input.txt", "r", stdin); Board* board = new_board(3); int move = ask_for_move(board, 'X', 'O'); destroy_board(board); assert(move == 5); }
void destroy_game(Game* game) { if(game == NULL) return ; else{ int i = 0; for(; i<32; i++) { if(game->man[i] != NULL) { destroy_chessman(game->man[i]); game->man[i] = NULL; } } if(game->board != NULL) destroy_board(game->board); if(game->red != NULL) destroy_user(game->red); if(game->black != NULL) destroy_user(game->black); game->board = NULL; game->red = NULL; game->black = NULL; free(game); game = NULL; } }
void test_display_empty_board() { Board* board = new_board(3); char expected[] = "1 2 3 4 5 6 7 8 9 "; show_board(board); destroy_board(board); assert(strstr(writer_log, expected)); }
void test_invalid_move() { Board* board = new_board(3); make_move(board, 0, 'X'); bool check = is_valid_move(board, 0); assert(!check); destroy_board(board); }
void test_display_played_board() { Board* board = draw_board(); char expected[] = "X O X O X O O X O "; show_board(board); destroy_board(board); assert(strstr(writer_log, expected)); }
void test_new_board() { Board* board = new_board(3); for(int i = 0; i < 9; i++) { assert(get_space(board, i) == '-'); } destroy_board(board); }
//program to implement the game of minesweeper //user enters the name of the executable, the row and column dimensions, number of mines, and optionally a seed for random number generator int main(int argc, char** argv){ Board board; //intitialize the board with struct type Board int row = 0; //intialize the number of rows int column = 0; //initialize the number of columns int mine = 0; //intiialize the number of mines read_args(argc, argv, &row, &column, &mine); board.row = row; board.col = column; board.mine = mine; board.seed = generate_seed(argv); board.tile = create_board(board); //create the board mine_location_generator(board); print_board(board); play_is_valid(board); destroy_board(board); //when the game is over destroy the board return 0; }
void test_board_is_done() { Board* board = new_board(3); make_move(board, 6, 'X'); make_move(board, 7, 'X'); make_move(board, 8, 'X'); assert(is_done(board)); destroy_board(board); }
void test_board_is_full() { Board* board = new_board(3); for(int i = 0; i < 9; i++) { make_move(board, i, 'X'); } assert(is_full(board)); destroy_board(board); }
void test_board_is_won_column() { Board* board = new_board(3); assert(!is_won(board)); make_move(board, 0, 'X'); make_move(board, 3, 'X'); make_move(board, 6, 'X'); assert(is_won(board)); destroy_board(board); }
void test_board_is_won_diag2() { Board* board = new_board(3); assert(!is_won(board)); make_move(board, 2, 'X'); make_move(board, 4, 'X'); make_move(board, 6, 'X'); assert(is_won(board)); destroy_board(board); }
void test_board_is_won_row() { Board* board = new_board(3); assert(!is_won(board)); make_move(board, 0, 'X'); make_move(board, 1, 'X'); make_move(board, 2, 'X'); assert(is_won(board)); destroy_board(board); }
void test_board_finds_winner() { Board* board = new_board(3); make_move(board, 2, 'X'); make_move(board, 1, 'O'); make_move(board, 4, 'X'); make_move(board, 0, 'O'); make_move(board, 6, 'X'); assert(winner(board) == 'X'); destroy_board(board); }
void test_handles_draw() { Board* board = draw_board(); Player* p1 = create_player('X', &fake_move); Player* p2 = create_player('O', &fake_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); assert(strstr(writer_log, "Draw")); }
/* ======================================= */ static void destroy_board_list() { Board *board; while(g_list_length(board_list)>0) { board = g_list_nth_data(board_list, 0); board_list = g_list_remove (board_list, board); destroy_board(board); } }
/* ======================================= */ void missing_destroy_board_list(GList *list) { Board *board; guint length = g_list_length(list); while( length-- ) { board = g_list_nth_data(list, length); destroy_board(board); } g_list_free(list); }
void test_handles_win() { Board* board = draw_board(); unset_move(board, 0); Player* p1 = create_player('O', &fake_move); Player* p2 = create_player('X', &fake_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); assert(strstr(writer_log, "1 O X O X O O X O")); assert(strstr(writer_log, "Player 1's turn")); assert(strstr(writer_log, "O O X O X O O X O")); assert(strstr(writer_log, "O wins!")); }
void test_gets_valid_move() { Board* board = draw_board(); unset_move(board, 1); Player* p1 = create_player('X', &incrementing_move); Player* p2 = create_player('O', &fake_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); current_move = 0; assert(strstr(writer_log, "Player 1's turn")); assert(strstr(writer_log, "Invalid move")); assert(strstr(writer_log, "X wins!")); }
void test_switches_turns() { Board* board = draw_board(); unset_move(board, 0); unset_move(board, 1); Player* p1 = create_player('X', &incrementing_move); Player* p2 = create_player('O', &incrementing_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); current_move = 0; assert(strstr(writer_log, "Player 1's turn")); assert(strstr(writer_log, "Player 2's turn")); assert(strstr(writer_log, "Draw")); }
//main function that starts the program //program to execute the game of Connect-4, except the user can define the size of the board and how many pieces are needed to win int main(int argc, char** argv){ int num_rows = 0; //initialize variables that will be entered by the user int num_columns = 0; int plays_to_win = 0; int turn_count = 1; read_args(argc, argv, &num_rows, &num_columns, &plays_to_win); //pass the variables as pointers so that their values can be updated char** board = create_board(num_rows, num_columns); execute_game(board, num_rows, num_columns, plays_to_win, turn_count); destroy_board(board, num_rows); //when the game is over, the board can be destroyed return 0; }
/** * Cleans up current thread and removes it from the list. */ void cleanup_handler(void *arg) { int i, j; response_s response; games_list_s **list = tdata.games_list; thread_s *thread = NULL; threads_list_s **tlist = tdata.threads_list; response.type = MSG_CLEANUP_RSP; response.error = MSG_RSP_ERROR_NONE; printf("Thread %d cleanup handler goes\n", (int) pthread_self()); for (i = 0; i < SPECTATORS_NO; i++) { if (tdata.spectators_fd[i] != -1) { FD_SET(tdata.spectators_fd[i], tdata.rd_fds); if (play == 1) { send_response_message(tdata.spectators_fd[i], &response); } } } for (j = 0; j < 2; j++) { if (tdata.players_fd[j] != -1) { FD_SET(tdata.players_fd[j], tdata.rd_fds); if (play == 1) { send_response_message(tdata.players_fd[j], &response); } } } get_thread_by_id(*tlist, &thread, tdata.game->id); if (thread != NULL) { pthread_mutex_lock(tdata.threads_list_mutex); remove_thread_from_list(tlist, thread); pthread_mutex_unlock(tdata.threads_list_mutex); } destroy_board(tdata.game->board); pthread_mutex_lock(tdata.games_list_mutex); remove_game_from_list(list, tdata.game); pthread_mutex_unlock(tdata.games_list_mutex); kill(tdata.parent_pid, SIGRTMIN + 11); }
void test_board_is_empty() { Board* board = new_board(3); assert(is_empty(board)); destroy_board(board); }
void test_board_is_draw() { Board* board = draw_board(); assert(is_draw(board)); destroy_board(board); }
void test_valid_move() { Board* board = new_board(3); bool check = is_valid_move(board, 0); assert(check); destroy_board(board); }
int main(void) { srand(time(NULL)); printf("The available commands are:\n" "I N - Inject an NxN board. This command will scan NxN letters to form a board\n" "G N - Generates a new NxN board where a letter doesn't appear more than N times\n" "P - Print the current board\n" "W N word - Insert a word into the dictionary with score N\n" "A N word - Insert a word and all prefixes in the dictionary. Non-proper prefixes get a score of 0\n" "R word - Delete a word\n" "S word - Search for a word and return its score (-1 if no such word exists)\n" "D - Dump the dictionary with the corresponding scores\n" "B - Find the best word (word with the highest score)\n" "Q - Quit\n" "> "); char **board = NULL; size_t board_dim = 0; trie = new_trie(); char op; while (scanf(" %c", &op) == 1) { if (op == 'I') { destroy_board(board, board_dim); scanf("%zu", &board_dim); board = inject_board(board_dim); } else if (op == 'P') { if (board == NULL) { printf("No board at the moment\n"); } else { print_board(board, board_dim); } } else if (op == 'G') { destroy_board(board, board_dim); scanf("%zu", &board_dim); board = generate_board(board_dim); } else if (op == 'W') { size_t word_score; scanf("%zu%s", &word_score, word_buff); insert_word(trie, word_buff, word_score); } else if (op == 'A') { size_t word_score; scanf("%zu%s", &word_score, word_buff); size_t i; for (i = 1; word_buff[i] != '\0'; i++) { char c = word_buff[i]; word_buff[i] = '\0'; insert_word(trie, word_buff, 0); word_buff[i] = c; } insert_word(trie, word_buff, word_score); } else if (op == 'R') { scanf("%s", word_buff); delete_word(trie, word_buff); } else if (op == 'S') { scanf("%s", word_buff); int s = word_score(trie, word_buff); if (s == -1) { printf("No such word: %s\n", word_buff); } else { printf("score(%s) = %d\n", word_buff, s); } } else if (op == 'D') { print_known_words(trie); } else if (op == 'B') { char *w = find_best_word(board, board_dim); if (w == NULL) { printf("No words found\n"); } else { printf("Best word: %s\n", w); } free(w); } else if (op == 'Q') { break; } else { fprintf(stderr, "Unrecognized operation: %c\n", op); } printf("> "); } destroy_trie(trie); destroy_board(board, board_dim); return 0; }
void test_board_invalid_move_when_taken() { Board* board = new_board(3); make_move(board, 0, 'X'); assert(make_move(board, 0, 'O') == -1); destroy_board(board); }
void test_board_invalid_move_when_out_of_range() { Board* board = new_board(3); assert(make_move(board, 9, 'O') == -1); destroy_board(board); }
void test_board_factor() { Board* board = new_board(3); assert(get_factor(board) == 3); destroy_board(board); }
void test_change_board() { Board* board = new_board(3); make_move(board, 0, 'X'); assert(get_space(board, 0) == 'X'); destroy_board(board); }
void test_board_not_full() { Board* board = new_board(3); assert(!is_full(board)); destroy_board(board); }