Problem with TIC Tac Toe (First real program)

I've been trying to figure out how to implement the function that checks if the space is valid. So that an X cannot replace an O. I've got some of the code started in the function validmove();. If someone can point me in the direction of how to code this correctly I would much appreciate it. I've spent around 4-5 hours trying to figure it out and I just cant seem to work it out.

Also since I'm new to programming I'm sure there is a lot else I could improve upon in my program so anything else you can suggest would be great.

Thanks,

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  #include <iostream>
#include <string>
#include <windows.h>

using namespace std;


char spot1 = '1';
char spot2 = '2';
char spot3 = '3';
char spot4 = '4';
char spot5 = '5';
char spot6 = '6';
char spot7 = '7';
char spot8 = '8';
char spot9 = '9';
char playerx;
char playero;
char playerxcheck;

// Checks to see if the move is not taken by X or O (This is the part im having an issue with)

bool validmove()
{
    while ()
    {
        if (spot1 != '1' && (playerx == '1' || playero == '1'))
            {
                cout<<"Invalid move, Try again."<<endl;
                if (playerx == '1')
                    {
                        cin>>playerx;
                    }
                    else if (playero == '1')
                    {
                        cin>>playero;
                    }



            }

    }
}

//Goes through all the different win scenarios 

bool gamewin()
{
    if ((spot1 ==  'X' && spot2 == 'X' && spot3 == 'X') || (spot4 ==  'X' && spot5 == 'X' && spot6 == 'X') || (spot7 ==  'X' && spot8 == 'X' && spot9 == 'X'))
    {
        cout<<"XX wins"<<endl;
        return true;
    }
    else if ((spot1 == 'X' && spot5 == 'X' && spot9 == 'X') || (spot3 == 'X' && spot5 == 'X' && spot7 == 'X'))
    {
        cout<<"X Wins"<<endl;
        return true;
    }
    else if ((spot1 == 'X' && spot4 == 'X' && spot7 == 'X') || (spot2 == 'X' && spot5 == 'X' && spot8 == 'X') || (spot3 == 'X' && spot6 == 'X' && spot9 == 'X'))
    {
        cout<<"X Wins"<<endl;
        return true;
    }
    else if ((spot1 ==  'O' && spot2 == 'O' && spot3 == 'O') || (spot4 ==  'O' && spot5 == '0' && spot6 == 'O' || spot7 ==  'O' && spot8 == 'O' && spot9 == 'O'))
    {
        cout<<"O Wins"<<endl;
        return true;
    }
    else if ((spot1 == 'O' && spot5 == 'O' && spot9 == 'O' || spot3 == 'O' && spot5 == 'O' && spot7 == 'O'))
    {
        cout<<"O Wins"<<endl;
        return true;
    }
    else if ((spot1 == 'O' && spot4 == 'O' && spot7 == 'O') || (spot2 == 'O' && spot5 == 'O' && spot8 == 'O') || (spot3 == 'O' && spot6 == 'O' && spot9 == 'O'))
    {
        cout<<"O Wins"<<endl;
        return true;
    }
    else if ((spot1 != '1' && spot2 != '2' && spot3 != '3') && (spot4 != '4' && spot5 != '5' && spot6 != '6') && (spot7 != '7' & spot8 != '8' && spot9 != '9'))
    {
        cout<<"The game is a draw."<<endl;
        return true;
    }
    else
    {
        return false;
    }

}

// I used this function to clear the screen before I found out about system ("cls");
void clearscreen()
{
    cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
}

void changeboard()
{
    if (playerx == '1')
    {
            spot1 = 'X';
    }
    else if (playerx == '2')
    {
            spot2 = 'X';
    }
    else if (playerx == '3')
    {
        spot3 = 'X';
    }
    else if (playerx == '4')
    {
        spot4 = 'X';
    }
    else if (playerx == '5')
    {
        spot5 = 'X';
    }
    else if (playerx == '6')
    {
        spot6 = 'X';
    }
    else if (playerx == '7')
    {
        spot7 = 'X';
    }
    else if (playerx == '8')
    {
        spot8 = 'X';
    }
    else if (playerx == '9')
    {
        spot9 = 'X';
    }

    if (playero == '1')
    {
            spot1 = 'O';
    }
    else if (playero == '2')
    {
            spot2 = 'O';
    }
    else if (playero == '3')
    {
        spot3 = 'O';
    }
    else if (playero == '4')
    {
        spot4 = 'O';
    }
    else if (playero == '5')
    {
        spot5 = 'O';
    }
    else if (playero == '6')
    {
        spot6 = 'O';
    }
    else if (playero == '7')
    {
        spot7 = 'O';
    }
    else if (playero == '8')
    {
        spot8 = 'O';
    }
    else if (playero == '9')
    {
        spot9 = 'O';
    }
}


//Shows the board on a new screen
void showboard()
{

    system ("cls");
    cout<<"+-----------------------------------------------+"<<endl;

    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"   "<<spot1<<"  |   "<<spot2<<"   |   "<<spot3<<"                |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"------"<<"+"<<"-------"<<"+"<<"-------"<<"             |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"   "<<spot4<<"  |   "<<spot5<<"   |   "<<spot6<<"                |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"------"<<"+"<<"-------"<<"+"<<"-------"<<"             |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"   "<<spot7<<"  |   "<<spot8<<"   |   "<<spot9<<"                |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"+-----------------------------------------------+"<<endl;

}




//Does both players turns and if the gamewin() = True then game is over.
int gamestart()
{
    cout<<"Welcome to Tic Tac Toe (By Daniel Yatch)"<<endl;


    showboard();
    cout<<endl<<endl;
    cout<<"Player X starts the game"<<endl;

    for (int x=0;x < 10; x++)
    {
        cout<<"Player X, enter a board position (1-9)."<<endl;
    cin>>playerx;

    validmove();
    changeboard();
    showboard();


    if (gamewin() == true)
    {
        return 0;
    }

        cout<<"Player O, enter a board position (1-9)."<<endl;
    cin>>playero;

    changeboard();
    showboard();


    if (gamewin() == true)
    {
        return 0;
    }
    }

}




int main()
{
    gamestart();


}
> so anything else you can suggest would be great.

Attempt this after you have read up on arrays, perhaps?

With the current approach (we do not yet know anything about arrays), something like this:

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
#include <iostream>
#include <cstdlib> // required for std::system()

char spot1 = '1';
char spot2 = '2';
char spot3 = '3';
char spot4 = '4';
char spot5 = '5';
char spot6 = '6';
char spot7 = '7';
char spot8 = '8';
char spot9 = '9';

bool valid_move( char move_entered )
{
    switch(move_entered)
    {
        case '1' : return spot1 == '1' ; // spot1 is unused (does not contain 'X' or 'O' )
        case '2' : return spot2 == '2' ;
        case '3' : return spot3 == '3' ;
        case '4' : return spot4 == '4' ;
        case '5' : return spot5 == '5' ;
        case '6' : return spot6 == '6' ;
        case '7' : return spot7 == '7' ;
        case '8' : return spot8 == '8' ;
        case '9' : return spot9 == '9' ;

        default: return false ;
    }
}

void make_move( char player /* 'X' or 'O' */ )
{
    char pos ;
    do
    {
        std::cout << "Player " << player << ", enter a board position (1-9): " ;
        std::cin >> pos ;

    } while( !valid_move(pos) ) ;

    switch(pos)
    {
        case '1' : spot1 = player ; break ; // spot1 is 'X' if the player is X, 'O' otherwise
        case '2' : spot2 = player ; break ;
        case '3' : spot3 = player ; break ;
        case '4' : spot4 = player ; break ;
        case '5' : spot5 = player ; break ;
        case '6' : spot6 = player ; break ;
        case '7' : spot7 = player ; break ;
        case '8' : spot8 = player ; break ;
        case '9' : spot9 = player ;
    }
}

//Shows the board on a new screen
void showboard()
{
    using namespace std ;

    system ("cls");
    cout<<"+-----------------------------------------------+"<<endl;

    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"   "<<spot1<<"  |   "<<spot2<<"   |   "<<spot3<<"                |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"------"<<"+"<<"-------"<<"+"<<"-------"<<"             |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"   "<<spot4<<"  |   "<<spot5<<"   |   "<<spot6<<"                |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"------"<<"+"<<"-------"<<"+"<<"-------"<<"             |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"   "<<spot7<<"  |   "<<spot8<<"   |   "<<spot9<<"                |"<<endl;
    cout<<"|"<<'\t'<<"     "<<"      "<<"|"<<"       "<<"|"<<"                    |"<<endl;
    cout<<"+-----------------------------------------------+"<<endl;

}

int gamestart()
{
    std::cout << "Welcome to Tic Tac Toe (By Daniel Yatch)\n" ;


    showboard();
    std::cout << "\n\nPlayer X starts the game\n" ;
    char current_player = 'X' ;

    for( int i=0 ; i < 9; ++i )
    {
        make_move( current_player ) ;
        std::cout << "************\n" ;
        showboard() ;
        // check for win
        // etc.

        // toggle the current player at each turn
        if( current_player == 'X' ) current_player = 'O' ;
        else current_player = 'X' ;
    }

    return 0 ;
}

int main()
{
    return gamestart() ;
}


Take it up from there.
Topic archived. No new replies allowed.