// Returns new position, can have side effects static int8_t ai_action(int8_t current_i, uint8_t t) { int8_t current_x, current_y; to_pos(current_i, ¤t_x, ¤t_y); int8_t player_x, player_y; to_pos(g_player_position_i, &player_x, &player_y); int8_t pdx = player_x - current_x; int8_t pdy = player_y - current_y; uint8_t start_off = 0; if(pdx < -1) start_off = 1; else if(pdx > 1) start_off = 2; else if(pdy > 1) start_off = 3; else if(pdy > -1){ g_hp -= t - SNAKE + 1; return current_i; } for(uint8_t i=start_off; i<start_off+4; i++){ uint8_t actual_i = i % 4; int8_t try_i = current_i + pgm_read_byte(&(possible_moves[actual_i])); if(try_i < 0 || try_i >= MAP_SIZE) continue; if(g_map[try_i] != EMPTY) continue; return try_i; } return current_i; }
int main(int argc, char * argv[]){ Board * board; int x, y; Pos pos; UCTNode * uct_tree = 0; UCTNode * ptr; board_initialize(); uct_initialize(); board = board_new(); while(1) { board_print(board, stdout); printf("\nBlack: "); if(scanf("%d %d", &x, &y) == -1) break; board_play(board, to_pos(x, y), BLACK); board_print(board, stdout); pos = uct_search(board, WHITE); board_play(board, pos, WHITE); } //for(ptr = uct_tree->child; ptr; ptr = ptr->next) // printf("%d ", ptr->move); puts(""); board_destroy(); uct_destroy(); return 0; }
// Returns true if move is valid static uint8_t move_player(int8_t key) { uint8_t x, y; to_pos(g_player_position_i, &x, &y); uint8_t old_x = x; uint8_t old_y = y; switch(key){ case DIR_UP: y--; break; case DIR_DOWN: y++; break; case DIR_LEFT: x--; break; case DIR_RIGHT: x++; break; default: return 0; //no move } g_seed += (x<<4) + y; if(y == 255 || x == 255 || y == MAP_H || x == MAP_W) return 0; // Map boundaries uint8_t i = get_pos(x, y); uint8_t t = g_map[i]; if(t == MOUNTAIN || t == TREE){ return 0; // Can't walk on these tiles } if(t == SNAKE || t == GOBLIN || t == ELLA || t == DRAGON){ // TODO: Require hitting enemy more than once g_map[i] = 0; return 1; } // TODO: Enable this /*// Immediately erase player lcd_locate(2+old_x*8, old_y+1); lcd_print_sprite(0); // Immediately draw player lcd_locate(2+x*8, y+1); lcd_print_sprite(12);*/ g_player_position_i = get_pos(x, y); if(t == STAIRS){ next_level(false); return 0; } if(t == BERRY){ if(g_hp < 254) g_hp+=3; g_map[i] = EMPTY; return 1; } if(t == STONE || t == BUSH) return 2; return 1; }