Pella86

Board.cpp

Jan 11th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. #include "Board.h"
  2.  
  3. typedef std::pair<int,int> PositionPair;
  4.  
  5. std::pair<int,int> rcFromStr(std::string str){
  6. #ifdef DEBUG
  7.     if(str.length() != 2){
  8.         PRINTVAR(str);
  9.         std::cerr<< " the string too long "<<std::endl;
  10.         exit(1);
  11.     }
  12.     if(str[0] < 'a' or str[0] > 'h'){
  13.         PRINTVAR(str);
  14.         std::cerr<<"the column is not formatted correctly"<<std::endl;
  15.         exit(1);
  16.     }
  17.     int cInpDebug = str[1];
  18.     cInpDebug -= 48; //ASCII Table map
  19.     if (cInpDebug < 1 or cInpDebug > 8) {
  20.         PRINTVAR(cInpDebug);
  21.         std::cerr<<"the column is not formatted correctly"<<std::endl;
  22.         exit(1);
  23.     }
  24. #endif
  25.     char cInp = str[0];
  26.     int getCol = cInp-'a';
  27.     int getRow = str[1]-1;
  28.     getRow -=  48;
  29.     getRow = 7- getRow; //invert the row because the index are reversed
  30.     std::pair<int, int> p(getRow,getCol);
  31.     return p;
  32. }
  33.  
  34.  
  35. std::string strFromRC(int row, int col){
  36. #ifdef DEBUG
  37.     if (row < 0 or row >= 8) {
  38.         PRINTVAR(row);
  39.         std::cerr<< "Row out of range"<<std::endl;
  40.     }
  41.     if (col < 0 or col >= 8) {
  42.         PRINTVAR(col);
  43.         std::cerr<< "Coloumn out of range"<<std::endl;
  44.     }
  45. #endif
  46.    
  47.     //open a stringstream to contain the newly formed string
  48.     std::stringstream ss;
  49.     //map the column to a char
  50.     char cCol = 'a'+col;
  51.     ss<<cCol<<(row);
  52.     return ss.str();
  53. }
  54.  
  55. MAPidSTRING::MAPidSTRING(){
  56.     //initialize the map with names
  57.     idToStr.insert(std::make_pair(NONE, "NONE"));
  58.     idToStr.insert(std::make_pair(PAWN, "PAWN"));
  59.     idToStr.insert(std::make_pair(KNIGHT, "KNIGHT"));
  60.     idToStr.insert(std::make_pair(BISHOP, "BISHOP"));
  61.     idToStr.insert(std::make_pair(ROOK, "ROOK"));
  62.     idToStr.insert(std::make_pair(KING, "KING"));
  63.     idToStr.insert(std::make_pair(QUEEN, "QUEEN"));
  64.    
  65.     idToOneLetterStr.insert(std::make_pair(NONE, "-"));
  66.     idToOneLetterStr.insert(std::make_pair(PAWN, "P"));
  67.     idToOneLetterStr.insert(std::make_pair(KNIGHT, "k"));
  68.     idToOneLetterStr.insert(std::make_pair(BISHOP, "B"));
  69.     idToOneLetterStr.insert(std::make_pair(ROOK, "R"));
  70.     idToOneLetterStr.insert(std::make_pair(KING, "K"));
  71.     idToOneLetterStr.insert(std::make_pair(QUEEN, "Q"));
  72.    
  73.     idColToStr.insert(std::make_pair(colNONE, "-"));
  74.     idColToStr.insert(std::make_pair(BLACK, "b"));
  75.     idColToStr.insert(std::make_pair(WHITE, "w"));
  76.    
  77.    
  78. }
  79. std::string const& MAPidSTRING::operator[](PiecesID ID){
  80.     //returns the string corresponding to the id
  81.     return idToStr[ID];
  82. }
  83.  
  84.  
  85. ChessBoard::ChessBoard(){
  86.     Piece wR1(ROOK,WHITE);
  87.     Piece wR2(ROOK,WHITE);
  88.     Piece wB1(BISHOP,WHITE);
  89.     Piece wB2(BISHOP,WHITE);
  90.     Piece wk1(KNIGHT,WHITE);
  91.     Piece wk2(KNIGHT,WHITE);
  92.     Piece wK(KING,WHITE);
  93.     Piece wQ(QUEEN,WHITE);
  94.    
  95.     putPiece("a1",wR1);
  96.     putPiece("b1",wk1);
  97.     putPiece("c1",wB1);
  98.     putPiece("d1",wQ);
  99.     putPiece("e1",wK);
  100.     putPiece("f1",wB2);
  101.     putPiece("g1",wk2);
  102.     putPiece("h1",wR2);
  103.    
  104.    
  105.     Piece bR1(ROOK,BLACK);
  106.     Piece bR2(ROOK,BLACK);
  107.     Piece bB1(BISHOP,BLACK);
  108.     Piece bB2(BISHOP,BLACK);
  109.     Piece bk1(KNIGHT,BLACK);
  110.     Piece bk2(KNIGHT,BLACK);
  111.     Piece bK(KING,BLACK);
  112.     Piece bQ(QUEEN,BLACK);
  113.    
  114.     putPiece("a8",bR1);
  115.     putPiece("b8",bk1);
  116.     putPiece("c8",bB1);
  117.     putPiece("d8",bQ);
  118.     putPiece("e8",bK);
  119.     putPiece("f8",bB2);
  120.     putPiece("g8",bk2);
  121.     putPiece("h8",bR2);
  122.    
  123.     Piece wP(PAWN,WHITE);
  124.     Piece bP(PAWN,BLACK);
  125.    
  126.     for (int i = 0; i<8; ++i) {
  127.         std::string pos = strFromRC(2,i);
  128.         putPiece(pos, wP);
  129.     }
  130.     for (int i = 0; i<8; ++i) {
  131.         std::string pos = strFromRC(7,i);
  132.         putPiece(pos, bP);
  133.     }
  134.    
  135. }
  136.  
  137. Piece const& ChessBoard::getPieceAt(std::string pos) const{
  138.     PositionPair p = rcFromStr(pos);
  139.     return board[p.first][p.second];
  140. }
  141.  
  142. bool ChessBoard::isEmpty(std::string pos){
  143.    
  144.     Piece const& p = getPieceAt(pos);
  145.     if (p.getId() == NONE) {
  146.         return true;
  147.     }
  148.     else {
  149.         return false;
  150.     }
  151.  
  152. }
  153.  
  154. void ChessBoard::putPiece(std::string pos, Piece p){
  155. #ifdef DEBUG
  156. if (!isEmpty(pos)) {
  157.     PRINTVAR(pos);
  158.     PRINTVAR(idStr.getOneLetterStr(p.getId()));
  159.     std::cerr<<"The position is already occupied"<<std::endl;
  160.     exit(1);
  161. }
  162. #endif
  163.     PositionPair position = rcFromStr(pos);
  164.     board[position.first][position.second] = p;
  165. }
  166. void ChessBoard::removePiece(std::string pos){
  167. #ifdef DEBUG
  168.     if (isEmpty(pos)) {
  169.         PRINTVAR(pos);
  170.         std::cerr<<"The position is already empty"<<std::endl;
  171.         exit(1);
  172.     }
  173. #endif
  174.    
  175.     PositionPair position = rcFromStr(pos);
  176.     board[position.first][position.second].setId() = NONE;
  177.     board[position.first][position.second].setColor() = colNONE;
  178. }
  179. void ChessBoard::movePiece(std::string fromTo){
  180.     //check if string has the right size
  181. #ifdef DEBUG
  182.     if (fromTo.length() != 5) {
  183.         PRINTVAR(fromTo);
  184.         std::cerr<< "String is too long or too short"<<std::endl;
  185.         exit(1);
  186.     }
  187. #endif
  188.     //split the from to string in two coordinates
  189.     std::stringstream ss;
  190.     ss<<fromTo[0]<<fromTo[1];
  191.     std::string from = ss.str();
  192.    
  193.     ss.str("");
  194.     ss<<fromTo[3]<<fromTo[4];
  195.     std::string to = ss.str();
  196.    
  197.     //get the piece in the original position
  198.     Piece const& From_Piece = getPieceAt(from);
  199.    
  200.     //check if on the position there is a piece (!= NONE) of the other color
  201.     if (getPieceAt(to).getColor() != getPieceAt(from).getColor() and getPieceAt(to).getId() != NONE) {
  202.         std::cout<< "you eaten a "<<idStr[getPieceAt(to).getId()]<<std::endl;
  203.         removePiece(to); //remove that piece
  204.         putPiece(to, From_Piece); //place the new piece
  205.         removePiece(from);//remove the piece from original position
  206.     }
  207.     else{ //the square is empty
  208.         putPiece(to,From_Piece);
  209.         removePiece(from);
  210.     }
  211. }
  212.  
  213. rcPair ChessBoard::getRowColAt(std::string pos) const{
  214.     return rcFromStr(pos);
  215. }
  216.  
  217.  
  218. void ChessBoard::printBoard() {
  219.     std::cout<<"-|--a-|--b-|--c-|--d-|--e-|--f-|--g-|--h-|"<<std::endl;
  220.     for (int row = 0; row<8; ++row) {
  221.         std::cout<< 8-row <<"|";
  222.         for (int col = 0; col<8; ++col) {
  223.             if ((row %2) == 0 xor (col%2) == 0) {
  224.                 std::cout<<"+"<<idStr.getColToStr(board[row][col].getColor())
  225.                     <<idStr.getOneLetterStr(board[row][col].getId())<<"+|";
  226.             }
  227.             else {
  228.                 std::cout<<"-"<<idStr.getColToStr(board[row][col].getColor())
  229.                 <<idStr.getOneLetterStr(board[row][col].getId())<<"-|";
  230.             }
  231.  
  232.            
  233.         }
  234.         std::cout<<std::endl;
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment