#include <stdio.h>
#include "old.h"

rline()
{
	char *p1;
	int c;

	p1 = sbuf;
	while((c = getchar()) != '\n')
		if(c <= 0)
			onhup(); else
			*p1++ = c;
	*p1++ = '\0';
}

pboard()
{
	int i, x, y, c, p;

	c = 0;
	i = 0;
	x = 8;
	while(x--) {
		if(!mantom || mfmt)
			sputchar('1'+x); else
			sputchar('8'-x);
		sputchar(' ');
		c++;
		y = 8;
		while(y--) {
			c++;
			sputchar(' ');
			if(p = board[i++])
				sputchar("kqrbnp PNBRQK"[p+6]); else
				if((c&1)!=0)
					sputchar('*'); else
					sputchar('-');
		}
		sputchar('\n');
		if(intrp)
			return;
	}
	if(mfmt)
		printf("\n   a b c d e f g h"); else
		printf("\n   q q q q k k k k\n   r n b     b n r");
		printf("\n");
}

out1(m)
{
	printf("%d. ", moveno);
	if(mantom)
		printf("... ");
	out(m);
	sputchar('\n');
}

out(m)
int m;
{
	int from, to, epf, pmf;

	from = m>>8;
	to = m&0377;
	if(mfmt) {
		algco(from);
		algco(to);
		return;
	}
	mantom? bmove(m): wmove(m);
	epf = pmf = 0;
	switch(amp[-1]) {

	case 0:
	case 1:
		stdp(board[to]);
	ed:
		sputchar('/');
		stdb(from);
		if(amp[-2]) {
			sputchar('x');
			stdp(amp[-2]);
			sputchar('/');
		} else
			sputchar('-');
		stdb(to);
		break;

	case 3:
		sputchar('o');
		sputchar('-');

	case 2:
		sputchar('o');
		sputchar('-');
		sputchar('o');
		break;

	case 4:
		epf = 1;
		sputchar('p');
		goto ed;

	case 5:
		pmf = 1;
		sputchar('p');
		goto ed;
	}
	if(pmf) {
		sputchar('(');
		sputchar('q');
		sputchar(')');
	}
	if(epf) {
		sputchar('e');
		sputchar('p');
	}
	if(check())
		sputchar('+');
	mantom? bremove(): wremove();
}

stdp(p)
int p;
{

	if(p < 0)
		p = -p;
	p = "ppnbrqk"[p];
	sputchar(p);
}

stdb(b)
int b;
{
	int r, f;

	r = b/8;
	if((f = b%8) < 4)
		sputchar('q'); else {
		sputchar('k');
		f = 7-f;
	}
	f = "rnb\0"[f];
	if(f)
		sputchar(f);
	sputchar(mantom? r+'1': '8'-r);
}

algco(p)
int p;
{
	sputchar('a'+(p%8));
	sputchar('8'-(p/8));
}

sputchar(c)
{

	switch(c) {

	case '\t':
		do
			sputchar(' ');
		while(column%8);
		return;

	case '\n':
		column = 0;
		break;

	default:
		column++;
	}
	putchar(c);
}

prtime(a, b)
{

	printf("time = %d/%d\n", a, b);
}

score1(m)
{
	if(intrp)
		return;
	if(!mantom) {
		if(moveno < 10)
			sputchar(' '); else
			sputchar(moveno/10 + '0');
		sputchar(moveno%10 + '0');
		sputchar('.');
		sputchar(' ');
	} else
		while(column < 20)
			sputchar(' ');
	out(m);
	if(mantom)
		sputchar('\n');
}

score()
{
	int *p;

	sputchar('\n');
	p = amp;
	while(amp[-1] != -1) {
		mantom? wremove(): bremove();
		decrem();
	}
	posit(score1, p);
	sputchar('\n');
}
