P
Pan Yongzhi
Hi all,
I wrote a simple C program, exactly at the bottom of this post. It's an
interactively game, needing user's input. When invoked at the Linux
shell, it works OK. But if I click in my ROX-Filer, nothing popups
since no GUI, but it then cosume almost 100% of my CPU time. I have to
kill it. I'd like to know if I can make it quit silently when It's
clicked. Thank you very much. Below is the whole source.
Pan
------------------------------------- start of source
---------------------------------------
/*
* 4digits - This program make the interactive game Guess-the-Number
game
* available on PC and other architectures.
*
* Copyright (C) 2005 Pan Yongzhi
* Reach me: http://pan.cdut.cn or fossilet@@163..com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA
* or see http://www.gnu.org/copyleft/gpl.html.
*
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <time.h>
# include <ctype.h>
# define VERSION_STRING "4digits-0.1"
# define COPYRIGHT \
"4digits 0.1, Dec 2005\n\
Copyright (C) 2004, 2005 Pan Yongzhi\n\
4digits comes with NO WARRANTY to the extent permitted by law. This
program\n\
is free software; you can redistribute it and/or modify it under the
terms\n\
of the GNU General Public License as published by the Free Software\n\
Foundation - version 2. For more information about these matters, see
the\n\
file named COPYING.\n\n"
int main(void)
{
int n; /* the 4-digit number to be guessed*/
char mstr[4]; /* string for the 4-digit number */
int m; /* the 4-digit number inputted */
int i,j; /* counters for the 4 digits, <=4*/
int cnt; /* counter for the time of guess, <=8*/
int A; /* number of digits both value and place is right*/
int B; /* number of digits only vaule but place is right*/
int nd[4]= {0,0,0,0}; /* 4x1 array for the 4 digits of n*/
int md[4]={0,0,0,0}; /* 4x1 arrays for the 4 digits of m*/
time_t t; /* time_t type variable, to be seed for random n*/
printf(COPYRIGHT);
cnt = 0;
srand((int) time(&t));
n = 1000 + (int) ( 8999.0 * rand() / RAND_MAX );
/* generate a random 4-digit number*/
/* if the digits of n is not different from each other, regenerate
it*/
while(nd[0]==nd[1] || nd[1]==nd[2] || nd[2]==nd[3] || nd[0]==nd[2] ||
nd[0]==nd[3] || nd[1]==nd[3])
{
srand(n);
n = 1000 + (int) ( 8999.0 * rand() / RAND_MAX );
for(i=0;i<4;i++)
nd=(int) ( n / pow(10,3-i) )%10;
}
do
{
printf("Input a 4-digit number:");
fflush(NULL);
scanf("%s", mstr);
m = atoi(mstr);
if ((m < 1000) | (m > 9999))
{
printf("%d: input error!\n", m);
cnt--;
continue;
}
for(i=0;i<4;i++)
md=(int) ( m / pow(10,3-i) )%10;
/*comparision of nd & md*/
A=0, B=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(md==nd[j])
(i==j) ? A++:B++;
printf("%dA%dB ",A,B);
printf("\t %d times left.\n", 7-cnt);
if(A==4)
printf("You win!\n"), exit(0);
}while(cnt++<=7);
printf("Hahah, you lose. It is %d.\n", n);
return 0;
}
------------------------------------- end of source
---------------------------------------
I wrote a simple C program, exactly at the bottom of this post. It's an
interactively game, needing user's input. When invoked at the Linux
shell, it works OK. But if I click in my ROX-Filer, nothing popups
since no GUI, but it then cosume almost 100% of my CPU time. I have to
kill it. I'd like to know if I can make it quit silently when It's
clicked. Thank you very much. Below is the whole source.
Pan
------------------------------------- start of source
---------------------------------------
/*
* 4digits - This program make the interactive game Guess-the-Number
game
* available on PC and other architectures.
*
* Copyright (C) 2005 Pan Yongzhi
* Reach me: http://pan.cdut.cn or fossilet@@163..com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA
* or see http://www.gnu.org/copyleft/gpl.html.
*
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <time.h>
# include <ctype.h>
# define VERSION_STRING "4digits-0.1"
# define COPYRIGHT \
"4digits 0.1, Dec 2005\n\
Copyright (C) 2004, 2005 Pan Yongzhi\n\
4digits comes with NO WARRANTY to the extent permitted by law. This
program\n\
is free software; you can redistribute it and/or modify it under the
terms\n\
of the GNU General Public License as published by the Free Software\n\
Foundation - version 2. For more information about these matters, see
the\n\
file named COPYING.\n\n"
int main(void)
{
int n; /* the 4-digit number to be guessed*/
char mstr[4]; /* string for the 4-digit number */
int m; /* the 4-digit number inputted */
int i,j; /* counters for the 4 digits, <=4*/
int cnt; /* counter for the time of guess, <=8*/
int A; /* number of digits both value and place is right*/
int B; /* number of digits only vaule but place is right*/
int nd[4]= {0,0,0,0}; /* 4x1 array for the 4 digits of n*/
int md[4]={0,0,0,0}; /* 4x1 arrays for the 4 digits of m*/
time_t t; /* time_t type variable, to be seed for random n*/
printf(COPYRIGHT);
cnt = 0;
srand((int) time(&t));
n = 1000 + (int) ( 8999.0 * rand() / RAND_MAX );
/* generate a random 4-digit number*/
/* if the digits of n is not different from each other, regenerate
it*/
while(nd[0]==nd[1] || nd[1]==nd[2] || nd[2]==nd[3] || nd[0]==nd[2] ||
nd[0]==nd[3] || nd[1]==nd[3])
{
srand(n);
n = 1000 + (int) ( 8999.0 * rand() / RAND_MAX );
for(i=0;i<4;i++)
nd=(int) ( n / pow(10,3-i) )%10;
}
do
{
printf("Input a 4-digit number:");
fflush(NULL);
scanf("%s", mstr);
m = atoi(mstr);
if ((m < 1000) | (m > 9999))
{
printf("%d: input error!\n", m);
cnt--;
continue;
}
for(i=0;i<4;i++)
md=(int) ( m / pow(10,3-i) )%10;
/*comparision of nd & md*/
A=0, B=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(md==nd[j])
(i==j) ? A++:B++;
printf("%dA%dB ",A,B);
printf("\t %d times left.\n", 7-cnt);
if(A==4)
printf("You win!\n"), exit(0);
}while(cnt++<=7);
printf("Hahah, you lose. It is %d.\n", n);
return 0;
}
------------------------------------- end of source
---------------------------------------