Example #1
0
static void
test_fen_basic(void)
{
	struct position *position;
	struct position *next;
	move move;
	char str[FEN_BUFFER_LENGTH + MOVE_STR_BUFFER_LENGTH];
	static const char *empty_fen = "8/8/8/8/8/8/8/8 w - - 0 1";
	unsigned hm, fm;
	int ep_index;
	enum player turn;

	position = position_allocate();
	next = position_allocate();
	assert(position_print_fen_full(position, str, 0, 1, 0, white)
	    == str + strlen(empty_fen));
	assert(strcmp(empty_fen, str) == 0);
	assert(NULL != position_read_fen_full(position, start_position_fen,
	    &ep_index, &fm, &hm, &turn));
	assert(ep_index == 0);
	assert(hm == 0 && fm == 1 && turn == white);
	assert(position_print_fen_full(position, str, 0, 1, 0, white)
	    == str + strlen(start_position_fen));
	assert(strcmp(str, start_position_fen) == 0);
	move = create_move_t(str_to_index("e2", white),
	    str_to_index("e4", white),
	    mt_pawn_double_push, pawn, 0, false);
	setup_registers();
	make_move(next, position, move);
	assert(position_piece_at(next, str_to_index("e2", black)) == nonpiece);
	assert(position_piece_at(next, str_to_index("e4", black)) == pawn);
	position_destroy(position);
	position_destroy(next);
}
Example #2
0
File: fen.c Project: GBuella/Taltos
static char*
FEN_print_board(const struct position *pos, char *str, enum player turn)
{
	int empty_count;

	for (int rank = rank_8; ; rank += RSOUTH) {
		empty_count = 0;
		for (int file = file_a; is_valid_file(file); file += EAST) {
			int i = ind(rank, file);
			if (turn == black)
				i = flip_i(i);
			enum piece p = position_piece_at(pos, i);
			enum player player = position_player_at(pos, i);
			if (p != nonpiece) {
				if (empty_count > 0) {
					*str++ = '0' + (char)empty_count;
					empty_count = 0;
				}
				if (turn == black)
					player = opponent_of(player);
				*str++ = square_to_char(p, player);
			}
			else {
				++empty_count;
			}
		}
		if (empty_count > 0)
			*str++ = '0' + empty_count;
		if (rank == rank_1)
			return str;
		else
			*str++ = '/';
	}
}