I’ve tryed to execute your code and it’s difficult to understand if it works properly or not, but it doesn’t seem to.
Anyway, there are parts you can definitely improve, even if your code worked.
1) In your class methods, you declare a new instance of that class.
Even if sometimes happens (for example, when you overload postfix increment or decrement operator --> operator++(int)), that’s not common.
(And in that case, the new instance is constructed by copy from the old one.)
Your class methods should at most modify the class properties, not work on a local instance that will be destroyed as soon as you exit the function.
Aside note: to make this point clearer, writing the class implementation in a separated .cpp file, different from the one where there’s main(), it’s preferable. But, if you don’t know how to link two .cpp files together, you can put off this choice.
Example of (wrong!) local instances:
1 2 3 4
|
void GameLogic::PlayerMoveLeft()
{
//board//
GameLogic UsingGameboard;
|
1 2 3 4 5 6
|
void GameLogic::MatchStart()
{
while (Continue)
{
//board//
GameLogic UsingGameboard;
|
etc. etc.
2) It seems you want your class to take care of… everything.
That’s usually not advisable. To realize something which resembles that video, you need to manage at least two things: the board and the computer moves. Your class should take care just of one of these (I suggest the board).
You can of course write more classes: one for the board, and one for the game logic, which also makes the computer moves - after all they are random.
- - -
My personal advice:
Do not spend the most of the time trying to make your program beautiful: make the logic work, then you can embellish your ‘graphic’.
Do start by something very simple. For example, implement a board like a 2D container, such as a
std::array<std::array<char, 3>, 3> or even a C-style 2D array, like
char board[3][3].
Put one single symbol in it and make it move, but inside the board boundaries:
may not go here
| may go here
| /
may not go here --> V /
|X| | |
may go here -----> | | | |
| | | | |
(Better start without the pipe | symbol - the simpler, the better)
When you are able to move symbols without errors, set up your ‘real’ board:
vvv <--- three symbols which ‘look’ downward
<--- an empty line
^^^ <--- three symbols which ‘look’ upward |
Write down the code to move every symbol ‘manually’ and to let the program determine the condition situation:
1) when a ^ is in the top line or when a v is in the bottom line
2) when (player in turn) cannot make any move
3) when there are either no ^ or no v left
At this point you can add the game logic (i.e. computer moves and learning).
Compile very, very often; make little steps; check before adding.