game help

hey, I posted earlier witha really bad code which I developed for an assignment. I am doing software design as a subject at school and with not C++ experience at all, our class had to create a tic tac toe game for the assignment.. This code before was absolutely terrible. With the help of a friend I managed to create a compilable code. Only problem: When I run it, it goes up to the part where players type in their name, their names are displayed on the screen, and the board is displayed. the game does not move further from their. It\ stays that way until you exit it.

THanks alot, your help will be appreciated
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
//SDD 2012
//Tic tac toe
//Year 12

#include <iostream>
#include <string>

using namespace std;

string playerone;
string playertwo;

//variables for spaces in the game board
char G;
char Grid[9];

//tic tac toe board for game play
char Print_Grid (char G)
{
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [0] && "|" && Grid [1] && "|" && Grid [2];
    cout << "         |        |        \n";
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
    cout << "         |        |        \n";
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [6] && "|" && Grid [7] && "|" && Grid [8];
    cout << "         |        |        \n";
    cout << "__________________________\n";


//if array has been filled then the game has ended
int number_of_moves = 0;

//to determine whose turn it is
char current_turn = '0';

//if winner has been found
bool foundwinner = false;

//check if game is over
bool gameover = false;

//main function
int main ()
{
    cout << "Tic Tac Toe";
    cout << "   begin   ";

    //allowing user to enter name and be identified by own name
    cout << "player one enter name\n";
    cin >> playerone;
    cout << "player two enter name\n";
    cin >> playertwo;

    //so player names can be shown on the screen.
    cout << playerone << "          " << playertwo << endl;


    Print_Grid(G);

    //while loop
    while (gameover == false)
    {
        void determine_turn();
        void get_move();
        void check_win();
    }


    return 0;

}

void determine_turn()
{
    playerone = 'X';
    playertwo = 'O';

    if (current_turn == 'X') //if X just had a turn change to O
    {
        current_turn = 'O';
    }
    else
    {
        current_turn = 'X';
    }


}


void get_move ()
{
    int move;
    if (current_turn == 'X')
    {
        cout << playerone << "select a location for 'X'" << endl;
        cin >> move;

        if (move == 1)
        {
            Grid[0] = 'X';
        }
        if (move == 2)
        {
            Grid[1] = 'X';
        }
        if (move == 3)
        {
            Grid[2] = 'X';
        }
        if (move == 4)
        {
            Grid[3] = 'X';
        }
        if (move == 5)
        {
            Grid[4] = 'X';
        }
        if (move == 6)
        {
            Grid[5] = 'X';
        }
        if (move == 7)
        {
            Grid[6] = 'X';
        }
        if (move == 8)
        {
            Grid[7] = 'X';
        }
        if (move == 9)
        {
            Grid[8] = 'X';
        }
    }
    else
    {
        cout << playertwo << "select location for '0'" << endl;
        cin >> move;

        if (move == 1)
        {
            Grid[0] = 'O';
        }
        if (move == 2)
        {
            Grid[1] = 'O';
        }
        if (move == 3)
        {
            Grid[2] = 'O';
        }
        if (move == 4)
        {
            Grid[3] = 'O';
        }
        if (move == 5)
        {
            Grid[4] = 'O';
        }
        if (move == 6)
        {
            Grid[5] = 'O';
        }
        if (move == 7)
        {
            Grid[6] = 'O';
        }
        if (move == 8)
        {
            Grid[7] = 'O';
        }
        if (move == 9)
        {
            Grid[8] = 'O';
        }
    }
}


void check_win()
{
    if
    (
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[6])) ||
        ((Grid[1] == Grid[4]) && (Grid[1] == Grid[7])) ||
        ((Grid[2] == Grid[3]) && (Grid[2] == Grid[8])) ||
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[2])) ||
        ((Grid[3] == Grid[3]) && (Grid[3] == Grid[5])) ||
        ((Grid[6] == Grid[3]) && (Grid[6] == Grid[8])) ||
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[8])) ||
        ((Grid[2] == Grid[3]) && (Grid[2] == Grid[6]))
    )
    {
        foundwinner = true;
        if (current_turn == '0')
        {
            cout << "the winner is" << playerone << endl;
        }
        else
        {
            cout << "the winner is" << playertwo << endl;
        }
    }
    else
    {
        foundwinner = false;

        if (foundwinner = true && number_of_moves == 9)
        {
            gameover = true;
        }
    }
}
closed account (o3hC5Di1)
Hi there,

