Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Данная программа реализует шахматную игру
- *Между человеком и компьютером
- */
- #include <cstring>
- #include <iostream>
- #include <cmath>
- #include <stdio.h>
- using namespace std;
- //class Board description
- //int deskout(char*); //deskout method
- int deskout(char* p)
- {
- p[0] = tolower(p[0]);
- return (p[0] > 'h') || (p[0] < 'a') || (p[1] > '8') || (p[1] < '1');
- }
- char* reflect_pos(char* s)
- {
- char* _position;
- _position = s;
- int reflect;
- static char str[2];
- char x = _position[0]; //буква, по горизонтали
- char y = _position[1]; //цифра, по вертикали
- reflect = 7 - ((int(x)-7)%10);
- x = 'a' + reflect;
- str[0]=x;
- str[1]=y;
- return str;
- }
- class Board
- {
- public:
- void printBoard(); //printg out the board
- void fillBoard(char*, int); //filling cell of the board
- int get_cell_value(char*);
- int free_cells = 64; //amout of free cellc, at start amout = 8*8
- int get_free_cells(){return free_cells;}
- void set_attack(int, int, int);
- protected:
- int cells[8][8] = {0}; //cells of the Board
- };
- inline void Board::set_attack(int n, int m, int s)
- {
- cells[n][m] = s;
- }
- inline int Board::get_cell_value(char* p)
- {
- int value;
- int x = p[0];
- int y = p[1];
- x = (x-7)%10;
- y = 7 - ((y-9)%10);
- value = cells[y][x];
- return value;
- }
- inline void Board::printBoard()
- {
- int i, j;
- cout << " a b c d e f g h" << endl;
- for(i = 0; i < 8; i++)
- {
- int a = 8;
- cout << a-i << ' ';
- for(j = 0; j < 8; j++)
- {
- switch(cells[i][j])
- {
- case 0:
- cout << '.' << ' ';
- break;
- case 1:
- cout << 'H' << ' ';
- break;
- case 2:
- cout << 'C' << ' ';
- break;
- case 3:
- cout << '+' << ' ';
- break;
- case 4:
- cout << 'x' << ' ';
- }
- }
- cout << a-i << ' ';
- cout << endl;
- }
- cout << " a b c d e f g h" << endl;
- }
- inline void Board::fillBoard(char* p, int s)
- {
- int x = p[0];
- int y = p[1];
- x = (x-7)%10;
- y = 7 - ((y-9)%10);
- cells[y][x] = s;
- }
- //virtual Figure class description
- class Gamer
- {
- public:
- Gamer(Board&, const char*); //pos inizialisation and getting adress of the Board object
- virtual void atack(char*) = 0; //virtual atack method
- virtual char* move(char*) = 0; //virtual move method
- virtual ~Gamer(){};
- const char* get_name(){return name;}
- int check_pos(char*);
- int query(char*); //this function determinetes who is playing now
- protected:
- Board* board; //adress of object Board
- const char* name; //player's name
- };
- inline int Gamer::check_pos(char* s)
- {
- if(board->get_cell_value(s) == 2)
- {
- cout << "This cell is already beasy by Computer!" << endl;
- cout << "Gave is over!" << endl;
- return 2;
- }
- if(board->get_cell_value(s) == 1)
- {
- cout << "This cell is already beasy by Human!" << endl;
- cout << "Game is over!" << endl;
- return 1;
- }
- if(board->get_cell_value(s) == 4)
- {
- cout << "This cell is under computers attack!" << endl;
- cout << "Game is over!" << endl;
- return 4;
- }
- }
- inline int Gamer::query(char* p)
- {
- cout << name << " > " << p << endl;
- return board->get_free_cells();
- }
- inline Gamer::Gamer(Board& b, const char* n)
- {
- board = &b;
- name = n;
- }
- //class Human description
- class Human: public Gamer
- {
- private:
- int pos_symbol = 1; //значение позиции
- int atack_symbol = 3; //значение атаки
- public:
- Human(Board& b, const char* n): Gamer(b, n){}
- virtual void atack(char*);
- virtual char* move(char*);
- };
- inline char* Human::move(char* s)
- {
- char* _position;
- _position = s;
- board->fillBoard(_position, pos_symbol);
- board->free_cells--;
- }
- inline void Human::atack(char* s)
- {
- //function description
- int i, j;
- int x = (s[0]-7)%10;
- int y = 7-(s[1]-9)%10;
- int dx, dy;
- for(i = 0; i < 8; i++)
- {
- dy = abs(y - i);
- for(j = 0; j < 8; j++)
- {
- dx = abs(x - j);
- if((dx+dy==3) && (dx!=0) && (dy!=0))
- {
- board->set_attack(i, j, atack_symbol);
- }
- }
- }
- }
- //class Computer description
- class Computer: public Gamer
- {
- private:
- int pos_symbol = 2; //значение позиции
- int atack_symbol = 4; //значение атаки
- public:
- Computer(Board& b, const char* n): Gamer(b, n){}
- virtual void atack(char*);
- virtual char* move(char*);
- };
- inline void Computer::atack(char* s)
- {
- //function description
- s = reflect_pos(s);
- int i, j;
- int x = (s[0]-7)%10;
- int y = 7-(s[1]-9)%10;
- int dx, dy;
- for(i = 0; i < 8; i++)
- {
- dy = abs(y - i);
- for(j = 0; j < 8; j++)
- {
- dx = abs(x - j);
- if((dx+dy==3) && (dx!=0) && (dy!=0))
- {
- board->set_attack(i, j, atack_symbol);
- }
- }
- }
- }
- inline char* Computer::move(char* s)
- {
- char* _position;
- _position = reflect_pos(s);
- cout << name << " > " << _position << endl;
- board->fillBoard(_position, pos_symbol);
- board->free_cells--;
- return _position;
- }
- //end of class description
- int main(int args, char* argv[])
- {
- char* pos;
- char* position;
- int i = 0;
- int length;
- Board board; //object of class Board
- Gamer* g[] = {new Human(board, "Human"), new Computer(board, "Computer")};
- while(cin >> pos, board.free_cells > 0)
- {
- length = strlen(pos);
- position = new char[length];
- strcpy(position, pos);
- if(deskout(position) > 0 || strlen(position) != 2)
- {
- cout << "Error while input!" << endl;
- cout << "Game is over!" << endl;
- return 1;
- }
- g[0]->query(position);
- if((g[0]->check_pos(position)) > 0)
- return 1;
- g[i]->move(position);
- g[i]->atack(position);
- board.printBoard();
- i++;
- g[i]->move(position);
- g[i]->atack(position);
- board.printBoard();
- if(i > 0)
- i = 0;
- delete position;
- }
- cout << "Winner is " << g[i]->get_name() << endl;
- delete g[1];
- delete g[2];
- //delete[] position;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement