Hey guys,
I am trying to get into the swing of Object Oriented Programming and with that said, I am making a Tic Tac Toe game just for the fun of it but I'm trying to pretty strictly use classes/objects. I've made several Tic Tac Toe games before when using more linear/procedural source code. However I am a little bit stuck. I tried to create a 2D array as a private member of my class and I now know that this isn't really possible. So I turned to vectors which I've seen from other resources to be my answer. But when I tried to make a 2D Vector equivalent to a 2D Array I've been running into some issues. I can't seem to get the equivalent to a basic 2D array which I could easily do\using arrays.
Here is my header file for my class so far...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#ifndef GAME_H
#define GAME_H
#include <vector>
#include <iostream>
using namespace std;
class Game
{
private:
char Player1Symbol;
char Player2Symbol;
int row;
int col;
vector <vector <char>> PrintBoard; //equivalent to a 2D array
public:
Game(int PlayerChoice, const int Row, const int Col)
{
if (PlayerChoice == 1)
{
Player1Symbol = 'X';
Player2Symbol = 'O';
}
else if (PlayerChoice == 2)
{
Player1Symbol = 'O';
Player2Symbol = 'X';
}
row = Row;
col = Col;
PrintBoard[row][col];
}
void SetGameBoard()
{
PrintBoard[0][0] = '1', PrintBoard[0][1] = '2', PrintBoard[0][2] = '3';
PrintBoard[1][0] = '4', PrintBoard[1][1] = '5', PrintBoard[1][2] = '6';
PrintBoard[2][0] = '7', PrintBoard[2][1] = '8', PrintBoard[2][2] = '9';
}
void PrintGameBoard()
{
cout << endl;
for (int x = 0; x <= row; x++)
{
for (int y = 0; y <= col; y++)
{
cout << "|" << PrintBoard[x][y] << "|";
}
cout << endl;
}
}
};
#endif
|
Every time I compile/build and then run it,I get a nasty Debug Assertion Failed!
It states: Expression: Vector Subscript out of range.
Earlier I think this failure was even causing me to enter a breakpoint?
Basically an error would show up and I couldn't rerun the program.
I would have tried to stop Debugging but after that I would get a constant LINK error for the .exe file.
Which I don't understand.
Clearly my logic must be wrong somewhere and I am surely out of bounds for that vector.
I simply want a vector that is similar/equivalent to an array like this one.
1 2 3 4 5 6 7 8 9
|
const int Row = 3;
const int Col = 3;
char PrintBoard [Row][Col] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
|
I even tried to initialize the vector in a similar fashion to this. ^
But the syntax didn't work. I think it said that I had to many arguments or something along those lines.
So now I am left with this, since my original train of thought didn't work when initializing...
1 2 3 4 5 6
|
void SetGameBoard()
{
PrintBoard[0][0] = '1', PrintBoard[0][1] = '2', PrintBoard[0][2] = '3';
PrintBoard[1][0] = '4', PrintBoard[1][1] = '5', PrintBoard[1][2] = '6';
PrintBoard[2][0] = '7', PrintBoard[2][1] = '8', PrintBoard[2][2] = '9';
}
|
^ Is this even proper for a 2D vector? ^
I also have a side question that isn't a great priority but I was wondering if there is any easy way to change the system background and font colors without using system(); (since I've read that it is frowned upon...) If the explanation is too much of a hassle for this little additional question, please ignore it.
Thanks,
MisterTams