#include <sys/bios.h>
#include <stdlib.h>

#define RES_WIN 1
#define RES_LOOS 2
#define RES_EVEN 0
#define RES_Q	3
#define CX_END	(224/8-1)
#define CY_END	(144/8-1)
#define KH_BASE_X (CX_END-7)
#define KH_BASE_Y (1)
unsigned char	*string[]={"グー ","チョキ","パー "," ? "},
		*string_res[4];

void main(int argc, char *argv[])
{
	unsigned char key,con,man,cont,
		res[3][4]=	{{RES_EVEN,RES_LOOS,RES_WIN },	/* con:グー	*/
				 {RES_WIN ,RES_EVEN,RES_LOOS},	/* con:チョキ	*/
				 {RES_LOOS,RES_WIN ,RES_EVEN},};	/* con:パー	*/
	unsigned int win=0,loos=0;

	string_res[RES_WIN] ="あなたの勝ち   ";
	string_res[RES_LOOS]="コンピュータの勝ち";
	string_res[RES_EVEN]="引き分け     ";
	string_res[RES_Q]   =" ?        ";

	text_screen_init();
	text_put_string(0,0,"ジャンケンポン");					/* Title() */

	text_put_string(KH_BASE_X,KH_BASE_Y,"X2:グー");					/* KeyHelp() */
	text_put_string(KH_BASE_X,KH_BASE_Y+1,"X3:チョキ");
	text_put_string(KH_BASE_X,KH_BASE_Y+2,"X4:パー");
	text_put_string(KH_BASE_X,KH_BASE_Y+3,"A :初めから");
	text_put_string(KH_BASE_X,KH_BASE_Y+4,"ST:終了");

	text_put_string(0,4,"コンピュータ: ?");
	text_put_string(0,5,"あなた   : ?");
	text_put_string(0,6,"結果    : ?");
	text_put_string(0,8,"成績    : 勝ち   0");
	text_put_string(8,9,"負け   0");
	key=NULL;
	cont=0;
	srand(sys_get_tick_count()%0x7fff);
	while(key!=KEY_START) {
		key=key_wait();
		if(cont++>10) {
			cont=0;
			srand(sys_get_tick_count()&0x7fff);
		}
		con=rand()%3;
		if	(key==KEY_X2)	man=0;
		else if (key==KEY_X3)	man=1;
		else if (key==KEY_X4)	man=2;
		else if (key==KEY_A)	man=3;
		else			man=4;
				

		if(man<3) {
			text_put_string(8,4,string[con]);		/* 結果の表示 */
			text_put_string(8,5,string[man]);
			text_put_string(8,6,string_res[res[con][man]]);

			win+=res[con][man]%2;				/* 成績の表示 */
			loos+=res[con][man]/2;
			text_put_numeric(11,8,4,NUM_PADSPACE|NUM_ALIGN_RIGHT,win);
			text_put_numeric(11,9,4,NUM_PADSPACE|NUM_ALIGN_RIGHT,loos);
		}
		if(man==3) {
			text_put_string(8,4,string[3]);		/* 結果の表示 */
			text_put_string(8,5,string[3]);
			text_put_string(8,6,string_res[3]);

			win =0;				/* 成績の表示 */
			loos=0;
			text_put_numeric(11,8,4,NUM_PADSPACE|NUM_ALIGN_RIGHT,win);
			text_put_numeric(11,9,4,NUM_PADSPACE|NUM_ALIGN_RIGHT,loos);
		}
	}
}
