Advertisement
BorrowTheProgrammer

practice1

Dec 22nd, 2020
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. /*
  2.  *Данная программа реализует шахматную игру
  3.  *Между человеком и компьютером
  4.  */
  5.  
  6. #include <cstring>
  7. #include <iostream>
  8. #include <cmath>
  9. #include <stdio.h>
  10.  
  11. using namespace std;
  12.  
  13. //class Board description
  14. //int deskout(char*);       //deskout method
  15. int deskout(char* p)
  16. {
  17.   p[0] = tolower(p[0]);
  18.   return (p[0] > 'h') || (p[0] < 'a') || (p[1] > '8') || (p[1] < '1');
  19. }
  20.  
  21. char* reflect_pos(char* s)
  22. {
  23.   char* _position;
  24.   _position = s;
  25.   int reflect;
  26.   static char str[2];
  27.   char x = _position[0];      //буква, по горизонтали
  28.   char y = _position[1];      //цифра, по вертикали
  29.   reflect = 7 - ((int(x)-7)%10);
  30.   x = 'a' + reflect;
  31.   str[0]=x;
  32.   str[1]=y;
  33.   return str;
  34. }
  35.  
  36. class Board
  37. {
  38. public:
  39.   void printBoard();       //printg out the board
  40.   void fillBoard(char*, int);    //filling cell of the board
  41.   int get_cell_value(char*);
  42.   int free_cells = 64;             //amout of free cellc, at start amout = 8*8
  43.   int get_free_cells(){return free_cells;}
  44.   void set_attack(int, int, int);
  45. protected:
  46.   int cells[8][8] = {0};        //cells of the Board
  47. };
  48. inline void Board::set_attack(int n, int m, int s)
  49. {
  50.   cells[n][m] = s;
  51. }
  52. inline int Board::get_cell_value(char* p)
  53. {
  54.   int value;
  55.   int x = p[0];
  56.   int y = p[1];
  57.   x  = (x-7)%10;
  58.   y = 7 - ((y-9)%10);
  59.   value = cells[y][x];
  60.   return value;
  61. }
  62.  
  63. inline void Board::printBoard()
  64. {
  65.   int i, j;
  66.   cout << "  a b c d e f g h" << endl;
  67.   for(i = 0; i < 8; i++)
  68.   {
  69.     int a = 8;
  70.     cout << a-i << ' ';
  71.     for(j = 0; j < 8; j++)
  72.     {
  73.       switch(cells[i][j])
  74.       {
  75.         case 0:
  76.           cout << '.' << ' ';
  77.           break;
  78.         case 1:
  79.           cout << 'H' << ' ';
  80.           break;
  81.         case 2:
  82.           cout << 'C' << ' ';
  83.           break;
  84.         case 3:
  85.           cout << '+' << ' ';
  86.           break;
  87.         case 4:
  88.           cout << 'x' << ' ';
  89.       }
  90.     }
  91.     cout << a-i << ' ';
  92.     cout << endl;
  93.   }
  94.   cout << "  a b c d e f g h" << endl;
  95. }
  96.  
  97. inline void Board::fillBoard(char* p, int s)
  98. {
  99.   int x = p[0];
  100.   int y = p[1];
  101.   x  = (x-7)%10;
  102.   y = 7 - ((y-9)%10);
  103.   cells[y][x] = s;
  104. }
  105. //virtual Figure class description
  106. class Gamer
  107. {
  108. public:
  109.   Gamer(Board&, const char*);   //pos inizialisation and getting adress of the Board object
  110.   virtual void atack(char*) = 0;  //virtual atack method
  111.   virtual char* move(char*) = 0;  //virtual move method
  112.   virtual ~Gamer(){};
  113.   const char* get_name(){return name;}
  114.   int check_pos(char*);
  115.   int query(char*);              //this function determinetes who is playing now
  116. protected:
  117.   Board* board;             //adress of object Board
  118.   const char* name;         //player's name
  119. };
  120.  
  121. inline int Gamer::check_pos(char* s)
  122. {
  123.   if(board->get_cell_value(s) == 2)
  124.   {
  125.     cout << "This cell is already beasy by Computer!" << endl;
  126.     cout << "Gave is over!" << endl;
  127.     return 2;
  128.   }
  129.   if(board->get_cell_value(s) == 1)
  130.   {
  131.     cout << "This cell is already beasy by Human!" << endl;
  132.     cout << "Game is over!" << endl;
  133.     return 1;
  134.   }
  135.   if(board->get_cell_value(s) == 4)
  136.   {
  137.     cout << "This cell is under computers attack!" << endl;
  138.     cout << "Game is over!" << endl;
  139.     return 4;
  140.   }
  141. }
  142. inline int Gamer::query(char* p)
  143. {
  144.   cout << name << " > " << p << endl;
  145.   return board->get_free_cells();
  146. }
  147. inline Gamer::Gamer(Board& b, const char* n)
  148. {
  149.   board = &b;
  150.   name = n;
  151. }
  152.  
  153. //class Human description
  154. class Human: public Gamer
  155. {
  156. private:
  157.   int pos_symbol = 1;       //значение позиции
  158.   int atack_symbol = 3;     //значение атаки
  159. public:
  160.   Human(Board& b, const char* n): Gamer(b, n){}
  161.   virtual void atack(char*);
  162.   virtual char* move(char*);
  163. };
  164.  
  165. inline char* Human::move(char* s)
  166. {
  167.   char* _position;
  168.   _position = s;
  169.   board->fillBoard(_position, pos_symbol);
  170.   board->free_cells--;
  171. }
  172.  
  173. inline void Human::atack(char* s)
  174. {
  175.   //function description
  176.   int i, j;
  177.   int x = (s[0]-7)%10;
  178.   int y = 7-(s[1]-9)%10;
  179.   int dx, dy;
  180.   for(i = 0; i < 8; i++)
  181.   {
  182.     dy = abs(y - i);
  183.     for(j = 0; j < 8; j++)
  184.     {
  185.       dx = abs(x - j);
  186.       if((dx+dy==3) && (dx!=0) && (dy!=0))
  187.       {
  188.         board->set_attack(i, j, atack_symbol);
  189.       }
  190.     }
  191.   }
  192.  
  193. }
  194.  
  195. //class Computer description
  196. class Computer: public Gamer
  197. {
  198. private:
  199.   int pos_symbol = 2;       //значение позиции
  200.   int atack_symbol = 4;     //значение атаки
  201. public:
  202.   Computer(Board& b, const char* n): Gamer(b, n){}
  203.   virtual void atack(char*);
  204.   virtual char* move(char*);
  205. };
  206.  
  207. inline void Computer::atack(char* s)
  208. {
  209.   //function description
  210.   s = reflect_pos(s);
  211.   int i, j;
  212.   int x = (s[0]-7)%10;
  213.   int y = 7-(s[1]-9)%10;
  214.   int dx, dy;
  215.   for(i = 0; i < 8; i++)
  216.   {
  217.     dy = abs(y - i);
  218.     for(j = 0; j < 8; j++)
  219.     {
  220.       dx = abs(x - j);
  221.       if((dx+dy==3) && (dx!=0) && (dy!=0))
  222.       {
  223.         board->set_attack(i, j, atack_symbol);
  224.       }
  225.     }
  226.   }
  227.  
  228. }
  229.  
  230. inline char* Computer::move(char* s)
  231. {
  232.   char* _position;
  233.   _position = reflect_pos(s);
  234.   cout << name << " > " << _position << endl;
  235.   board->fillBoard(_position, pos_symbol);
  236.   board->free_cells--;
  237.   return _position;
  238. }
  239. //end of class description
  240.  
  241. int main(int args, char* argv[])
  242. {
  243.   char* pos;
  244.   char* position;
  245.   int i = 0;
  246.   int length;
  247.   Board board;    //object of class Board
  248.   Gamer* g[] = {new Human(board, "Human"), new Computer(board, "Computer")};
  249.   while(cin >> pos, board.free_cells > 0)
  250.   {
  251.     length = strlen(pos);
  252.     position = new char[length];
  253.     strcpy(position, pos);
  254.     if(deskout(position) > 0 || strlen(position) != 2)
  255.     {
  256.       cout << "Error while input!" << endl;
  257.       cout << "Game is over!" << endl;
  258.       return 1;
  259.     }
  260.     g[0]->query(position);
  261.     if((g[0]->check_pos(position)) > 0)
  262.       return 1;
  263.     g[i]->move(position);
  264.     g[i]->atack(position);
  265.     board.printBoard();
  266.     i++;
  267.     g[i]->move(position);
  268.     g[i]->atack(position);
  269.     board.printBoard();
  270.     if(i > 0)
  271.       i = 0;
  272.     delete position;
  273.   }
  274.   cout << "Winner is " << g[i]->get_name() << endl;
  275.  
  276.   delete g[1];
  277.   delete g[2];
  278.   //delete[] position;
  279.   return 0;
  280. }
  281.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement