Example #1
0
bool DnDController::add_tile_cmd (const CommandParamList& params)
{
  if (params.verify_signature ("iis")) {
    quint16 x = (quint16)params.get_param (0)->get_int ();
    quint16 y = (quint16)params.get_param (1)->get_int ();
    QString img = params.get_param (2)->get_str ().c_str ();

    add_tile (Tile::TILE_IMAGE, x, y, 0, 0, img);
  }
  else if (params.verify_signature ("iiiis")) {
    quint16 x = (quint16)params.get_param (0)->get_int ();
    quint16 y = (quint16)params.get_param (1)->get_int ();
    quint16 w = (quint16)params.get_param (2)->get_int ();
    quint16 h = (quint16)params.get_param (3)->get_int ();
    QString txt = params.get_param (4)->get_str ().c_str ();

    add_tile (Tile::TILE_CUSTOM, x, y, w, h, txt);
  }
  else {
    qDebug () << "Invalid add_tile params";
    return false;
  }

  return true;
}
Example #2
0
void init_board(int board[SIZE][SIZE]) {
  for (int i = 0; i < SIZE; i++) {
    for (int j = 0; j < SIZE; j++) {
      board[i][j] = 0;
    }
  }
  add_tile(board);
  add_tile(board);
}
static void i_fg(void)
{
    size_t sz_is, sz_pts;
    int x, y, w, h;
    const bool *m;
    INDEX_T *is = NULL;
    v2f_t *scrpts = NULL;
    v2f_t *texpts = NULL;
    w = e.levels[e.i].bmp.w;
    h = e.levels[e.i].bmp.h;
    m = e.levels[e.i].bmp.m;
    er.fg.no_indices = 0;
    er.fg.tex = er.fg_tex;
    er.fg.scl.x = 2.0f / (float) w;
    er.fg.scl.y = 2.0f / (float) h;
    er.fg.rot.x = 0; /* sin(0) = 0 */
    er.fg.rot.y = 1; /* cos(0) = 1 */
    er.fg.mov.x = er.fg.mov.y = -1;
    for (y = 0; y < h; ++y)
        for (x = 0; x < w; ++x)
            add_tile(x, y, w, m, &er.fg.no_indices, &is, &scrpts, &texpts);
    add_dest(&er.fg.no_indices, &is, &scrpts, &texpts);
    sz_is = er.fg.no_indices * sizeof(*is);
    sz_pts= er.fg.no_indices / 6 * 4 * sizeof(*scrpts);
    er.fg.indices= mk_vbo(GL_ELEMENT_ARRAY_BUFFER, sz_is, is, GL_STATIC_DRAW);
    er.fg.scrpts = mk_vbo(GL_ARRAY_BUFFER, sz_pts, scrpts, GL_STATIC_DRAW);
    er.fg.texpts = mk_vbo(GL_ARRAY_BUFFER, sz_pts, texpts, GL_STATIC_DRAW);
    free(texpts);
    free(scrpts);
    free(is);
}
Example #4
0
unsigned int send_add_tile(unsigned int i, int x, int y, int w, int h)
{
    send_event(EVENT_ADD_TILE);
    send_index(i);
    send_value(x);
    send_value(y);
    send_value(w);
    send_value(h);

    return add_tile(i, x, y, w, h);
}
Example #5
0
void recv_add_tile(void)
{
    unsigned int i = recv_index();

    int x = recv_value();
    int y = recv_value();
    int w = recv_value();
    int h = recv_value();

    add_tile(i, x, y, w, h);
}
Example #6
0
void MapEntities::add_entity(MapEntity* entity) {
	if(entity == NULL) {
		return;
	}
	if(entity->is_tile()) {
		add_tile(static_cast<Tile*> (entity));
	} else {
		if(entity->is_detector()) {
			std::cout << entity->get_entity_type() << std::endl;
			this->detector_entities.push_back(static_cast<Detector*>(entity));
		}	
		if(entity->is_obstacle()) {
			std::cout << entity->get_entity_type() << std::endl;
			this->obstacle_entities.push_back(entity);
		}
	}
	this->all_entities.push_back(entity);

	entity->set_map(map);
}
int main()
{
  grid testG = new_grid();
  printf("grid created\n");

  add_tile(testG);
  
  bool isGameOver = game_over(testG);
  
  if(isGameOver) // check if an empty grid is a gameover
    {
      return EXIT_FAILURE;
    }
  else
    printf("game not over with an empty grid\n");

  // FILLING THE GRID WITH UNIQUE VALUES
  int k = 1;
  for(int i = 0; i < GRID_SIDE; i++)
    {
      for(int j = 0; j < GRID_SIDE; j++)
	{
	  set_tile(testG, i, j, (tile)k);
	  printf("[%d][%d]:%d\n", i, j, get_tile(testG, i, j));
	  k++;
	}
    }

  isGameOver = game_over(testG);
  if(!isGameOver)
    return EXIT_FAILURE; // check if it's game over with a grid filled with unique values
  else
    printf("gameover with a grid filled with unique values\n");
  
  delete_grid(testG);
  printf("grid deleted\n");

  return EXIT_SUCCESS;
}
Example #8
0
int main(void) {
  int board[SIZE][SIZE] = { 0 };
  int empty_spot[2];

  // Ensure rand() returns random value.
  srand(time(NULL));

  init_board(board);
  print_board(board);
  printf("Up: 'k' Down: 'j' Left: 'h' Right: 'l'\n");

  while(1) {
    if (has_move_left(board)) {
      perform_user_action(board);
      if (!board_full(board)) add_tile(board);
      print_board(board);
    } else {
      print_board(board);
      printf("You're all done! Loading new game.\n");
      init_board(board);
    }
  }
}
Example #9
0
/*-
 * Add a forced tile to a given forced vertex.  Basically an easy job,
 * since we know what to add.  But it might fail if adding the tile
 * would cause some untiled area to become enclosed.  There is also another
 * more exotic culprit: we might have a dislocation.  Fortunately, they
 * are very rare (the PRL article reported that perfect tilings of over
 * 2^50 tiles had been generated).  There is a version of the algorithm
 * that doesn't produce dislocations, but it's a lot hairier than the
 * simpler version I used.
 */
