не могу найти ошибку типа vector subscript out of range
В dev-c++ программа работает, а в VS2019 прерывается с ошибкой "vector subscript out of range". Если закомментировать строку ниже, то работает и там и там.
std::cout << "feasible_moves_w[w_rand].end_cell" << feasible_moves_w[w_rand].end_cell << '\n' << "piece_b[aa]=" << piece_b[bb] << '\n'; piece_b[bb] = 64; std::cout << "figura chernih vibila\n";
Что не так с присвоением piece_b[bb] = 64? Программа полностью.
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <string>
struct piece_move
{
char piece;
int start_cell;
char move_type;
int end_cell;
};
bool crange(const int& value)
{
return ((value >= 0) && (value < 64));
}
bool is_white(const char& value)
{
return ((value == 'P') || (value == 'R') || (value == 'N') || (value == 'B') || (value == 'Q') || (value == 'K'));
}
bool is_black(const char& value)
{
return ((value == 'p') || (value == 'r') || (value == 'n') || (value == 'b') || (value == 'q') || (value == 'k'));
}
bool snag(const char& value1, const char& value2)
{
if ((value2 == ' ') || (value1 == ' '))
return 0;
else
return (is_white(value1) == is_white(value2));
}
bool enemy(const char& value1, const char& value2)
{
if ((value2 == ' ') || (value1 == ' '))
return 0;
else
return (is_white(value1) == is_black(value2));
}
namespace board
{
enum board
{
A1 = 0, B1 = 1, C1 = 2, D1 = 3, E1 = 4, F1 = 5, G1 = 6, H1 = 7,
A2 = 8, B2 = 9, C2 = 10, D2 = 11, E2 = 12, F2 = 13, G2 = 14, H2 = 15,
A3 = 16, B3 = 17, C3 = 18, D3 = 19, E3 = 20, F3 = 21, G3 = 22, H3 = 23,
A4 = 24, B4 = 25, C4 = 26, D4 = 27, E4 = 28, F4 = 29, G4 = 30, H4 = 31,
A5 = 32, B5 = 33, C5 = 34, D5 = 35, E5 = 36, F5 = 37, G5 = 38, H5 = 39,
A6 = 40, B6 = 41, C6 = 42, D6 = 43, E6 = 44, F6 = 45, G6 = 46, H6 = 47,
A7 = 48, B7 = 49, C7 = 50, D7 = 51, E7 = 52, F7 = 53, G7 = 54, H7 = 55,
A8 = 56, B8 = 57, C8 = 58, D8 = 59, E8 = 60, F8 = 61, G8 = 62, H8 = 63,
};
}
void moves(const std::vector<char>& cells, std::vector<piece_move>& moves, const int& piece)
{
switch (cells[piece])
{
case 'P':
if ((crange(piece + 8)) && (crange(piece + 8)) && (cells[piece + 8] == ' '))
{
moves.push_back({ 'P',piece,'-',piece + 8 });
if ((piece / 8 == 1) && (crange(piece + 16)) && (cells[piece + 16] == ' ') && (cells[piece + 8] == ' '))
moves.push_back({ 'P',piece,'-',piece + 16 });
}
if ((crange(piece + 8 - 1)) && (piece % 8 != 0) && (is_black(cells[piece + 8 - 1])))
moves.push_back({ 'P',piece,'x',piece + 8 - 1 });
if ((crange(piece + 8 + 1)) && (piece % 8 != 7) && (is_black(cells[piece + 8 + 1])))
moves.push_back({ 'P',piece,'x',piece + 8 + 1 });
break;
case 'p':
if ((crange(piece - 8)) && (crange(piece - 8)) && (cells[piece - 8] == ' '))
{
moves.push_back({ 'p',piece,'-',piece - 8 });
if ((piece / 8 == 6) && (crange(piece - 16)) && (cells[piece - 16] == ' ') && (cells[piece - 8] == ' '))
moves.push_back({ 'p',piece,'-',piece - 16 });
}
if ((crange(piece - 8 - 1)) && (piece % 8 != 0) && (is_white(cells[piece - 8 - 1])))
moves.push_back({ 'p',piece,'x',piece - 8 - 1 });
if ((crange(piece - 8 + 1)) && (piece % 8 != 7) && (is_white(cells[piece - 8 + 1])))
moves.push_back({ 'P',piece,'x',piece - 8 + 1 });
break;
}
}
int main()
{
std::vector<char> cells;
cells.reserve(64);
std::vector<int> piece_w;
std::vector<int> piece_b;
piece_b.reserve(16);
std::vector<piece_move> feasible_moves_w;
feasible_moves_w.reserve(256);
std::vector<piece_move> feasible_moves_b;
feasible_moves_b.reserve(256);
cells.push_back('R');
cells.push_back('N');
cells.push_back('B');
cells.push_back('Q');
cells.push_back('K');
cells.push_back('B');
cells.push_back('N');
cells.push_back('R');
cells.push_back('P');
cells.push_back('P');
cells.push_back('P');
cells.push_back('P');
cells.push_back('P');
cells.push_back('P');
cells.push_back('P');
cells.push_back('P');
for (int ii = 0; ii < 32; ++ii)
cells.push_back(' ');
cells.push_back('p');
cells.push_back('p');
cells.push_back('p');
cells.push_back('p');
cells.push_back('p');
cells.push_back('p');
cells.push_back('p');
cells.push_back('p');
cells.push_back('r');
cells.push_back('n');
cells.push_back('b');
cells.push_back('q');
cells.push_back('k');
cells.push_back('b');
cells.push_back('n');
cells.push_back('r');
for (auto ii = 0; ii < 64; ++ii)
{
std::cout << cells[ii] << ' ';
if ((ii + 1) % 8 == 0)
std::cout << '\n';
}
piece_w.push_back(board::A2);
piece_w.push_back(board::B2);
piece_w.push_back(board::C2);
piece_w.push_back(board::D2);
piece_w.push_back(board::E2);
piece_w.push_back(board::F2);
piece_w.push_back(board::G2);
piece_w.push_back(board::H2);
piece_w.push_back(board::A1); piece_w.push_back(board::H1);
piece_w.push_back(board::B1);
piece_w.push_back(board::C1); piece_w.push_back(board::F1);
piece_w.push_back(board::D1);
piece_w.push_back(board::E1);
piece_b.push_back(board::A7);
piece_b.push_back(board::B7);
piece_b.push_back(board::C7);
piece_b.push_back(board::D7);
piece_b.push_back(board::E7);
piece_b.push_back(board::F7);
piece_b.push_back(board::G7);
piece_b.push_back(board::H7);
piece_b.push_back(board::A8); piece_b.push_back(board::H8);
piece_b.push_back(board::B8); piece_b.push_back(board::G8);
piece_b.push_back(board::C8); piece_b.push_back(board::F8);
piece_b.push_back(board::D8);
piece_b.push_back(board::E8);
int count2{ 0 };
srand(time(NULL));
int w_rand{ 0 }, b_rand{ 0 };
for (int aa = 0; aa < 1000; ++aa)
{
for (int ii = 0; ii < piece_w.size(); ++ii)
{
moves(cells, feasible_moves_w, piece_w[ii]);
}
for (auto it : piece_b)
{
moves(cells, feasible_moves_b, it);
}
if (((feasible_moves_w.size()) > 0) && ((feasible_moves_b.size()) > 0))
{
w_rand = rand() % (feasible_moves_w.size());
b_rand = rand() % (feasible_moves_b.size());
}
else break;
if (feasible_moves_w[w_rand].move_type == '-')
{
for (auto ii = 0; ii < piece_w.size(); ++ii)
{
if (piece_w[ii] == feasible_moves_w[w_rand].start_cell)
{
piece_w[ii] = feasible_moves_w[w_rand].end_cell; cells[feasible_moves_w[w_rand].start_cell] = ' '; cells[feasible_moves_w[w_rand].end_cell] = feasible_moves_w[w_rand].piece;
}
}
}
else if (feasible_moves_w[w_rand].move_type == 'x')
{
for (auto ii = 0; ii < piece_w.size(); ++ii)
{
if (piece_w[ii] == feasible_moves_w[w_rand].start_cell)
{
piece_w[ii] = feasible_moves_w[w_rand].end_cell; cells[feasible_moves_w[w_rand].start_cell] = ' '; cells[feasible_moves_w[w_rand].end_cell] = feasible_moves_w[w_rand].piece;
for (auto bb = 0; bb < piece_b.size(); ++bb)
{
if (piece_b[bb] == feasible_moves_w[w_rand].end_cell)
{
std::cout << "feasible_moves_w[w_rand].end_cell" << feasible_moves_w[w_rand].end_cell << '\n' << "piece_b[aa]=" << piece_b[bb] << '\n'; piece_b[bb] = 64; std::cout << "figura chernih vibila\n";
}
}
}
}
}
else std::cerr << "FIGNYA v feasible_moves_w=" << feasible_moves_w[w_rand].move_type << '\n';
feasible_moves_w.clear();
feasible_moves_b.clear();
}
for (auto ii = 0; ii < 64; ++ii)
{
std::cout << cells[ii] << ' ';
if ((ii + 1) % 8 == 0)
std::cout << '\n';
}
for (auto it : piece_w)
std::cout << "piece_w[]=" << it << '\n';
for (auto it : piece_b)
std::cout << "piece_b[]=" << it << '\n';
return 0;
}
Ответы (1 шт):
Автор решения: AlexGlebe
→ Ссылка
После записывания сообщения figura chernih vibila вы сами ставите piece_b[bb] = 64; А потом в функции move делаете switch (cells.at(piece)) (было раньше switch (cells[piece])) что приводит к крушению. Сначала надо проверять, что она ушла на звёзды посмотреть.
Помогает такое :
void moves(const std::vector<char>& cells,
std::vector<piece_move>& moves, const int& piece)
{
if(piece==64)
return;
switch (cells.at(piece))
Читаемость кода плохая. Длинные строки программы неудобны.