Advertisement
Guest User

Chess.cpp

a guest
May 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <typeinfo>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7.  
  8. class Figure
  9. {
  10. protected:
  11. int x;
  12. int y;
  13. public:
  14. Figure (int x1,int y1){
  15. if (x1 < 'a' || x1 > 'h' || y1 < '1' || y1 > '8') throw "incorrect position (class Figure)\n" ;
  16. x = x1-'a';
  17. y = y1 - '0'-1;
  18. }
  19.  
  20. virtual bool move(int x,int y)=0;
  21. virtual ~Figure(){}
  22.  
  23. int get_x(){ return x;}
  24. int get_y(){ return y;}
  25. };
  26.  
  27. /**
  28. * К О Р О Л Ь
  29. */
  30.  
  31. class King: public Figure
  32.  
  33. {
  34.  
  35. public:
  36. King( int x , int y ):Figure(x,y){}
  37.  
  38.  
  39. bool move( int x1, int y1 ){
  40. if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false;// throw "out of board\n" ;
  41.  
  42. if (abs(x-x1) <=1 && abs(y-y1) <= 1 && ( x != x1 || y != y1))
  43. {
  44. x = x1;
  45. y = y1;
  46. return true;
  47.  
  48. }
  49. else return false ;//throw" ERROR moving King\n";
  50. }
  51. };
  52.  
  53. /**
  54. * Л А Д Ь Я
  55. */
  56.  
  57. class Rook :public Figure
  58.  
  59. {
  60.  
  61. public:
  62. Rook( int x , int y ):Figure(x,y){}
  63.  
  64.  
  65. bool move( int x1, int y1 ){
  66. if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false; //throw "out of board\n" ;
  67.  
  68. if (abs(x-x1) <=7 && abs(y-y1) <= 7 && (( x != x1 && y == y1) || ( x == x1 && y != y1)) )
  69. {
  70. x = x1;
  71. y = y1;
  72. return true;
  73. }
  74. else return false;//throw " ERROR moving Rook\n";
  75.  
  76. }
  77. };
  78.  
  79. /**
  80. * К О Р О Л Е В А
  81. */
  82.  
  83. class Queen :public Figure
  84.  
  85. {
  86.  
  87. public:
  88. Queen( int x , int y ):Figure(x,y){}
  89.  
  90.  
  91. bool move( int x1, int y1 ){
  92. cout<<"MOVING QUEEN FROM "<<x<<y<<"to"<<x1<<y1<<endl;
  93. if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false;//throw "out of board" ; /// исправить для всех
  94.  
  95. if ((abs(x-x1) <= 7 && abs(y-y1) <= 7 ) &&
  96. (( x != x1 && y == y1) || ( x == x1 && y != y1) || (x+x1 == y+y1) || (x-x1 == y-y1)) &&
  97. ( x != x1 || y != y1)
  98. )
  99. {
  100. x = x1;
  101. y = y1;
  102. return true;
  103. }
  104. else return false ;//throw " ERROR moving Queen\n";
  105.  
  106. }
  107. };
  108.  
  109. /**
  110. * К О Н Ь
  111. */
  112.  
  113. class Knight : public Figure
  114. {
  115.  
  116. public:
  117. Knight( int x , int y ):Figure(x,y){}
  118.  
  119.  
  120. bool move( int x1, int y1 ){
  121. if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false; //throw "out of board\n" ;
  122.  
  123.  
  124. if (((abs(x-x1) <= 2 && abs(y-y1) <= 1) || (abs(x-x1) <= 1 && abs(y-y1) <= 2)) && ( x != x1 || y != y1) )
  125. {
  126. x = x1;
  127. y = y1;
  128. return true;
  129.  
  130. }
  131. else return false;//throw " ERROR moving Knight\n";
  132.  
  133. }
  134. };
  135.  
  136. /**
  137. * С Л О Н
  138. */
  139.  
  140. class Bishop : public Figure
  141.  
  142. {
  143.  
  144. public:
  145. Bishop( int x , int y ):Figure(x,y){}
  146.  
  147. void setCoordinates( int x, int y );
  148. int getX();
  149. int getY();
  150. bool move( int x1, int y1 ){
  151. if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false;//throw "out of board\n" ;
  152.  
  153. if ((abs(x-x1) <= 7 && abs(y-y1) <= 7 ) && ((x+x1 == y+y1) || (x-x1 == y-y1) ) && ( x != x1 || y != y1) )
  154. {
  155. x = x1;
  156. y = y1;
  157. return true;
  158.  
  159. }
  160. else return false;//throw " ERROR moving Bishop\n";
  161.  
  162. }
  163. };
  164.  
  165. /**
  166. * просто главный класс
  167. */
  168.  
  169. class ChessChecker
  170. {
  171. Figure* board[64];// "шахматная доска" - массив указателей на базовый класс, каждый указатель - адрес фигуры или null
  172. class Parser // производный класс// метод который читает строчку
  173. {
  174. public:
  175. const char* f;
  176. int x_from;
  177. int y_from;
  178. int x_to;
  179. int y_to;
  180.  
  181. bool get_re()
  182. {
  183. char a;
  184. while ((a=cin.get()) == ' ' || a =='\t' || a =='\n') ;// сделать через ссылку на обьект от класса Figure
  185. if (a == 'K' || a == 'k') // дописать всех
  186. f = typeid(King).name();
  187. else
  188.  
  189. if (a == 'Q' || a == 'q')
  190. f = typeid(Queen).name();
  191. else
  192. if (a == 'R' || a == 'r')
  193. f = typeid(Rook).name();
  194. else
  195. if (a == 'N' || a == 'n')
  196. f = typeid(Knight).name();
  197. else
  198. if (a == 'B' || a == 'b')
  199. f = typeid(Bishop).name();
  200. else
  201. if (a == 'e' && cin.get () == 'x' && cin.get () == 'i'&& cin.get () == 't' && cin.get () == '\n') {return false;}
  202.  
  203.  
  204. else throw "ERROR: incorrect name of figure\n";
  205.  
  206. while ((a=cin.get()) == ' ' || a =='\t' || a =='\n') ;
  207. if ( a >= 'a' && a<= 'h')
  208. x_from =a - 'a';
  209. else throw "ERROR-1\n";
  210. a=cin.get();
  211. if ( a >= '1' && a<= '8')
  212. y_from = a - '0' -1;
  213. while ((a=cin.get()) == ' ' || a =='\t' || a =='\n') ;
  214. if ( a >= 'a' && a<= 'h')
  215. x_to = a - 'a';
  216. else throw "ERROR-2\n";
  217. a=cin.get();
  218. if ( a >= '1' && a<= '8')
  219. y_to = a - '0' -1;
  220. return true;
  221.  
  222. }
  223. };
  224.  
  225.  
  226. Parser P;
  227.  
  228. public:
  229. ChessChecker(){
  230. for (int i = 0; i < 64; ++i)
  231. {
  232. board[i] = NULL;
  233. }
  234. }
  235. ~ChessChecker() {
  236. for (int i = 0; board[i] != NULL; ++i) delete board[i];
  237. }
  238.  
  239. void add_figure(char t,int x1,int y1){
  240. int i;
  241. cout<<"ADD FIGURE "<<endl;
  242. if (board[63]!= NULL) throw "NO PLACE FOR Figure\n";
  243. for (i = 0; i < 64 && board[i] != NULL; ++i){ // просматриваем сущ фигуры
  244. if (board[i]->get_x() == x1 - 'a' && board[i]->get_y() == y1-1) throw " pos is busy\n"; // позиция свободна?
  245. }
  246. cout << "FIGURE "<<t << endl;
  247. switch (t)
  248. {
  249. case 'K' : case 'k': // дописать для ост
  250. board[i] = new King(x1,y1);
  251. break;
  252. case 'Q' : case 'q':
  253. board[i] = new Queen(x1,y1);
  254. //cout << "Queen" << endl;
  255. break;
  256. case 'N' : case 'n':
  257. board[i] = new Knight(x1,y1);
  258. break;
  259. case 'R' : case 'r':
  260. board[i] = new Rook(x1,y1);
  261. break;
  262. case 'B' : case 'b':
  263. board[i] = new Bishop(x1,y1);
  264. break;
  265.  
  266. default: ;
  267. }
  268. cout<<" FIGURE ADDED "<<endl;
  269. }
  270. bool check (){
  271. if (P.get_re() == false) return false;
  272. int i;
  273. for ( i = 0; board[i] != NULL && i < 64; ++i){
  274.  
  275. if(board[i]-> get_x() == P.x_from && board[i]-> get_y() == P.y_from && (strcmp(typeid(*board[i]).name(), P.f)==0)) break ;// нашли фигуру
  276.  
  277. }
  278.  
  279. if (i == 64 || board[i] == NULL) throw " no figure on this pos\n";
  280. cout << "control" << endl;
  281.  
  282. if (board[i]->move(P.x_to,P.y_to)){
  283. cout << "OK\n" ;
  284. // сделали ход
  285. /**
  286. * позиция свободна? или освободить (убрать из массива фигуру)
  287. */
  288.  
  289. int j;
  290. for ( j = 0; board[j] != NULL && j < 64; ++j){
  291. if (j== i) continue; /******************добавлено, чтобы не удалить фигуру, которую только что подвинули*/
  292. if(board[j]-> get_x() == P.x_to && board[j]-> get_y() == P.y_to) break;
  293. }
  294. if (j < 64 && board[j] != NULL){ // удалили фигуру с позиции на которую ход
  295. delete board[j];
  296.  
  297. for (int i = j+1;i < 64;i++)
  298. {
  299. board[i -1] = board[i];
  300. if (board[i] == NULL) break;
  301. }
  302. }
  303. }
  304. else cout << " ERROR-3 (supposedly impossible move )\n";
  305. return true;
  306. }
  307.  
  308. };
  309. int main()
  310.  
  311. {
  312. char y[2] = {'y',0};
  313. try {
  314. ChessChecker a;
  315. cout << " -> | START | <- \n";
  316. cout << " The following chess pieces are created at the specified positions :\n";
  317. a.add_figure('Q','a','1');
  318. a.add_figure('k','e','5');
  319. a.add_figure('r','h','7');
  320.  
  321. while(!strcmp ( y,"y")){
  322. try{
  323. cout << "Please, enter the shape and the desired position :\n";
  324.  
  325. if (a.check() == false) {cout << "f w " << endl; return 0;}
  326. }
  327. catch(const char* s){cout<<"INCORRECT: "<<s<<endl;}
  328. catch(...){cout<<"INCORRECT\n";}
  329. cout << "continue?(y/n)"<< endl;
  330. cin >> y;
  331.  
  332. }
  333. }
  334. catch(const char* ex){cerr<< ex << endl;}
  335. return 0;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement