TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Peter Shimmin
from: Frank Adam
date: 1996-04-04 16:04:04
subject: Student desperate f

G'Day Peter,
 
-=> Quoting Peter Shimmin to All <=-

 PS> I am to write a program to play the well-known game of Nim. The game
 PS> the remainder on dividing the stones left by 3; this will be 0, 1 or 2.
You can find the remainder by:  remainder = stonesleft % 3;

 PS> If it is 0, then take 2 stones; if it is 2, take 1 stone. If the
put all that into a switch (remainder)
                    {
                     case 0: stonesleft -= 2;break; take 2
                     case 1: stonesleft -= 1;break;      1
                     case 2: stonesleft -= 2;break;      2
                     }
  followed by if(stonesleft <= 0) won = 1;  (or 2  player/comp)         
                  
 PS> strategy, and then asks  the user how many stones she/he wishes to
 PS> take. When the number of stones reaches zero the program annonces the
Here is an algo you may want to play with, i think it's best if *you* work 
it out, but if you have probs you're welcome to email me or ask here.
I didn't bother including vars, prints etc... it's only a skeleton.

#define FALSE -1

main()
{
 setup();
 play();
 return 0;
 }

void setup()
{
while(!done)
{ 
 fgets(temp_str_var,6,stdin);   /* this limits to a five digit number; */
 totaltiles = checknum(temp_str_var);
 if(totaltiles > 0 ) done = 1; 
 else errormessg(0);                                             
 } /* end while */

done = 0;
while(!done) /* get starter */
{   
 key = getch();         /* get a key */
 switch(key){
              case '0' : starter = 0;done = 1;  /*starter is global */  
              case '1' : starter = 1;done = 1;
              default  : errormessg(1);
              }
            } /* end while */
/* all done. outahere  */
}                              

int checknum(char *input) /* check/convert string and return int if good */
{
 char *ptr; /* make a pointer, btw this function is complete as is */
 
 ptr = input; /* assign it to the string  */
 if(*ptr == '\n' || *ptr == '\0') return FALSE; /* empty or error */  
 
 while(*ptr != '\n') /* with fgets this'll be the second last character. */
  { if(!isdigit( (unsigned int)*ptr++) return FALSE; }
  return atoi(input); /* convert to integer and return it */
 }  
}

void play()
{
 if(starter == 0)
 {
  while(!won)  /* won would be global in this case initialized to 0*/
  {compmove();playermove();}
  }
    
 else 
 {
  while(!won) {playermove();compmove();}
  }
announce winner, set off fireworks,play anthem in b/ground,pay money etc..  
}   

compmove() and playermove() should be obvious as to what they'd do,i hope:)
              


  L8r Frank (fadam{at}ozemail.com.au).
      
___ Blue Wave/DOS v2.21
       

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 78/0 620/243 623/630 632/349 635/503 544 727 711/401 409 410
SEEN-BY: 711/413 430 808 809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 635/544 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.