void test_expectation(Heuristic h, int depth, int *score, int *maxtile) { Game g= init_game(h); if (score != NULL) *score = playExpected2048(g, depth); if (maxtile != NULL) *maxtile = max_tile(g->board); game_free(g); }
void test_minimax(Heuristic h, int *score, int *maxtile) { Game g = init_game(h); if (score != NULL) *score = play2048(g); if (maxtile != NULL) *maxtile = max_tile(g->board); game_free(g); }
int main() { init_curses(); const char *exit_msg = ""; srandom(time(NULL)); struct game_t game = {0}; int last_turn = game.turns; get_highscore_filepath(&game); load_highscore(&game); place_tile(&game); place_tile(&game); while (1) { print_game(&game); if (lose_game(game)) { exit_msg = "lost"; goto lose; } last_turn = game.turns; switch (getch()) { case 'h': case KEY_LEFT: case 'a': move_left(&game); break; case 'j': case KEY_DOWN: case 's': move_down(&game); break; case 'k': case KEY_UP: case 'w': move_up(&game); break; case 'l': case KEY_RIGHT: case 'd': move_right(&game);break; case 'q': exit_msg = "quit"; goto end; } if (last_turn != game.turns) place_tile(&game); } lose: move(7, 0); printw("You lose! Press q to quit."); while (getch() != 'q'); end: endwin(); if(game.score > game.highscore) { game.highscore = game.score; save_highscore(&game); } printf("You %s after scoring %d points in %d turns, " "with largest tile %d. The local highscore is %d points.\n", exit_msg, game.score, game.turns, 1 << max_tile((tile_t *)game.board), game.highscore); free(game.highscorefile); return 0; }
void test_random(int *score, int *maxtile) { printf("Playing random...\n"); Game g = init_game(squaresum_heuristic); PointList pl = open_spaces(g->board); while (!pl_empty(pl)) { pl_free(pl); make_move(g, (Move)randint(4)); pl = open_spaces(g->board); } pl_free(pl); *score = g->score; *maxtile = max_tile(g->board); game_free(g); }
int main() { init_curses(); const char *exit_msg = ""; srand(time(NULL)); struct game_t game = {0}; int last_turn = game.turns; place_tile(&game); place_tile(&game); while (1) { print_game(&game); if (lose_game(game)) { exit_msg = "lost"; goto lose; } last_turn = game.turns; switch (getch()) { case 'h': case KEY_LEFT: move_left(&game); break; case 'j': case KEY_DOWN: move_down(&game); break; case 'k': case KEY_UP: move_up(&game); break; case 'l': case KEY_RIGHT: move_right(&game); break; case 'q': exit_msg = "quit"; goto end; } if (last_turn != game.turns) place_tile(&game); } lose: move(7, 0); printw("You lose! Press q to quit."); while (getch() != 'q'); end: endwin(); printf("You %s after scoring %d points in %d turns, " "with largest tile %d\n", exit_msg, game.score, game.turns, 1 << max_tile((tile_t *)game.board)); return 0; }
double evaluation_function(int* board) { if (is_impossible(board)) { return INT_MIN; } if(heuristic == 1) { return min_possibility(board); } return ( (smoothness(board) * smoothness_constant) + (max_tile(board) * max_tile_constant) + (log(free_tiles(board)) * free_tiles_constant) + (max_placement(board) * max_placement_constant) + (monotonicity(board) * monotonicity_constant) ); }
double min_possibility(int* board) { int modifier[] = { // Top left corner 15, 14, 13, 12, 8, 9, 10, 11, 7, 6, 5, 4, 0, 1, 2, 3 }; int sub = 15 - max_tile(board); int min_possibility = INT_MAX; int sum = 0; for (int i = 0; i < 16; i++) { if (modifier[i] - sub >= 0) { modifier[i] = sub - modifier[i]; } if (board[i] == 0) { modifier[i] = 0; } else { modifier[i] = (modifier[i] + board[i]) * (16 - modifier[i]) * board[i] * board[i]; } sum = sum + abs(modifier[i]); } if (sum < min_possibility) { min_possibility = sum; } return (-(float)min_possibility) + ((float)free_tiles(board) * (float)free_tiles(board) * (float)free_tiles(board) * 5.15); }
int main() { init_curses(); const char *exit_msg = ""; struct game_t game = { 0 }; int last_turn = game.turns; place_tile(&game); place_tile(&game); gotoxy(0, 7); printf("Press ENTER to quit."); int cur_key; while (1) { print_game(&game); if (lose_game(game)) { exit_msg = "lost"; goto lose; } last_turn = game.turns; switch (cur_key = keyb_getkey(1)) { case 22: move_left(&game); break; case 38: move_down(&game); break; case 6: move_up(&game); break; case 54: move_right(&game); break; case 96: exit_msg = "quit"; goto end; } if (last_turn != game.turns) place_tile(&game); } lose: gotoxy(0, 7); printf("You lose! Press ENTER to quit."); while (keyb_getkey(1) != 96); end: gotoxy(0, 7); printf("You %s after scoring %d points in %d turns, " "with largest tile %d\n", exit_msg, game.score, game.turns, 1 << max_tile((tile_t *)game.board)); sys_sleep(3000); return 0; }