Off Topic > Off Topic
Programming Megathread
$trinick:
--- Quote from: Otis Da HousKat on May 04, 2016, 08:11:09 PM ---SQL is so wordy it feels like you're shouting commands at the computer.
--- End quote ---
SELECT NAME FROM PLAYER WHERE ID = 2000
It literally sounds like you're commanding the computer. Makes sense to be in caps, it seems meek otherwise.
--- Quote from: blueblur121 on May 04, 2016, 06:10:02 PM ---Wait, people did that?
Thats loving stupid.
--- End quote ---
I mean, back in the beginning I'm not even sure there was a lower case character set. It probably just stuck.
Dodger:
does anyone have any good references for actually getting good and knowledgeable with object orientated C++?
Like I get the basics of implementing common ADTs like stacks and queues, but I suck with the more advanced memory management functionalities and stuff like good exception handling. I want to get better.
McJob:
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:
--- Code: ---#include <vector>
vector<vector<char>> myVec;
--- End code ---
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:
--- Code: ---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);
}
}
--- End code ---
Now, this all works until the program gets to a specific link in a specific function, as shown below:
--- Code: ---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;
}
--- End code ---
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:
--- Code: ---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;
}
--- End code ---
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.
devildogelite:
From my experience with 3d arrays in my Crypto class, I've found it to be a huge pain in the ass passing them through functions. Have you checked to see what values are in the vector to make sure it's actually passing through and isn't just null.
SetGaming:
When you're pulling the xPos and yPos values from cin, does this make them a string? If so, the compiler could be interpreting the xPos and yPos by their strict string/char value, instead of a number. That'd be my guess, at least.