static int
add_forced_tile(ModeInfo * mi, forced_node_c * node)
{
	tiling_c   *tp = &tilings[MI_SCREEN(mi)];
	unsigned    side;
	vertex_type_c vtype;
	rule_match_c hits[MAX_TILES_PER_VERTEX * N_VERTEX_RULES];
	int         n;

	if (node->forced_sides == (S_LEFT | S_RIGHT))
		side = NRAND(2) ? S_LEFT : S_RIGHT;
	else
		side = node->forced_sides;
	n = match_rules(node->vertex, hits, True);
	n = find_completions(node->vertex, hits, n, side, &vtype /*, True */ );
	if (n <= 0) {
		tp->done = True;
		if (MI_IS_VERBOSE(mi)) {
			(void) fprintf(stderr, "Weirdness in add_forced_tile()\n");
			(void) fprintf(stderr, "n = %d\n", n);
		}
	}
	return add_tile(mi, node->vertex, side, vtype);
}
Example #10
0
void map_fragment::add_tiles(const gamemap& map, const std::set<map_location>& locs)
{
	BOOST_FOREACH(const map_location& loc, locs) {
		add_tile(map, loc);
	}
Example #11
0
void map_fragment::add_tiles(const gamemap& map, const std::set<map_location>& locs)
{
	for (const map_location& loc : locs) {
		add_tile(map, loc);
	}
}
Example #12
0
void play_game()
{
	board *b = &grid;

	do {

		switch (os_wait_for_key()) {
			case ESC_KEY:
				return;

			case UP_KEY:
				if (can_move_up(b)) {
					shift_up(b);
					merge_up(b);
					shift_up(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			case DOWN_KEY:
				if (can_move_down(b)) {
					shift_down(b);
					merge_down(b);
					shift_down(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			case LEFT_KEY:
				if (can_move_left(b)) {
					shift_left(b);
					merge_left(b);
					shift_left(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			case RIGHT_KEY:
				if (can_move_right(b)) {
					shift_right(b);
					merge_right(b);
					shift_right(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			default:
				continue;
		}

		add_tile(b);
		display_board(b);

	} while (!game_is_lost());
}
Example #13
0
void sync_display(void)
{
#ifdef CONF_MPI
    MPI_Status stat;
    int        size = 0;
#endif
    int        rank = 0;

    char name[MAXNAME];
    int  i = 0;

    gethostname(name, MAXNAME);

#ifdef CONF_MPI

    assert_mpi(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
    assert_mpi(MPI_Comm_size(MPI_COMM_WORLD, &size));

    if (rank)
    {
        /* Send the name to the root, recieve the host index. */

        MPI_Send(name, MAXNAME, MPI_BYTE, 0,0, MPI_COMM_WORLD);
        MPI_Recv(&i,   4,       MPI_BYTE, 0,0, MPI_COMM_WORLD, &stat);
    }
    else
    {
        int j, k;

        /* Find a host definition for the root. */

        i = find_display(name);

        /* Recieve a name from each client, send a host definition index. */

        for (j = 1; j < size; ++j)
        {
            MPI_Recv(name, MAXNAME, MPI_BYTE, j,0, MPI_COMM_WORLD, &stat);
            k = find_display(name);
            MPI_Send(&k,   4,       MPI_BYTE, j,0, MPI_COMM_WORLD);
        }
    }
#else

    i = find_display(name);

#endif

    /* If no host definition was found, create a default. */

    if (i == 0)
    {
        i = add_host(DEFAULT_NAME, DEFAULT_X, DEFAULT_Y, DEFAULT_W, DEFAULT_H);
            add_tile(i,            DEFAULT_X, DEFAULT_Y, DEFAULT_W, DEFAULT_H);

        host[i].flags = HOST_FRAMED;
    }

    /* Note the indexed host definition as current. */

    current_host = host[i];

    /* Position the server window, if necessary. */

    if (rank || (current_host.flags & HOST_FRAMED) == 0)
        set_window_pos(current_host.win_x, current_host.win_y);
}
Example #14
0
/* One step of the growth algorithm. */
void
draw_penrose(ModeInfo * mi)
{
	int         i = 0, n;
	forced_node_c *p;
	tiling_c   *tp;

	if (tilings == NULL)
		return;
	tp = &tilings[MI_SCREEN(mi)];
	if (tp->fringe.nodes == NULL)
		return;

	MI_IS_DRAWN(mi) = True;
	p = tp->forced.first;
	if (tp->busyLoop > 0) {
		tp->busyLoop--;
		return;
	}
	if (tp->done || tp->failures >= 100) {
		init_penrose(mi);
		return;
	}
	/* Check for the initial "2-gon". */
	if (tp->fringe.nodes->prev == tp->fringe.nodes->next) {
		vertex_type_c vtype = (unsigned char) (VT_TOTAL_MASK & LRAND());

		MI_CLEARWINDOW(mi);

		if (!add_tile(mi, tp->fringe.nodes, S_LEFT, vtype))
			free_penrose(tp);
		return;
	}
	/* No visible nodes left. */
	if (tp->fringe.n_nodes == 0) {
		tp->done = True;
		tp->busyLoop = COMPLETION;	/* Just finished drawing */
		return;
	}
	if (tp->forced.n_visible > 0 && tp->failures < 10) {
		n = NRAND(tp->forced.n_visible);
		for (;;) {
			while (p->vertex->off_screen)
				p = p->next;
			if (i++ < n)
				p = p->next;
			else
				break;
		}
	} else if (tp->forced.n_nodes > 0) {
		n = NRAND(tp->forced.n_nodes);
		while (i++ < n)
			p = p->next;
	} else {
		fringe_node_c *fringe_p = tp->fringe.nodes;

		n = NRAND(tp->fringe.n_nodes);
		i = 0;
		for (; i <= n; i++)
			do {
				fringe_p = fringe_p->next;
			} while (fringe_p->off_screen);
		add_random_tile(fringe_p, mi);
		tp->failures = 0;
		return;
	}
	if (add_forced_tile(mi, p))
		tp->failures = 0;
	else
		tp->failures++;
}
Example #15
0
/*-
 * Add a randomly chosen tile to a given vertex.  This requires more checking
 * as we must make sure the new tile conforms to the vertex rules at every
 * vertex it touches. */
static void
add_random_tile(fringe_node_c * vertex, ModeInfo * mi)
{
	fringe_node_c *right, *left, *far;
	int         i, j, n, n_hits, n_good;
	unsigned    side, fc, no_good, s;
	vertex_type_c vtypes[MAX_COMPL];
	rule_match_c hits[MAX_TILES_PER_VERTEX * N_VERTEX_RULES];
	tiling_c   *tp = &tilings[MI_SCREEN(mi)];

	if (MI_NPIXELS(mi) > 2) {
		tp->thick_color = NRAND(MI_NPIXELS(mi));
		/* Insure good contrast */
		tp->thin_color = (NRAND(2 * MI_NPIXELS(mi) / 3) + tp->thick_color +
				  MI_NPIXELS(mi) / 6) % MI_NPIXELS(mi);
	} else {
		unsigned long temp = tp->thick_color;

		tp->thick_color = tp->thin_color;
		tp->thin_color = temp;
	}
	n_hits = match_rules(vertex, hits, False);
	side = NRAND(2) ? S_LEFT : S_RIGHT;
	n = find_completions(vertex, hits, n_hits, side, vtypes /*, False */ );
	/* One answer would mean a forced tile. */
	if (n <= 0) {
		tp->done = True;
		if (MI_IS_VERBOSE(mi)) {
			(void) fprintf(stderr, "Weirdness in add_random_tile()\n");
			(void) fprintf(stderr, "n = %d\n", n);
		}
	}
	no_good = 0;
	n_good = n;
	for (i = 0; i < n; i++) {
		fc = fringe_changes(mi, vertex, side, vtypes[i], &right, &far, &left);
		if (fc & FC_BAG) {
			tp->done = True;
			if (MI_IS_VERBOSE(mi)) {
				(void) fprintf(stderr, "Weirdness in add_random_tile()\n");
				(void) fprintf(stderr, "fc = %d, FC_BAG = %d\n", fc, FC_BAG);
			}
		}
		if (right) {
			s = (((fc & FC_CUT_FAR) && (fc & FC_CUT_LEFT)) ? S_RIGHT : S_LEFT);
			if (!legal_move(right, s, VT_RIGHT(vtypes[i]))) {
				no_good |= (1 << i);
				n_good--;
				continue;
			}
		}
		if (left) {
			s = (((fc & FC_CUT_FAR) && (fc & FC_CUT_RIGHT)) ? S_LEFT : S_RIGHT);
			if (!legal_move(left, s, VT_LEFT(vtypes[i]))) {
				no_good |= (1 << i);
				n_good--;
				continue;
			}
		}
		if (far) {
			s = ((fc & FC_CUT_LEFT) ? S_RIGHT : S_LEFT);
			if (!legal_move(far, s, VT_FAR(vtypes[i]))) {
				no_good |= (1 << i);
				n_good--;
			}
		}
	}
	if (n_good <= 0) {
		tp->done = True;
		if (MI_IS_VERBOSE(mi)) {
			(void) fprintf(stderr, "Weirdness in add_random_tile()\n");
			(void) fprintf(stderr, "n_good = %d\n", n_good);
		}
	}
	n = NRAND(n_good);
	for (i = j = 0; i <= n; i++, j++)
		while (no_good & (1 << j))
			j++;

	if (!add_tile(mi, vertex, side, vtypes[j - 1])) {
		tp->done = True;
		if (MI_IS_VERBOSE(mi)) {
			(void) fprintf(stderr, "Weirdness in add_random_tile()\n");
		}
		free_penrose(tp);
	}
}
Example #16
0
File: 2048.c Project: Frky/2048
int main(void) {
    int dir;
    srand(time(NULL));
    uint8_t nb_empty_box, nb_merge;
#if USE_BIN 
    /* Creation of a grid */
    grid_t grid = create_empty_grid_bin();
    /* Grid backup */
    grid_t saved_grid;
    /* Initialisation of the grid */
    grid = init_grid_bin(grid, &nb_empty_box);
    /* Display the grid */
    display_grid_bin(grid);
    /* Main loop for a game */
    while (nb_empty_box > 0 || merge_possible_bin(grid)) {
        do {
            saved_grid = grid;
            /* While the move does not lead to a change on the grid */
            // dir = random_move();
            dir = best_move_bin(grid, nb_empty_box);
            /* Trying to play the move */
            grid = move_bin(grid, dir, &nb_merge);
            nb_empty_box += nb_merge;
        } while (grid == saved_grid);
        /* Add tile */
        grid = add_tile_bin(grid);
        nb_empty_box--;
        /* Display the new grid */
        display_grid_bin(grid);
        printf("Empty boxes: %u\n", nb_empty_box);
    }
#else
    /* Creation of a grid */
    uint16_t **grid = create_empty_grid();
    /* Initialisation of the grid */
    init_grid(grid);
    /* Display the grid */
    display_grid(grid);
#if DEBUG
    printf("Value: %i\n", evaluate(grid));
#endif
    /* Main loop for a game */
    while (!game_over(grid)) {
        /* While the move does not lead to a change on the grid */
        do {
#if IA
#if RANDOM
            dir = random_move();
#else
            dir = best_move(grid);
#endif
#else
            dir = ask_dir();
#endif
        /* Trying to play the move */
        } while (!move(grid, dir));
        /* Add a new tile at the end of each round */
        add_tile(grid);
        /* Display the new grid */
        display_grid(grid);
#if DEBUG
        printf("Value: %i\n", evaluate(grid));
#endif
    }
#endif
    return 0;
}