/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of SanKit -- A multiplatform set of C code
 * for reading and writing PGN formatted chess games. In addition
 * it can be used as an engine to determine in real time if proposed
 * moves are legal or not. It appears to generate near perfect PGN
 * from incorrectly formatted files.
 *
 * Copyright 1994 Steven J. Edwards All Rights Reserved.
 * Copyright 2009-2011 David Hoerl All Rights Reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*>>> auto.c: SAN project stub for Beal Autoplay Protocol */

/* Revised: 1993.05.16 */

#ifdef CLI

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "sandef.h"

#include "sanbap.h"
#include "sandsp.h"
#include "sanmnd.h"
#include "sanmpu.h"
#include "sanutl.h"

int auto_startup(void);
int auto_readmove(int *move);
int auto_makemove(int move);

/*--> auto_startup: called by auto command to initiate autoplay */
nonstatic
int auto_startup(void)
{
	int flag;
	mptrT mptr;
	siT response;
	char tv[tL], sv[tL];

	flag = 1;

	do {
		do {
			SANDspLine("AP: auto_startup: Is a move available (0=no, 1=yes)?");
			if(fgets(tv, tL, stdin) == NULL) {
				SANFatal("auto_startup: unexpected EOF on input");
			}
			response = -1;
			sscanf(tv, "%hd", &response);
		} while((response != 0) && (response != 1));

		if(response == 1) {
			mptr = NULL;
			do {
				SANDspLine("AP: Enter outstanding move");
				if(fgets(tv, tL, stdin) == NULL) {
					SANFatal("auto_startup: unexpected EOF on input");
				} else {
					sscanf(tv, "%s", sv);
					mptr = SANDecodeAux(sv);
					if(mptr == NULL) {
						SANDspLine("AP: Try again");
					} else {
						SANPlayMove(mptr);
					}
				}
			} while(mptr == NULL);
		}
	} while(flag && (response != 0));

	return flag ? 0 : 1;
}

/*--> auto_readmove: called to check on an incoming move */
nonstatic
int auto_readmove(int *move)
{
	int flag;
	mptrT mptr;
	siT msi;
	siT response;
	char tv[tL], sv[tL];

	flag = 1;

	do {
		SANDspLine("AP: auto_readmove: Is a move available (0=no, 1=yes)?");
		if(fgets(tv, tL, stdin) == NULL) {
			SANFatal("auto_readmove: unexpected EOF on input");
		}
		response = -1;
		sscanf(tv, "%hd", &response);
	} while((response != 0) && (response != 1));

	if(response == 0) {
		flag = 0;
	} else {
		mptr = NULL;
		do {
			SANDspLine("AP: Enter incoming move");
			if(fgets(tv, tL, stdin) == NULL) {
				SANFatal("auto_readmove: unexpected EOF on input");
			} else {
				sscanf(tv, "%s", sv);
				mptr = SANDecodeAux(sv);
				if(mptr == NULL) {
					SANDspLine("AP: Try again");
				}
			}
		} while(mptr == NULL);

		msi = SANBAPConvM(mptr);
		*move = (int) msi;
	}

	return flag ? 0 : 1;
}

/*--> auto_makemove: called to communicate an outgoing move */
nonstatic
int auto_makemove(int move)
{
	int flag;
	siT msi;
	siT response;
	char tv[tL];

	flag = 1;

	msi = move;
	SANDspStrSINL("AP: auto_makemove: outgoing move: ", msi);

	do {
		SANDspLine("AP: Was move sent okay (0=no, 1=yes)?");
		if(fgets(tv, tL, stdin) == NULL) {
			SANFatal("auto_makemove: unexpected EOF on input");
		}
		response = -1;
		sscanf(tv, "%hd", &response);
	} while((response != 0) && (response != 1));

	if(response == 0) {
		flag = 0;
	}

	return flag ? 0 : 1;
}

#endif

/*<<< auto.c: EOF */
