void set_mine(field_t *f, int x, int y) { assert(!(x < 0 || x >= f->width)); assert(!(y < 0 || y >= f->height)); square_t *s = get_square(f, x, y); s->type = MINED; int i, j; for (j = y-1; j <= y+1; j++) { if (j < 0 || j >= f->height) { continue; } for (i = x-1; i <= x+1; i++) { if (i < 0 || i >= f->width) { continue; } if ( (i == x) && (j == y) ) { continue; } s = get_square(f, i, j); s->mines += 1; } } }
double primary() { Token t = ts.get(); switch (t.kind) { case '(': { double d = expression(); t = ts.get(); if (t.kind != ')') error("'(' expected"); return d; } case sqrtsymb: return get_square(); case powsymb: return get_pow(); case '-': return - primary(); case name: { t2 = ts.get(); cin >> if(t.kind == '=') return set_value(); return get_value(t.name); } case '+': return primary(); case number: return t.value; default: error("primary expected"); } }
static guint create_app (void) { GtkWidget *canvas, *window, *box, *hbox, *control; window = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK(on_destroy), NULL); gtk_widget_set_usize (GTK_WIDGET(window), 400, 300); box = gtk_vbox_new (FALSE, 2); gtk_container_add(GTK_CONTAINER(window), box); canvas = mate_canvas_new(); gtk_box_pack_start_defaults (GTK_BOX (box), canvas); get_square(mate_canvas_root(MATE_CANVAS(canvas))); get_circle(mate_canvas_root(MATE_CANVAS(canvas))); hbox = gtk_hbox_new(FALSE, 2); gtk_box_pack_start (GTK_BOX (box), hbox, FALSE, FALSE, 0); control = matecomponent_widget_new_control("OAFIID:Circle_Controller", NULL); gtk_box_pack_start (GTK_BOX (hbox), control, FALSE, FALSE, 0); control = matecomponent_widget_new_control("OAFIID:Square_Controller", NULL); gtk_box_pack_start (GTK_BOX (hbox), control, FALSE, FALSE, 0); gtk_widget_show_all (GTK_WIDGET(window)); return FALSE; }
void print_field(field_t *f, int debug) { printf("%s", "\n"); int width = f->width; int height = f->height; int i, j; square_t *s; printf(" "); for (i = 0; i < width; i++) { printf(" %d ", i); } printf("\n\n"); for (j = 0; j < height; j++) { printf("%d ", j); for (i = 0; i < width; i++) { s = get_square(f, i, j); if (s->status == HIDDEN && !debug) { printf(" # "); } else if (s->status == REVEALED || debug) { if (s->type == MINED) { printf(" * "); } else { printf(" %d ", s->mines); } } } printf("\n"); } printf("\n"); }
void pick_up(void){ struct itemlist* i = get_square(player.x, player.y, player.ele)->i; if(!i){ putmsg("There is nothing to pick up here."); } else { // TODO what would you like to pick up? blabla } }
void disperse(t_data *data, t_spell *spell) { srand(time(NULL)); disperse_linemate(data, spell); disperse_deraumere(data, spell); disperse_sibur(data, spell); disperse_mendiane(data, spell); disperse_phiras(data, spell); disperse_thystame(data, spell); gui_broadcast(data, gui_bct, get_square(data, spell->x, spell->y)); }
void destroy_field(field_t *f) { int i, j; square_t *s; for (j = 0; j < f->height; j++) { for (i = 0; i < f->width; i++) { s = get_square(f, i, j); free(s); } } free(f->field); free(f); }
static void disperse_thystame(t_data *data, t_spell *spell) { int i; t_square *target; i = data->spell_tab[spell->level - 1][6]; while (i--) { target = get_square(data, rand() % data->x, rand() % data->y); target->thystame += 1; gui_broadcast(data, gui_bct, target); data->map[spell->x][spell->y].thystame -= 1; } }
int check_square(field_t *f, int x, int y) { assert(!(x < 0 || x >= f->width)); assert(!(y < 0 || y >= f->height)); square_t *s = get_square(f, x, y); s->status = REVEALED; if (s->type == MINED) { return MINE_FOUND; } else { return NO_MINE_FOUND; } }
int spell_check(t_data *data, t_player *player, int status) { int player_nb; t_square *square; square = get_square(data, player->x, player->y); player_nb = get_player_nb(data, player, status); if (player_nb >= data->spell_tab[player->level - 1][0] && square->linemate >= data->spell_tab[player->level - 1][1] && square->deraumere >= data->spell_tab[player->level - 1][2] && square->sibur >= data->spell_tab[player->level - 1][3] && square->mendiane >= data->spell_tab[player->level - 1][4] && square->phiras >= data->spell_tab[player->level - 1][5] && square->thystame >= data->spell_tab[player->level - 1][6]) return (1); else return (0); }
int main() { initialize(); for (int i = 32; i < 100; ++i) { int n1 = get_square(i); for (int n3 = lower_bound; n3 < upper_bound; ++n3) { int mask = square; int forbidden = mask; if (!update_mask2(&mask, n3, 2, &forbidden)) { continue; } int n2 = merge(n1, n3); if (!update_mask2(&mask, n2, 3, &forbidden)) { continue; } for (int n5 = lower_bound; n5 < upper_bound; ++n5) { int inter_mask = mask; int inter_forbidden = forbidden; if (!update_mask2(&inter_mask, n5, 4, &inter_forbidden)) { continue; } int n4 = merge(n3, n5); if (!update_mask2(&inter_mask, n4, 5, &inter_forbidden)) { continue; } int n6 = merge(n5, n1); if (!update_mask2(&inter_mask, n6, 6, &inter_forbidden)) { continue; } std::cout << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << std::endl; std::cout << n1 + n2 + n3 + n4 + n5 + n6 << std::endl; } } } return 0; }
int ser_prend(t_env *env, t_fd *fd, char *cmd) { t_square *sq; int quantity_sq; t_resource res; sq = get_square(env, fd->trantor.pos_x, fd->trantor.pos_y); res = str_to_resource(cmd + 6); quantity_sq = nb_res_in_inventory(&sq->content, res); if (quantity_sq < 1) { send_cmd_to_client(fd, MSG_KO); return (-1); } add_resource(&fd->trantor.inventory, res); del_resource(&sq->content, res); send_infos(env, fd, res); return (0); }
int ser_incantation(t_env *env, t_fd *fd, char *cmd) { t_square *sq; t_trantorian *trantor; t_list *trantors_fd; t_incantation incant; trantor = &fd->trantor; sq = get_square(env, trantor->pos_x, trantor->pos_y); incant = incantation_to_evolve(trantor->level); trantors_fd = get_lst_trantor(env, trantor, incant.players); if (test_incantation_feasability(trantor, &incant, fd, trantors_fd) == -1) return (-1); printf("new incantation for level %d\n", incant.beg_level); modify_trantor(env, trantors_fd, trantor, &incant); test_for_victory(env); gfx_pic(env, trantors_fd); gfx_pie(env, trantor->pos_x, trantor->pos_y); return (0); (void)cmd; }
int Alex_Ayerdi::free_neighbors(int i, int j) { int count = 0; // examine the 8 possible neighborings unless not possible positions if ((i+1)>0 && j>0 && (i+1)<9 && j<9 && get_square(i+1, j) == 0) count++; if ((i+1)>0 && (j-1)>0 && (i+1)<9 && (j-1)<9 && get_square(i+1, j-1) == 0) count++; if (i>0 && (j-1)>0 && i<9 && (j-1)<9 && get_square(i, j-1) == 0) count++; if ((i-1)>0 && (j-1)>0 && (i-1)<9 && (j-1)<9 && get_square(i-1, j-1) == 0) count++; if ((i-1)>0 && j>0 && (i-1)<9 && j<9 && get_square(i-1, j) == 0) count++; if ((i-1)>0 && (j+1)>0 && (i-1)<9 && (j+1)<9 && get_square(i-1, j+1) == 0) count++; if (i>0 && (j+1)>0 && i<9 && (j+1)<9 && get_square(i, j+1) == 0) count++; if ((i+1)>0 && (j+1)>0 && (i+1)<9 && (j+1)<9 && get_square(i+1, j+1) == 0) count++; return count; }
void target_based_agent::target_square(vector<vector<int> > &screen, loc amidar_loc) { loc target_loc = nearest_unpainted_loc(screen, amidar_loc); // cout << "Target found: " << target_loc.first << ", " << target_loc.second << endl; pair<loc, loc> edge = get_edge(target_loc); vector<loc> square_vec = get_square(edge); if (!square_vec.empty()) { // cout << "Part of a square" << endl; int least_index = -1; double least_dist = 10000; for (int i = 0; i < square_vec.size(); ++i) { double dist = euclidean_distance(square_vec[i], amidar_loc); if (dist < least_dist) { least_dist = dist; least_index = i; } } int left_index = least_index - 1, right_index = least_index + 1; if (least_index == 0) left_index = square_vec.size() - 1; else if (least_index == square_vec.size() - 1) right_index = 0; bool right_edge_painted = edge_painted(make_pair(square_vec[least_index], square_vec[right_index]), screen); bool left_edge_painted = edge_painted(make_pair(square_vec[least_index], square_vec[least_index]), screen); bool next_right_edge_painted = edge_painted(make_pair(square_vec[right_index], square_vec[(right_index + 1) % square_vec.size()]), screen); bool next_left_edge_painted = edge_painted(make_pair(square_vec[left_index], square_vec[(right_index + 1) % square_vec.size()]), screen); if (right_edge_painted) { if (left_edge_painted) { // cout << "both left and right edges painted" << endl; if (next_right_edge_painted) { // 3 edges painted targets.push(square_vec[left_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); sleep(10); return; } else if (next_left_edge_painted) { // 3 edges painted targets.push(square_vec[right_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); return; } else { double right_edge_length = euclidean_distance(square_vec[least_index], square_vec[right_index]); double left_edge_length = euclidean_distance(square_vec[least_index], square_vec[left_index]); if (right_edge_length < left_edge_length) { /*targets.push(square_vec[least_index]);*/ targets.push(square_vec[right_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); targets.push(square_vec[left_index]); return; } else { /*targets.push(square_vec[least_index]);*/ targets.push(square_vec[left_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); targets.push(square_vec[right_index]); return; } } } else { if (next_right_edge_painted) { if (next_left_edge_painted) { targets.push(square_vec[least_index]); targets.push(square_vec[left_index]); return; } else { targets.push(square_vec[least_index]); targets.push(square_vec[left_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); return; } } else { targets.push(square_vec[least_index]); targets.push(square_vec[left_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); targets.push(square_vec[right_index]); return; } } } else { if (left_edge_painted) { if (next_right_edge_painted) { if (next_left_edge_painted) { targets.push(square_vec[least_index]); targets.push(square_vec[right_index]); return; } else { targets.push(square_vec[least_index]); targets.push(square_vec[right_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); targets.push(square_vec[left_index]); return; } } else { if (next_left_edge_painted) { targets.push(square_vec[least_index]); targets.push(square_vec[right_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); return; } else { targets.push(square_vec[least_index]); targets.push(square_vec[left_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); targets.push(square_vec[right_index]); return; } } } else { targets.push(square_vec[least_index]); targets.push(square_vec[right_index]); targets.push(square_vec[(right_index + 1) % square_vec.size()]); targets.push(square_vec[left_index]); targets.push(square_vec[least_index]); return; } } } else { if (euclidean_distance(edge.first, amidar_loc) < euclidean_distance(edge.second, amidar_loc)) { targets.push(edge.first); targets.push(edge.second); } else { targets.push(edge.second); targets.push(edge.first); } } }
int Alex_Ayerdi::eval(int cpuval) { // originally used score, but it led to bad ai // instead we evaluate based maximizing the // difference between computer's available move count // and the player's. Additionally, corners will be // considered as specially beneficial since they cannot ever be // flipped. int score = 0; // evaluation score // count available moves for computer and player int mc = 0; int mp = 0; for (int i=1; i<9;i++) { for (int j=1; j<9; j++) { if (move_is_valid(i, j, cpuval)) mc++; if (move_is_valid(i, j, -1*cpuval)) mp++; } } // add the difference to score (scaled) score += 20*(mc - mp); // the number is just some scale determined through playing //score += 7*mc; /* // additionally, if mp is 0 this is real good so add some more points, // and if mc is 0 this is real bad so subtract more points // because of skipped turns if (mp == 0) score += 50; if (mc == 0) score -= 50; */ // count corners for computer and player int cc = 0; int cp = 0; if (get_square(1, 1) == cpuval) cc++; else if (get_square(1, 1) == -1*cpuval) cp++; if (get_square(1, 8) == cpuval) cc++; else if (get_square(1, 8) == -1*cpuval) cp++; if (get_square(8, 1) == cpuval) cc++; else if (get_square(8, 1) == -1*cpuval) cp++; if (get_square(8, 8) == cpuval) cc++; else if (get_square(8, 8) == -1*cpuval) cp++; // add the difference to score (scaled) score += 200*(cc - cp); /* // squares adjacent to corners on edges also useful, but not as much since it could lead to a corner int ac = 0; int ap = 0; if (get_square(1, 2) == cpuval) ac++; else if (get_square(1, 2) == -1*cpuval) ap++; if (get_square(2, 1) == cpuval) ac++; else if (get_square(2, 1) == -1*cpuval) ap++; if (get_square(1, 7) == cpuval) ac++; else if (get_square(1, 7) == -1*cpuval) ap++; if (get_square(2, 8) == cpuval) ac++; else if (get_square(2, 8) == -1*cpuval) ap++; if (get_square(7, 1) == cpuval) ac++; else if (get_square(7, 1) == -1*cpuval) ap++; if (get_square(8, 2) == cpuval) ac++; else if (get_square(8, 2) == -1*cpuval) ap++; if (get_square(7, 8) == cpuval) ac++; else if (get_square(7, 8) == -1*cpuval) ap++; if (get_square(8, 7) == cpuval) ac++; else if (get_square(8, 7) == -1*cpuval) ap++; score += 30*(ac - ap); // scale so bigger depths are worth more score += 10*depth; */ // limit the amount of space around our pieces so we don't surround as much (which leads to big gains endgame for opponent) int sc = 0; int sp = 0; // counts for open spaces neighboring a player/comp's pieces for (int i=1; i<9;i++) { for (int j=1; j<9; j++) { if (get_square(i, j) == cpuval) { //add count to sc sc += free_neighbors(i, j); } if (get_square(i, j) == -1*cpuval) { //add count to sp sp += free_neighbors(i, j); } } } score -= 10*(sc - sp); // subtract because we are trying to minimize it return score; }