Tic tac toe help please

Hello Community,

I am trying to build a simple tic tac toe game. And my issue is that my grid when I input a grid space the input is not read and I get an echo of what I just got. Please help

Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include "Tic-Tac-Toe.h"
#include <iostream>

class Tic_Tac_Toe
{
private:
	char square[3][3];
	int x;
public:
	void draw_board();
	void setsquare(int s);
	int getsquare();
	int player_input(int x);
};

implementation file
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
#include "Tic-Tac-Toe.h"
void Tic_Tac_Toe::draw_board()
{
	char square[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++)
		{
			square[i][j];
		}
	}
	std::cout << "\n\n\t\t        |         |         \n";
	std::cout << "\t\t    " << square[0][0] << "   |    " << square[0][1] << "    |    " << square[0][2] << "    \n";
	std::cout << "\t\t________|_________|_________\n";
	std::cout << "\t\t        |         |         \n";
	std::cout << "\t\t    " << square[1][0] << "   |    " << square[1][1] << "    |    " << square[1][2] << "    \n";
	std::cout << "\t\t________|_________|_________\n";
	std::cout << "\t\t        |         |         \n";
	std::cout << "\t\t    " << square[2][0] << "   |    " << square[2][1] << "    |    " << square[2][2] << "    \n";
	std::cout << "\t\t        |         |         \n";
	
}
void Tic_Tac_Toe::setsquare(int s)
{
	x = s;
}
int Tic_Tac_Toe::getsquare()
{
	return x;
}
int Tic_Tac_Toe::player_input(int s)
{
	switch (x)
	{
	case 1:
		square[0][0] = 'X';
		break;
	case 2:
		square[0][1] = 'X';
		break;
	case 3:
		square[0][2] = 'X';
		break;
	case 4:
		square[1][0] = 'X';
		break;
	case 5:
		square[1][1] = 'X';
		break;
	case 6:
		square[1][2] = 'X';
		break;
	case 7:
		square[2][0] = 'X';
		break;
	case 8:
		square[2][1] = 'X';
		break;
	case 9:
		square[2][2] = 'X';
	default:
		std::cout << "Invalid entry please try again.";
		break;
	}
	return x;
}

Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Tic-Tac-Toe.h"
#include <iostream>
using namespace std;
int main()
{	
	// Datatype int 'x' variable is the input of player one.
	
	cout << "\tHello and welcome to Tic-Tac-Toe\n";
	cout << "\tPlayer:[x]\n\tCPU:[O]";
	Tic_Tac_Toe t1;
	t1.draw_board();
	int x;
	cout << "Player choice:";
	cin >> x;
	t1.setsquare(x);
	t1.draw_board();
	system("pause");
	return 0;
}
> char square[3][3] = { {'1','2','3'},{'4','5','6'},{'7','8','9'} };
You're NOT copying your local variable 'square' into your class member variable 'square'.

Topic archived. No new replies allowed.