My first thought on seeing this code was, "OMG, who the hell would do it like that!" This was followed immediately with, "but, how could you do it like that..."
Here's an example of your code using functions. I should note that a number of your seed patterns are invalid.
CODE
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<cstdlib>
#include<ctime>
char x1, x2, x3, y1, y2, y3, z1, z2, z3;
bool isFull() {
return (!(
(x1==' ')||(x2==' ')||(x3==' ')
||(y1==' ')||(y2==' ')||(y3==' ')
||(z1==' ')||(z2==' ')||(z3==' ')
));
}
void placeNumber(const int w, char &cell) {
if (cell==' ') {
switch (w) {
case 1: cell='1';break;
case 2: cell='2';break;
case 3: cell='3';break;
case 4: cell='4';break;
case 5: cell='5';break;
case 6: cell='6';break;
case 7: cell='7';break;
case 8: cell='8';break;
case 9: cell='9';
}
} else {
cout<<"play again";
}
}
void showStatus() {
cout<<"Cells Numbers"<<endl;
cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
cout<<7<<" | "<<8<<" | "<<9<<endl;
cout<<"\n\nGrid Status\n";
cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
cout<<endl;
}
void set123(char &cell1,char &cell2,char &cell3) {
cell1='1'; cell2='2'; cell3='3';
}
bool isValidSet(const char cell1, const char cell2, const char cell3) {
if ((cell1==' ')||(cell2==' ')||(cell3==' ')) { return false; }
return ((cell1!=cell2) && (cell1!=cell3) && (cell2!=cell3));
}
int main () {
x1=x2=x3=y1=y2=y3=z1=z2=z3=' ';
srand( time(0) );
int face =1+rand() %15;
switch (face) {
case 1: set123(x3,y2,x1); break;
case 2: set123(z3,x1,y2); break;
case 3: set123(x2,y1,z2); break;
case 4: set123(y3,x2,z1); break;
case 5: set123(z2,y2,x3); break;
case 6: set123(z1,x2,y2); break;
case 7: set123(y2,x2,z3); break;
case 8: set123(x3,y2,z3); break;
case 9: set123(y3,y2,x2); break;
case 10: set123(x1,y2,z2); break;
case 11: set123(x3,x2,z2); break;
case 12: set123(y1,z1,x3); break;
case 13: set123(x2,y2,y1); break;
case 14: set123(y1,y2,y3); break;
case 15: set123(z3,z2,z1); break;
}
while (!isFull()) {
int q,w;
showStatus();
cout<<" enter the cell number or 0 to exit: ";
cin>>q;
if (q == 0) break;
cout<<"\n enter the a value: ";
cin>>w;
switch(q) {
case 1: placeNumber(w, x1); break;
case 2: placeNumber(w, x2); break;
case 3: placeNumber(w, x3); break;
case 4: placeNumber(w, y1); break;
case 5: placeNumber(w, y2); break;
case 6: placeNumber(w, y3); break;
case 7: placeNumber(w, z1); break;
case 8: placeNumber(w, z2); break;
case 9: placeNumber(w, z3); break;
}
}
if (isValidSet(x1, x2, x3)&& isValidSet(y1, y2, y3)&& isValidSet(z1, z2, z3)
&& isValidSet(x1, y1, z1)&& isValidSet(x2, y2, z2)&& isValidSet(x3, y3, z3))
{
cout<<"you win"<<endl;
} else {
cout<<"you lose"<<endl;
}
return 0;
}
Hope this makes sense and helps.