Tic Tac Toe using classes

My problem is that the game does not make any move on the matrix that is displayed.

Here is my header file containing the TicTacToe class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef TICTACTOE_H
#define TICTACTOE_H

class TicTacToe
{
private:
	char board[3][3];

public:
	void DrawBoard();
	void GetMove(char player);
	void TogglePlayer(char player);
	bool IsValidMove();
	bool DetermineWinner();
	bool DetermineDraw();
};

#endif 



Here is the implementation file that uses the following functions:

DrawBoard() to draw the board
GetMove() to make the move
TogglePlayer() and toggle the 'X' and 'O'.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include "TicTacToe.h"

using namespace std;

void TicTacToe::DrawBoard()
{
	system("cls");
	cout <<"\tWelcome to the Classes Tic Tac Toe! \n";
	char board[3][3] =
	{ 
	  {'1','2','3'},
	  {'4','5','6'},
	  {'7','8','9'},
	};

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
}

void TicTacToe::GetMove(char player)
{
	int move;

	if( move = 1)
	{
		board[0][0] = player;
	}
	else if(move = 2)
	{
		board[0][1] = player;
	}
	else if(move = 3)
	{
		board[0][2] = player;
	}
	else if(move = 4)
	{
		board[1][0] = player;
	}
	else if(move = 5)
	{
		board[1][1] = player;
	}
	else if(move = 6)
	{
		board[1][2] = player;
	}
	else if(move = 7)
	{
		board[2][0] = player;
	}
	else if(move = 8)
	{
		board[2][1] = player;
	}
	else if(move = 9)
	{
		board[2][2] = player;
	}
}

void TicTacToe::TogglePlayer(char player)
{
	if (player == 'X')
		player = 'O';
	else if(player == 'O')
		player = 'X';
}



Here is the main file, where I define the object game.
Game is using the function GetMove(), to make the move on the board, but this does not happen (why?).

game.DrawBoard(), displays the matrix fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "TicTacToe.h"

using namespace std;

int main()
{
	TicTacToe game;
	char player = 'X';

	game.GetMove(player);
	game.DrawBoard();
        game.TogglePlayer(player);

	system("pause");
}
Don't you have to update the drawing of the board?
Last edited on
May or may not be the only problem... but I see two right here:
1
2
3
4
5
6
void TicTacToe::GetMove(char player)
{
	int move;

	if( move = 1)
        ...

1. the = operator is assigning 1 to move. You probably want equality test ==.
2. If you do change it it do ==, you're still gonna get errors because move is uninitialized, it will contain some random junk value.
Last edited on
Thank you all for your answers.

I corrected the problem, don't know how I missed that but anyway:

1
2
3
4
5
6
7
8
9
10
11
void TicTacToe::GetMove(char player)
{
	int move;
	cout <<"Enter the number of the field you would like to move:\n";
	cin >> move;

	if( move == 1)
	{
		board[0][0] = player;
	}
       . . .


Still I am not sure how to update the board.
Topic archived. No new replies allowed.