]> git.sesse.net Git - stockfish/commitdiff
Retire PieceLetters struct
authorMarco Costalba <mcostalba@gmail.com>
Fri, 10 Jun 2011 13:50:39 +0000 (15:50 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 10 Jun 2011 16:12:10 +0000 (17:12 +0100)
Use a much simpler std::string instead.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/position.cpp

index 4613ec4d63eb87594c5c4127ddd6a1006710a72e..ef90913cdfa9ed99c4d69a8227e9aafe45154cda 100644 (file)
@@ -20,7 +20,6 @@
 #include <cassert>
 #include <cstring>
 #include <fstream>
 #include <cassert>
 #include <cstring>
 #include <fstream>
-#include <map>
 #include <iostream>
 #include <sstream>
 
 #include <iostream>
 #include <sstream>
 
@@ -77,33 +76,8 @@ namespace {
   // Bonus for having the side to move (modified by Joona Kiiski)
   const Score TempoValue = make_score(48, 22);
 
   // Bonus for having the side to move (modified by Joona Kiiski)
   const Score TempoValue = make_score(48, 22);
 
-  struct PieceLetters : public std::map<char, Piece> {
-
-    PieceLetters() {
-
-      operator[]('K') = WK; operator[]('k') = BK;
-      operator[]('Q') = WQ; operator[]('q') = BQ;
-      operator[]('R') = WR; operator[]('r') = BR;
-      operator[]('B') = WB; operator[]('b') = BB;
-      operator[]('N') = WN; operator[]('n') = BN;
-      operator[]('P') = WP; operator[]('p') = BP;
-      operator[](' ') = PIECE_NONE;
-      operator[]('.') = PIECE_NONE_DARK_SQ;
-    }
-
-    char from_piece(Piece p) const {
-
-      std::map<char, Piece>::const_iterator it;
-      for (it = begin(); it != end(); ++it)
-          if (it->second == p)
-              return it->first;
-
-      assert(false);
-      return 0;
-    }
-  };
-
-  PieceLetters pieceLetters;
+  // To convert a Piece to and from a FEN char
+  const string PieceToChar(".PNBRQK  pnbrqk  ");
 }
 
 
 }
 
 
@@ -192,17 +166,19 @@ void Position::from_fen(const string& fen, bool isChess960) {
 
   char token;
   int hmc, fmn;
 
   char token;
   int hmc, fmn;
-  std::istringstream ss(fen);
+  size_t p;
   Square sq = SQ_A8;
   Square sq = SQ_A8;
+  std::istringstream ss(fen);
 
   clear();
 
   clear();
+  ss >> std::noskipws;
 
   // 1. Piece placement field
 
   // 1. Piece placement field
-  while (ss.get(token) && token != ' ')
+  while ((ss >> token) && !isspace(token))
   {
   {
-      if (pieceLetters.find(token) != pieceLetters.end())
+      if ((p = PieceToChar.find(token)) != string::npos)
       {
       {
-          put_piece(pieceLetters[token], sq);
+          put_piece(Piece(p), sq);
           sq++;
       }
       else if (isdigit(token))
           sq++;
       }
       else if (isdigit(token))
@@ -214,23 +190,23 @@ void Position::from_fen(const string& fen, bool isChess960) {
   }
 
   // 2. Active color
   }
 
   // 2. Active color
-  if (!ss.get(token) || (token != 'w' && token != 'b'))
+  if (!(ss >> token) || (token != 'w' && token != 'b'))
       goto incorrect_fen;
 
   sideToMove = (token == 'w' ? WHITE : BLACK);
 
       goto incorrect_fen;
 
   sideToMove = (token == 'w' ? WHITE : BLACK);
 
-  if (!ss.get(token) || token != ' ')
+  if (!(ss >> token) || !isspace(token))
       goto incorrect_fen;
 
   // 3. Castling availability
       goto incorrect_fen;
 
   // 3. Castling availability
-  while (ss.get(token) && token != ' ')
+  while ((ss >> token) && !isspace(token))
       if (!set_castling_rights(token))
           goto incorrect_fen;
 
   // 4. En passant square
   char col, row;
       if (!set_castling_rights(token))
           goto incorrect_fen;
 
   // 4. En passant square
   char col, row;
-  if (   (ss.get(col) && (col >= 'a' && col <= 'h'))
-      && (ss.get(row) && (row == '3' || row == '6')))
+  if (   ((ss >> col) && (col >= 'a' && col <= 'h'))
+      && ((ss >> row) && (row == '3' || row == '6')))
   {
       st->epSquare = make_square(file_from_char(col), rank_from_char(row));
 
   {
       st->epSquare = make_square(file_from_char(col), rank_from_char(row));
 
@@ -241,7 +217,7 @@ void Position::from_fen(const string& fen, bool isChess960) {
   }
 
   // 5. Halfmove clock
   }
 
   // 5. Halfmove clock
-  if (ss >> hmc)
+  if (ss >> std::skipws >> hmc)
       st->rule50 = hmc;
 
   // 6. Fullmove number
       st->rule50 = hmc;
 
   // 6. Fullmove number
@@ -337,10 +313,12 @@ const string Position::to_fen() const {
 
   string fen;
   Square sq;
 
   string fen;
   Square sq;
-  char emptyCnt = '0';
+  char emptyCnt;
 
   for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/')
   {
 
   for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/')
   {
+      emptyCnt = '0';
+
       for (File file = FILE_A; file <= FILE_H; file++)
       {
           sq = make_square(file, rank);
       for (File file = FILE_A; file <= FILE_H; file++)
       {
           sq = make_square(file, rank);
@@ -352,16 +330,13 @@ const string Position::to_fen() const {
                   fen += emptyCnt;
                   emptyCnt = '0';
               }
                   fen += emptyCnt;
                   emptyCnt = '0';
               }
-              fen += pieceLetters.from_piece(piece_on(sq));
+              fen += PieceToChar[piece_on(sq)];
           } else
               emptyCnt++;
       }
 
       if (emptyCnt != '0')
           } else
               emptyCnt++;
       }
 
       if (emptyCnt != '0')
-      {
           fen += emptyCnt;
           fen += emptyCnt;
-          emptyCnt = '0';
-      }
   }
 
   fen += (sideToMove == WHITE ? " w " : " b ");
   }
 
   fen += (sideToMove == WHITE ? " w " : " b ");
@@ -413,7 +388,7 @@ void Position::print(Move move) const {
               piece = PIECE_NONE_DARK_SQ;
 
           char c = (color_of_piece_on(sq) == BLACK ? '=' : ' ');
               piece = PIECE_NONE_DARK_SQ;
 
           char c = (color_of_piece_on(sq) == BLACK ? '=' : ' ');
-          cout << c << pieceLetters.from_piece(piece) << c << '|';
+          cout << c << PieceToChar[piece] << c << '|';
       }
   }
   cout << dottedLine << "Fen is: " << to_fen() << "\nKey is: " << st->key << endl;
       }
   }
   cout << dottedLine << "Fen is: " << to_fen() << "\nKey is: " << st->key << endl;