Пример #1
0
Файл: game.c Проект: needs/anko
int new_game(game_t *game, int width, int height, gen_params_t *params, long sim_speed)
{
	assert(game != NULL);
	assert(width > 0);
	assert(height > 0);
	assert(params != NULL);

	memset(game, 0, sizeof(*game));

	if (!alloc_board(&game->board[0], width, height))
		goto err_board1;
	if (!alloc_board(&game->board[1], width, height))
		goto err_board2;
	generate(&game->board[0], params);
	game->current = &game->board[0];
	game->old = &game->board[1];

	game->sim_speed = sim_speed;
	game->sim_timer = sim_speed;
	game->player_count = 0;
	memcpy(&game->gen_params, params, sizeof(gen_params_t));

	return 1;

err_board2:
	free_board(&game->board[0]);
err_board1:
	return 0;
}
Пример #2
0
int main() {
	int **board;
	int m, n, sx, sy;

	scanf("%d%d%d%d", &m, &n, &sx, &sy);

	if (m >= 20 || n >= 20 || sx > m || sy > n) {
		printf(NO);
		exit(1);
	}

	board = alloc_board(m, n);
	board[sx - 1][sy - 1] = 1;

	printf(move(board, m, n, sx - 1, sy - 1, 0) ? YES : NO);

	free_board(board, m);

	return 0;
}
Пример #3
0
/*
 * Returns NULL if failing to allocate.
 *
 * If successful, then game->playerc is set to HT_PLAYERS_MAX. The
 * players are allocated and initialized according to player_init.
 *
 * You must still initialize the rest of the game after calling this
 * function (the players are only initialized because their player
 * indices correspond to the array in struct game, and then we may as
 * well fully initialize them).
 *
 * These variables are fully allocated, but not initialized:
 *
 * *	game->rules
 *
 * *	game->b
 */
struct game *
alloc_game (
	const unsigned short	bwidth,
	const unsigned short	bheight
	)
{
	unsigned short		i;
	const unsigned short blen = (unsigned short)(bwidth * bheight);
	struct game * const	g	= malloc(sizeof(* g));
	if (NULL == g)
	{
		return	NULL;
	}

	g->rules	= alloc_ruleset(bwidth, bheight);
	if (NULL == g->rules)
	{
		free	(g);
		return	NULL;
	}

	/*
	 * This is set later and uses ruleset information.
	 */
	g->b	= alloc_board(blen);
	if (NULL == g->b)
	{
		free_ruleset	(g->rules);
		free		(g);
		return		NULL;
	}

	g->movehist	= alloc_listmh(HT_LISTMH_CAP_DEF);
	if (NULL == g->movehist)
	{
		free_board	(g->b);
		free_ruleset	(g->rules);
		free		(g);
		return		NULL;
	}

	g->playerc = HT_PLAYERS_MAX;
	g->players = malloc(sizeof(* g->players) * (size_t)g->playerc);
	if (NULL == g->players)
	{
		free_listmh	(g->movehist);
		free_board	(g->b);
		free_ruleset	(g->rules);
		free		(g);
		return		NULL;
	}
	for (i = (unsigned short)0; i < g->playerc; i++)
	{
		g->players[i]	= alloc_player();
		if (NULL == g->players[i]
		|| !player_init(g->players[i], i))
		{
			unsigned short j;
			for (j = (unsigned short)0; j <= i
/* Check added for GCC's loop optimizations: */
				&& j <= HT_PLAYERS_MAX
				; j++)
			{
				if (NULL != g->players[j])
				{
					free_player(g->players[j]);
				}
			}
			free		(g->players);
			free_listmh	(g->movehist);
			free_board	(g->b);
			free_ruleset	(g->rules);
			free		(g);
			return		NULL;
		}
	}

	return	g;
}