Takes hours for program to run

When I run the program it takes a few hours to print the screen.

Can you help me figure out why it's taking so long to just print the screen?

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
#include <iostream>
#include <stdlib.h>
#include <random>
#include <time.h>
#include <windows.h>
#include <tuple>

using namespace std;

char obstacle[50][50];

int x = 0;
int y = 0;
int rx = 0;
int ry = 0;
int rcx = 0;
int rcy = 0;


tuple <int, int> Obstacle_generate()
{
        srand(time(0));
        int rx = rand()%50+0;
        int ry = rand()%50+0;

        return make_tuple (ry,rx);
}

tuple <int, int> Player_generate()
{
        srand(time(0));
        int rcx = rand()%50+0;
        int rcy = rand()%50+0;

        return make_tuple (rcy,rcx);
}

void Check_obstacle()
{
    bool ob_bool = false;
    tuple <int,int> t_obstacle;

            while(ob_bool != true)
        {
            t_obstacle = Obstacle_generate();

            ry = get<0>(t_obstacle);
            rx = get<1>(t_obstacle);

            if (obstacle[ry][rx] != 'O' && obstacle[ry][rx] != 'X')
            {
                obstacle[ry][rx] = 'O';
                ob_bool = true;
            }
        }
}

void Check_player()
{
    bool pl_bool = false;
    tuple <int,int> player;

        while(pl_bool != true)
        {
            player = Player_generate();

            rcy = get<0>(player);
            rcx = get<1>(player);

            if (obstacle[rcy][rcx] != 'O' && obstacle[rcy][rcx] != 'X')
            {
                obstacle[rcy][rcx] = 'X';
                pl_bool = true;
            }
        }

}

void Game_screen()
{

    int ob_count = 0;

    bool ob_check;
    bool pl_check;

    Check_player();

    for(y = 0; y < 50; y++)
    {
            for(x = 0; x < 50; x++)
            {
                ob_check = false;

                Check_obstacle();

                if(obstacle[y][x] != obstacle[ry][rx] && obstacle[y][x] != obstacle[rcy][rcx])
                {
                    obstacle[y][x] = ' ';
                    cout << obstacle[y][x] ;
                }

                else if(obstacle[y][x] == obstacle[ry][rx] && ob_check == false)
                {
                    cout << obstacle[ry][rx];
                    ob_check = true;
                }

                else if(obstacle[y][x] == obstacle[rcy][rcx] && pl_check == false)
                {
                    cout << obstacle[rcy][rcx];
                    pl_check = true;
                }
            }
        cout << "\n";
    }
    cout << "\n";
}

void Move_input()
{

}

void Clear_screen()
{

    system("cls");
}

void End_game()
{
    cout << "end game";
}

int main()
{

    string GAME;
    long long int count = 0;

    cout << "Type start to play.\n\n";

    cin >> GAME;
    if (GAME == "start")
    {
        do{
                cout << count;
            Clear_screen();
            Game_screen();
            cin >> GAME;
            Move_input();
        count++;
        }while(GAME != "quit");
    }

    End_game();
    return 0;
}
Last edited on
You should not initialise random seed each time when you call function.
Remove all srand(time(0)) and place a single one as first line in main()
Due to the way your program designed, all functions using srand() can block your program for at least a second.
Call srand() just once at the start of the program.

To understand why this makes a difference, suppose this is how rand() is implemented:
1
2
3
4
5
6
7
8
9
int next_value;

void srand(int x){
    next_value = x;
}

int rand(){
    return next_value++;
}
Knowing that time(0) gets the current time in seconds, can you guess what will happen?
Thanks MiiNiPaa and helios for your advice, it worked.

@helios I don't think I understand what you're trying to ask me. If I understand correctly the output wouldn't be random and will actually output a pattern.

But I do know that this implementation of a random number generator isn't really that good at being random. So now the trouble is how do I implement a true random device into the code.

Fixed 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
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
#include <iostream>
#include <stdlib.h>
#include <random>
#include <time.h>
#include <windows.h>
#include <tuple>
#include <functional>

using namespace std;

char obstacle[50][50];

int x = 0;
int y = 0;
int rx = 0;
int ry = 0;
int rcx = 0;
int rcy = 0;

tuple <int, int> Obstacle_generate()
{
        int rx = rand()%50+0;
        int ry = rand()%50+0;

        return make_tuple (ry,rx);
}

tuple <int, int> Player_generate()
{
        int rcx = rand()%50+0;
        int rcy = rand()%50+0;

        return make_tuple (rcy,rcx);
}

void Check_obstacle()
{
    bool ob_bool = false;
    tuple <int,int> t_obstacle;

            while(ob_bool != true)
        {
            t_obstacle = Obstacle_generate();

            ry = get<0>(t_obstacle);
            rx = get<1>(t_obstacle);

            if (obstacle[ry][rx] != 'O' && obstacle[ry][rx] != 'X')
            {
                obstacle[ry][rx] = 'O';
                ob_bool = true;
            }
        }
}

void Check_player()
{
    bool pl_bool = false;
    tuple <int,int> player;

        while(pl_bool != true)
        {
            player = Player_generate();

            rcy = get<0>(player);
            rcx = get<1>(player);

            if (obstacle[rcy][rcx] != 'O' && obstacle[rcy][rcx] != 'X')
            {
                obstacle[rcy][rcx] = 'X';
                pl_bool = true;
            }
        }

}

void Game_screen()
{

    int ob_count = 0;

    bool ob_check;
    bool pl_check;

    Check_player();

    for(y = 0; y < 50; y++)
    {
            for(x = 0; x < 50; x++)
            {
                ob_check = false;

                Check_obstacle();

                if(obstacle[y][x] != obstacle[ry][rx] && obstacle[y][x] != obstacle[rcy][rcx])
                {
                    obstacle[y][x] = ' ';
                    cout << obstacle[y][x] ;
                }

                else if(obstacle[y][x] == obstacle[ry][rx] && ob_check == false)
                {
                    cout << obstacle[ry][rx];
                    ob_check = true;
                }

                else if(obstacle[y][x] == obstacle[rcy][rcx] && pl_check == false)
                {
                    cout << obstacle[rcy][rcx];
                    pl_check = true;
                }
            }
        cout << "\n";
    }
    cout << "\n";
}

void Move_input()
{
       //empty
}

void Clear_screen()
{

    system("cls");
}

void End_game()
{
    cout << "end game";
}

int main()
{

    string GAME;
    long long int count = 0;

    srand(time(0)); //This was moved to here@@@@@@@

    cout << "Type start to play.\n\n";

    cin >> GAME;
    if (GAME == "start")
    {
        do{
                cout << count;
            Clear_screen();
            Game_screen();
            cin >> GAME;
            Move_input();
        count++;
        }while(GAME != "quit");
    }

    End_game();
    return 0;
}


helios is telling you, that if you do, for example, srand(5), next rand() will return, say, 3. If you do srand(5) again, rand() will return 3 again.
time() returns time in secons on Windows machine. And program can make thousands of function calls in that second. Having srand() in each function that uses rand() means that all numbers generated in same second will bethe same.

There was your problem: check_obstacle() generates obstacle, rejects it, generates again same obstacle (because you had srand(time(0))), rejects it... etc.
Until second changes. Then you have another chance. If it will be rejected again, you need to wait for next second again...
Oh wow, I see now.
Thanks for explaining it to me.
Topic archived. No new replies allowed.