/*
 *	File name:			strategy_template.c
 *	Purpose:			Example: defines a robot strategy for playing poker.
 *	Functions:			computer_bet, computer_draw, strategy_init,
 *						strategy_exit, computer_outcome, computer_hear.
 *	Created:			01 Dec 91
 *	Last Modified:		01 Dec 91
 *	Comments:
 *		"Strategy" files must contain six externally visible functions:
 *
 *		strategy_init - called once during startup.
 *		strategy_exit - called once upon orderly exit.
 *		computer_bet - called whenever a bet is needed.
 *		computer_draw - called whenever a draw of cards is needed.
 *		computer_outcome - called at the end of each hand.
 *		computer_hear - called whenever anyone sends a text message.
 *
 *		The interfaces to these six functions are well-defined and should 
 *		remain stable.  You can use this file to entirely alter the playing 
 *		methods of a robot player without being concerned with server 
 *		communication or anything else.
 *
 *	History:
 *		01 Dec 91/JVE	Created.
 */

# include	<string.h>
# include	"standard.h"
# include	"cards.h"		/* for crandom, suit, card macros */
# include	"5draw.h"		/* for MAX_PLAYERS */
# include	"knowledge.h"
# include	"robot.h"
# include	"robot_main_protos.h"
# include	"strategy_protos.h"

int		Pot(KNOWLEDGE k[]);


/*
 ***********************************************************************
 *
 * FUNCTION NAME:	strategy_init
 *
 * DESCRIPTION:		Any initialization needed.
 *
 * PARAMETERS: 
 *		NAME		I/O		DESCRIPTION
 *		----		---		-----------
 *
 * RETURN CODES: 
 *		TRUE		I am ready to play.
 *		FALSE		I'm not playing, just watching.
 *
 * COMMENTS:
 *
 **********************************************************************
 */

int		strategy_init()

	{
	/*
	 *	Initialize random number generator, initialize knowledge base,
	 *	whatever.
	 */
	return TRUE;
	}

/*
 ***********************************************************************
 *
 * FUNCTION NAME:	strategy_exit
 *
 * DESCRIPTION:		Actions to be performed on shutdown.
 *
 * PARAMETERS: 
 *		NAME		I/O		DESCRIPTION
 *		----		---		-----------
 *
 * RETURN CODES: none
 *
 * COMMENTS:
 *
 **********************************************************************
 */

void	strategy_exit()

	{
	talk( "Gotta go.  Bye." );
	}


/*
 ***********************************************************************
 *
 * FUNCTION NAME:	computer_bet
 *
 * DESCRIPTION:		Make a bet.
 *
 * PARAMETERS: 
 *		NAME		I/O		DESCRIPTION
 *		----		---		-----------
 *		knowledge	 I		Everything we know about the current hand.
 *		me			 I		Index in knowledge[] of this robot.
 *		required	 I		Total $$ this hand to bet to stay in.
 *
 * RETURN CODES: 
 *	(int)		Amount of additional cash to bet.
 *	(-1)		Fold.
 *
 * COMMENTS:
 *
 **********************************************************************
 */

int		computer_bet( knowledge, me, required )

KNOWLEDGE		knowledge[];
int				me;
int				required;

	{
	/*
	 *	Call: return required - knowledge[me].bet
	 *	Pass: return 0
	 *	Fold: return -1
	 *
	 *	If Pass/Call/Raise and you don't meet 'required' as the total for
	 *	the hand, the robot interface will force a Call.  This is because
	 *	the usual cause of the server rejecting a bet is due to raising
	 *	too many $$ or too many times during the betting session.
	 */
	return -1;
	}


/*
 ***********************************************************************
 *
 * FUNCTION NAME:	computer_draw
 *
 * DESCRIPTION:		Discard some cards and draw others.
 *
 * PARAMETERS: 
 *		NAME		I/O		DESCRIPTION
 *		----		---		-----------
 *		knowledge	 I		Everything we know about the current hand.
 *		me			 I		Index in knowledge[] of this robot.
 *		cmd			 O		Constructed "discard" command.
 *
 * RETURN CODES: none
 *
 * COMMENTS:
 *
 **********************************************************************
 */

void	computer_draw( knowledge, me, cmd )

KNOWLEDGE		knowledge[];
int				me;
char			*cmd;

	{
	standard_draw( knowledge[me].cards, cmd );
	}


/*
 ***********************************************************************
 *
 * FUNCTION NAME:	computer_outcome
 *
 * DESCRIPTION:		Is informed of the outcome of each hand.
 *
 * PARAMETERS: 
 *		NAME		I/O		DESCRIPTION
 *		----		---		-----------
 *		knowledge	 I		Everything we know about the current hand.
 *		me			 I		Index in knowledge[] of this robot.
 *		winner		 I		Index in knowledge[] winning player.
 *
 * RETURN CODES: none
 *
 * COMMENTS:
 *
 **********************************************************************
 */

void	computer_outcome( knowledge, me, winner )

KNOWLEDGE		knowledge[];
int				me;
int				winner;

	{
	}


/*
 ***********************************************************************
 *
 * FUNCTION NAME:	computer_hear
 *
 * DESCRIPTION:		Listen to incoming text message.
 *
 * PARAMETERS: 
 *		NAME		I/O		DESCRIPTION
 *		----		---		-----------
 *		knowledge	 I		Everything we know about the current hand.
 *		from		 I		Who is message from?
 *		text		 I		Text of message.
 *
 * RETURN CODES: none
 *
 * COMMENTS:
 *
 **********************************************************************
 */

void	computer_hear( knowledge, from, text )

KNOWLEDGE		knowledge[];
char			*from;
char			*text;

	{
	/*
	 *	This causes the robot to exit if he receives a message
	 *	containing the string "EXIT" anywhere in it.
	 *
	 *	You can use the 'from' argument to make sure only EXIT messages
	 *	from *you* to your robot are honored :-)
	 */
	if ( strstr( text, myname ) == 0 )
		if ( strstr( text, "EXIT" ) != NULL )
			{
			strategy_exit();
			robot_exit();
			/*NOTREACHED*/
			}
	}


/*
 ***********************************************************************
 *
 * FUNCTION NAME:	Pot
 *
 * DESCRIPTION:		Compute total pot for hand.
 *
 * PARAMETERS: 
 *		NAME		I/O		DESCRIPTION
 *		----		---		-----------
 *		k			 I		knowledge of this hand.
 *
 * RETURN CODES: 
 *		(int)		The total pot.
 *
 * COMMENTS:
 *
 **********************************************************************
 */

int			Pot( KNOWLEDGE k[] )

	{
	int		i;
	int		pot = 0;

	for ( i = 0; i < MAX_PLAYERS; i++ )
		{
		if ( k[i].valid )
			{
			pot += k[i].bet;
			}
		}
	return pot;
	}
