Tic-Tac-Toe

Pages: 12
Hi, im trying to creat tic tac toe game, but it is not working.
When i compile it, and i go past welcome message it only displays "1:"
after which program ceases to work as if return(0) was triggered;

Here is the code:
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
#include <iostream>
using namespace std;

int userinput ();
const char* row[10];
int input = 0;
bool a;

bool check (bool a)
{
    if(row[1] = row[2], row[2] = row[3])
    {
        a = true;
        return (a);
    }
    else if(row[4] = row[5], row[5] = row[6])
    {
        a = true;
        return (a);
    }
    else if(row[7] = row[8], row[8] = row[9])
    {
        a = true;
        return (a);
    }
    else if(row[1] = row[4], row[4] = row[7])
    {
        a = true;
        return (a);
    }
        else if(row[2] = row[5], row[5] = row[8])
    {
        a = true;
        return (a);
    }
        else if(row[3] = row[6], row[6] = row[9])
    {
        a = true;
        return (a);
    }
        else if(row[1] = row[5], row[5] = row[9])
    {
        a = true;
        return (a);
    }
        else if(row[3] = row[5], row[5] = row[7])
    {
        a = true;
        return (a);
    }
    else
    {
        return (a);
    }
}

int main ()
{
    cout << "Hello, welcome to game of Tic-Tac-Toe! It's a 2 player game!\n";
    cin.ignore();
    next_turn:
    cout << "1: " << row[1] << " 2:" << row[2] << " 3:" << row[3] << "\n";
    cout << "4: " << row[4] << " 5:" << row[5] << " 6:" << row[6] << "\n";
    cout << "7: " << row[7] << " 8:" << row[8] << " 9:" << row[9] << "\n";
    cout << "Player O, where do you want to put O ? Insert number.\n";
    cin >> input;
    row[input] = "o";
    check(a);
    if(a = true){goto victoryO;}
    cout << "1:" << row[1] << "         2:" << row[2] << "          3:" << row[3] << "\n";
    cout << "4:" << row[4] << "         5:" << row[5] << "          6:" << row[6] << "\n";
    cout << "7:" << row[7] << "         8:" << row[8] << "          9:" << row[9] << "\n";
    cout << "Player X, where do you want to put X ? Insert number.\n";
    cin >> input;
    row [input] = "X";
    if(a = true){goto victoryX;}
    goto next_turn;
    victoryO:
        cout << "Player O is victorious!\n";
        cin.ignore();
        return 0;
    victoryX:
        cout << "Player X is victorious!\n";
        cin.ignore();
        return 0;
}



Line 68: You don't save the result of calling check()
Should be:
1
2
 
  a = check();


There is no need to pass a into check since you're passing it in by value, not reference and check doesn't need it.

Your if statements in check are defective. You're using the assignment operator, not the equality operator. Also, the comma is not appropriate.
Should be:
1
2
    if(row[1] == row[2] && row[2] == row[3])
      return true;

There's also no need for the else on the following ifs.
If i perform

Line 68: You don't save the result of calling check()
Should be:
a = check();


It returns me error called:
"error: too few arguments to function 'bool check(bool)'"

If i change it back the way it was, error is gone, but it still only gets to line 60, after i press enter, it only displays "1:" and whatever number is inputed afterwards, program terminates itself.

Where is the problem ? Thank you for response.
Line 9:
bool check ()

You don't need to pass it anything, as when you return from the function, you're saving the value in main. With the above statement, from AbstractionAction:
a = check();
Lynx876

Thanks, it surely helps the code, but the output still isn't fixing anything at all.
Program still gives symtomps mentioned in my post above yours.
I think, maybe something is wrong with my cout ?
So, I fixed it... lol.

Line 5:
const char* row[10];

Const? Later in your program, you'll try to assign something to row. 'O' or an 'X'. which, being a const is unable to change!

I see why you made it a const char *, because when you were trying to assign:
row[input] = "o";

You were receiving an error about it assigning a const char pointer to ...

You assign a char like so:
row [input] = 'X';

Note the ' ' ( single quotes ).