You need to close the code block of the Print_grid function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//tic tac toe board for game play
char Print_Grid (char G)
{
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [0] && "|" && Grid [1] && "|" && Grid [2];
    cout << "         |        |        \n";
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
    cout << "         |        |        \n";
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [6] && "|" && Grid [7] && "|" && Grid [8];
    cout << "         |        |        \n";
    cout << "__________________________\n";
} // closing curly brace here 


Try that and please do let us know if the problem persists.

All the best,
NwN

Thanks. I didn't realise I forgot the curly brace. However, after that nothing happens . Whatever was on the screen stays, players cant even input move.
print_Grid function should return void.
For what i can see, your program has to be completely remade.
Try checking my Tic-Tac-Toe code (Windows only :c):

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
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

char Choice;
char Player1[20];
char Player2[20];
int State[10]={0}; //State of the squares from 1 to 9
int Win;

void Color(char color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

class Game{public: //1All the functions are here

    static void Square(int square) { //Used in the display
        if(State[square]==0){ //If state is 0, shows the square number
            Color(8); //Grey
            cout << " " << square << " ";
        }
        else if(State[square]==1){
            Color(12); //Brigth Red
            cout << " X "; //If state is 1, X
        }
        else if(State[square]==2){
            Color(9); //Brigth Blue
            cout << " O "; //If state is 2, O
        }
        Color(15); //Brigth White
    };

    static void Check(int c) { //Used in the main game code

        if(c==0){ //If everything is used...
            for(int x=1; State[x]>0; x++) {
                if(x==9){Win=0; break;} //It's a draw
            }
        }
        else{ //All the possible options of winning
            if((State[1]==c && State[2]==c && State[3]==c)
            || (State[4]==c && State[5]==c && State[6]==c)
            || (State[7]==c && State[8]==c && State[9]==c)
            || (State[1]==c && State[4]==c && State[7]==c)
            || (State[2]==c && State[5]==c && State[8]==c)
            || (State[3]==c && State[6]==c && State[9]==c)
            || (State[1]==c && State[5]==c && State[9]==c)
            || (State[3]==c && State[5]==c && State[7]==c)
            ){Win=c; return;} //If someone wins, it shows it in the display
        }
    };

    static void Display(int turn) { //Finally, the display itself.
        system("cls");
        if(Win>2){ //If Win is'nt 0 or 1 or 2
            if(turn==1){
                Color(12);
                cout << "\n --" << Player1 << "'s turn--";
            }
            if(turn==2){
                Color(9);
                cout << "\n --" << Player2 << "'s turn--";
            }
            Color(15);
            cout << "\n Choose the square with numbers 1-9 to place an ";
            if(turn==1){cout << "\" X \".\n";} else{cout << "\" O \".\n";}
        }
        //All the square displaying
        cout << "\n ";
        Game::Square(1); cout << "|";
        Game::Square(2); cout << "|";
        Game::Square(3); cout << "";
        cout << "\n -----------\n ";
        Game::Square(4); cout << "|";
        Game::Square(5); cout << "|";
        Game::Square(6); cout << "";
        cout << "\n -----------\n ";
        Game::Square(7); cout << "|";
        Game::Square(8); cout << "|";
        Game::Square(9); cout << endl;
        if(Win==0){cout << "\n --Draw--"; return;}
        if(Win==1){
            Color(12);
            cout << "\n --" << Player1 << " wins!--"; return;
        }
        if(Win==2){
            Color(9);
            cout << "\n --" << Player2 << " wins!--"; return;
        }
        Color(15);
    };

    static void Main(int turn) { // And finally, the main game code.
        Display(turn); //Displays and chooses wich turn it is
Loop:   Choice=getch();
        switch(Choice) { //All the keys to display an X or O in the screen
            default: goto Loop;
/*1*/       case '1':
                if(State[1]==0){State[1]=turn;} //Only editable if it has'nt been edited before
                else{goto Loop;} //If it has been edited before, it loops the key scanning
                break;
/*2*/       case '2':
                if(State[2]==0){State[2]=turn;}
                else{goto Loop;}
                break;
/*3*/       case '3':
                if(State[3]==0){State[3]=turn;}
                else{goto Loop;}
                break;
/*4*/       case '4':
                if(State[4]==0){State[4]=turn;}
                else{goto Loop;}
                break;
/*5*/       case '5':
                if(State[5]==0){State[5]=turn;}
                else{goto Loop;}
                break;
/*6*/       case '6':
                if(State[6]==0){State[6]=turn;}
                else{goto Loop;}
                break;
/*7*/       case '7':
                if(State[7]==0){State[7]=turn;}
                else{goto Loop;}
                break;
/*8*/       case '8':
                if(State[8]==0){State[8]=turn;}
                else{goto Loop;}
                break;
/*9*/       case '9':
                if(State[9]==0){State[9]=turn;}
                else{goto Loop;}
                break;
            /*case 'd': case 'D': Win=0; break;
            case 'r': case 'R': Win=1; break;
            case 'g': case 'G': Win=2; break;*/
        }
        Game::Check(1); //Checks if player 1 or player 2 wins
        Game::Check(2);
        if(Win==3){Game::Check(0);} //Then checks if it's a draw
        if(Win<3){return;}
    };
}Game;

int main()
{
    Start:
    system("cls"); Win=3;
    int x=0; while(x<10){State[x]=0; x=x+1;}
    Color(15);
    cout << "\n       .:Tic-Tac-Toe:.\n\n (Names can have 15 characters max. and be 1 word only)\n";
    Color(12);
    cout << "\n Player 1";
    Color(15);
    cout << ", choose a name\n@ ";
    cin >> Player1;
    Color(9);
    cout << "\n Player 2";
    Color(15);
    cout << ", choose a name\n@ ";
    cin >> Player2;
    Game:
    system("cls"); Win=3;
    x=0; while(x<10){State[x]=0; x=x+1;}
    while(true)
    { //The turns
        if(Win<3){Game.Display(0); goto Exit;}
        Game.Main(1);
        if(Win<3){Game.Display(0); goto Exit;}
        Game.Main(2);
        if(Win<3){Game.Display(0); goto Exit;}
    }
    Exit:
    Color(15);
    cout << "\n\n 1 = Play again\n 2 = Change names\n 3 = Exit\n\n@ ";
    cin >> Choice;
    switch(Choice) {
        default: break;
        case '1': goto Game;
        case '2': goto Start;
    }
    cout << "\n Closing Window...\n";
    return 0;
}


Please notice, this is a very old game I made, some extremely stupid things and use of goto could appear.
Last edited on
@Samrux Your code contains too many goto loops.
Sorry, I did'nt notice your post, I edited mine 1 minute after:
Please notice, this is a very old game I made, some extremely stupid things and use of goto could appear.


I feel stupid looking at all those gotos...
Last edited on
@samrux:
your last cout is useless unless they can see it :D (not that it changes the functionality). And if you only have one argument following a case in a switch, then you don't have to explicitly break? Sorry if these answers are obvious, but I'm really new to coding with anything that has switches.
The last cout can be seen as long as the user doesn't pick 1 or 2. As for your second question, I suppose you mean something like this:-

1
2
3
4
5
6
7
8
9
switch(x)
{
case 1:
    print 1;
    break; // this break statement is needed 
case 2:
    print 2;
    break; // this however isn't
}


Edit:

Change this:-
1
2
3
4
5
6
while (gameover == false)
    {
        void determine_turn();
        void get_move();
        void check_win();
    }


To:-
1
2
3
4
5
6
while (gameover == false)
    {
        determine_turn();
        get_move();
        check_win();
    }


You do not specify return type when calling functions.
Last edited on
closed account (o3hC5Di1)
@ OP: Having a closer look at that print_grid function reveals many more errors, I'm surprised this would even compile:

- you have not initialised the G or Grid variables for as far as I can see, yet you're passing char G to the function.
- however, you're not using G, you're using Grid within that function.

So your funciton should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Print_Grid (char[] Grid) //void here, as Disch said
{
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [0] && "|" && Grid [1] && "|" && Grid [2];
    cout << "         |        |        \n";
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
    cout << "         |        |        \n";
    cout << "__________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [6] && "|" && Grid [7] && "|" && Grid [8];
    cout << "         |        |        \n";
    cout << "__________________________\n";
} // closing curly brace here  


You will also have to initialise the Grid array, or it will not contain any values for you to display.
Easiest way to do so is:

1
2
//line 15
char Grid[9] = { '_', '_', '_', '_', '_', '_', '_', '_', '_' }; //fill array with underscores 
Last edited on
Use while loops in place of all your goto instead. Goto's are so bad in programming.
closed account (o3hC5Di1)
The OP's code doesn't contain goto's - 2 different tic tac toe programs in one thread is confusing.

-N
Ok. i got rid of the voids in the main program to call the subprograms, but it did not pick that up. Instead it said 'determine_move', and the other two were not declared in this scope.
I just put the called functions above the main program instead of underneath. It works differently, but there are pretty big errors in the program. If any of you have a C++ compiler, put this code in and try it, Im not sure how to describe the errors
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
//Maryann Dickinson
//SDD 2012
//Tic tac toe
//l

#include <iostream>
#include <string>

using namespace std;

string playerone;
string playertwo;

//variables for spaces in the game board
char G;
char Grid[9] = {'_','_', '_', '_', '_', '_', '_', '_', '_'};

//tic tac toe board for game play
void Print_Grid (char[])
{
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [0] && "|" && Grid [1] && "|" && Grid [2];
    cout << "         |        |        \n";
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
    cout << "         |        |        \n";
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [6] && "|" && Grid [7] && "|" && Grid [8];
    cout << "         |        |        \n";
    cout << "___________________________\n";
}

//if array has been filled then the game has ended
int number_of_moves = 0;

//to determine whose turn it is
char current_turn = '0';

//if winner has been found
bool foundwinner = false;

//check if game is over
bool gameover = false;

void determine_turn()
{
    playerone = 'X';
    playertwo = 'O';


    if (current_turn == 'X') //if X just had a turn change to O
    {
        current_turn = 'O';
    }
    else
    {
        current_turn = 'X';
    }


}


void get_move ()
{

    int move;
    if (current_turn == 'X')
    {
        cout << playerone << "select a location for 'X'" << endl;
        cin >> move;

        if (move == 1)
        {
            Grid[0] = 'X';
        }
        if (move == 2)
        {
            Grid[1] = 'X';
        }
        if (move == 3)
        {
            Grid[2] = 'X';
        }
        if (move == 4)
        {
            Grid[3] = 'X';
        }
        if (move == 5)
        {
            Grid[4] = 'X';
        }
        if (move == 6)
        {
            Grid[5] = 'X';
        }
        if (move == 7)
        {
            Grid[6] = 'X';
        }
        if (move == 8)
        {
            Grid[7] = 'X';
        }
        if (move == 9)
        {
            Grid[8] = 'X';
        }
    }
    else
    {
        cout << playertwo << "select location for '0'" << endl;
        cin >> move;

        if (move == 1)
        {
            Grid[0] = 'O';
        }
        if (move == 2)
        {
            Grid[1] = 'O';
        }
        if (move == 3)
        {
            Grid[2] = 'O';
        }
        if (move == 4)
        {
            Grid[3] = 'O';
        }
        if (move == 5)
        {
            Grid[4] = 'O';
        }
        if (move == 6)
        {
            Grid[5] = 'O';
        }
        if (move == 7)
        {
            Grid[6] = 'O';
        }
        if (move == 8)
        {
            Grid[7] = 'O';
        }
        if (move == 9)
        {
            Grid[8] = 'O';
        }
    }
}


void check_win()
{
    if
    (
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[6])) ||
        ((Grid[1] == Grid[4]) && (Grid[1] == Grid[7])) ||
        ((Grid[2] == Grid[3]) && (Grid[2] == Grid[8])) ||
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[2])) ||
        ((Grid[3] == Grid[3]) && (Grid[3] == Grid[5])) ||
        ((Grid[6] == Grid[3]) && (Grid[6] == Grid[8])) ||
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[8])) ||
        ((Grid[2] == Grid[3]) && (Grid[2] == Grid[6]))
    )
    {
        foundwinner = true;
        if (current_turn == '0')
        {
            cout << "the winner is" << playerone << endl;
        }
        else
        {
            cout << "the winner is" << playertwo << endl;
        }
    }
    else
    {
        foundwinner = false;

        if (foundwinner = true && number_of_moves == 9)
        {
            gameover = true;
        }
    }
}


//main function
int main ()
{
    cout << "Tic Tac Toe";
    cout << "   begin   ";

    //allowing user to enter name and be identified by own name
    cout << "player one enter name\n";
    cin >> playerone;
    cout << "player two enter name\n";
    cin >> playertwo;

    //so player names can be shown on the screen.
    cout << playerone << "          " << playertwo << endl;


    char Print_Grid(G);

    //while loop
    while (gameover == false)
    {
        while (gameover == false)
    {
        determine_turn();
        get_move();
        check_win();
    }




    }


    return 0;

}
Topic archived. No new replies allowed.