Need a bit of help with C++, guys. And I apologise if the code snippets here are very messy; I've been just running through trying to implement everything, and I plan on coming back and cleaning all of my code and adding some better comments when it's ready.
I'm working on a version of Tic-Tac-Toe that would ideally support resizable maps. The game works perfectly as-is using a hardcoded array, which I can use a constant to quickly change the size of the grid, but I want to upgrade the system to using a vector instead so the user can specify the size of the grid when they start the game.
While I could use a single dimension array/vector and simply do a bit more math, using a multi-dimensional vector is more easily readable for myself. Moving on, I'm aware of how to initialise a multi-dimensional vector, as so:
#include <vector>
vector<vector<char>> myVec;
Theoretically, this is supposed to be usable the exact same as an array (with the [][] operators), so all I should need to do to make my program compatible is switch the function arguments to take this type of vector instead of an array, and change the initialisation function like so:
void InitialiseBoard(std::vector<std::vector<char>> boardVector)
{
for (int i = 0; i < BOARD_SIZE; i++)
{
std::vector<char> vecRow;
for (int j = 0; j < BOARD_SIZE; j++)
{
vecRow.push_back(' ');
}
boardVector.push_back(vecRow);
}
}
Now, this all works until the program gets to a specific link in a specific function, as shown below:
PlayerInput GetInput(std::vector<std::vector<char>> boardVector, bool *isPlayerOne)
{
char xPos = 0;
char yPos = 0;
while (true)
{
if (*isPlayerOne) std::cout << "\nPlayer One [X], please select a ROW [A - " << (char)('A' + (BOARD_SIZE - 1)) << "]: ";
else std::cout << "\nPlayer Two [O], please select a ROW [A - " << (char)('A' + (BOARD_SIZE - 1)) << "]: ";
while (true)
{
std::cin >> yPos;
if (yPos == QUIT_KEY_UPPER || yPos == QUIT_KEY_LOWER) { std::cout << '\n'; return PlayerInput::EXIT; }
else if (yPos == RESTART_KEY_UPPER || yPos == RESTART_KEY_LOWER) { std::cout << '\n'; return PlayerInput::RESTART; }
else if (yPos < 'A' || (yPos > ('A' + (BOARD_SIZE - 1)) && yPos < 'a') || yPos >('a' + BOARD_SIZE)) { SanitiseCoordInput(InputCoords::YAXIS); }
else if (yPos >= 'A' && yPos <= ('A' + BOARD_SIZE)) { yPos -= 'A'; break; }
else if (yPos >= 'a' && yPos <= ('a' + BOARD_SIZE)) { yPos -= 'a'; break; }
}
//Clear the input buffer even if the input was correct to prevent format breaking
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (*isPlayerOne) std::cout << "Player One [X], please select a COLUMN [1 - " << BOARD_SIZE << "]: ";
else std::cout << "Player Two [O], please select a COLUMN [1 - " << BOARD_SIZE << "]: ";
while (true)
{
std::cin >> xPos;
if (xPos == QUIT_KEY_UPPER || xPos == QUIT_KEY_LOWER) { std::cout << '\n'; return PlayerInput::EXIT; }
else if (xPos == RESTART_KEY_UPPER || xPos == RESTART_KEY_LOWER) { std::cout << '\n'; return PlayerInput::RESTART; }
else if (xPos < '1' || xPos > ('1' + (BOARD_SIZE - 1))) { SanitiseCoordInput(InputCoords::XAXIS); }
else { xPos = GetFixedNumber(xPos - '0'); break; }
}
/* THIS IS THE LINE WITH THE PROBLEM!!!! */
if (boardVector[xPos][yPos] == ' ') break;
else std::cout << "\nThere's already something at \'" << (char)(yPos + 'A') << (int)(xPos + 1) << "\'! Please try again.\n";
}
boardVector[xPos][yPos] = *isPlayerOne ? 'X' : 'O';
std::cout << '\n';
return PlayerInput::MOVE;
}
For some reason, Visual Studio breaks at if (boardVector[xPos][yPos] == ' ') break;, complaining that "Vector subscript out of range." It's able to correctly take all the input, but once it does that simple check for an empty space, kaput. I get that it's a standard out-of-range error that should indicate numbers that are are less than or greater than the vector's range, EXCEPT that this code works perfectly if I'm using a standard char[][] array and I designed several functions to prevent this kind of situation. I cannot see anything in this code that would cause an issue.
Does anybody have an idea on what the issue is here?
Here's the GetFixedPosition() function code, in case it helps:
int GetFixedNumber(int input)
{
int midNum = BOARD_SIZE / 2;
if (BOARD_SIZE % 2 != 0 && input == midNum) input++;
else if (input > midNum) input = BOARD_SIZE - input;
else if (input < midNum) input = -(input - BOARD_SIZE);
return input;
}
It's job is simply to swap the position of the numbers that the user inputs because the display of the grid is opposite the way the array is organised, and to also remove 1 from the result because the array is 0-indexed.