Pella86

GameEngine.cpp

Jan 11th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #include "GameEngine.h"
  2.  
  3.  
  4.  
  5. bool isStrColOK(std::string answer){
  6.     //recive the format 'a1'
  7.     bool isOK = true;
  8.     if (answer[0] < 'a' or answer[0] > 'h') { //check if column is right
  9.         isOK = false;
  10.     }
  11.     return isOK;
  12. }
  13.  
  14. bool isStrRowOK(std::string answer){
  15.     bool isOK = true;
  16.     int cInpDebug = answer[1];
  17.     cInpDebug -= 48; //to map the number between 1 and 8
  18.     if (cInpDebug < 1 or cInpDebug > 8) { //check if row is right
  19.         isOK = false;
  20.     }
  21.     return isOK;
  22. }
  23.  
  24.  
  25.  
  26. std::string HPlayer::selectUnit() const{
  27.     //takes an input fromt the player
  28.     std::string answer;
  29.     bool BADa = true;
  30.     //parse the string:
  31.     do{
  32.         std::cout<< "Input a position:\n in the format (xy) x = column(letter), column y = row(number)"<<std::endl;
  33.         std::getline(std::cin, answer);
  34.         if (answer.length() != 2) { //check if size is right
  35.             std::cout << "The string is too long, try again"<< std::endl;
  36.             BADa = true;
  37.         }
  38.         else if (!isStrColOK(answer)) { //check if column is right
  39.             std::cout<<"The column is not formatted correctly, try again"<<std::endl;
  40.             BADa = true;
  41.         }
  42.         else if (!isStrRowOK(answer)) { //check if row is right
  43.             std::cout<< "The row is not formatted correctly, try again"<<std::endl;
  44.             BADa = true;
  45.         }
  46.         else {
  47.             //if after all the checks is good return the string
  48.             BADa = false;
  49.         }
  50.  
  51.     }while (BADa);
  52.    
  53.     return answer;
  54. }
  55.  
  56. std::string HPlayer::mkMove() const{
  57.     std::string answer;
  58.     bool BADa = true;
  59.     do {
  60.         std::cout<< "Input a movement:\n in the format (xy:xy) x = column(letter), column y = row(number)"<<std::endl;
  61.         std::getline(std::cin, answer);
  62.        
  63.         std::stringstream ss;
  64.         ss << answer[0] << answer[1];
  65.         std::string from = ss.str();
  66.        
  67.         ss.str("");
  68.         ss << answer[3] << answer[4];
  69.         std::string to = ss.str();
  70.        
  71.         if (answer.length() != 5) {
  72.             std::cout<< "String is too long or too short"<<std::endl;
  73.             BADa = true;
  74.         }
  75.         else if (!isStrRowOK(from) or !isStrColOK(from)) {
  76.             std::cout<< "from position not formatted correctly, try again"<<std::endl;
  77.             BADa = true;
  78.         }
  79.         else if (!isStrRowOK(to) or !isStrColOK(to)) {
  80.             std::cout<< "to position not formatted correctly, try again"<<std::endl;
  81.             BADa = true;           
  82.         }
  83.         else {
  84.             BADa = false;
  85.         }
  86.     } while (BADa);
  87.    
  88.     return answer;
  89. }
  90.  
  91. std::string HPlayer::printPlayer(){
  92.     std::stringstream ss;
  93.     std::string colstr;
  94.     if (color == WHITE) {
  95.         colstr = "White";
  96.     }
  97.     else {
  98.         colstr = "Black";
  99.     }
  100.  
  101.     ss<< "Human player " << colstr;
  102.     return ss.str();
  103. }
  104.  
  105.  
  106.  
  107. std::string CPUPlayer::selectUnit() const{
  108.     std::string a = "a1";
  109.     std::cout<< "I'm in the CPU select unit"<<std::endl;
  110.     return a;
  111. }
  112. std::string CPUPlayer::mkMove() const{
  113.     std::string b = "a1:b1";
  114.     std::cout<< "I'm in the CPU mkMove"<<std::endl;
  115.     return b;
  116. }
  117.  
  118.  
  119. std::string CPUPlayer::printPlayer(){
  120.     std::stringstream ss;
  121.     std::string colstr;
  122.     if (color == WHITE) {
  123.         colstr = "White";
  124.     }
  125.     else {
  126.         colstr = "Black";
  127.     }
  128.    
  129.     ss<< "CPU player " << colstr;
  130.     return ss.str();
  131. }
  132.  
  133. void GEngine::setPlayers(){
  134.     std::string answer;
  135.     bool BADa = true;
  136.     do {
  137.         std::cout<<"player1 is white and is CPU or Human?"<<std::endl;
  138.         std::getline(std::cin, answer);
  139.         if (answer == "h" or answer == "human") {
  140.             p1 = new HPlayer(WHITE);
  141.             BADa = false;
  142.         }
  143.         else if(answer == "pc" or answer == "c" or answer == "cpu"
  144.                 or answer == "CPU"){
  145.             p1 = new CPUPlayer(WHITE);
  146.             BADa = false;
  147.         }
  148.         else {
  149.             BADa = true;
  150.         }
  151.     } while (BADa);
  152.     do {
  153.         std::cout<<"player2 is black and is CPU or Human?"<<std::endl;
  154.         std::getline(std::cin, answer);
  155.         if (answer == "h" or answer == "human") {
  156.             p2 = new HPlayer(BLACK);
  157.             BADa = false;
  158.         }
  159.         else if(answer == "pc" or answer == "c" or answer == "cpu"
  160.                 or answer == "CPU"){
  161.             p2 = new CPUPlayer(BLACK);
  162.             BADa = false;
  163.         }
  164.         else {
  165.             BADa = true;
  166.         }
  167.     } while (BADa);
  168.    
  169. }
  170.  
  171.  
  172. GEngine::GEngine(){
  173.     setPlayers();
  174. }
  175.  
  176. void GEngine::printBoard(){
  177.     board.printBoard();
  178. }
  179.  
  180.  
  181. void GEngine::gameLoop(){
  182.     bool endgame = false;
  183.     while (!endgame) {
  184.         //print the board:
  185.         board.printBoard();
  186.        
  187.         //ask move to p1
  188.         std::string move = "invalid";
  189.         do{
  190.             //check if move is valid
  191.             std::cout<< "Player 1 ";
  192.             move = p1->mkMove();
  193.         }while (!isValidMove(move, p1));
  194.         board.movePiece(move);
  195.         //reset move
  196.         board.printBoard();
  197.        
  198.         //ask move p2
  199.         move = "invalid";
  200.         do {
  201.             std::cout<< "Player 2 ";
  202.             move = p2->mkMove();
  203.         } while (!isValidMove(move, p2));
  204.         board.movePiece(move);
  205.        
  206.         //end turn
  207.         std::cout<< "END TURN\n\n"<<std::endl;
  208.     }
  209. }
  210.  
  211. bool GEngine::isValidMove(std::string mv, Player* p){
  212.     //mv is already correctly formatted
  213.     //get the from to..
  214.    
  215.     bool isValid = true;
  216.     std::stringstream ss;
  217.     ss << mv[0]<< mv[1];
  218.     std::string from = ss.str();
  219.    
  220.     ss.str("");
  221.     ss<< mv[3]<< mv[4];
  222.     std::string to = ss.str();
  223.  
  224.     //check if the piece is there
  225.     if (board.getPieceAt(from).getId() == NONE) {
  226.         std::cout<<"Invalid move: Position "<< from << " empty"<<std::endl;
  227.         return isValid = false;
  228.     }
  229.     //check if the piece is the right color
  230.     if (board.getPieceAt(from).getColor() != p->getColor() ) {
  231.         std::cout<<"Invalid move: Picked "<< from <<" piece with wrong color"<<std::endl;
  232.         return isValid = false;
  233.     }
  234.    
  235.     //check if the piece in the to position is not of player color
  236.     if (board.getPieceAt(to).getColor() == p->getColor()) {
  237.         std::cout << "Invalid move: Positon "<< to <<" already occupied by a piece of the same color"<<std::endl;
  238.         return isValid = false;
  239.     }
  240.     //check if piece can move there
  241.     return isValid = true;
  242. }
  243.  
  244. void GEngine::printPlayers() const {
  245.     std::cout << p1->printPlayer()<<std::endl;
  246.     std::cout << p2->printPlayer()<<std::endl;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment