Tic-Tac-Toe program not working :(

As the title says, my Tic-Tac-Toe program does not work. Could anyone take a look at my code and tell me why? The compiler says that, in the winner() function I'm doing a comparison between an integer and a pointer, and that I can't do that. But I'm not, at least not according to me. What do you think?

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

void choosingTile(string& tiles1, int& turn){
    srand(static_cast<unsigned int>(time(0)));
    int number, correct;
    while(correct != 1 && number >= 0 && number <= 8){
        correct = 0;
        if(turn == 0){
            cout << "Enter the number of the tile you want to choose: "; cin >> number;
            turn++;
        }else if(turn == 1){
            number = rand()%9;
            turn--;
        }
        if(tiles1[number] == 'X')
            cout << "You chose an incorrect tile!" << endl;
        else if(tiles1[number] == 'O');
        else{
            if(turn == 1)
                tiles1[number] == 'X';
            else if(turn == 0)
                tiles1[number] == 'O';
            correct++;
        }
    }
}

void winner(string tiles2, int& win){
    for(int x=0;x<tiles2.size();x+=3){
        if(tiles2.substr(x, 3)=="XXX")
            win = 0;
        else if(tiles2.substr(x, 3)=="OOO")
            win = 1;
    }for(int x=0;x<3;x++){
        string test = tiles2[x] + tiles2[x+3] + tiles2[x+6];
        if(test == "XXX")
            win = 0;
        else if(test == "OOO")
            win = 1;
    }
    if(tiles2[0] == "X" && tiles2[4] == "X" && tiles2[8] == "X")
        win = 0;
    else if(tiles2[0] == "O" && tiles2[4] == "O" && tiles2[8] == "O")
        win = 1;
    if(tiles2[2] == "X" && tiles2[4] == "X" && tiles2[6] == "X")
        win = 0;
    else if(tiles2[2] == "O" && tiles2[4] == "O" && tiles2[6] == "O")
        win = 1;
}

int main(){
    string tiles = "012345678";
    int win = -1, turn = 0;
    while(win != 0 && win != 1){
        cout << "    -= Tic-Tac-Toe =-\n\n\t" << tiles[0] << " | " << tiles[1] << " | " << tiles[2] << "\n"
             << "\t----------\n"
             << "\t" << tiles[3] << " | " << tiles[4] << " | " << tiles[5] << "\n"
             << "\t----------\n"
             << "\t" << tiles[6] << " | " << tiles[7] << " | " << tiles[8] << "\n\n";
        choosingTile(tiles, turn);
        winner(tiles, win);
    }
    if(win == 1)
        cout << "The computer won!" << endl;
    else
        cout << "You won!" << endl;
}
You have a number of problems:

Line 8: You don't want to call srand from within a loop. That will reset the random number sequence back to the beginning. You want to call srand once at the beginning of the program.

Line 24, 26: Those are not assignment statements.

Line 39: You're trying to add three characters together resulting in an int, then assign that int to a string. You're not concantenating characters as you're expecting.

Line 45: You're trying to compare an int to a string (3 times). not possible. You need to use character literals.

Line 47: Same thing.

Line 49: Same thing.

Line 51: Same thing.

And that's just the errors reported by the compiler. I didn't look for logic errors.
What do you suggest that I do instead? Sorry, I'm not very skilled at this.

I thought that when you used the plus sign with strings you added them together, not that you created an integer...
Last edited on
I got it working now, I'm not sure that this code is better though. I could probably shorten it down a little bit.

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

void choosingTile(string& tiles, int& turn){
    srand(static_cast<unsigned int>(time(0)));
    int number = -1;
    while(!(number >= 0 && number <= 8)){
        if(turn == 0){
            while(!(number >= 0 && number <= 8) || (tiles[number]=='X'||tiles[number]=='O')){
                cout << "Enter the number of the tile that you want to choose: "; cin >> number;
            }
            tiles[number] = 'X';
            turn = 1;
        }
        else if(turn == 1){
            do{
                number = rand()%9;
            }while(tiles[number]=='X'||tiles[number]=='O');
            tiles[number] = 'O';
            turn = 0;
        }
    }
}

int determineWinner(string tiles){
    string test, ett, tva, tre;
    int grej = 0;
    for(int x=0;x<9;x+=3){
        if(tiles.substr(x,3)=="XXX")
            return 1;
        else if(tiles.substr(x,3)=="OOO")
            return 2;
    }for(int x=0;x<3;x++){
        ett = tiles[x], tva = tiles[x+3], tre = tiles[x+6];
        test = ett + tva + tre;
        if(test == "XXX")
            return 1;
        else if(test == "OOO")
            return 2;
    }
    ett = tiles[0], tva = tiles[4], tre = tiles[8];
    test = ett + tva + tre;
    if(test == "XXX")
        return 1;
    else if(test == "OOO")
        return 2;
    ett = tiles[2], tva = tiles[4], tre = tiles[6];
    test = ett + tva + tre;
    if(test == "XXX")
        return 1;
    else if(test == "OOO")
        return 2;
    else{
        for(int x=0;x<9;x++){
            if(tiles[x] == 'X' || tiles[x] == 'O')
                grej++;
        }
        if(grej == 9)
            return 3;
    }
}

int main(){
    string tiles = "0123456789";
    int win = 0, turn = 0;
    while(win != 1 && win != 2 && win != 3){
        system("CLS");
        cout << "    -= Tic-Tac-Toe =-\n\n\t" << tiles[0] << " | " << tiles[1] << " | " << tiles[2] << "\n"
             << "\t----------\n"
             << "\t" << tiles[3] << " | " << tiles[4] << " | " << tiles[5] << "\n"
             << "\t----------\n"
             << "\t" << tiles[6] << " | " << tiles[7] << " | " << tiles[8] << "\n\n";
        choosingTile(tiles, turn);
        win = determineWinner(tiles);
    }
    if(win == 1)
        cout << "Congratulations! You won!" <<  endl;
    else if(win == 2)
        cout << "The computer won!" << endl;
    else
        cout << "No one won!" << endl;
}
Last edited on
I thought that when you used the plus sign with strings you added them together, not that you created an integer.


Depends on how the + sign is being used.
In your original line 39, tiles2[x] + tiles2[x+3] is evaluated first. The compiler evaluates this as adding two characters, which results in an int. The compiler hasn't considered the left side of the statement yet.
Topic archived. No new replies allowed.