示例#1
0
const int GoBoard::FindUniqueLiberties(const int stone, const GoBlock* block) const
{
  LOG_DEBUG<<"Testing "<<stone<<" against "<<block->anchor;
  int uniqueLiberties = 0;

  if(North(stone) != -1 && State.stones[North(stone)] == NONE && !IsLibertyOfBlock(North(stone),block->anchor))
    {
      ++uniqueLiberties;
    }
  if(South(stone) != -1 && State.stones[South(stone)] == NONE && !IsLibertyOfBlock(South(stone),block->anchor))
    {
      ++uniqueLiberties;
    }
  if(West(stone) != -1 && State.stones[West(stone)] == NONE && !IsLibertyOfBlock(West(stone),block->anchor))
    {
      ++uniqueLiberties;
    }
  if(East(stone) != -1 && State.stones[East(stone)] == NONE && !IsLibertyOfBlock(East(stone),block->anchor))
    {
      ++uniqueLiberties;
    }

  //  int lib = 0;
  //  for(int i = 0; i < 4; i++)
  //    {
  //      if(i == 0)
  //        lib = North(stone);
  //      else if(i == 1)
  //        lib = South(stone);
  //      else if(i == 2)
  //        lib = West(stone);
  //      else if(i == 3)
  //        lib = East(stone);
  //      if(State.stones[lib] == NONE)
  //        {
  //          if(State.blockPointers[North(lib)] != block &&
  //              State.blockPointers[South(lib)] != block &&
  //              State.blockPointers[West(lib)] != block &&
  //              State.blockPointers[East(lib)] != block)
  //            ++uniqueLiberties;
  //        }
  //    }
  return uniqueLiberties;
}
示例#2
0
const int GoBoard::FindCommonLiberties(const int point, const int anchor) const
{
  int commonLiberties = 0;
  if(North(point)!=-1 && point != anchor && IsLibertyOfBlock(North(point),anchor))
    {
      ++commonLiberties;
    }
  if(South(point)!=-1 && point != anchor && IsLibertyOfBlock(South(point),anchor))
    {
      ++commonLiberties;
    }
  if(West(point)!=-1 && point != anchor && IsLibertyOfBlock(West(point),anchor))
    {
      ++commonLiberties;
    }
  if(East(point)!=-1 && point != anchor && IsLibertyOfBlock(East(point),anchor))
    {
      ++commonLiberties;
    }
  return commonLiberties;
}
示例#3
0
const bool GoBoard::IsLibertyOfBlock(const int point, const int anchor) const
{
  if(!Occupied(anchor))
    return false;
  const GoBlock* block = State.blockPointers[anchor];
  if(North(point) != -1 && State.blockPointers[North(point)] == block && State.blockPointers[North(point)] != State.blockPointers[point])

    {
      return true;
    }
  if(South(point) != -1 && State.blockPointers[South(point)] == block && State.blockPointers[South(point)] != State.blockPointers[point])
    {
      return true;
    }
  if(West(point) != -1 && State.blockPointers[West(point)] == block && State.blockPointers[West(point)] != State.blockPointers[point])
    {
      return true;
    }
  if(East(point) != -1 && State.blockPointers[East(point)] == block && State.blockPointers[East(point)] != State.blockPointers[point])
    {
      return true;
    }
  return false;
}
示例#4
0
const float GoBoard::GetScoreInternal() const
{
  float score = -komi; //Positive score = black win
  //  LOG_VERBOSE << "Post komi"<<score<<std::endl;
  //Count captures

  score += State.bw_prisoners[S_BLACK] - State.bw_prisoners[S_WHITE];
  //  LOG_VERBOSE << "Caps "<<State.bw_prisoners[S_BLACK]<<" "<<State.bw_prisoners[S_WHITE]<< " " << score+komi << std::endl;
  //  LOG_VERBOSE << "Post caps"<<score<<std::endl;
  int bstones = 0;
  int wstones = 0;
  int bterr = 0;
  int wterr = 0;
  for(int i = 0; i<Size()*Size(); ++i)
    {
      //Count stones
      if(State.stones[i] == B_BLACK)
	++bstones;
      else if(State.stones[i] == B_WHITE)
	++wstones;
      else if(State.stones[i] == NONE) //Count empty points
	{
	  if(State.stones[North(i)] == B_BLACK
	     || State.stones[South(i)] == B_BLACK
	     || State.stones[West(i)] == B_BLACK
	     || State.stones[East(i)] == B_BLACK
	     )
	    ++bterr;
	  else
	    ++wterr;
	}
    }
  //  LOG_VERBOSE << bstones << " " <<wstones<< " "<<bterr<< " "<<wterr;
  score += bstones + bterr - wstones - wterr;
  //  LOG_VERBOSE << "Finished score"<<score<<std::endl;
  return score;
}
示例#5
0
const bool GoBoard::IsTrueEye(const int point, const int boardColor)
{
  if(
     (North(point) == -1 || (State.stones[North(point)] == boardColor))
     && (South(point) == -1 || (State.stones[South(point)] == boardColor))
     && (West(point) == -1 || (State.stones[West(point)] == boardColor))
     && (East(point) == -1 || (State.stones[East(point)] == boardColor))
     )//Results in potential eye at point. May still be false
    {
      //Get diagonal colors
      int NW = West(point) != -1 && North(point) != -1 ? State.stones[North(West(point))] : -1;
      int NE = East(point) != -1 && North(point) != -1 ? State.stones[North(East(point))] : -1;
      int SW = West(point) != -1 && South(point) != -1 ? State.stones[South(West(point))] : -1;
      int SE = East(point) != -1 && South(point) != -1 ? State.stones[South(East(point))] : -1;

      LOG_DEBUG << NW << NE << SW <<SE<<State.stones[point];

      int numValid = 0;
      int numSameCol = 0;
      if(NW != -1)
	++numValid;
      if(NE != -1)
	++numValid;
      if(SW != -1)
	++numValid;
      if(SE != -1)
	++numValid;

      if(NW == boardColor)
	++numSameCol;
      if(NE == boardColor)
	++numSameCol;
      if(SW == boardColor)
	++numSameCol;
      if(SE == boardColor)
	++numSameCol;

      if((float)numSameCol / (float)numValid > 0.5)
	return true;
    }
  return false;
}
示例#6
0
int GoBoard::West(const GoPoint p) const
{
  return West(Pos(p));
}
示例#7
0
void GoBoard::UpdateBlocks(int pos, int color)
{
  int boardColor = color == S_BLACK? B_BLACK : B_WHITE;
  //Remove blockliberties of opposite color
  if(North(pos) != -1 && IsLibertyOfBlock(pos,North(pos)))
    {
      State.blockPointers[North(pos)]->liberties--;
      //    LOG_DEBUG << "Removed liberty for block at "<< North(pos);
    }
  if(South(pos) != -1 &&
     (State.blockPointers[South(pos)] != State.blockPointers[North(pos)]) //These are in place on west and east as well, to prevent removing too several liberties from the same group
     && IsLibertyOfBlock(pos,South(pos)))
    {
      State.blockPointers[South(pos)]->liberties--;
      //    LOG_DEBUG << "Removed liberty for block at "<< North(pos);
    }
  if(West(pos) != -1
     && (State.blockPointers[West(pos)] != State.blockPointers[North(pos)] && State.blockPointers[West(pos)] != State.blockPointers[South(pos)])
     && IsLibertyOfBlock(pos,West(pos)))
    State.blockPointers[West(pos)]->liberties--;
  if(East(pos) != -1
     && (State.blockPointers[East(pos)] != State.blockPointers[North(pos)] && State.blockPointers[East(pos)] != State.blockPointers[South(pos)] && State.blockPointers[East(pos)] != State.blockPointers[West(pos)])
     && IsLibertyOfBlock(pos,East(pos)))
    State.blockPointers[East(pos)]->liberties--;

  if(State.numNeighbours[color][pos] == 0) //Solo stone. Create new block
    {
      GoBlock* b = blocks[blockPointer];
      ++blockPointer;
      b->anchor = pos;
      b->color = color;
      b->board = this;
      b->liberties= State.numNeighboursEmpty[pos];
      State.blockPointers[pos] = b;
    }
  else //Not solo. Attach to block
    {
      int uniqueLiberties = 0;
      if (South(pos) != -1 && State.stones[South(pos)] == boardColor)
	{
	  //          commonLiberties = FindCommonLiberties(pos,South(pos));
	  uniqueLiberties = FindUniqueLiberties(pos, State.blockPointers[South(pos)]);
	  State.blockPointers[pos] = State.blockPointers[South(pos)];
	}
      else if (North(pos) != -1 && State.stones[North(pos)] == boardColor)
	{
	  //          commonLiberties = FindCommonLiberties(pos,North(pos));
	  uniqueLiberties = FindUniqueLiberties(pos, State.blockPointers[North(pos)]);
	  State.blockPointers[pos] = State.blockPointers[North(pos)];
	}
      else if (West(pos) != -1 && State.stones[West(pos)] == boardColor)
	{
	  //          commonLiberties = FindCommonLiberties(pos,West(pos));
	  uniqueLiberties = FindUniqueLiberties(pos, State.blockPointers[West(pos)]);
	  State.blockPointers[pos] = State.blockPointers[West(pos)];
	}
      else if (East(pos) != -1 && State.stones[East(pos)] == boardColor)
	{
	  //          commonLiberties = FindCommonLiberties(pos,East(pos));
	  uniqueLiberties = FindUniqueLiberties(pos, State.blockPointers[East(pos)]);
	  State.blockPointers[pos] = State.blockPointers[East(pos)];
	}
      else
	LOG_ERROR <<"Something went terribly wrong";
      //Subtract one since the new stone is always placed on a liberty for the block.
      State.blockPointers[pos]->liberties += uniqueLiberties;
      //Detect neighboring blocks to perform join.
      //Has similar neighbor but not recently attached block
      if(State.stones[North(pos)] == boardColor && State.blockPointers[North(pos)] != State.blockPointers[pos])
	State.blockPointers[pos]->ImportBlock(State.blockPointers[North(pos)]);
      if(State.stones[South(pos)] == boardColor && State.blockPointers[South(pos)] != State.blockPointers[pos])
	State.blockPointers[pos]->ImportBlock(State.blockPointers[South(pos)]);
      if(State.stones[West(pos)] == boardColor && State.blockPointers[West(pos)] != State.blockPointers[pos])
	State.blockPointers[pos]->ImportBlock(State.blockPointers[West(pos)]);
      if(State.stones[East(pos)] == boardColor && State.blockPointers[East(pos)] != State.blockPointers[pos])
	State.blockPointers[pos]->ImportBlock(State.blockPointers[East(pos)]);
      //      if(copyBlock != 0)
      //        {
      //          std::vector<int> commonLiberties;
      //          GoBlock* curBlock = State.blockPointers[pos];
      //          curBlock->ImportBlock(copyBlock);
      //        }

    }
  State.blockPointers[pos]->addStone(pos);
  KillSurroundingDeadBlocks(pos);

}
void Interface::MainGame()
{
	string input = "";
	vector<string> parsedInput;
	bool quitGame = false;
	Look();
	cout << "Type 'help' (no ' marks) for a short explanation of basic commands." << endl;
	cout << "Make sure to use the 'growth' command before gaining experience!" << endl;
	while(!quitGame)
	{
		Prompt();
		getline(cin, input);
		for (int i = 0; i < input.size(); i++)
			input[i] = tolower(input[i]);
		parsedInput = ParseInput(input);
		command cmd = CommandControl::GetInstance()->GetCommand(parsedInput.front());
		string target = parsedInput.back();
		if (cmd == LOOK)
			Look();
		if (cmd == GO_NORTH)
			North();
		if (cmd == GO_SOUTH)
			South();
		if (cmd == GO_EAST)
			East();
		if (cmd == GO_WEST)
			West();
		if (cmd == GO_DOWN)
			Down();
		if (cmd == GO_UP)
			Up();
		if (cmd == KILL)
			Kill(parsedInput.back());
		if (cmd == SCORE)
			Score();
		if (cmd == ABILITIES)
			Abilities();
		if (cmd == HELP)
			Help();
		if (cmd == GROWTH)
			Growth();
		if (cmd == QUIT)
			Quit();
		if (cmd == INVENTORY)
			Inventory();
		if (cmd == EQUIPMENT)
			Equipment();
		if (cmd == WEAR)
			Wear(parsedInput.back());
		if (cmd == REMOVE)
			Remove(parsedInput.back());
		if (cmd == EXAMINE)
		{	
			command targetCmd = CommandControl::GetInstance()->GetCommand(parsedInput.back());
			if (targetCmd == INVENTORY)
				ExaInv();
			else if (targetCmd == EQUIPMENT)
				ExaEquip();
			else if (targetCmd == LOOK)
				ExaLook();
			else
				cout << "That is not a valid target to examine." << endl;
		}
		if (cmd == GET)
			Get(parsedInput.back());
		if (cmd == DROP)
			Drop(parsedInput.back());
		if (cmd == USE)
			Use(parsedInput.back());
		if (cmd == SAVE)
			SavePlayer();
	}
}
/* compute the next generation of the world */
void NextGeneration(char **oldWorld, char **newWorld, int row, int column)
{
      //RULES:
	  //Live cell fewer than 2 live neighbours dies
	  //Live Cell more than 3 live neigbours dies
	  //Live cell 2 or 3 neigbours lives
	  //Dead cell with 3 neigbours becomes alive.
	  
	  //primitives
	  int i, j;
	  int neigbours = 0;
	  
	  //loop through visible non border region
	  //CHECK
	  for (i = 0; i < row; i++)
	  {
	  	for (j = 0; j < column; j++)
	  	{
	  		neigbours = 0;
			//sum up the neigbours
	  		neigbours += North(oldWorld, i, j, row, column);
	  		neigbours += NorthEast(oldWorld, i, j, row, column);
	  		neigbours += NorthWest(oldWorld, i, j, row, column);
	  		neigbours += South(oldWorld, i, j, row, column);
	  		neigbours += SouthEast(oldWorld, i, j, row, column);
	  		neigbours += SouthWest(oldWorld, i, j, row, column);
	  		neigbours += East(oldWorld, i, j, row, column);
	  		neigbours += West(oldWorld, i, j, row, column);
	  		
	  		//check for the rules above
	  		//check if the cell is alive
	  		if (LiveCell(oldWorld, i, j) == TRUE)
	  		{
	  			//Live cell fewer than 2 live neighbours dies
	  			if (neigbours < 2)
	  			{
	  				newWorld[i][j]=' ';
	  				//DEBUG
	  				//printf("\n\nIt's Dead\n");
	  			}
	  			//Live Cell more than 3 live neigbours dies
	  			else if (neigbours > 3)
	  			{
	  				newWorld[i][j]=' ';
	  				//printf("\n\nIt's Dead\n");
	  			}
	  			
				//Live cell 2 or 3 neigbours lives
					//this is redundant since it will stay alive
			  }
	  		else
	  		
	  			//Dead cell with 3 neigbours becomes alive.	
	  			if (neigbours == 3)
	  			{
	  				//It shall be reborn!
					newWorld[i][j]='x';
	  			}
	  		}	
	  	}
}