Problem With Tic Tac Toe Function

Hi, there's an issue with my void input function

When it's called, i ask the user for a choice for where they want to go on the board, i want to then input that choice into the board which is global

char grid[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

char player = 'X';
char computer = 'O';
int n;


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

void input()
{
cout << endl;
cout << "Please select where you want to go " << endl << endl;
int choice;
cin >> choice;

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (grid[i][j] == choice)
grid[i][j] = player;

}
cout << endl;
}

}
Last edited on
Hello Zainyorkshireman,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



It is hard to tell without the rest of the program, but if these:
1
2
3
4
5
char grid[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

char player = 'X';
char computer = 'O';
int n;

are global variables try to avoid them as any line of code can change them and it is harder to track down where it went wrong. It is usually better to define them in "main" and pass them to the functions that need them.

Your if statement if (grid[i][j] == choice) is comparing an "int" to a "char". The number 1 and the char '1' have two different values (1) and (49). These would never be considered equal. You could define "choice" as a "char" and then you would be comparing the same type of variables.

I will work up a little program to give it a better test.

Andy
This gets you started. Note the changes. Global stuff is OK. The 1,2, ... 9 is a good idea :)

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
#include <iostream>

using namespace std;

void input();
void draw();

// GLOBAL STUFF
char grid[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
const char player = 'X';
const char computer = 'O';
int n;

int main()
{
    draw();
    while(1) // NEED TO IMPROVE ON THIS FOR GAME LOOP
    {
        input();
        draw();
    }
    
    return 0;
}

void input()
{
    cout << endl;
    cout << "Please select where you want to go <1-9>: ";
    int choice;
    cin >> choice;
    grid[choice / 3][choice % 3 - 1] = player;
}

void draw()
{
    cout << endl;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << grid[i][j];
        }
        cout << endl;
    }
}
PS n isn't used so far. If it is the number of spaces available for a move then the game is over when n == 0, or when someone has won. So that wouldbe part of the while condition.

Note also that there is still lots of work to define a win or drawer and then how the computer makes a move.

Also have to alternate between player and computer in the loop.
Last edited on
Hi all, thanks for your input, Handy Andy, i changed the int to a char and now it works fine. There is a lot more to it which i didn't post as this was the only problematic function.

n merely tracks the amount of turns that have gone and won't allow it to surpass 9 for obvious reasons. I also have a draw scenario that works fine.

Again, i appreciate the help, it's been doing my head in
Hello Zainyorkshireman,

I came up with this as a start. You may fine parts of it useful:
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
76
#include <cctype>
#include <iostream>
#include <iomanip>
#include <limits>

constexpr int MAXROW{ 3 }, MAXCOL{ 3 };

void draw(char grid[][MAXCOL])
{
	std::cout << std::endl;
	for (int i = 0; i < 3; i++)
	{
		std::cout << "    ";

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

void input(char grid[][MAXCOL], char player)
{
	char choice;

	//std::cout << std::endl;
	std::cout << "\n Please select where you want to go: ";
	std::cin >> choice;

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (grid[i][j] == choice)
				grid[i][j] = player;
			else if((grid[i][j] == 'X' || grid[i][j] == 'O'))
				std::cout << "\n    Square already taken!\n";

		}

		//std::cout << std::endl;
	}

}

int main()
{
	bool cont{ true };
	char yesNo{};
	char grid[MAXROW][MAXCOL] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	constexpr char player = 'X';
	constexpr char computer = 'O';
	int n;

	do
	{
	        draw(grid);
	        input(grid, player);
		draw(grid);

		std::cout << "\n  Do another? ";
		std::cin >> yesNo;

		if (std::toupper(yesNo) != 'Y') cont = false;
	} while (cont);

	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.	
}

The do/while loop in "main" is just for testing for now, but you may find it useful in the future.

Andy
Topic archived. No new replies allowed.