git » fp-git.git » master » tree

[master] / poprecog / connect4 / boards.c

/**
 * Code below was generated by a machine, so please don't edit
 */
 
#include <iostream.h>

class classBoard {
	public:
	unsigned long long board[2];
	int actualPlayer;
	unsigned long long operationsNumber;

	void clear() {
		board[0] = 0;
		board[1] = 0;
		actualPlayer = 0;
		operationsNumber = 0;
	};
	int putToken44(int column);
	int removeToken44(int column);
	int miniMax44(int depth);
};

inline int classBoard::putToken44(int column) {
	//cout << "p" << column << " ";
	register unsigned long long *b=&board[actualPlayer];
	switch(column) {
		case 0:
			switch((board[0]|board[1])&4369) {
				case 0:
					*b|=1;
					if (((*b&=15)==15) ||
					    ((*b&=4369)==4369) ||
					    ((*b&=33825)==33825))
						return 2;
					break;
				case 1:
					*b|=16;
					if (((*b&=240)==240) ||
					    ((*b&=4369)==4369))
						return 2;
					break;
				case 17:
					*b|=256;
					if (((*b&=3840)==3840) ||
					    ((*b&=4369)==4369))
						return 2;
					break;
				case 273:
					*b|=4096;
					if (((*b&=61440)==61440) ||
					    ((*b&=4369)==4369) ||
					    ((*b&=4680)==4680))
						return 2;
					break;
				default:
					return -2;
					break;
			};
			break;
		case 1:
			switch((board[0]|board[1])&8738) {
				case 0:
					*b|=2;
					if (((*b&=15)==15) ||
					    ((*b&=8738)==8738))
						return 2;
					break;
				case 2:
					*b|=32;
					if (((*b&=240)==240) ||
					    ((*b&=8738)==8738) ||
					    ((*b&=33825)==33825))
						return 2;
					break;
				case 34:
					*b|=512;
					if (((*b&=3840)==3840) ||
					    ((*b&=8738)==8738) ||
					    ((*b&=4680)==4680))
						return 2;
					break;
				case 546:
					*b|=8192;
					if (((*b&=61440)==61440) ||
					    ((*b&=8738)==8738))
						return 2;
					break;
				default:
					return -2;
					break;
			};
			break;
		case 2:
			switch((board[0]|board[1])&17476) {
				case 0:
					*b|=4;
					if (((*b&=15)==15) ||
					    ((*b&=17476)==17476))
						return 2;
					break;
				case 4:
					*b|=64;
					if (((*b&=240)==240) ||
					    ((*b&=17476)==17476) ||
					    ((*b&=4680)==4680))
						return 2;
					break;
				case 68:
					*b|=1024;
					if (((*b&=3840)==3840) ||
					    ((*b&=17476)==17476) ||
					    ((*b&=33825)==33825))
						return 2;
					break;
				case 1092:
					*b|=16384;
					if (((*b&=61440)==61440) ||
					    ((*b&=17476)==17476))
						return 2;
					break;
				default:
					return -2;
					break;
			};
			break;
		case 3:
			switch((board[0]|board[1])&34952) {
				case 0:
					*b|=8;
					if (((*b&=15)==15) ||
					    ((*b&=34952)==34952) ||
					    ((*b&=4680)==4680))
						return 2;
					break;
				case 8:
					*b|=128;
					if (((*b&=240)==240) ||
					    ((*b&=34952)==34952))
						return 2;
					break;
				case 136:
					*b|=2048;
					if (((*b&=3840)==3840) ||
					    ((*b&=34952)==34952))
						return 2;
					break;
				case 2184:
					*b|=32768;
					if (((*b&=61440)==61440) ||
					    ((*b&=34952)==34952) ||
					    ((*b&=33825)==33825))
						return 2;
					break;
				default:
					return -2;
					break;
			};
			break;
	};
	actualPlayer=!actualPlayer;
	// totalNumberOfRows=10
	return 0;
}

inline int classBoard::removeToken44(int column) {
	//cout << "r" << column << " ";
	actualPlayer=!actualPlayer;
	switch(column) {
		case 0:
			switch((board[0]|board[1])&4369) {
				case 1:
					board[actualPlayer]&=1048574;
					break;
				case 17:
					board[actualPlayer]&=1048559;
					break;
				case 273:
					board[actualPlayer]&=1048319;
					break;
				case 4369:
					board[actualPlayer]&=1044479;
					break;
			};
			break;
		case 1:
			switch((board[0]|board[1])&8738) {
				case 2:
					board[actualPlayer]&=1048573;
					break;
				case 34:
					board[actualPlayer]&=1048543;
					break;
				case 546:
					board[actualPlayer]&=1048063;
					break;
				case 8738:
					board[actualPlayer]&=1040383;
					break;
			};
			break;
		case 2:
			switch((board[0]|board[1])&17476) {
				case 4:
					board[actualPlayer]&=1048571;
					break;
				case 68:
					board[actualPlayer]&=1048511;
					break;
				case 1092:
					board[actualPlayer]&=1047551;
					break;
				case 17476:
					board[actualPlayer]&=1032191;
					break;
			};
			break;
		case 3:
			switch((board[0]|board[1])&34952) {
				case 8:
					board[actualPlayer]&=1048567;
					break;
				case 136:
					board[actualPlayer]&=1048447;
					break;
				case 2184:
					board[actualPlayer]&=1046527;
					break;
				case 34952:
					board[actualPlayer]&=1015807;
					break;
			};
			break;
	};
};

inline int classBoard::miniMax44(int depth) {
	register short move;
	register short best = 0;
	register int res;
	
	//cout << " m" << depth << " ";
	
	operationsNumber++;

	if (!depth)
	  return 1;
	for (move=0; move<4; move++) {
		//cout << "m";// << move << " b" << best << " ";
		//cout << "b";
		//cout << "";
		res = putToken44(move);
		if (res == -2) {
			//cout << "X ";
		  continue;
		}
		if (res == 2) {
 			actualPlayer = !actualPlayer;
			removeToken44(move);
			return 2;
		}
		//cout << "^";
		/*if (!(depth-1))
		  best = 1;
		else*/
		//if (depth-1==0)
		//  best = 1;
		//else
		//cout << depth-1 << "! ";
		if (depth-1==0)
		  best=1;
		else {
		switch (miniMax44(depth-1)) {
			case -2: // won
			  //cout << "-2\"";
				removeToken44(move);
				return 2;
			case 2: // lose
			  //cout << "2\"";
				if (!best)
				  best = -2;
			default: //draw
			  //cout << "d\"";
				best = 1;
		}
	}
		removeToken44(move);
	}
	//cout << "ret" << best << " ";
	return best;
}