this beggining of tic tac toe program, why it no update?

Pages: 12
Oct 9, 2012 at 3:09pm
does c++ hate me? im trying o make a tic tac toe game work, this is only the beggining but when i test it, no board updaty :(
trying to understand why it wont update the characters in "getboard"

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

using namespace std;

char a,b,c,d,e,f,g,h,i,j;// so these represent the empty spaces in getbrd
char x = 'X';
char o = 'O';// these are what i want to replace the empty spaces with                  
string name;
string name2;
char brdlog [3] [3];// im not using this yet
void getbrdstart ();//this is just the display to start with
void getbrd ();
int input;
void plyr1 ();//the function that usees a switch statment to replace spaces in                 \\getbrd


int main ()

{
getbrdstart ();

cout << "TIC TAC TOE BY DEVONREVENGE\n"<<endl;

cout << "player 1 enter your name..."<<endl;
cin >> name;
cout << "player 2 enter your name..."<<endl;
cin >>name2;
plyr1 ();
getbrd ();



return 0;
}

void getbrd ()
{
cout << a << "|" << b << "|" << c<<endl;
cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << d<<"|"<<e<<"|"<< f <<endl;
  cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << g<<"|"<<h<<"|"<< i <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"<< endl;

}

void getbrdstart ()
{
cout << "1" << "|" << "2" << "|" << "3"<<endl;
cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << "4"<<"|"<<"5"<<"|"<< "6" <<endl;
  cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << "7"<<"|"<<"8"<<"|"<< "9\n \n" <<endl;
}


void plyr1 ()
{
cout << name << "input number to place your O"<<endl;
cin >> input;
switch (input)
{
case '1':
a=o;
break;
case '2':
b=o;
break;
case '3':
c=o;
break;
case '4':
d=o;
break;
case '5':
e=o;
break;
case '6':
f=o;
break;
case '7':
g=o;
break;
case '8':
h=o;
break;
case '9':
i=o;
break;
}
}

Oct 9, 2012 at 3:36pm
trying to understand why it wont update the characters in "getboard"

What is the output? Looks to me like you're giving cout a lot of uninitialized chars
Oct 9, 2012 at 4:01pm
it outputs the blank board
Oct 9, 2012 at 4:01pm
they not supposed to show up till they are initialized
Oct 9, 2012 at 4:14pm
Correct me if I'm wrong, but when you call plyr1(), only one of the variables between a and i has a valid value
Let's say I input 3, then c = 'O'. But a, b, d, e, f, g, h and i will still be uninitialized

And a tip: getbrdstart looks neater if you make it

1
2
3
4
5
cout << "1|2|3" <<endl;
cout << "-+-+-" <<endl;
cout << "4|5|6" <<endl;
cout << "-+-+-" <<endl;
cout << "7|8|9" <<endl << endl;

;)
Last edited on Oct 9, 2012 at 4:15pm
Oct 9, 2012 at 4:21pm
In function plyr1 (), the global variable "input" is used. This has type "int".
But all the case conditions test for a character, not an integer.
Oct 9, 2012 at 5:05pm
i love you chervil
Oct 10, 2012 at 10:51am
okay sos i fixed that now im having problems with logic...seems anyone can win if they complete the row how do i fix this and, is there a more elegant way to do 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
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
#include <iostream>
#include <string>

using namespace std;

int playloop = 0;
char a,b,c,d,e,f,g,h,i,j;
char x = 'X';
char o = 'O';
string name;
string name2;
void plyr2 ();
void checkboard();
char brdlog [3] [3];
void getbrdstart ();
void getbrd ();
char input;
void plyr1 ();
int gameloop;
int playloop2;

int main ()

{
getbrdstart ();

cout << "TIC TAC TOE BY DEVONREVENGE\n"<<endl;

cout << "player 1 enter your name..."<<endl;
cin >> name;
cout << "player 2 enter your name..."<<endl;
cin >>name2;

while (gameloop < 10)
{
 while (playloop < 10)
 {
 plyr1 ();
 getbrd ();
 checkboard();
 break;
 }
 while (playloop2 < 10)
 {
 plyr2 ();
 getbrd ();
 checkboard();
 break;
 }
}

return 0;
}

void getbrd ()
{
system("CLS");
cout << a << "|" << b << "|" << c<<endl;
cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << d<<"|"<<e<<"|"<< f <<endl;
  cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << g<<"|"<<h<<"|"<< i << endl;

}

