int random (int a,int b)
{
if (b<a) return 0;
int good;
random:
int r=rand();
if (r>=a) good=1;
else goto random;
if (r<=b) good+=1;
else goto random;
if (good==2)return r;
else return 0;
}
int coor[16][16];
int x=8;
int y=8;
int action;
void move();
void print();
int main()
{
coor[x][y]=1;
cs();
cout << "X: " << x << " Y: " << y << "\n";
print();
move();
}
void move()
{
cin >> action;
switch(action)
{
case 2:
coor[x][y]=0;
y++;
break;
case 8:
coor[x][y]=0;
y--;
break;
case 6:
coor[x][y]=0;
x++;
break;
case 4:
coor[x][y]=0;
x--;
break;
}
if (x<0)x=0;
if (x>15)x=15;
if (y<0)y=0;
if (y>15)x=15;
main();
}
void print()
{
int pX=0;
int pY=0;
while(pX<=16 && pY<=15)
{
cout << coor[pX][pY];
pX++;
if (pX==16)
{
cout << "\n";
pX=0;
pY++;
}
}
}
suggestions and comments are welcome!
also, some 1 knows a good source to learn GUI C++? _________________
"I finally started thinking outside of the box, only to find myself in a larger box."
was gonna rewrite your program but got lazy. just changed one function:
Code:
int random (int a,int b) {
assert( a < b );
// use assert instead since 0 is potentially a valid return
// assert is for debugging, I would advise sanitizing before calling
// or returning an invalid value ( -1 ? )
//if ( b < a ) {
// return 0;
//}
// assuming b - a < RAND_MAX
return a + ( rand() % ( b - a ) );
}
your code is a horrible example of 'c++'. you make absolutely no use of the OOP paradigm. your code is basically unreadable. it is counter-intuitive, messy and you hardcode in magic numbers without comments or #define. the style of your programming is an open invitation for logic bugs as well as providing next to no readability value.
variables that do not have to be global should be localised to a function scope and if they are needed elsewhere, should be passed as parameters. this decouples code and breaks the dependencies you have created. it is in general, bad style to call main().
don't get started on gui code until you understand basic C++.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum