/************************************/

/*             8-mok Game           */

/* Throw 3 dices, and get the cube! */

/*                                  */

/*                     --[cppig1995]*/

/************************************/

/*                                  */

/* ISO/IEC 9899:1999 (aka C99)      */

/*                                  */

/************************************/



#include <stdlib.h>



/************************************/



#define rmod(n) (rand() / (RAND_MAX / (n) + 1))



int aRandom(int p, int b);

int aThrowDice();

int aThrow3Dice(int *d1, int *d2, int *d3);

int cb_4op(int m, int n1, int n2);

int aCheckBluff(int d1, int d2, int d3, int n);

int aComputer();



int aRandom(int p, int b)

{

	int r = rmod(b);

	return r > p;

}



int aThrowDice()

{

	return rmod(6) + 1;

}



int aThrow3Dice(int *d1, int *d2, int *d3)

{

	*d1 = aThrowDice();

	*d2 = aThrowDice();

	*d3 = aThrowDice();

	

	return 1;

}



int cb_4op(int m, int n1, int n2)

{

	switch(m)

	{

		case 1	:	return n1 + n2;

		case 2	:	return n1 - n2;

		case 3	:	return n1 * n2;

		case 4	:	return n1 / n2;

		default	:	return -1;

	}

	

	return -1;

}



int aCheckBluff(int d1, int d2, int d3, int n)

{

	int i, j, r;

	for(i = 1; i <= 4; i++)

	{

		r = cb_4op(i, d1, d2);

		for(j = 1; j <= 4; j++)

		 if(cb_4op(j, r, d3) == n) return 0;

	}

	return 1;

}



int aComputer()

{

	int d1 = 0, d2 = 0, d3 = 0, n = 0;

	aThrow3Dice(&d1, &d2, &d3);

	//TODO: Implement this part

	return n;

}



/*---------------------------*/



typedef unsigned char BOARD_MODE;

#define BS_ERROR	(-1)

#define BS_EMPTY 	( 0)

#define BS_COMPUTER 	( 1)

#define BS_HUMAN	( 2)



typedef struct

	{

		unsigned short isBluff : 1;

		unsigned short d3 : 3;

		unsigned short d2 : 3;

		unsigned short d1 : 3;



	} DICE_INFO;



typedef struct

	{

		BOARD_MODE bm;

		DICE_INFO di;

		

	} BOARD_STATUS;

	

typedef BOARD_STATUS GameBoard[18];



#define gbGetMinIndex() (1)

#define gbGetMaxIndex() (18)

BOARD_MODE gbInitialize(GameBoard b);

BOARD_MODE gbSetColumn(GameBoard b, int index, BOARD_MODE value);

BOARD_MODE gbGetColumn(GameBoard b, int index);

BOARD_MODE gbCheckForfeit(GameBoard b);

DICE_INFO gbGetDiceInfo(GameBoard b, int index, DICE_INFO value);

DICE_INFO gbGetDiceInfo(GameBoard b, int index);

// Line 120 - 127 : not Implemented, but only exist as prototype

int gbSetBluffBit(GameBoard b, int index, int value);

int gbGetBluffBit(GameBoard b, int index);

int gbSetDice1(GameBoard b, int index, int value);

int gbGetDice1(GameBoard b, int index);

int gbSetDice2(GameBoard b, int index, int value);

int gbGetDice2(GameBoard b, int index);

int gbSetDice3(GameBoard b, int index, int value);

int gbGetDice3(GameBoard b, int index);



BOARD_MODE gbInitialize(GameBoard b)

{

	for(int i = gbGetMinIndex(); i <= gbGetMaxIndex(); i++)

		gbSetColumn(b, i, BS_EMPTY);

	return BS_EMPTY;

}



BOARD_MODE gbSetColumn(GameBoard b, int index, BOARD_MODE value)

{

	if(index < gbGetMinIndex() || index > gbGetMaxIndex) return BS_ERROR;

	else return b[index - 1].bm = value;

}



BOARD_MOD gbGetColumn(GameBoard b, int index)

{

	if(index < gbGetMinIndex() || index > gbGetMaxIndex) return BS_ERROR;

	else return b[index - 1].bm;

}



BOARD_MODE gbCheckForfeit(GameBoard b)

{

	static int cubePoints[4][8] = {

					 {  1,  2,  4,  5,  7,  8, 10, 11, }

					 {  2,  3,  5,  6,  8,  9, 11, 12, }

					 {  7,  8, 10, 11, 13, 14, 16, 17, }

					 {  8,  9, 11, 12, 14, 15, 17, 18, }

					};

	int i, j, k, b;

	for(i = 0; i < 4; i++)

	{

	  for(j = 0; j < 8; j++)

	  {

	  

	  }

	}

	  

	  

	return BS_EMPTY;

}