int max(t_env *e, int p, int depth) { int i; int v; int vmax; i = -1; if (depth == 0 && check_win(e, p) == 0) return (evaluate(e)); else if (check_win(e, p) == 1) return (MAX_VALUE - (MAX_DEPTH - depth) * 10); else { vmax = -MAX_VALUE; while (++i < e->w) { if (e->board[0][i] == '.') { board_insert(e, i, p); v = min(e, p % 2 + 1, depth - 1); vmax = (v > vmax ? v : vmax); board_delete(e, i); } } } return (vmax); }
int minimax(t_env *e, int depth) { int i; int v; int bestvalue; int move; i = -1; bestvalue = -MAX_VALUE; while (++i < e->w) { if (e->board[0][i] == '.') { board_insert(e, i, 2); v = min(e, 1, depth); if (v > bestvalue) { bestvalue = v; move = i; } board_delete(e, i); } } move = displace(e, bestvalue, move); return (move); }
Test(Player, MoveUp) { player_t *pl = player_create_at((vector2d_t){12, 12}); board_t *bd = board_create(24, 24); pl->p_dir = (vector2d_t){0, -1}; cr_assert_eq(pl->p_pos.v_y, 12); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 11); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 10); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 9); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 8); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 7); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 6); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 5); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 4); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 3); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 2); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 1); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 0); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 23, "got %d", pl->p_pos.v_y); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 22); cr_assert_eq(pl->p_pos.v_x, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_y, 21); cr_assert_eq(pl->p_pos.v_x, 12); board_delete(bd); player_delete(pl); }
void test_board_get_square(void) { Board *b; b = board_new(1LLU, 2LLU); CU_ASSERT_EQUAL(board_get_square(b, A1), BLACK_SQUARE); CU_ASSERT_EQUAL(board_get_square(b, B1), WHITE_SQUARE); CU_ASSERT_EQUAL(board_get_square(b, C1), EMPTY_SQUARE); b = board_delete(b); }
void test_board_count_pieces(void) { Board *b; b = board_new(1LLU, 2LLU); CU_ASSERT_EQUAL(board_count_pieces(b, BLACK_SQUARE), 1); CU_ASSERT_EQUAL(board_count_pieces(b, WHITE_SQUARE), 1); CU_ASSERT_EQUAL(board_count_pieces(b, EMPTY_SQUARE), 62); b = board_delete(b); }
Test(Player, Move) { player_t *pl = player_create_at((vector2d_t){12, 12}); board_t *bd = board_create(24, 24); cr_assert_eq(pl->p_pos.v_x, 12); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 13); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 14); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 15); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 16); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 17); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 18); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 19); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 20); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 21); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 22); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 23); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 0); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 1); cr_assert_eq(pl->p_pos.v_y, 12); player_move_foward(pl, bd); cr_assert_eq(pl->p_pos.v_x, 2); cr_assert_eq(pl->p_pos.v_y, 12); board_delete(bd); player_delete(pl); }
int main(int argc, char** argv){ int i; int j; int* score = (int*) malloc(sizeof(int)); if (SDL_Init(SDL_INIT_EVERYTHING) == -1){ printf("SDL_Init: %s\n", SDL_GetError()); return 1; } // Unicode support for hiscore table SDL_EnableUNICODE(1); if(TTF_Init()==-1) { printf("TTF_Init: %s\n", TTF_GetError()); return 1; } SDL_Surface* screen = SDL_SetVideoMode(608, 640, 32, SDL_SWSURFACE); SDL_WM_SetCaption("martet", "martet"); SDL_Surface* board = create_surface(320, 640); SDL_Surface* border = load_image("../data/tetborder.png"); draw_surface(321, 0, border, screen, NULL); draw_text(390, 35, screen, "Next tetromino:", 255, 255, 255); draw_text(330, 200, screen, "Score: ", 255, 255, 255); // create menu struct Menu* menu = menu_create(); menu_addelement(menu, "Start Game"); menu_addelement(menu, "Quit"); menu->active_element = &menu->elements[0]; menu->active_element->active = 1; bool running = true; while (running){ if (menu_martet(screen, board, menu, score) == KEYEVENT_EXIT) break; // fill the board with blocks on game over for (i = 0; i < BOARD_HEIGHT; i++) { char* line = get_board_line(i); for (j = 0; j < BOARD_WIDTH; j++) { line[j] = rand() % 7 + '0'; } draw_board(board); draw_surface(0, 0, board, screen, NULL); SDL_Flip(screen); SDL_Delay(50); } manage_hiscores(screen, board, score); board_delete(); } menu_destroy(menu); SDL_FreeSurface(border); SDL_FreeSurface(board); free(score); SDL_Quit(); return 0; }
void test_board_count_difference(void) { Board *b; SquareSet blacks, whites; blacks = 0xFFFFFFFFFFFFFFFFULL; whites = 0x0000000000000000ULL; b = board_new(blacks, whites); CU_ASSERT_EQUAL(board_count_difference(b, BLACK_PLAYER), 64); CU_ASSERT_EQUAL(board_count_difference(b, WHITE_PLAYER), -64); b = board_delete(b); }
void test_board_new(void) { SquareSet b, w; Board *empty_board; b = 0LLU; w = 0LLU; empty_board = board_new(b, w); CU_ASSERT_PTR_NOT_NULL(empty_board); empty_board = board_delete(empty_board); CU_ASSERT_PTR_NULL(empty_board) }
void test_board_compare(void) { Board *a, *b; a = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL); b = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), 0); a = board_delete(a); b = board_delete(b); a = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL); b = board_new(0x0000000000000000ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), +1); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000000ULL, 0x0000000000000000ULL); b = board_new(0x0000000000000001ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), -1); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); b = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), 0); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000007ULL, 0x0100000000000000ULL); b = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), +1); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); b = board_new(0x0000000000000007ULL, 0x0100000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), -1); a = board_delete(a); b = board_delete(b); }