And, that was it! (:
Yes! Thank you very much, it did indeed worked.
Last problem this program has is, after inputing anything, it doesent matter, becouse by default all fields are empty, therefore are the same, so immideatly when program has option to check for winners, it says player O won.

Any idea how to make it detect X and O only ? Ignoring the fact all fields are same, becouse they are empty ?
In your if-statements, you're only checking if they are the same, not for a value! (:

if 1 == X && 1 == 2 && 2 == 3
I know that, but if that's what i need to do, there will be ALOT of code as there will be practically twice as much code, becouse i need to insert same thing for O as i will for X.

I tried to make it switch between X and O so after player O inputs it checks for O's and after player's X turn it check's for X.

Here is the code

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
#include <iostream>
using namespace std;

int userinput ();
char row[10];
int input = 0;
bool a;
char change;

bool check ()
{
    if(row[1]  == change && row[1] == row[2] && row[2] == row[3])
    {
        return true;
    }
    if(row[4]  == change && row[4] == row[5] && row[5] == row[6])
    {
        return true;
    }
    if(row[7]  == change && row[7] == row[8] && row[8] == row[9])
    {
        return true;
    }
    if(row[1]  == change && row[1] == row[4] && row[4] == row[7])
    {
        return true;
    }
    if(row[2]  == change && row[2] == row[5] && row[5] == row[8])
    {
        return true;
    }
    if(row[3]  == change && row[3] == row[6] && row[6] == row[9])
    {
        return true;
    }
    if(row[1]  == change && row[1] == row[5] && row[5] == row[9])
    {
        return true;
    }
    if(row[3]  == change && row[3] == row[5] && row[5] == row[7])
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main ()
{
    cout << "Hello, welcome to game of Tic-Tac-Toe! It's a 2 player game!\n";
    cin.ignore();
    next_turn:
    cout << "1: " << row[1] << " 2:" << row[2] << " 3:" << row[3] << "\n";
    cout << "4: " << row[4] << " 5:" << row[5] << " 6:" << row[6] << "\n";
    cout << "7: " << row[7] << " 8:" << row[8] << " 9:" << row[9] << "\n";
    cout << "Player O, where do you want to put O ? Insert number.\n";
    cin >> input;
    row[input] = 'o';
    change == 'o';
    a = check();
    if(a = true){goto victoryO;}
    cout << "1:" << row[1] << "         2:" << row[2] << "          3:" << row[3] << "\n";
    cout << "4:" << row[4] << "         5:" << row[5] << "          6:" << row[6] << "\n";
    cout << "7:" << row[7] << "         8:" << row[8] << "          9:" << row[9] << "\n";
    cout << "Player X, where do you want to put X ? Insert number.\n";
    cin >> input;
    row [input] = 'X';
    change == 'x';
    if(a = true){goto victoryX;}
    goto next_turn;
    victoryO:
        cout << "Player O is victorious!\n";
        cin.ignore();
        return 0;
    victoryX:
        cout << "Player X is victorious!\n";
        cin.ignore();
        return 0;
}


I believe this is last of my problems in this one, thank you and AbstractAnon for help!

EDIT: Forgot to add, problem is, it still declares player o victorious after it checks.
Last edited on
The first error I see, are lines 61 and 70.
The way you have them, looks like they should be within an if-statement etc.
assignment/equality!

And looking at your check function, it looks fine. Is there a problem with it?
Also, in that function, you don't need the else at the end, just return false, as none of the above statements returned true. (:

Edit:
I thought they were else if statements! It only returns false, when the last if-statement is not true!
Last edited on
I do not know if there is a problem witch chek function, you know ?

I changed 61 and 70 so it is "=" not "==" but it still holds onto Player O as victorious!

Updated code:


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
#include <iostream>
using namespace std;

int userinput ();
char row[10];
int input = 0;
bool a;
char change;

bool check ()
{
    if(row[1]  == change && row[1] == row[2] && row[2] == row[3])
    {
        return true;
    }
    if(row[4]  == change && row[4] == row[5] && row[5] == row[6])
    {
        return true;
    }
    if(row[7]  == change && row[7] == row[8] && row[8] == row[9])
    {
        return true;
    }
    if(row[1]  == change && row[1] == row[4] && row[4] == row[7])
    {
        return true;
    }
    if(row[2]  == change && row[2] == row[5] && row[5] == row[8])
    {
        return true;
    }
    if(row[3]  == change && row[3] == row[6] && row[6] == row[9])
    {
        return true;
    }
    if(row[1]  == change && row[1] == row[5] && row[5] == row[9])
    {
        return true;
    }
    if(row[3]  == change && row[3] == row[5] && row[5] == row[7])
    {
        return true;
    }
return false;
}

int main ()
{
    cout << "Hello, welcome to game of Tic-Tac-Toe! It's a 2 player game!\n";
    cin.ignore();
    next_turn:
    change = 'o';
    cout << "1:" << row[1] << "        2:" << row[2] << "        3:" << row[3] << "\n";
    cout << "4:" << row[4] << "        5:" << row[5] << "        6:" << row[6] << "\n";
    cout << "7:" << row[7] << "        8:" << row[8] << "        9:" << row[9] << "\n";
    cout << "Player O, where do you want to put O ? Insert number.\n";
    cin >> input;
    row[input] = 'o';
    a = check();
    if(a = true){goto victoryO;}
    cout << "1:" << row[1] << "         2:" << row[2] << "          3:" << row[3] << "\n";
    cout << "4:" << row[4] << "         5:" << row[5] << "          6:" << row[6] << "\n";
    cout << "7:" << row[7] << "         8:" << row[8] << "          9:" << row[9] << "\n";
    cout << "Player X, where do you want to put X ? Insert number.\n";
    cin >> input;
    row [input] = 'x';
    change = 'x';
    if(a = true){goto victoryX;}
    goto next_turn;
    victoryO:
        cout << "Player O is victorious!\n";
        cin.ignore();
        return 0;
    victoryX:
        cout << "Player X is victorious!\n";
        cin.ignore();
        return 0;
}
ahaha, can't believe I missed that. From my last post about assignment/equality. Look at what you check for, for a winner.
Lines 60 and 68! (:
Yes! It finally works!
Magnificent, thank you very much, y'all helped alot, even if problems were so tiny yet had such influence on output!
Thanks for help! :)
Last edited on
aha! No problem. Happy coding!

And remember assignment/equality. That seemed to be your main problem!

Also, try not to use goto! At first, I had a problem finding your program flow.
http://www.cplusplus.com/doc/tutorial/control/
I find goto very easy to use as i can jump quickly somewhere and put conditions with if's for jump if i need. They seem best kind of loop for me.
Asking for help on most C++ forums, and using goto, you'll encounter some people 'bashing' your code!

I just re-wrote main from your program, with a do-while loop. I and many other people find this more readable.

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
int main ()
{
	cout << "Hello, welcome to game of Tic-Tac-Toe! It's a 2 player game!\n";
	cout << "Press enter to continue.\n";
	cin.ignore();

	do
	{
		// toggle the players char
		if( change == 'x' )
			change = 'o';
		else
			change = 'x'; // As change is un-initialized, change will be 'x'. Hopefully! lol

		cout << "1:" << row[1] << "        2:" << row[2] << "        3:" << row[3] << "\n";
		cout << "4:" << row[4] << "        5:" << row[5] << "        6:" << row[6] << "\n";
		cout << "7:" << row[7] << "        8:" << row[8] << "        9:" << row[9] << "\n";
		cout << "Player O, where do you want to put O ? Insert number.\n";
		cin >> input;

		row[input] = change;

		a = check();

	}while( a == false ); // while check returns false. Loop.

	cout << "Player " << change << " is victorious!\n";
	cin.ignore();
        
	return 0;
}
Last edited on
I find goto very easy to use as i can jump quickly somewhere and put conditions with if's for jump if i need. They seem best kind of loop for me.

If you think gotos are easy, you've never had do debug a program with hundreds of gotos pointing backwards and forwards, and try and figure out how you got to a certain point in the code.

The problem with gotos is not where does it go, but rather "how did I get here"? Consider a program with 14 gotos that all jump to the same label. No way to tell how you got to the label. By comparison, a structured loop (while, do while, for) has an explicit entrance and an explicit exit. No question about the flow of the program.

As a programming manager, if I found one of my programmers using gotos instead of a structured loop, he wouldn't be working for me for very long. I'm not here to bash your code, but rather to try and illustrate why gotos are evil.
Well i surely wouldn't use goto in a big project, but for small codes where there is only 1 goto with one place for it to go, seems reasonable right ?
My advice, would be to use loops straight away, to get a better understanding of them.

Else, when you come to implement loops in bigger programs, you'll find yourself getting stuck.

And believe me, you will! I'm writing Tetris at the moment and a certain loop is kicking my ass, been trying to debug it for about an hour now. And the thing is, I know it's simple! ahaha
Last edited on
foxar wrote:
seems reasonable right ?

No. The use of gotos is the result of either being lazy, lack of coding discipline, not understanding structured programming, or not understanding how to code a proper loop. It doesn't matter if it is one goto or 100. It's still a bad practice.

You say you wouldn't use a goto in a big project. Well programs often start small and frequently morph in much bigger programs. At what point do you go back and recode your program to use proper loops rather than gotos? 10 gotos? 20? 100?
It's possible to create program flow using gotos that you can't easily convert to conventional loops. How much code are you going to have to redo? I don't pay programmers to redo their code. I expect it to be written correctly the first time.

The fact that you're trying to defend the use of goto tells me you don't get it.
Pages: 12