コード例 #1
0
ファイル: ConnectBoard.cpp プロジェクト: Jerdak/Connect-N
///Add move to board
bool ConnectBoard::AddMove(const int &column, const bool& is_me){
	
	if(data_ == NULL){
		fprintf(stderr,"Cannot add move to board, data_ is NULL.");
		return false;
	}
	if(!ValidColumn(column))return false;
	
	int colCt = ColumnCount(column);
	if(colCt == height_){
		fprintf(stderr,"Cannot add more tokens to column %d, height full",column);
		return false;
	}
	int node = GetNodeIndex(colCt,column);
	if(!ValidNode(node)){
		fprintf(stderr,"Cannot add node to board %d",column);
		return false;
	}
	
	data_[node].used = true;
	data_[node].is_me = is_me;

	/*
		Using the lookup kernel noted below we make the assumption that new moves added to the board
		have possible parents where there are 1's and possible child where there are 0's.

		The 'if' statement below (in the 'for' loop) could be collapsed to a single 
		statement but it serves readability by leaving both outcomes explicitly written.
	*/
	{
		//Lookup kernel, tells new node what parents to look for first, then what child.
		int reverseKernel[] = {1, 1, 0,		//		| 1 0 0 |
							   1,-1, 0,		// ==>  | 1   0 |
							   1, 0, 0};	//		| 1 1 0 |

		for(int r = -1; r < 2; r++){
			for(int c = -1; c < 2; c++){
				int rk = (r+1)*3 + (c + 1);
				int rki = 8 - rk;
				if(reverseKernel[rk]==1){
					int parentNode = GetNodeIndex(colCt + r,column + c);
					if(ValidNode(parentNode) && data_[parentNode].is_me == is_me && data_[parentNode].used){
					//	fprintf(stderr,"Adding parentnode[%d] to child[%d]\n",parentNode,node);
						data_[node].parent[rk] = &data_[parentNode];
						data_[parentNode].child[rki] = &data_[node];
					}
				} else if(reverseKernel[rk]==0){
					int childNode = GetNodeIndex(colCt + r,column + c);
					if(ValidNode(childNode) && data_[childNode].is_me == is_me && data_[childNode].used){
					//	fprintf(stderr,"Adding childnode[%d] to parent[%d]\n",childNode,node);
						data_[node].child[rk] = &data_[childNode];
						data_[childNode].parent[rki] = &data_[node];
					}
				}
			}
		}
	}
	return true;
}
コード例 #2
0
ファイル: ConnectBoard.cpp プロジェクト: Jerdak/Connect-N
// Remove a single move from the board.
bool ConnectBoard::RemoveMove(const int &column, const bool& is_me){
	if(data_ == NULL){
		fprintf(stderr,"Cannot remove move to board, data_ is NULL.");
		return false;
	}
	if(!ValidColumn(column))return false;

	int colCt = ColumnCount(column)-1;
	int node = GetNodeIndex(colCt,column);
	if(!ValidNode(node))return false;


	if(!data_[node].used){
		fprintf(stderr,"Cannot remove token from column %d, column is empty",column);
		return false;
	}
	if(data_[node].is_me != is_me){
		fprintf(stderr,"Cannot remove token from column %d, users do not match",column);
		return false;
	}
	data_[node].used = false;
	
	//Orphan this node's child and remove it from its parents.
	{
		for(int f = 0; f < 9; f++){
			int fi = 8-f;
			if(data_[node].parent[f] != NULL){
				data_[node].parent[f]->child[fi] = NULL;
			}
			if(data_[node].child[f] != NULL){
				data_[node].child[f]->parent[fi] = NULL;
			}
			data_[node].child[f] = NULL;
			data_[node].parent[f] = NULL;
		}
	}
	return true;
}
コード例 #3
0
ファイル: ConnectBoard.cpp プロジェクト: Jerdak/Connect-N
///Direct node access (TODO:  Consider dropping)
ConnectBoard::Node* ConnectBoard::operator()(const int row, const int col) const{
	if(!ValidNode(row,col))return NULL;
	return &data_[GetNodeIndex(row,col)];
}
コード例 #4
0
ファイル: ConnectBoard.cpp プロジェクト: Jerdak/Connect-N
///Check if node index (by row and column) is in range.
bool ConnectBoard::ValidNode(const int &row, const int &col) const{
	if(row<0 || row >= height_ || col<0 || col >= width_)return false;
	return ValidNode(GetNodeIndex(row,col));
}
 inline int  GetDistance(int v) { assert(ValidNode(v)); return curr_dist[v]; }
 inline int GetInMask(int v) const { assert(ValidNode(v)); return reach_mask[1][v]; }