std::string MoveToStrLong(Move mv)
{
	std::string s = FldToStr(mv.From()) + FldToStr(mv.To());
	switch (mv.Promotion())
	{
	case QW: case QB: s += "q"; break;
	case RW: case RB: s += "r"; break;
	case BW: case BB: s += "b"; break;
	case NW: case NB: s += "n"; break;
	default: break;
	}
	return s;
}
std::string Position::Fen() const
{
  const std::string names = "-?PpNnBbRrQqKk";
  std::string fen;
  int empty = 0;
  for (int f = 0; f < 64; ++f)
  {
    PIECE p = m_board[f];
    if (p)
    {
      if (empty)
      {
        char t1[3];
        sprintf(t1, "%d", empty);
        fen += std::string(t1);
        empty = 0;
      }
      fen += names.substr(p, 1);
    }
    else
      ++empty;

    if (Col(f) == 7)
    {
      if (empty)
      {
        char t1[3];
        sprintf(t1, "%d", empty);
        fen += std::string(t1);
        empty = 0;
      }
      if (f != 63)
        fen += "/";
    }
  }

  if (m_side == WHITE)
    fen += " w";
  else
    fen += " b";

  if (m_castlings & 0x33)
  {
    fen += " ";
    if (m_castlings & WHITE_O_O)   
      fen += "K";
    if (m_castlings & WHITE_O_O_O) 
      fen += "Q";
    if (m_castlings & BLACK_O_O)   
      fen += "k";
    if (m_castlings & BLACK_O_O_O) 
      fen += "q";
  }
  else
    fen += " -";

  if (m_ep == NF)
    fen += " -";
  else
  {
    fen += " ";
    fen += FldToStr(m_ep);
  }

  return fen;
}
Exemple #3
0
string MoveToStrShort(Move mv, Position& pos, MoveList* mvlist)
{
  if (mv.Piece() == KW && mv.From() == E1 && mv.To() == G1) return "O-O";
  if (mv.Piece() == KW && mv.From() == E1 && mv.To() == C1) return "O-O-O";
  if (mv.Piece() == KB && mv.From() == E8 && mv.To() == G8) return "O-O";
  if (mv.Piece() == KB && mv.From() == E8 && mv.To() == C8) return "O-O-O";

  FLD from = mv.From();
  FLD to = mv.To();
  PIECE piece = mv.Piece();
  PIECE captured = mv.Captured();
  PIECE promotion = mv.Promotion();

  string strPiece, strFrom, strTo, strCapture, strPromotion;
  switch (piece)
  {
    case PW: case PB: break;
    case NW: case NB: strPiece = "N"; break;
    case BW: case BB: strPiece = "B"; break;
    case RW: case RB: strPiece = "R"; break;
    case QW: case QB: strPiece = "Q"; break;
    case KW: case KB: strPiece = "K"; break;
    default: break;
  }

  strTo = FldToStr(to);
  if (captured)
  {
    strCapture = "x";
    if (piece == PW || piece == PB)
      strFrom = FldToStr(from).substr(0, 1);
  }

  switch (promotion)
  {
    case QW: case QB: strPromotion += "=Q"; break;
    case RW: case RB: strPromotion += "=R"; break;
    case BW: case BB: strPromotion += "=B"; break;
    case NW: case NB: strPromotion += "=N"; break;
  }

  bool ambiguity = false;
  bool uniqRow = true;
  bool uniqCol = true;

  if (piece != PW && piece != PB)
  {
    for (MoveList* ptr = mvlist; ptr->mv; ++ptr)
    {
      Move mv1 = ptr->mv;

      if (mv1 == mv) continue;
      if (mv1.To() != to) continue;
      if (mv1.Piece() != piece) continue;

      if (!pos.MakeMove(mv1)) continue;
      pos.UnmakeMove();

      ambiguity = true;
      if (Row(mv1.From()) == Row(from)) uniqRow = false;
      if (Col(mv1.From()) == Col(from)) uniqCol = false;
    }
  }

  if (ambiguity)
  {
    if (uniqCol)
      strFrom = FldToStr(from).substr(0, 1);
    else if (uniqRow)
      strFrom = FldToStr(from).substr(1, 1);
    else
      strFrom = FldToStr(from);
  }

  return strPiece + strFrom + strCapture + strTo + strPromotion;
}
std::string MoveToStrShort(Move mv, Position& pos)
{
	if (mv == Move(E1, G1, KW) || mv == Move(E8, G8, KB))
		return "O-O";
	if (mv == Move(E1, C1, KW) || mv == Move(E8, C8, KB))
		return "O-O-O";

	PIECE piece = mv.Piece();
	FLD from = mv.From();
	FLD to = mv.To();
	PIECE captured = mv.Captured();
	PIECE promotion = mv.Promotion();
	std::string strFrom, strTo, strPiece, strCaptured, strPromotion;

	switch (piece)
	{
	case PW: case PB:
		if (captured)
			strFrom = FldToStr(from).substr(0, 1);
		break;
	case NW: case NB: strPiece = "N"; break;
	case BW: case BB: strPiece = "B"; break;
	case RW: case RB: strPiece = "R"; break;
	case QW: case QB: strPiece = "Q"; break;
	case KW: case KB: strPiece = "K"; break;
	default: break;
	}

	if (captured)
		strCaptured = "x";

	strTo = FldToStr(to);

	switch (promotion)
	{
	case QW: case QB: strPromotion = "=Q"; break;
	case RW: case RB: strPromotion = "=R"; break;
	case BW: case BB: strPromotion = "=B"; break;
	case NW: case NB: strPromotion = "=N"; break;
	default: break;
	}

	// resolve ambiguity

	int uniq_col = 1;
	int uniq_row = 1;
	bool ambiguity = false;

	int row0 = Row(from);
	int col0 = Col(from);

	MoveList mvlist;
	mvlist.GenAllMoves(pos);

	for (int i = 0; i < mvlist.Size(); ++i)
	{
		Move mvi = mvlist[i].m_mv;

		if (mvi.To() != to)
			continue;
		if (mvi.Piece() != piece)
			continue;
		if (mvi.From() == from)
			continue;
		if (!pos.MakeMove(mvi))
			continue;
		pos.UnmakeMove();

		ambiguity = true; // two or more pieces of the same type can move to field
		int row1 = Row(mvi.From());
		int col1 = Col(mvi.From());

		if (row0 == row1)
			uniq_row = 0;

		if (col0 == col1)
			uniq_col = 0;
	}

	if (ambiguity)
	{
		if (uniq_col)
			strFrom = FldToStr(from).substr(0, 1);
		else if (uniq_row)
			strFrom = FldToStr(from).substr(1, 1);
		else
			strFrom = FldToStr(from);
	}

	return strPiece + strFrom + strCaptured + strTo + strPromotion;
}