void getbrdstart ()
{
cout << "1" << "|" << "2" << "|" << "3"<<endl;
cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << "4"<<"|"<<"5"<<"|"<< "6" <<endl;
  cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << "7"<<"|"<<"8"<<"|"<< "9\n \n" <<endl;
}


void plyr1 ()
{
cout << name << " input number to place your O"<<endl;
cin >> input;
switch (input)
{
case '1':
a=o;
break;
case '2':
b=o;
break;
case '3':
c=o;
break;
case '4':
d=o;
break;
case '5':
e=o;
break;
case '6':
f=o;
break;
case '7':
g=o;
break;
case '8':
h=o;
break;
case '9':
i=o;
break;
}
}

void plyr2 ()
{
cout << name2 << " input number to place your X"<<endl;
cin >> input;
switch (input)
{
case '1':
a=x;
break;
case '2':
b=x;
break;
case '3':
c=x;
break;
case '4':
d=x;
break;
case '5':
e=x;
break;
case '6':
f=x;
break;
case '7':
g=x;
break;
case '8':
h=x;
break;
case '9':
i=x;
break;
}
}

void checkboard()
{
if ((a,d,g) == o|| (a,b,c) == o|| (d,e,f) ==o|| (g,h,i)==o || (a,d,g)==o || (c,f,i)==o || (b,e,h)==o || (a,e,i)==o || (c,e,g)  == o)
{
cout << name << " WINS!!!"<< endl;
playloop = playloop+20;
playloop2 = playloop2+20;
gameloop = gameloop+20;
}
else if ((a,d,g)== x || (a,b,c)==x|| (d,e,f)==x || (g,h,i)==x || (a,d,g)==x || (c,f,i)==x || (b,e,h)==x || (a,e,i)==x || (c,e,g)  == x)
{
  cout << name2 << " WINS!!!"<< endl;
  playloop = playloop+20;
  playloop2 = playloop2+20;
  gameloop = gameloop+20;
}


}
Oct 10, 2012 at 11:25am
Why the variables a to j, as well as the char brdlog [3] [3]; ?

Normally, you would just use the 2d array to do everything.

with this:
if ((a,d,g) == o|| (a,b,c) == o|| .....

Does that work? The right way to do this is :

 
if (a  == o || d == o || g == o)


However there is a much easier way.

You are trying to check a win via rows columns and diagonals - this is much easier to do with the 2d array. For example a diagonal check involves checking [0][0], [1][1], and [2][2] and this is much easier with nested for loops. You should also have functions to do each of the row , col, diag checks.

Good Luck !!
Oct 10, 2012 at 12:26pm
fanks for the luck i will play with these :)

Last edited on Oct 10, 2012 at 12:33pm
Oct 10, 2012 at 5:01pm
CHECK OUT THESE WEIRD MYSTERY NUMBERS!!!

so you each go two spaces in the board increment each time!!! weird yes, put an o or x on one of them...they both get the value!! spooky

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

using namespace std;

int playloop = 0;
char x = 'X';
char o = 'O';
char a,b;
string name;
string name2;
void plyr2 ();
void checkboard();
char brdlog [2] [2];
void getbrdstart ();
void getbrd ();
char input;
void plyr1 ();
int gameloop;
int playloop2;

int main ()

{

getbrdstart ();

cout << "TIC TAC TOE BY DEVONREVENGE\n"<<endl;

cout << "player 1 enter your name..."<<endl;
cin >> name;
cout << "player 2 enter your name..."<<endl;
cin >>name2;

while (gameloop < 10)
{
 while (playloop < 10)
 {
 plyr1 ();
 getbrd ();
 checkboard();
 break;
 }
 while (playloop2 < 10)
 {
 plyr2 ();
 getbrd ();
 checkboard();
 break;
 }
}

return 0;
}

void getbrd ()
{
system("CLS");
cout << brdlog [0] [0] << "|" << brdlog [0] [1] << "|" << brdlog [0] [2]<<endl;
cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << brdlog [1] [0] <<"|"<<brdlog [1] [1] <<"|"<< brdlog [1] [2] <<endl;
  cout << "-"<< "+" << "-" << "+"<< "-"<<endl;
cout << brdlog [2] [0] <<"|"<< brdlog [2] [1] <<"|"<< brdlog [2] [2] << endl;

}

void getbrdstart ()
{
cout << "1|2|3"<<endl;
cout << "-+-+-"<<endl;
cout << "4|5|6" <<endl;
  cout << "-+-+-"<<endl;
cout << "7|8|9\n \n" <<endl;
}


void plyr1 ()
{
cout << name << " input number to place your O"<<endl;
cin >> input;
switch (input)
{
case '1':
brdlog [0] [0] =o;
break;
case '2':
brdlog [0] [1] =o;
break;
case '3':
brdlog [0] [2] =o;
break;
case '4':
brdlog [1] [0] =o;
break;
case '5':
brdlog [1] [1] =o;
break;
case '6':
brdlog [1] [2] =o;
break;
case '7':
brdlog[2] [0] =o;
break;
case '8':
brdlog [2] [1] =o;
break;
case '9':
brdlog [2] [2] =o;
break;
}
}


void plyr2()
{
cout << name2 << " input number to place your X"<<endl;
cin >> input;
switch (input)
{
case '1':
brdlog [0] [0] =x;
break;
case '2':
brdlog [0] [1] =x;
break;
case '3':
brdlog [0] [2] =x;
break;
case '4':
brdlog [1] [0] =x;
break;
case '5':
brdlog [1] [1] =x;
break;
case '6':
brdlog [1] [2] =x;
break;
case '7':
brdlog[2] [0] =x;
break;
case '8':
brdlog [2] [1] =x;
break;
case '9':
brdlog [2] [2] =x;
break;
}
}

void checkboard()
{


//sort this out later


}
Oct 10, 2012 at 5:11pm
also you ever had problems with the 'dream in code forum?'the members are like the gestapo.
Oct 10, 2012 at 5:18pm
This might be unrelated, but you defined the board array having 2 rows and 2 columns, not 3

also you ever had problems with the 'dream in code forum?'the members are like the gestapo.

Lol? What do you mean?
Oct 10, 2012 at 5:41pm
2 rows and 2 columns yes but dont arrays start with 0?

so many admins tracking you all the time...and the bad rep points...they seem to make you answer for yourself and then tell you off for bein stupid :'(
Last edited on Oct 10, 2012 at 5:41pm
Oct 10, 2012 at 5:48pm
closed account (3qX21hU5)
So just did a quick look at the code and not sure if I missed something but it looks to me that if I placed a X on number 1 on the board, the other player can still place a O on number 1 and delete the X.
Oct 10, 2012 at 5:52pm
Yes, they start with 0 to count the position, but when you define their size the number in the brackets represents the number of elements, not the position.
char array[1]
is the definition of an array with one element, which you will access using array[0]
char array[2]
has 2 elements, array[0] and array[1]
And so on
----------------------------
To be honest i never visited that forum. Sadly there will always be people like that, and the internet is always even worse
Last edited on Oct 10, 2012 at 5:57pm
Oct 10, 2012 at 5:57pm
yeah i got a lot more to do for now mr brandon sir, this is just the begining, ah i see maeriden thanks for that, i was wondering about mystery ghost numbers, was sure they started with zero in that way...well okay anythoughts on using a program that checks for spaces with x or o that could also be used by the computer player to see where everything is...maybe i should work that out got to practice and use your expert help when im stumped...thanks tho guys, buy you all ice creams if i could
Oct 10, 2012 at 6:08pm
closed account (3qX21hU5)
Still quite new myself at C++ but for your question.

well okay anythoughts on using a program that checks for spaces with x or o that could also be used by the computer player to see where everything is


You might be able to incorperate isalpha() into your program to test where there is a digit or letter on the space.
Last edited on Oct 10, 2012 at 6:09pm
Oct 10, 2012 at 6:38pm
hrmms kay...good to hear a new lil operator thing to work out...had enough troble with bool alpha though as it is...am still at that stage :/
----------------
yeah if i found them first and not you guys i wouldn't have made it past my hello world prgrm
Last edited on Oct 10, 2012 at 6:40pm
Oct 10, 2012 at 6:48pm
closed account (3qX21hU5)
If you need more info on the isalpha this link should help explain the cctype functions. Usefull functions for altering strings or chars and for testing.

http://www.cplusplus.com/reference/clibrary/cctype/
Pages: 12