Problem with printing board?

I've created a tic-tac-toe game than can be played against the computer, the board seems to change correctly, but there seems to be an error in printing the board? Could you please give a look to my code and say? (also there's a problem with altering its elements)

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

using namespace std;

char board[9];

void ini_board(){
for (int i=0; i<9; i++){
    board[i] = 'E';
}
}

void print_board(){
for (int i=0; i<9; i++){
    if ((i+1)%3==0){
        cout<<board[i]<<" ";
        cout<<"\n";
        cout<<string(5, '-');
        cout<<"\n";
    }
    else{
        cout<<board[i]<<":";
    }
}
}

void give_victory(bool &x, bool &y){
char winner;
if (board[0]==board[1]==board[2] && board[0]!='E'){
    winner = board[0];
}
else if(board[3]==board[4]==board[5] && board[3]!='E'){
    winner = board[3];
}
else if(board[6]==board[7]==board[8] && board[6]!='E'){
    winner = board[6];
}
else if(board[0]==board[4]==board[8] && board[0]!='E'){
    winner = board[0];
}
else if(board[2]==board[4]==board[6] && board[2]!='E'){
    winner = board[2];
}
if (winner=='X'){
    x = true;
}
else if(winner=='Y'){
    y = true;
}
}

void play(int &place, bool &x, bool &y){
for (int i=0; i<9; i++){
    if (board[i]!='E'){
        board[i] = 'X';
    }
    give_victory(x, y);
    if (x==true){
        place = i;
    }
    else if (x==false){
        board[i] = 'E';
    }
}
for (int i=0; i<9; i++){
    if (board[i]!='E'){
        board[i] = 'Y';
    }
    give_victory(x, y);
    if (y==true){
        place = i;
    }
    else if(y==false){
        board[i] = 'E';
    }
}
if (x==false && y==false){
    unsigned int de = rand() + 8;
    while(board[de]=='E'){
        de = rand() + 8;
    }
    place = de;
}

}
void cl_sc(){
cout<<string(1000, '\n');
}
int main(){
ini_board();
print_board();
bool x, y;
x = false;
y = false;
int plays = 0;
int cord;
int comp;
while (x==false && y==false && plays<10){
cout<<"Enter place: (1,2,3,4,5,6,7,8,9)\n";
cin>>cord;
while (board[cord]!='E' ||  cord>9){
    cl_sc();
    print_board();
    cout<<"Invalid! Enter again!\n";

}
board[cord] = 'Y';
give_victory(x, y);
plays++;
if (y==true){
    break;
}
play (comp, x, y);
board[comp] = 'X';
plays++;
cl_sc();
print_board();
}
return 0;
}
Last edited on
Line 2: Not sure what compiler you're using, but <random> is not a standard header. rand() is in the <cstdlib> header.

You don't call srand() anywhere to initialize the random number generator. You're going to get the same series of pseudo-random numbers every time you run your program.

Line 3: <time.h> is a C header. The correct C++ header is <ctime>.

Line 20: You use std::string, but you haven't included the <string> header.

Line 46: winner is being used without having been initialized.



Done, I included string, cstdlib, I used srand(time(NULL)) in the start of main, and initialized winner to a space. By the way, I'm using Code::Blocks with g++ following C++0x ISO standard. Here's the updated code, but the problem still persists.

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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

char board[9];

void ini_board(){
for (int i=0; i<9; i++){
    board[i] = 'E';
}
}

void print_board(){
for (int i=0; i<9; i++){
    if ((i+1)%3==0){
        cout<<board[i]<<" ";
        cout<<"\n";
        cout<<string(5, '-');
        cout<<"\n";
    }
    else{
        cout<<board[i]<<":";
    }
}
}

void give_victory(bool &x, bool &y){
char winner=' ';
if (board[0]==board[1] && board[0]==board[2] && board[0]!='E'){
    winner = board[0];
}
else if(board[3]==board[4] && board[3]==board[5] && board[3]!='E'){
    winner = board[3];
}
else if(board[6]==board[7] && board[6]==board[8] && board[6]!='E'){
    winner = board[6];
}
else if(board[0]==board[4] && board[0]==board[8] && board[0]!='E'){
    winner = board[0];
}
else if(board[2]==board[4] && board[2]==board[6] && board[2]!='E'){
    winner = board[2];
}
if (winner=='X'){
    x = true;
}
else if(winner=='Y'){
    y = true;
}
}

void play(int &place, bool &x, bool &y){
for (int i=0; i<9; i++){
    if (board[i]!='E'){
        board[i] = 'X';
    }
    give_victory(x, y);
    if (x==true){
        place = i;
        return;
    }
    else if (x==false){
        board[i] = 'E';
    }
}
for (int i=0; i<9; i++){
    if (board[i]!='E'){
        board[i] = 'Y';
    }
    give_victory(x, y);
    if (y==true){
        place = i;
        return;
    }
    else if(y==false){
        board[i] = 'E';
    }
}
if (x==false && y==false){
    unsigned int de = rand() + 8;
    while(board[de]=='E'){
        de = rand() + 8;
    }
    place = de;
}

}
void cl_sc(){
cout<<string(1000, '\n');
}
int main(){
srand(time(NULL));
ini_board();
print_board();
bool x, y;
x = false;
y = false;
int plays = 0;
int cord;
int comp;
while (x==false && y==false && plays<10){
cout<<"Enter place: (1,2,3,4,5,6,7,8,9)\n";
cin>>cord;
while (board[cord]!='E' ||  cord>9){
    cl_sc();
    print_board();
    cout<<"Invalid! Enter again!\n";
    cin>>cord;
}
board[cord] = 'Y';
give_victory(x, y);
plays++;
if (y==true){
    break;
}
play (comp, x, y);
plays++;
cl_sc();
print_board();
}
if (x==true){
    cout<<"Player wins!";
}
else{
    cout<<"Computer wins!";
}
return 0;
}
Topic archived. No new replies allowed.