Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <typeinfo>
- #include <cstring>
- using namespace std;
- class Figure
- {
- protected:
- int x;
- int y;
- public:
- Figure (int x1,int y1){
- if (x1 < 'a' || x1 > 'h' || y1 < '1' || y1 > '8') throw "incorrect position (class Figure)\n" ;
- x = x1-'a';
- y = y1 - '0'-1;
- }
- virtual bool move(int x,int y)=0;
- virtual ~Figure(){}
- int get_x(){ return x;}
- int get_y(){ return y;}
- };
- /**
- * К О Р О Л Ь
- */
- class King: public Figure
- {
- public:
- King( int x , int y ):Figure(x,y){}
- bool move( int x1, int y1 ){
- if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false;// throw "out of board\n" ;
- if (abs(x-x1) <=1 && abs(y-y1) <= 1 && ( x != x1 || y != y1))
- {
- x = x1;
- y = y1;
- return true;
- }
- else return false ;//throw" ERROR moving King\n";
- }
- };
- /**
- * Л А Д Ь Я
- */
- class Rook :public Figure
- {
- public:
- Rook( int x , int y ):Figure(x,y){}
- bool move( int x1, int y1 ){
- if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false; //throw "out of board\n" ;
- if (abs(x-x1) <=7 && abs(y-y1) <= 7 && (( x != x1 && y == y1) || ( x == x1 && y != y1)) )
- {
- x = x1;
- y = y1;
- return true;
- }
- else return false;//throw " ERROR moving Rook\n";
- }
- };
- /**
- * К О Р О Л Е В А
- */
- class Queen :public Figure
- {
- public:
- Queen( int x , int y ):Figure(x,y){}
- bool move( int x1, int y1 ){
- cout<<"MOVING QUEEN FROM "<<x<<y<<"to"<<x1<<y1<<endl;
- if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false;//throw "out of board" ; /// исправить для всех
- if ((abs(x-x1) <= 7 && abs(y-y1) <= 7 ) &&
- (( x != x1 && y == y1) || ( x == x1 && y != y1) || (x+x1 == y+y1) || (x-x1 == y-y1)) &&
- ( x != x1 || y != y1)
- )
- {
- x = x1;
- y = y1;
- return true;
- }
- else return false ;//throw " ERROR moving Queen\n";
- }
- };
- /**
- * К О Н Ь
- */
- class Knight : public Figure
- {
- public:
- Knight( int x , int y ):Figure(x,y){}
- bool move( int x1, int y1 ){
- if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false; //throw "out of board\n" ;
- if (((abs(x-x1) <= 2 && abs(y-y1) <= 1) || (abs(x-x1) <= 1 && abs(y-y1) <= 2)) && ( x != x1 || y != y1) )
- {
- x = x1;
- y = y1;
- return true;
- }
- else return false;//throw " ERROR moving Knight\n";
- }
- };
- /**
- * С Л О Н
- */
- class Bishop : public Figure
- {
- public:
- Bishop( int x , int y ):Figure(x,y){}
- void setCoordinates( int x, int y );
- int getX();
- int getY();
- bool move( int x1, int y1 ){
- if (x1 < 0 || x1 > 7 || y1 < 0 || y1 > 7) return false;//throw "out of board\n" ;
- if ((abs(x-x1) <= 7 && abs(y-y1) <= 7 ) && ((x+x1 == y+y1) || (x-x1 == y-y1) ) && ( x != x1 || y != y1) )
- {
- x = x1;
- y = y1;
- return true;
- }
- else return false;//throw " ERROR moving Bishop\n";
- }
- };
- /**
- * просто главный класс
- */
- class ChessChecker
- {
- Figure* board[64];// "шахматная доска" - массив указателей на базовый класс, каждый указатель - адрес фигуры или null
- class Parser // производный класс// метод который читает строчку
- {
- public:
- const char* f;
- int x_from;
- int y_from;
- int x_to;
- int y_to;
- bool get_re()
- {
- char a;
- while ((a=cin.get()) == ' ' || a =='\t' || a =='\n') ;// сделать через ссылку на обьект от класса Figure
- if (a == 'K' || a == 'k') // дописать всех
- f = typeid(King).name();
- else
- if (a == 'Q' || a == 'q')
- f = typeid(Queen).name();
- else
- if (a == 'R' || a == 'r')
- f = typeid(Rook).name();
- else
- if (a == 'N' || a == 'n')
- f = typeid(Knight).name();
- else
- if (a == 'B' || a == 'b')
- f = typeid(Bishop).name();
- else
- if (a == 'e' && cin.get () == 'x' && cin.get () == 'i'&& cin.get () == 't' && cin.get () == '\n') {return false;}
- else throw "ERROR: incorrect name of figure\n";
- while ((a=cin.get()) == ' ' || a =='\t' || a =='\n') ;
- if ( a >= 'a' && a<= 'h')
- x_from =a - 'a';
- else throw "ERROR-1\n";
- a=cin.get();
- if ( a >= '1' && a<= '8')
- y_from = a - '0' -1;
- while ((a=cin.get()) == ' ' || a =='\t' || a =='\n') ;
- if ( a >= 'a' && a<= 'h')
- x_to = a - 'a';
- else throw "ERROR-2\n";
- a=cin.get();
- if ( a >= '1' && a<= '8')
- y_to = a - '0' -1;
- return true;
- }
- };
- Parser P;
- public:
- ChessChecker(){
- for (int i = 0; i < 64; ++i)
- {
- board[i] = NULL;
- }
- }
- ~ChessChecker() {
- for (int i = 0; board[i] != NULL; ++i) delete board[i];
- }
- void add_figure(char t,int x1,int y1){
- int i;
- cout<<"ADD FIGURE "<<endl;
- if (board[63]!= NULL) throw "NO PLACE FOR Figure\n";
- for (i = 0; i < 64 && board[i] != NULL; ++i){ // просматриваем сущ фигуры
- if (board[i]->get_x() == x1 - 'a' && board[i]->get_y() == y1-1) throw " pos is busy\n"; // позиция свободна?
- }
- cout << "FIGURE "<<t << endl;
- switch (t)
- {
- case 'K' : case 'k': // дописать для ост
- board[i] = new King(x1,y1);
- break;
- case 'Q' : case 'q':
- board[i] = new Queen(x1,y1);
- //cout << "Queen" << endl;
- break;
- case 'N' : case 'n':
- board[i] = new Knight(x1,y1);
- break;
- case 'R' : case 'r':
- board[i] = new Rook(x1,y1);
- break;
- case 'B' : case 'b':
- board[i] = new Bishop(x1,y1);
- break;
- default: ;
- }
- cout<<" FIGURE ADDED "<<endl;
- }
- bool check (){
- if (P.get_re() == false) return false;
- int i;
- for ( i = 0; board[i] != NULL && i < 64; ++i){
- if(board[i]-> get_x() == P.x_from && board[i]-> get_y() == P.y_from && (strcmp(typeid(*board[i]).name(), P.f)==0)) break ;// нашли фигуру
- }
- if (i == 64 || board[i] == NULL) throw " no figure on this pos\n";
- cout << "control" << endl;
- if (board[i]->move(P.x_to,P.y_to)){
- cout << "OK\n" ;
- // сделали ход
- /**
- * позиция свободна? или освободить (убрать из массива фигуру)
- */
- int j;
- for ( j = 0; board[j] != NULL && j < 64; ++j){
- if (j== i) continue; /******************добавлено, чтобы не удалить фигуру, которую только что подвинули*/
- if(board[j]-> get_x() == P.x_to && board[j]-> get_y() == P.y_to) break;
- }
- if (j < 64 && board[j] != NULL){ // удалили фигуру с позиции на которую ход
- delete board[j];
- for (int i = j+1;i < 64;i++)
- {
- board[i -1] = board[i];
- if (board[i] == NULL) break;
- }
- }
- }
- else cout << " ERROR-3 (supposedly impossible move )\n";
- return true;
- }
- };
- int main()
- {
- char y[2] = {'y',0};
- try {
- ChessChecker a;
- cout << " -> | START | <- \n";
- cout << " The following chess pieces are created at the specified positions :\n";
- a.add_figure('Q','a','1');
- a.add_figure('k','e','5');
- a.add_figure('r','h','7');
- while(!strcmp ( y,"y")){
- try{
- cout << "Please, enter the shape and the desired position :\n";
- if (a.check() == false) {cout << "f w " << endl; return 0;}
- }
- catch(const char* s){cout<<"INCORRECT: "<<s<<endl;}
- catch(...){cout<<"INCORRECT\n";}
- cout << "continue?(y/n)"<< endl;
- cin >> y;
- }
- }
- catch(const char* ex){cerr<< ex << endl;}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement