tic-tac-toe HELP

i'am a beginner in c++ and i was assigned to make Tic Tac Toe game
i thought first i should make the shape of squares
then enumerate each one of them
then ask the user to enter square number then his choice (x / o )
now i wanna replace square number with x or o but i don't have any idea how to do this ..any help ??

this is what i made till now

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
 using namespace std ;
 enum squares {sq1=1,sq2,sq3,sq4,sq5,sq6,sq7,sq8,sq9};;


 void shape ()
{



    cout << " "<<sq7<<" |"<<" "<<sq8<<" | "<<sq9<<" |\n"<<"____________\n"<<" "<<sq4<<" |"<<" "<<sq5<<" | "<<sq6<<" |\n"<<"____________\n"<< " "<<sq1<<" |"<<" "<<sq2<<" | "<<sq3<<" |\n";
}
void result (int choice  , int result)
{
    switch (result)
{


case (1):
{
    if ( choice == 'x')
        {
          // here i wanna  replace sq1 in cout below with x , HOW ???
                cout << " "<<sq7<<" |"<<" "<<sq8<<" | "<<sq9<<" |\n"<<"____________\n"<<" "<<sq4<<" |"<<" "<<sq5<<" | "<<sq6<<" |\n"<<"____________\n"<< " "<<sq1<<" |"<<" "<<sq2<<" | "<<sq3<<" |\n";

        }
     else if ( choice== '0')
     {

     }

}
case (2):
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
case (3):
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
case (4) :
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
case (5):
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
case (6):
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
case (7):
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
case (8):
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
case (9) :
{
    if ( choice == 'x')
        {

        }
     else if ( choice== '0')
     {

     }

}
}
}
int main ()
{
    int enter =0 ;
    char  choise  ;
    shape ();
    cout << "Enter you square number \n ";
    cin >> enter ;
    cout << " Enter your choice ( x/o ) \n";
    cin >> choise ;
    result (choise , enter);

for (int n = 1 ; n < 10 ; n ++)
{


    }


}

  
Hello ReHaMuhamed,

You may want to rethink your approach here. This game/program is usually done to teach the use of arrays.In this case a 2D array.

Not sure if the switch will be useful or not. In any case watch you indenting. "case 1:" is mostly indented properly, but not quite what it should be. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
switch (result)
{


    case (1):
    //{  // <--- This set of braces not need in a case block
        if ( choice == 'x')
        {
          // here i wanna  replace sq1 in cout below with x , HOW ???
                cout << " "<<sq7<<" |"<<" "<<sq8<<" | "<<sq9<<" |\n"<<"____________\n"<<" "<<sq4<<" |"<<" "<<sq5<<" | "<<sq6<<" |\n"<<"____________\n"<< " "<<sq1<<" |"<<" "<<sq2<<" | "<<sq3<<" |\n";

        }
        else if ( choice== '0')
        {

        }

        break;  // <--- This statement missing on all cases.
}


The switch may not be the best way to do this program, but it is a good time to mention indentation. Notice how the {} braces are lined up in the same column. This makes it easier to find the opening and closing braces. Also it helps when finding a problem.

Hint you will most likely work with something like: char board[MAXSIZE]MAXSIZE];. Where MAXSIZE is defined as constexpr size_t MASIZE{ 3 };. Not that you will ever need to change MAXSIZE, but the concept will work for other programs in the future.

When you are thinking about the program think bout the board as 3 rows of 3 columns. This will help to visualize what you are working with. Also keep in mind that arrays start at 0 not 1.

Hope that helps,

Andy
Hello ReHaMuhamed,

I loaded up your program this morning, Although it worked I made some changes.

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
void shape()
{
	std::cout << " " << sq1 << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << " \n";
}

void result(int choice, int result)
{
	switch (result)
	{


	case (1):
	//{  // This is not needed in the case block.
		//if (choice == 'X')
		//{
		//	// here i wanna  replace sq1 in cout below with x , HOW ???
                //  <--- You could hard code the "X" or "O".
		//	std::cout << " " << "X" << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << "\n";

		//}
		//else if (choice == '0')  // <--- The if part not needed here. Also the comparison is to zero not the letter O.
		//{

		//}
		
		// This works better.
		std::cout << " " << static_cast<char> (choice) << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << "\n";
		
		break;  // <--- This statement was missing in all the case statements.
	//}  // This is not needed in the case block. 


I changed the "shape" function. IMHO the numbers were backwards, To me this looks better. Also removed the pipe character at the end of each line.

1
2
// here i wanna  replace sq1 in cout below with x , HOW ???
std::cout << " " << sq1 << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << "\n";


Sort answer, tell it to do so. See above code for two examples. The first in the commented code is hard coded. Not the best way. You would need to code two different statements for each case. The second example makes use of the variable "choice" which is a better way and only needs one line for either "X" or "O".

In case 1 the whole if/else if can be done away with if favor of one line. This concept can be repeated or each case.

After thinking about I realized that the whole switch will not work. Each case statement will overwrite anything that was done previously. And since this is all hard coded there is no way to determine who won. This is where an array would be of more use. Choices of "X" or "O" would be stored in the array and a nested for loops would be used to print the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	int enter = 0;
	char  choise;

	shape();

	std::cout << "Enter your square number \n ";  // <--- Changed you.
	std::cin >> enter;

	std::cout << " Enter your choice ( x/o ) \n";
	std::cin >> choise;

	choise = toupper(choise);  // <--- Added.

	std::cout << std::endl;  // <--- To add a blank line after the last cin.

	result(choise, enter);

	//for (int n = 1; n < 10; n++)
	//{


	//} 


Notice the comments for the changes I made. Do not be afraid to use blank lines to break up your code, It makes it easier to read.

Part of main would benefit from a do/while loop to allow more than one run of the program. With your program the while condition is harder to figure out since you have no way to tell when the game is over or who won.

Hope that helps,

Andy
Topic archived. No new replies allowed.