int is_effectual_move(Board b, Move m) { Board tmp = rotate_for_move(b, m); int this, i; int data[BOARDSIZE]; for (int r=0; r<BOARDSIZE; r++) { i = 0; memset(data, 0, sizeof(int)*BOARDSIZE); for (int c=BOARDSIZE-1; c >= 0; c--) { this = bget(tmp, r, c); if (this != 0) { if (data[i] == 0) { data[i] = this; } else if (this == data[i]) { data[i] += this; i++; } else { i++; data[i] = this; } } } for (int c=0; c<BOARDSIZE; c++) { if (bget(tmp, r, c) != data[BOARDSIZE-1-c]) { free_board(tmp); return 1; } } } free_board(tmp); return 0; }
int main(int argc, char **argv) { int x = MAX_X; int y = MAX_Y; int mines = MAX_MINES; // printf("%d %d %d\n\n", x, y, mines); if(argc==1) { struct Board *board = init_board(x, y, mines); init_AI(x, y, mines); int game=0; struct Point target; while (game >= 0) { print_board(board); printf("\n"); print_AI_grid(); printf("Target?"); target = AI_get_target(); printf("\nTargetting: %d %d\n", target.x, target.y); game = shoot(board, target.x, target.y); printf("Result: %d \n\n",game); AI_send_result(target.x, target.y, game); } printf("\n"); if(game==-1) printf("LOST\n"); else if(game==-2) printf("WON\n"); free_board(board); free_AI(); } else { int total=atoi(argv[1]); int i; int won=0; int last_percent=0, percent=0; for(i=0;i<total;i++) { struct Board *board = init_board(x, y, mines); init_AI(x, y, mines); int game=0; struct Point target; while (game >= 0) { target = AI_get_target(); game = shoot(board, target.x, target.y); AI_send_result(target.x, target.y, game); } if(game==-2) won++; free_board(board); free_AI(); percent = (int)((float)i/total*100); if(last_percent < percent) { printf("%d percent done, ratio: %f \n", percent, (float)won/(i+1)); last_percent = percent; } } printf("Won %d out of %d\nRatio: %f \n", won, total, (float)won/total); } return 0; }
void game_over(game_t *game) { assert(game != NULL); free_board(game->current); free_board(game->old); }
int main() { int good_queens[] = {4, 1, 3, 5, 7, 2, 0, 6}; int bad_queens[] = {4, 3, 1, 5, 7, 2, 0, 6}; board good_board = new_board_from_queens(8, good_queens); board bad_board = new_board_from_queens(8, bad_queens); boards solutions, diagonals; int n; printf("==== print_board(good_board) ====\n"); print_board(good_board); printf("==== print_board_as_perm(good_board) ====\n"); print_board_as_perm(good_board); printf("==== valid_diagonals ====\n"); printf(" good board : %i \n", valid_diagonals(8, good_queens)); printf(" bad board : %i \n", valid_diagonals(8, bad_queens)); free_board(good_board); free_board(bad_board); printf("==== small n behavior ====\n"); for (n=0; n<4; n++){ printf(" n=%i : ", n); solutions = queens_search(n); print_boards_as_perms(solutions); free_boards(solutions); } printf("==== queens_search n=4 ====\n"); solutions = queens_search(4); print_boards(solutions); free_boards(solutions); printf("==== search n=8 ====\n"); solutions = queens_search(8); printf(" number of solutions = %i \n", solutions->count); free_boards(solutions); diagonals = queens_search_diagonals(8); printf(" number of queens_search_diagonals = %i \n", diagonals->count); free_boards(diagonals); diagonals = in_both_diagonals(solutions); // same, another way printf(" number of in_both_diagonals = %i \n", diagonals->count); free_boards(diagonals); printf("==== queens_search_diagonals for various n ====\n", n); for (n=4; n<15; n++){ reset_timer(); diagonals = queens_search_diagonals(n); printf(" n = %2i ; count = %7i ; time = ", n, diagonals->count); print_elapsed(); printf("\n"); fflush(stdout); free_boards(diagonals); } printf("=== memory check ===\n"); printf(" malloc() + calloc() - free() = %i \n\n", get_allocation_count()); return 0; }
int play2048_noply(Game g) { int size; while(!is2048(g->board)) { //print_board(g->board); Move* moves = effectual_moves(g->board, &size); // Test end conditions if (moves == NULL) break; if (size == 0) { free(moves); break; } int i; Move bestMove = moves[0]; double bestHeur = 0; for (i = 0; i < size; i++) { Game test = test_move_norand(g, moves[i]); PointList pl = open_spaces(test->board); if (pl->N == 0) {//This should never happen game_free(test); pl_free(pl); continue; } listNode *node = pl->points.head; double maxheur = 0; while (node != NULL) { Board cpy = board_cpy(test->board); Board cpy2 = board_cpy(test->board); Point p = (Point)node->data; place(cpy,p->r, p->c, 2); double twoheur = (*(test->h))(cpy); place(cpy2, p->r, p->c, 4); double fourheur = (*(test->h))(cpy2); double heur = twoheur > fourheur ? twoheur : fourheur; maxheur = maxheur > heur ? maxheur : heur; node = node->next; free_board(cpy); free_board(cpy2); } if (maxheur > bestHeur) { bestHeur = maxheur; bestMove = moves[i]; } game_free(test); pl_free(pl); } make_move(g, bestMove); free(moves); } //printf("oneboard:\n"); //print_board(g->board); return g->score; }
void test_is_square_being_attacked_by_king(void) { // black king attacks c5 // white king attacks f2 char *test_fen = "8/p3p1P1/2p5/pkP5/8/1p1pP1P1/pP1P1pK1/8 w - - 0 1"; struct position *pos = allocate_board(); consume_fen_notation(test_fen, pos); //print_board(pos); assert_true(TEST_is_king_attacking_square(pos, c5, BLACK)); assert_true(TEST_is_king_attacking_square(pos, f2, WHITE)); free_board(pos); // black king attacks h6 // white king attacks b7 test_fen = "8/1p2pp1k/1KP4P/3PP3/4p3/2pp2P1/P2p2P1/8 w - - 0 1"; pos = allocate_board(); consume_fen_notation(test_fen, pos); //print_board(pos); assert_true(TEST_is_king_attacking_square(pos, h6, BLACK)); assert_true(TEST_is_king_attacking_square(pos, b7, WHITE)); free_board(pos); // black king attacks g2 // white king attacks h7 test_fen = "8/p5Pp/PP5K/p4p2/P4ppP/3p4/3p2Pk/8 w - - 0 1"; pos = allocate_board(); consume_fen_notation(test_fen, pos); //print_board(pos); assert_true(TEST_is_king_attacking_square(pos, g2, BLACK)); assert_true(TEST_is_king_attacking_square(pos, h7, WHITE)); free_board(pos); // black king attacks g7 // white king attacks c7 test_fen = "3K2k1/2p3P1/Pp5p/P3P2P/P7/4p1p1/P1p1P3/8 w - - 0 1"; pos = allocate_board(); consume_fen_notation(test_fen, pos); //print_board(pos); assert_true(TEST_is_king_attacking_square(pos, g7, BLACK)); assert_true(TEST_is_king_attacking_square(pos, c7, WHITE)); free_board(pos); }
int new_game(game_t *game, int width, int height, gen_params_t *params, long sim_speed) { assert(game != NULL); assert(width > 0); assert(height > 0); assert(params != NULL); memset(game, 0, sizeof(*game)); if (!alloc_board(&game->board[0], width, height)) goto err_board1; if (!alloc_board(&game->board[1], width, height)) goto err_board2; generate(&game->board[0], params); game->current = &game->board[0]; game->old = &game->board[1]; game->sim_speed = sim_speed; game->sim_timer = sim_speed; game->player_count = 0; memcpy(&game->gen_params, params, sizeof(gen_params_t)); return 1; err_board2: free_board(&game->board[0]); err_board1: return 0; }
void test_add_thread() { Board *board; Thread *thread; board = new_board("test", "TEST", 2, 0); assert(board != NULL); thread = new_thread(NULL, 0, 0); /* Add the first thread */ add_thread_to_board(board, thread); assert(board->first_thread == 0); assert(board->last_thread == 0); /* Add a second thread */ add_thread_to_board(board, thread); assert(board->first_thread == 0); assert(board->last_thread == 1); /* Make sure the post count hasn't magically changed */ assert(board->post_count == 0); /* Add a third thread, should remove the first one */ add_thread_to_board(board, thread); assert(board->first_thread == 1); assert(board->last_thread == 0); /* Check post count again */ assert(board->post_count == 0); free_board(board); free_thread(thread); }
END_TEST START_TEST (test_select_command_15) { struct command command_data; command_data.id = 0; my_strcpy( command_data.name, "play", MAX_TOKEN_LENGTH ); my_strcpy( command_data.gtp_argv[0], "black", MAX_TOKEN_LENGTH ); my_strcpy( command_data.gtp_argv[1], "d4", MAX_TOKEN_LENGTH ); command_data.gtp_argc = 2; init_board(BOARD_SIZE_DEFAULT); init_known_commands(); select_command(&command_data); fail_unless( get_output_error() == false, "play command identified" ); command_data.id = 0; my_strcpy( command_data.name, "undo", MAX_TOKEN_LENGTH ); command_data.gtp_argc = 0; select_command(&command_data); fail_unless( get_output_error() == false, "undo command identified" ); free_board(); }
void tfree_struct(t_tetris *tetris) { free_list_tetri(tetris); delwin(tetris->wgame); delwin(tetris->wscore); delwin(tetris->wnext); free_board(tetris); free_options(tetris); free(tetris->key_tab); }
static void test_init_board1(void ** state) { Board b = init_board1(); assert_non_null(&b); assert_int_equal(b.nNbPos, 4); assert_int_equal(b.nNbTunnels, 0); assert_int_equal(b.nNbBonus, 0); assert_int_equal(b.nClosingWalls, 0); free_board(b); }
void test_is_square_being_attacked_by_pawn(void) { // black pawn attacks c4 // black pawn attacks d4 // black pawn attacks e4 // black pawn attacks g4 // black pawn attacks h4 // white pawn attacks e5 // white pawn attacks d5 // white pawn attacks g5 char *test_fen_1 = "r2qkb1r/p1pb1p2/2n2n2/1p1pp1pp/2BPP1QP/BPN2N2/P1P2PP1/R3K2R b KQkq - 1 9"; struct position *pos = allocate_board(); consume_fen_notation(test_fen_1, pos); //print_board(pos); assert_true(TEST_is_pawn_attacking_square(pos, c4, BLACK)); assert_true(TEST_is_pawn_attacking_square(pos, d4, BLACK)); assert_true(TEST_is_pawn_attacking_square(pos, e4, BLACK)); assert_true(TEST_is_pawn_attacking_square(pos, g4, BLACK)); assert_true(TEST_is_pawn_attacking_square(pos, h4, BLACK)); assert_true(TEST_is_pawn_attacking_square(pos, e5, WHITE)); assert_true(TEST_is_pawn_attacking_square(pos, d5, WHITE)); assert_true(TEST_is_pawn_attacking_square(pos, g5, WHITE)); free_board(pos); test_fen_1 = "1nr3n1/ppqNkp2/1R3p2/bQ4PK/1PpRp1PP/P2bBPqp/4P1r1/1B1Q1N2 w - - 0 1"; pos = allocate_board(); consume_fen_notation(test_fen_1, pos); //print_board(pos); assert_true(TEST_is_pawn_attacking_square(pos, b6, BLACK)); assert_true(TEST_is_pawn_attacking_square(pos, f3, BLACK)); assert_true(TEST_is_pawn_attacking_square(pos, a5, WHITE)); assert_true(TEST_is_pawn_attacking_square(pos, d3, WHITE)); assert_true(TEST_is_pawn_attacking_square(pos, f6, WHITE)); assert_true(TEST_is_pawn_attacking_square(pos, d3, WHITE)); assert_true(TEST_is_pawn_attacking_square(pos, e4, WHITE)); free_board(pos); }
static void test_add_bonus(void ** state) { Board b = init_board1(); int nb_snakes = 0; Serpent** snakes = malloc(nb_snakes * sizeof(Serpent*)); add_bonus(&b, snakes, nb_snakes); assert_non_null(b.pTabBonus); assert_int_equal(b.nNbBonus, 1); free_board(b); free_snakes(snakes, nb_snakes); }
static void test_init_board_1v1(void ** state) { Board b = init_board_1v1(); assert_non_null(&b); assert_int_equal(b.nNbPos, 2); assert_int_equal(b.nNbTunnels, 0); assert_int_equal(b.nNbBonus, 0); assert_int_equal(b.nClosingWalls, 0); assert_int_equal(b.nBoardWidth, 2*b.nBoardHeight); free_board(b); }
static void test_add_wall(void ** state) { Board b = init_board1(); int nWalls = b.nSize; int nb_snakes = 0; Serpent** snakes = malloc(nb_snakes * sizeof(Serpent*)); add_wall(&b, snakes, nb_snakes); assert_int_equal(b.nSize, nWalls+1); free_board(b); free_snakes(snakes, nb_snakes); }
int main(int argc, char* argv[]) { board b; if(argc < 2) { print_help(); exit(1); } initscr(); cbreak(); curs_set(0); keypad(stdscr, TRUE); noecho(); if(streq(argv[1], "-d")) { int x = getch(); clear(); mvprintw(0,0,"%i", x); getch(); echo(); curs_set(1); endwin(); exit(0); } if(streq(argv[1], "-e") || streq(argv[1], "--editor")) { b = start_editor(); } else { b = create_board_from_name(argv[1]); } int generation = 0; while(true) { mvprintw(0,0,"Generation: %d", generation); generation++; print_board(1, 0, b); refresh(); board b1 = tick_board(b); free(b); b = b1; long one_second = 1000000; usleep(one_second/6); } free_board(b); echo(); curs_set(1); endwin(); return 0; }
void debug_move(void) { char *test_fen = "2Q3qb/pN3P1p/P4qr1/1KP1BnP1/1p2pPNp/2rkP3/pP3npP/4QbRB w - - 0 1"; struct position *pos = allocate_board(); consume_fen_notation(test_fen, pos); //print_board(pos); assert_true(is_sq_attacked(pos, e6, WHITE) == true); assert_true(is_sq_attacked(pos, g7, WHITE) == false); free_board(pos); }
void test_create_board() { Board *board; board = new_board("test", "Test", 1, 0); assert(board != NULL); assert(strcmp(board->name, "test") == 0); assert(strcmp(board->fullname, "Test") == 0); assert(board->max_threads == 1); assert(board->threads != NULL); assert(board->first_thread == -1); assert(board->last_thread == -1); assert(board->post_count == 0); free_board(board); }
/** * @brief Destroys a game. * @param game the game to destroy * @see new_game(), new_standard_game(), new_game_from_parameters(), new_game_copy() */ void free_game(Game *game) { int i; game->piece_configuration->nb_games--; if (game->piece_configuration->nb_games == 0) { /* free the piece configuration if it is not used anymore */ for (i = 0; i < game->piece_configuration->nb_pieces; i++) { free_piece(&game->piece_configuration->pieces[i]); } FREE(game->piece_configuration->pieces); FREE(game->piece_configuration); } free_board(game->board); FREE(game); }
static void test_add_tunnels(void ** state) { Board b = init_board1(); int nb_snakes = 0; Serpent** snakes = malloc(nb_snakes * sizeof(Serpent*)); add_tunnels(&b, snakes, nb_snakes); assert_non_null(b.pTunnels); int i; for (i = 0; i < b.nNbTunnels; i++) { assert_non_null(b.pTunnels[i]); } assert_int_equal(b.nNbTunnels, i); free_board(b); free_snakes(snakes, nb_snakes); }
END_TEST START_TEST (test_select_command_16) { struct command command_data; command_data.id = 0; my_strcpy( command_data.name, "showboard", MAX_TOKEN_LENGTH ); command_data.gtp_argc = 0; init_board(BOARD_SIZE_DEFAULT); init_known_commands(); select_command(&command_data); fail_unless( get_output_error() == false, "showboard command identified" ); free_board(); }
END_TEST START_TEST (test_fixed_handicap_1) { struct command command_data; command_data.id = 0; my_strcpy( command_data.name, "fixed_handicap", MAX_TOKEN_LENGTH ); my_strcpy( command_data.gtp_argv[0], "9", MAX_TOKEN_LENGTH ); command_data.gtp_argc = 1; init_board(19); init_known_commands(); select_command(&command_data); fail_unless( get_output_error() == false, "fixed_handicap 9 set" ); free_board(); }
static void test_init_walls(void ** state) { Board b = init_board1(); Point p; int i, j; for (i = 0; i < b.nBoardWidth; i++) { for (j = 0; j < b.nBoardHeight; j++) { if (i == 0 || j == 0 || i == b.nBoardWidth-1 || j == b.nBoardHeight-1) { p.x = i; p.y = j; assert_int_equal(belongs_to_tab(p, b.pPtsMur, b.nSize), 1); } } } free_board(b); }
int shift(Board b, Move m) { Board tmp = rotate_for_move(b, m); int this, i, points = 0; int data[BOARDSIZE]; for (int r=0; r<BOARDSIZE; r++) { i = 0; memset(data, 0, sizeof(int)*BOARDSIZE); for (int c=BOARDSIZE-1; c >= 0; c--) { this = bget(tmp, r, c); if (this != 0) { if (data[i] == 0) { data[i] = this; } else if (this == data[i]) { data[i] += this; points += data[i]; i++; } else { i++; data[i] = this; } } } /* for (int x=0; x<=i; x++) printf("%d ", data[x]); printf("| "); for (int x=i+1; x<BOARDSIZE; x++) printf("%d ", data[x]); printf("\n"); */ for (int c=0; c<BOARDSIZE; c++) place(tmp, r, c, data[BOARDSIZE-1-c]); } Board tmp2 = invert_rotate_for_move(tmp, m); free(b->data); b->data = tmp2->data; free_board(tmp); free(tmp2); // DO NOT FREE tmp2->data return points; }
END_TEST START_TEST (test_select_command_9) { struct command command_data; init_board(BOARD_SIZE_MIN); init_known_commands(); init_brains(); command_data.id = 0; my_strcpy( command_data.name, "genmove", MAX_TOKEN_LENGTH ); my_strcpy( command_data.gtp_argv[0], "BLACK", MAX_TOKEN_LENGTH ); command_data.gtp_argc = 1; select_command(&command_data); fail_unless( get_output_error() == false, "genmove command identified" ); free_board(); }
int main() { int **board; int m, n, sx, sy; scanf("%d%d%d%d", &m, &n, &sx, &sy); if (m >= 20 || n >= 20 || sx > m || sy > n) { printf(NO); exit(1); } board = alloc_board(m, n); board[sx - 1][sy - 1] = 1; printf(move(board, m, n, sx - 1, sy - 1, 0) ? YES : NO); free_board(board, m); return 0; }
static void test_handle_tunnels(void ** state) { Board b = init_board1(); int nb_snakes = 1; Serpent** snakes = malloc(nb_snakes * sizeof(Serpent*)); Serpent* snake = malloc(sizeof(Serpent)); Point p; p.x = 3; p.y = 5; init_snake(snake, 1, 50, top, p); snakes[0] = snake; add_tunnels(&b, snakes, nb_snakes); snake->tete.x = b.pTunnels[0]->entree.x; snake->tete.y = b.pTunnels[0]->entree.y; b.pTunnels[0]->nNbSorties = 1; handle_tunnels(snake, &b); assert_int_equal(snake->tete.x, b.pTunnels[0]->sorties[0].x); assert_int_equal(snake->tete.y, b.pTunnels[0]->sorties[0].y); free_board(b); free_snakes(snakes, nb_snakes); }
int main(int argc, char** argv) { check_input_count(argc); int x = parse_param(argv[1]); int y = parse_param(argv[2]); int initial_cells = parse_param(argv[3]); Board* board_struct = initialize_board_struct(x, y, initial_cells); initialize_board(board_struct); printf("------ Initial Board ------\n"); print_board(board_struct); printf("------ ------------- ------\n"); int i = 0; while (board_struct->current_cells > 0 && i < 50000) { printf("------ Step %d ------\n", i + 1); update_board_state(board_struct); print_board(board_struct); i++; } free_board(board_struct); }
static void test_is_cell_free(void ** state) { Board b = init_board1(); int nb_snakes = 1; Serpent** snakes = malloc(nb_snakes * sizeof(Serpent*)); Serpent* snake = malloc(sizeof(Serpent)); Point p; p.x = 3; p.y = 5; init_snake(snake, 1, 50, top, p); snakes[0] = snake; p.x = 10; p.y = 10; assert_int_equal(is_cell_free(p, &b, snakes, nb_snakes), 1); p.x = 0; p.y = 0; assert_int_equal(is_cell_free(p, &b, snakes, nb_snakes), 0); p.x = snake->tete.x; p.y = snake->tete.y; assert_int_equal(is_cell_free(p, &b, snakes, nb_snakes), 0); free_board(b); free_snakes(snakes, nb_snakes); }
void free_game ( struct game * const g ) { if (NULL != g->b) { free_board (g->b); } if (NULL != g->rules) { free_ruleset (g->rules); } if (NULL != g->movehist) { free_listmh (g->movehist); } if (NULL != g->players) { unsigned short i; for (i = (unsigned short)0; i < g->playerc; i++) { if (NULL != g->players[i]) { free_player (g->players[i]); } } free (g->players); /* splint thinks g->rules isn't released "if (NULL != g->players)". */ /*@i2@*/\ } free (g); }