12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include "Queen.h"
- #include "Position.h"
- #include <utility>
- #include <stdexcept>
- #include <iostream>
- Queen::Queen(Position pos, char c) {
- if (c != 'B' and c != 'W')
- throw std::invalid_argument("Impossible color of figure!");
- position = pos;
- color = c;
- }
- Figure* Queen::copy() {
- static Queen q(position, color);
- Figure* f = &q;
- return f;
- }
- vector<Position> Queen::get_moves()
- {
- vector<Position> moves;
- // Сначала ходы по диагоналям
- for(int i = 1; i < 8; i++) {
- for(int j = -1; j < 2; j+=2) {
- for(int k = -1; k < 2; k+=2) {
- // Не добавляем невозможные ходы
- if (not (position.posSym + i*j <= 'H' and position.posNum + i*k <= '8' and position.posSym + i*j >= 'A' and position.posNum + i*k >= '1'))
- continue;
- // Position создаст ошибку, если создать невозможный ход
- Position p;
- try {
- p = Position((char)(position.posSym+i*j), (char)(position.posNum+i*k));
- }
- catch (const std::invalid_argument) {
- p = Position();
- }
- moves.push_back(p);
- }
- }
- }
- // По прямым
- for (int i = 1; i < 9; i++) {
- Position p;
- Position p2;
- // Position создаст ошибку, если создать невозможный ход
- try {
- if ('0'+i != position.posNum) p2 = Position(position.posSym, (char)('0'+i));
- if ('0'+i+16 != position.posSym) p = Position((char)('0'+i+16), position.posNum);
- }
- catch (const std::invalid_argument) {
- continue;
- }
- if ('0'+i+16 != position.posSym) {
- moves.push_back(p);
- }
- if ('0'+i != position.posNum) {
- moves.push_back(p2);
- }
- }
- return moves;
- }
- const char* Queen::print()
- {
- const char* me;
- if (color == 'B')
- me = "♛";
- else
- me = "♕";
- return me;
- }
|