12 Replies - 1026 Views - Last Post: 12 March 2008 - 09:20 PM Rate Topic: -----

#1 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

First C program, NIM gane

Posted 12 March 2008 - 05:24 PM

This is my first C program. I am trying to implement a NIM game. I am pretty sure I am not using "scanf" right. Any help is appreciated.


  
* This program implements a NIM game with normal 
  * win rule.  The game starts with three piles
  * of sticks each containing between 1 and 50 sticks 
  * which are represented with a “*”. The number of
  * sticks in each pile is specified by the players.
  * The two players take turns removing any positive 
  * number of sticks from a pile that is not empty.  
  * The player that takes the last stick wins.  The
  * program refreshes the number of sticks in each pile
  * after every turn and prints out graphically the number 
  * of sticks in each pile.  The program checks for several
  * different errors throughout. 
  * 
*/

#include <stdio.h>
#include <stdlib.h>

#define MSG_0 0
#define MSG_1 1
#define MSG_2 2
#define MSG_3 3
#define MSG_4 4
#define MSG_5 5
#define MSG_6 6
#define MSG_7 7
#define MSG_8 8
#define MSG_9 9

void printPiles (int pile1, int pile2, int pile3);
int printMsg (int msgCode, int player);

/* Main function	*/
int main ()
{


  /* declare variables */
		#define EMPTY 0
int pile1;
int pile2;
int pile3;
int A;
int pile;
int player;
int condition;
int flag;

/* implement the game */
		player = 1;
		printMsg (MSG_0, 1);
		scanf(“%d %d %d”, &pile1, &pile2, &pile3);
		
		scanf = flag;
		condition = 1;
		while(condition == 1) 
		{
		if(flag != 3)
			{
			printMsg (MSG_5, 1);
 			end(1);
			}

		if(pile1 > 50 || pile1 < 1)
			printMsg (MSG_6, 1);
			
		if(pile2 > 50 || pile2 < 1)
			printMsg (MSG_6, 1);	
		
		if(pile3 > 50 || pile3 < 1)
			printMsg (MSG_6, 1);

		else
			condition = 0;
		}

		

		printPiles(pile1, pile2, pile3);


	if (pile1 > 0 || pile2 > 0 || pile3 >0)
	{
		if (player == 1)
		{
			printMsg (MSG_1, 1);
			player = player + 1;
		}

		else
		{
			printMsg (MSG_1, 2);
			player = player - 1;
		}

		printMsg (MSG_2, 1);
		scanf(“%d”, &pile);
		if (scanf == 0)
		{	
			printMsg (MSG_5, 1);
			exit(1);
		}
		printMsg (MSG_3, 1);
		scanf("%d", &A);
		if (scanf == 0)
		{
			printMsg (MSG_5, 1);
			exit(1);
		}
		
		
		if (pile == 1)
		{
				if (pile1 != 0)
				{
					pile1 = pile1 – A;
					if (pile1 <= 0)
						pile1 = 0;
				}
				
				else
					printMsg (MSG_8, 1);

		}
	   		

		else if (pile == 2)
		{
				if (pile2 != 0)
				{
					pile2 = pile2 – A;
					if (pile1 <= 0)
						pile2 = 0;
				}
				
				else
					printMsg (MSG_8, 1);

		}
		
		else if (pile == 3)
		{
				if (pile3 != 0)
				{
					pile3 = pile3 – A;
					if (pile3 <= 0)
						pile3 = 0;
				}
				
				else
					printMsg (MSG_8, 1);

		}

		else  
 		 	printMsg (MSG_7, 1);
	}	
	
	else
	{
		if (player = 1)
			player = player + 1;
		else
			player = player - 1;
		
		printMsg (MSG_1, 1);
			exit(1);
	}	
		


  return 0;
}

/* Function: printPiles()
 * This function visually prints out the three piles with asterisks
 * Inputs: the number of sticks in pile1, pile2, pile3
 * Output: none
 */
void printPiles (int pile1, int pile2, int pile3)
{
  /* declare variables */

int counter;  



  /* write your printing code here */

		pile1 = counter;
		printf("Pile #1: ");
		while (counter > 0)
		{
			printf(*);
			counter = counter - 1; 				
		}

		pile2 = counter;
		printf("Pile #2: ");
		while (counter > 0)
		{
			printf(*);
			counter = counter - 1;
		}

		pile3 = counter;
		printf("Pile #3: ");
		while (counter > 0)
		{
			printf(*);
			counter = counter - 1;
		}		


}

/* Printing messages:
 * This function prints out a message corresponding to a message code.  
 * Input: the message code, the player ID for the outputs for a particular player.
 * Output: 0 if no error, 1 if error. 
 */
int printMsg (int msgCode, int player)
{

  if ( msgCode < 0 || msgCode > 9)
	return 1;
  if ( msgCode == 1 || msgCode == 4 )
	if ( player < 1 || player > 2 )
	  return 1;

  switch (msgCode)
  {
	case 0: printf("Enter pile sizes (separated by spaces): "); break;
	case 1: printf("Player %d's turn:\n", player); break;
	case 2: printf("Pick a pile #: "); break;
	case 3: printf("Remove how many sticks? "); break;
	case 4: printf("Congratulations! Player %d is the winner!\n", player); break;
	case 5: printf("Invalid input, end of game!\n"); break;
	case 6: printf("Invalid pile sizes. Try again.\n"); break;
	case 7: printf("Invalid pile. Try again.\n"); break;
	case 8: printf("That pile is empty. Try again.\n"); break;
	case 9: printf("You have to take at least one stick!\n"); break;
	default: break;
  }
  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: First C program, NIM gane

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: First C program, NIM gane

Posted 12 March 2008 - 05:34 PM

scanf(“%d %d %d”, &pile1, &pile2, &pile3);

scanf = flag;


you mean...

flag = scanf(“%d %d %d”, &pile1, &pile2, &pile3); ???

you are also using the formatted quote and not the ones in the standard ASCII table

change the quotes inside scanf.

This post has been edited by letthecolorsrumble: 12 March 2008 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: First C program, NIM gane

Posted 12 March 2008 - 05:39 PM

I'm not sure. I am getting a lot of errors when i try to compile it. It seems that all of the errors are around where I use scanf.

I fixed the quotes throughout. Still getting a lot of errors. haha

mp3.c:55:9: warning: character constant too long for its type
mp3.c: In function ‘main’:
mp3.c:55: warning: passing argument 1 of ‘scanf’ makes pointer from integer without a cast
mp3.c:57: error: invalid lvalue in assignment
mp3.c:64: warning: implicit declaration of function ‘end’
mp3.c:100:9: warning: multi-character character constant
mp3.c:100: warning: passing argument 1 of ‘scanf’ makes pointer from integer without a cast
mp3.c:107:9: warning: multi-character character constant
mp3.c:107: warning: passing argument 1 of ‘scanf’ makes pointer from integer without a cast
mp3.c:119: error: stray ‘\342’ in program
mp3.c:119: error: stray ‘\200’ in program
mp3.c:119: error: stray ‘\223’ in program
mp3.c:119: error: expected ‘;’ before ‘A’
mp3.c:134: error: stray ‘\342’ in program
mp3.c:134: error: stray ‘\200’ in program
mp3.c:134: error: stray ‘\223’ in program
mp3.c:134: error: expected ‘;’ before ‘A’
mp3.c:148: error: stray ‘\342’ in program
mp3.c:148: error: stray ‘\200’ in program
mp3.c:148: error: stray ‘\223’ in program
mp3.c:148: error: expected ‘;’ before ‘A’
mp3.c:164: warning: suggest parentheses around assignment used as truth value
mp3.c:194:10: warning: character constant too long for its type
mp3.c: In function ‘printPiles’:
mp3.c:194: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
mp3.c:197: error: expected expression before ‘)’ token
mp3.c:202:10: warning: character constant too long for its type
mp3.c:202: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
mp3.c:205: error: expected expression before ‘)’ token
mp3.c:210:10: warning: character constant too long for its type
mp3.c:210: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
mp3.c:213: error: expected expression before ‘)’ token
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: First C program, NIM gane

Posted 12 March 2008 - 05:49 PM

Well, the errors are gone, but important is that you should always code inside a program like notepad or any other simple text editor; do not ever copy your code to and from a word-processor like WORD or Open Office Writer.

The formatting in these programs can create a beginner's nightmare.

And when you want to assign a value to a variable say:

int counter;

counter = pile1; and not pile1 = counter; the latter statement will assign the value of counter to pile1 and not what you actually want to happen.

/* This program implements a NIM game with normal
  * win rule.  The game starts with three piles
  * of sticks each containing between 1 and 50 sticks
  * which are represented with a “*”. The number of
  * sticks in each pile is specified by the players.
  * The two players take turns removing any positive
  * number of sticks from a pile that is not empty.  
  * The player that takes the last stick wins.  The
  * program refreshes the number of sticks in each pile
  * after every turn and prints out graphically the number
  * of sticks in each pile.  The program checks for several
  * different errors throughout.
  *
*/

#include <stdio.h>
#include <stdlib.h>

#define MSG_0 0
#define MSG_1 1
#define MSG_2 2
#define MSG_3 3
#define MSG_4 4
#define MSG_5 5
#define MSG_6 6
#define MSG_7 7
#define MSG_8 8
#define MSG_9 9

void printPiles (int pile1, int pile2, int pile3);
int printMsg (int msgCode, int player);

/* Main function    */
int main () {


	/* declare variables */
        #define EMPTY 0
		int pile1;
		int pile2;
		int pile3;
		int A;
		int pile;
		int player;
		int condition;
		int flag;

	/* implement the game */
        player = 1;
        printMsg (MSG_0, 1);
        flag = scanf("%d %d %d", &pile1, &pile2, &pile3);
        
        //scanf = flag;
        condition = 1;
        while(condition == 1) {
			if(flag != 3) {
				printMsg (MSG_5, 1);
				exit(1);
			}
		}
        if(pile1 > 50 || pile1 < 1)
            printMsg (MSG_6, 1);
            
        if(pile2 > 50 || pile2 < 1)
            printMsg (MSG_6, 1);    
        
        if(pile3 > 50 || pile3 < 1)
            printMsg (MSG_6, 1);

        else
            condition = 0;
        

        

        printPiles(pile1, pile2, pile3);


    if (pile1 > 0 || pile2 > 0 || pile3 >0) {
        if (player == 1) {
            printMsg (MSG_1, 1);
            player = player + 1;
        }

        else {
            printMsg (MSG_1, 2);
            player = player - 1;
        }

        printMsg (MSG_2, 1);
        flag = scanf("%d", &pile);
        if (flag == 0) {    
            printMsg (MSG_5, 1);
            exit(1);
        }
        printMsg (MSG_3, 1);
        scanf("%d", &A);
        if (scanf == 0)
        {
            printMsg (MSG_5, 1);
            exit(1);
        }
        
        
        if (pile == 1)
        {
                if (pile1 != 0)
                {
                    pile1 = pile1 - A;
                    if (pile1 <= 0)
                        pile1 = 0;
                }
                
                else
                    printMsg (MSG_8, 1);

        }
               

        else if (pile == 2)
        {
                if (pile2 != 0)
                {
                    pile2 = pile2 - A;
                    if (pile1 <= 0)
                        pile2 = 0;
                }
                
                else
                    printMsg (MSG_8, 1);

        }
        
        else if (pile == 3)
        {
                if (pile3 != 0)
                {
                    pile3 = pile3 - A;
                    if (pile3 <= 0)
                        pile3 = 0;
                }
                
                else
                    printMsg (MSG_8, 1);

        }

        else  
             printMsg (MSG_7, 1);
    }    
    
    else
    {
        if (player = 1)
            player = player + 1;
        else
            player = player - 1;
        
        printMsg (MSG_1, 1);
            exit(1);
    }    
        


  return 0;
}

/* Function: printPiles()
* This function visually prints out the three piles with asterisks
* Inputs: the number of sticks in pile1, pile2, pile3
* Output: none
*/
void printPiles (int pile1, int pile2, int pile3) {
  /* declare variables */

int counter;  



  /* write your printing code here */

        counter = pile1;
        printf("Pile #1: ");
        while (counter > 0)
        {
            printf("*");
            counter = counter - 1;                 
        }

        counter = pile2;
        printf("Pile #2: ");
        while (counter > 0)
        {
            printf("*");
            counter = counter - 1;
        }

        counter = pile3;
        printf("Pile #3: ");
        while (counter > 0)
        {
            printf("*");
            counter = counter - 1;
        }        


}

/* Printing messages:
* This function prints out a message corresponding to a message code.  
* Input: the message code, the player ID for the outputs for a particular player.
* Output: 0 if no error, 1 if error.
*/
int printMsg (int msgCode, int player)
{

  if ( msgCode < 0 || msgCode > 9)
    return 1;
  if ( msgCode == 1 || msgCode == 4 )
    if ( player < 1 || player > 2 )
      return 1;

  switch (msgCode)
  {
    case 0: printf("Enter pile sizes (separated by spaces): "); break;
    case 1: printf("Player %d's turn:\n", player); break;
    case 2: printf("Pick a pile #: "); break;
    case 3: printf("Remove how many sticks? "); break;
    case 4: printf("Congratulations! Player %d is the winner!\n", player); break;
    case 5: printf("Invalid input, end of game!\n"); break;
    case 6: printf("Invalid pile sizes. Try again.\n"); break;
    case 7: printf("Invalid pile. Try again.\n"); break;
    case 8: printf("That pile is empty. Try again.\n"); break;
    case 9: printf("You have to take at least one stick!\n"); break;
    default: break;
  }
  return 0;
}



The program does not run after the inputs, but that is for some other post.

Happy Coding :)
Was This Post Helpful? 0
  • +
  • -

#5 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: First C program, NIM gane

Posted 12 March 2008 - 06:01 PM

Thank you very much for the help. Were the quotes and integers the only thing that was wrong?
Also, I am getting a warning

mp3.c:156: warning: suggest parentheses around assignment used as truth value
line 156) if (player = 1)
Was This Post Helpful? 0
  • +
  • -

#6 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: First C program, NIM gane

Posted 12 March 2008 - 06:12 PM

Well it didnt show up in my complier, but it should be changed to

if(player == 1)
Was This Post Helpful? 0
  • +
  • -

#7 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: First C program, NIM gane

Posted 12 March 2008 - 06:14 PM

Thanks, you're a lifesaver.
Was This Post Helpful? 0
  • +
  • -

#8 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: First C program, NIM gane

Post icon  Posted 12 March 2008 - 07:24 PM

I am having difficulties using the debugger that was recomended for this assignment. I am pretty sure there is somthing wrong with the program after the first input.

  
 * This program implements a NIM game with normal 
   * win rule.  The game starts with three piles 
   * of sticks each containing between 1 and 50 sticks 
   * which are represented with a “*”. The number of 
   * sticks in each pile is specified by the players. 
   * The two players take turns removing any positive 
   * number of sticks from a pile that is not empty.   
   * The player that takes the last stick wins.  The 
   * program refreshes the number of sticks in each pile 
   * after every turn and prints out graphically the number 
   * of sticks in each pile.  The program checks for several 
   * different errors throughout. 
   * 
 */  
   
 #include <stdio.h>  
 #include <stdlib.h>  
   
 #define MSG_0 0  
 #define MSG_1 1  
 #define MSG_2 2  
 #define MSG_3 3  
 #define MSG_4 4  
 #define MSG_5 5  
 #define MSG_6 6  
 #define MSG_7 7  
 #define MSG_8 8  
 #define MSG_9 9  
   
 void printPiles (int pile1, int pile2, int pile3);  
 int printMsg (int msgCode, int player);  
   
 /* Main function	*/  
 int main () {  
   
   
	 /* declare variables */  
		 #define EMPTY 0  
		 int pile1;  
		 int pile2;  
		 int pile3;  
		 int A;  
		 int pile;  
		 int player;  
		 int condition;  
		 int flag;  
   
	 /* implement the game */  
		 player = 1;  
		 printMsg (MSG_0, 1);  
		flag = scanf("%d %d %d", &pile1, &pile2, &pile3);  
		   
		   if(flag != 3) 
	   {  
				 printMsg (MSG_5, 1);  
				 exit(1);  
		   }  
		 
	condition = 1;  
		 while(condition == 1)  
	{ 
		 if(pile1 > 50 || pile1 < 1)  
			 printMsg (MSG_6, 1);  
			   
		 if(pile2 > 50 || pile2 < 1)  
			 printMsg (MSG_6, 1);	  
		   
		 if(pile3 > 50 || pile3 < 1)  
			 printMsg (MSG_6, 1);  
   
		 else  
			 condition = 0;  
		   
   	}
		   
   
		 printPiles(pile1, pile2, pile3);  
   
   
	 if (pile1 > 0 || pile2 > 0 || pile3 >0) {  
		 if (player == 1) {  
			 printMsg (MSG_1, 1);  
			 player = player + 1;  
		 }  
   
		 else {  
			 printMsg (MSG_1, 2);  
			 player = player - 1;  
		 }  
   
		 printMsg (MSG_2, 1);  
		 flag = scanf("%d", &pile);  
		 if (flag == 0) {	  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  
		 printMsg (MSG_3, 1);  
		 scanf("%d", &A);  
		 if (scanf == 0)  
		 {  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  
		   
		   
		 if (pile == 1)  
		 {  
				 if (pile1 != 0)  
				 {  
					 pile1 = pile1 - A;  
					 if (pile1 <= 0)  
						 pile1 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
				  
   
		 else if (pile == 2)  
		 {  
				 if (pile2 != 0)  
				 {  
					 pile2 = pile2 - A;  
					 if (pile1 <= 0)  
						 pile2 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
		   
		 else if (pile == 3)  
		 {  
				 if (pile3 != 0)  
				 {  
					 pile3 = pile3 - A;  
					 if (pile3 <= 0)  
						 pile3 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
   
		 else	
			  printMsg (MSG_7, 1);  
	 }	  
	   
	 else  
	 {  
		 if (player == 1)  
			 player = player + 1;  
		 else  
			 player = player - 1;  
		   
		 printMsg (MSG_1, 1);  
			 exit(1);  
	 }	  
		   
   
   
   return 0;  
 }  
   
 /* Function: printPiles() 
 * This function visually prints out the three piles with asterisks 
 * Inputs: the number of sticks in pile1, pile2, pile3 
 * Output: none 
 */  
 void printPiles (int pile1, int pile2, int pile3) {  
   /* declare variables */  
   
 int counter;	
   
   
   
   /* write your printing code here */  
   
		 counter = pile1;  
		 printf("Pile #1: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;				   
		 }  
   
		 counter = pile2;  
		 printf("Pile #2: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }  
   
		 counter = pile3;  
		 printf("Pile #3: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }		  
   
   
 }  
   
 /* Printing messages: 
 * This function prints out a message corresponding to a message code.   
 * Input: the message code, the player ID for the outputs for a particular player. 
 * Output: 0 if no error, 1 if error. 
 */  
 int printMsg (int msgCode, int player)  
 {  
   
   if ( msgCode < 0 || msgCode > 9)  
	 return 1;  
   if ( msgCode == 1 || msgCode == 4 )  
	 if ( player < 1 || player > 2 )  
	   return 1;  
   
   switch (msgCode)  
   {  
	 case 0: printf("Enter pile sizes (separated by spaces): "); break;  
	 case 1: printf("Player %d's turn:\n", player); break;  
	 case 2: printf("Pick a pile #: "); break;  
	 case 3: printf("Remove how many sticks? "); break;  
	 case 4: printf("Congratulations! Player %d is the winner!\n", player); break;  
	 case 5: printf("Invalid input, end of game!\n"); break;  
	 case 6: printf("Invalid pile sizes. Try again.\n"); break;  
	 case 7: printf("Invalid pile. Try again.\n"); break;  
	 case 8: printf("That pile is empty. Try again.\n"); break;  
	 case 9: printf("You have to take at least one stick!\n"); break;  
	 default: break;  
   }  
   return 0;  
 } 

Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: First C program, NIM gane

Posted 12 March 2008 - 07:32 PM

Please don't post duplicate topics.

*topics merged*
Was This Post Helpful? 0
  • +
  • -

#10 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: First C program, NIM gane

Posted 12 March 2008 - 07:54 PM

Does anyone see an error in the first few steps? After the pile size inputs are entered it seems to get hung up or not go anywhere.
Was This Post Helpful? 0
  • +
  • -

#11 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: First C program, NIM gane

Posted 12 March 2008 - 08:24 PM

Now, the program exits after it asks for how many sticks to remove. There is obviously a mistake in one of "if" statements. Again, any help is always appreciated

Here is the updated code:

   * This program implements a NIM game with normal 
   * win rule.  The game starts with three piles 
   * of sticks each containing between 1 and 50 sticks 
   * which are represented with a “*”. The number of 
   * sticks in each pile is specified by the players. 
   * The two players take turns removing any positive 
   * number of sticks from a pile that is not empty.   
   * The player that takes the last stick wins.  The 
   * program refreshes the number of sticks in each pile 
   * after every turn and prints out graphically the number 
   * of sticks in each pile.  The program checks for several 
   * different errors throughout. 
   * 
 */  
   
 #include <stdio.h>  
 #include <stdlib.h>  
   
 #define MSG_0 0  
 #define MSG_1 1  
 #define MSG_2 2  
 #define MSG_3 3  
 #define MSG_4 4  
 #define MSG_5 5  
 #define MSG_6 6  
 #define MSG_7 7  
 #define MSG_8 8  
 #define MSG_9 9  
   
 void printPiles (int pile1, int pile2, int pile3);  
 int printMsg (int msgCode, int player);  
   
 /* Main function	*/  
 int main () {  
   
   
	 /* declare variables */  
		 #define EMPTY 0  
		 int pile1;  
		 int pile2;  
		 int pile3;  
		 int A;  
		 int pile;  
		 int player;  
		 int condition;  
		 int flag;  
   
	 /* implement the game */  
		 player = 1;  
		 printMsg (MSG_0, 1);  
		flag = scanf("%d %d %d", &pile1, &pile2, &pile3);  
		   
		   if(flag != 3) 
	   {  
				 printMsg (MSG_5, 1);  
				 exit(1);  
		   }  
		 
	condition = 1;  
		 while(condition == 1)  
	{ 
		 if(pile1 > 50 || pile1 < 1)  
			 printMsg (MSG_6, 1);  
			   
		 if(pile2 > 50 || pile2 < 1)  
			 printMsg (MSG_6, 1);	  
		   
		 if(pile3 > 50 || pile3 < 1)  
			 printMsg (MSG_6, 1);  
   
		 else  
			 condition = 0;  
		   
   	}
		   
   
		 printPiles(pile1, pile2, pile3);  
   
   
	 if (pile1 > 0 || pile2 > 0 || pile3 >0) {  
		 if (player == 1) {  
			 printMsg (MSG_1, 1);  
			 player = player + 1;  
		 }  
   
		 else {  
			 printMsg (MSG_1, 2);  
			 player = player - 1;  
		 }  
   
		 printMsg (MSG_2, 1);  
		 flag = scanf("%d", &pile);  
		 if (flag == 0) {	  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  
		 printMsg (MSG_3, 1);  
		 flag = scanf("%d", &A);  
		 if (flag == 0)  
		 {  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  
		   
		   
		 if (pile == 1)  
		 {  
				 if (pile1 != 0)  
				 {  
					 pile1 = pile1 - A;  
					 if (pile1 <= 0)  
						 pile1 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
				  
   
		 else if (pile == 2)  
		 {  
				 if (pile2 != 0)  
				 {  
					 pile2 = pile2 - A;  
					 if (pile1 <= 0)  
						 pile2 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
		   
		 else if (pile == 3)  
		 {  
				 if (pile3 != 0)  
				 {  
					 pile3 = pile3 - A;  
					 if (pile3 <= 0)  
						 pile3 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
   
		 else	
			  printMsg (MSG_7, 1);  
	 }	  
	   
	 else  
	 {  
		 if (player == 1)  
			 player = player + 1;  
		 else  
			 player = player - 1;  
		   
		 printMsg (MSG_1, 1);  
			 exit(1);  
	 }	  
		   
   
   
   return 0;  
 }  
   
 /* Function: printPiles() 
 * This function visually prints out the three piles with asterisks 
 * Inputs: the number of sticks in pile1, pile2, pile3 
 * Output: none 
 */  
 void printPiles (int pile1, int pile2, int pile3) {  
   /* declare variables */  
   
 int counter;	
   
   
   
   /* write your printing code here */  
   
		 counter = pile1;  
		 printf("Pile #1: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;				   
		 }  
   
		 counter = pile2;  
		 printf("\nPile #2: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }  
   
		 counter = pile3;  
		 printf("\nPile #3: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }		  
   
   
 }  
   
 /* Printing messages: 
 * This function prints out a message corresponding to a message code.   
 * Input: the message code, the player ID for the outputs for a particular player. 
 * Output: 0 if no error, 1 if error. 
 */  
 int printMsg (int msgCode, int player)  
 {  
   
   if ( msgCode < 0 || msgCode > 9)  
	 return 1;  
   if ( msgCode == 1 || msgCode == 4 )  
	 if ( player < 1 || player > 2 )  
	   return 1;  
   
   switch (msgCode)  
   {  
	 case 0: printf("Enter pile sizes (separated by spaces): "); break;  
	 case 1: printf("\nPlayer %d's turn:\n", player); break;  
	 case 2: printf("Pick a pile #: "); break;  
	 case 3: printf("Remove how many sticks? "); break;  
	 case 4: printf("Congratulations! Player %d is the winner!\n", player); break;  
	 case 5: printf("Invalid input, end of game!\n"); break;  
	 case 6: printf("Invalid pile sizes. Try again.\n"); break;  
	 case 7: printf("Invalid pile. Try again.\n"); break;  
	 case 8: printf("That pile is empty. Try again.\n"); break;  
	 case 9: printf("You have to take at least one stick!\n"); break;  
	 default: break;  
   }  
   return 0;  
 } 


Was This Post Helpful? 0
  • +
  • -

#12 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: First C program, NIM gane

Posted 12 March 2008 - 09:17 PM

Here is my most recent code. It seems to make it correctly to the line in made in the code but then exits after that. I have been looking at this for far to long and a fresh set of eyes could do it good.

It asks for a pile (1, 2 or 3) then asks how many sticks (*) to remove from the pile. I have if statement s to subtract from the pile chosen. It seems to skip by these statements which must mean that my variable "pile" is not equal to 1,2, or 3.

Please Help

   * This program implements a NIM game with normal 
   * win rule.  The game starts with three piles 
   * of sticks each containing between 1 and 50 sticks 
   * which are represented with a “*”. The number of 
   * sticks in each pile is specified by the players. 
   * The two players take turns removing any positive 
   * number of sticks from a pile that is not empty.   
   * The player that takes the last stick wins.  The 
   * program refreshes the number of sticks in each pile 
   * after every turn and prints out graphically the number 
   * of sticks in each pile.  The program checks for several 
   * different errors throughout. 
   * 
 */  
   
 #include <stdio.h>  
 #include <stdlib.h>  
   
 #define MSG_0 0  
 #define MSG_1 1  
 #define MSG_2 2  
 #define MSG_3 3  
 #define MSG_4 4  
 #define MSG_5 5  
 #define MSG_6 6  
 #define MSG_7 7  
 #define MSG_8 8  
 #define MSG_9 9  
   
 void printPiles (int pile1, int pile2, int pile3);  
 int printMsg (int msgCode, int player);  
   
 /* Main function	*/  
 int main () {  
   
   
	 /* declare variables */  
		 #define EMPTY 0  
		 int pile1;  
		 int pile2;  
		 int pile3;  
		 int A;  
		 int pile;  
		 int player;  
		 int condition;  
		 int flag;  
   
	 /* implement the game */  
		 player = 1;  
		 printMsg (MSG_0, 1);  
		flag = scanf("%d %d %d", &pile1, &pile2, &pile3);  
		   
		   if(flag != 3) 
	   {  
				 printMsg (MSG_5, 1);  
				 exit(1);  
		   }  
		 
	condition = 1;  
		 while(condition == 1)  
	{ 
		 if(pile1 > 50 || pile1 < 1)  
			 printMsg (MSG_6, 1);  
			   
		 if(pile2 > 50 || pile2 < 1)  
			 printMsg (MSG_6, 1);	  
		   
		 if(pile3 > 50 || pile3 < 1)  
			 printMsg (MSG_6, 1);  
   
		 else  
			 condition = 0;  
		   
   	}
		   
   
		 printPiles(pile1, pile2, pile3);  
   
   
	 if (pile1 > 0 || pile2 > 0 || pile3 >0) {  
		 if (player == 1) {  
			 printMsg (MSG_1, 1);  
			 player = player + 1;  
		 }  
   
		 else {  
			 printMsg (MSG_1, 2);  
			 player = player - 1;  
		 }  
   
		 printMsg (MSG_2, 1);  
		 flag = scanf("%d", &pile);  
		 if (flag == 0) {	  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  
		 printMsg (MSG_3, 1);  
		 flag = scanf("%d", &A);  
		 if (flag == 0)  
		 {  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  
		   
		   
		 if (pile == 1)  
		 {  
				 if (pile1 != 0)  
				 {  
					 pile1 = pile1 - A;  
					 if (pile1 <= 0)  
						 pile1 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
				  
   
		 else if (pile == 2)  
		 {  
				 if (pile2 != 0)  
				 {  
					 pile2 = pile2 - A;  
					 if (pile1 <= 0)  
						 pile2 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
		   
		 else if (pile == 3)  
		 {  
				 if (pile3 != 0)  
				 {  
					 pile3 = pile3 - A;  
					 if (pile3 <= 0)  
						 pile3 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
   
		 else	
			  printMsg (MSG_7, 1);  
	 }	  
	   
	 else  
	 {  
		 if (player == 1)  
			 player = player + 1;  
		 else  
			 player = player - 1;  
		   
		 printMsg (MSG_1, 1);  
			 exit(1);  
	 }	  
		   
   
   
   return 0;  
 }  
   
 /* Function: printPiles() 
 * This function visually prints out the three piles with asterisks 
 * Inputs: the number of sticks in pile1, pile2, pile3 
 * Output: none 
 */  
 void printPiles (int pile1, int pile2, int pile3) {  
   /* declare variables */  
   
 int counter;	
   
   
   
   /* write your printing code here */  
   
		 counter = pile1;  
		 printf("Pile #1: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;				   
		 }  
   
		 counter = pile2;  
		 printf("\nPile #2: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }  
   
		 counter = pile3;  
		 printf("\nPile #3: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }		  
   
   
 }  
   
 /* Printing messages: 
 * This function prints out a message corresponding to a message code.   
 * Input: the message code, the player ID for the outputs for a particular player. 
 * Output: 0 if no error, 1 if error. 
 */  
 int printMsg (int msgCode, int player)  
 {  
   
   if ( msgCode < 0 || msgCode > 9)  
	 return 1;  
   if ( msgCode == 1 || msgCode == 4 )  
	 if ( player < 1 || player > 2 )  
	   return 1;  
   
   switch (msgCode)  
   {  
	 case 0: printf("Enter pile sizes (separated by spaces): "); break;  
	 case 1: printf("\nPlayer %d's turn:\n", player); break;  
	 case 2: printf("Pick a pile #: "); break;  
	 case 3: printf("Remove how many sticks? "); break;  
	 case 4: printf("Congratulations! Player %d is the winner!\n", player); break;  
	 case 5: printf("Invalid input, end of game!\n"); break;  
	 case 6: printf("Invalid pile sizes. Try again.\n"); break;  
	 case 7: printf("Invalid pile. Try again.\n"); break;  
	 case 8: printf("That pile is empty. Try again.\n"); break;  
	 case 9: printf("You have to take at least one stick!\n"); break;  
	 default: break;  
   }  
   return 0;  
 } 



Here is my most recent code. It seems to make it correctly to the line in made in the code but then exits after that. I have been looking at this for far to long and a fresh set of eyes could do it good.

It asks for a pile (1, 2 or 3) then asks how many sticks (*) to remove from the pile. I have if statement s to subtract from the pile chosen. It seems to skip by these statements which must mean that my variable "pile" is not equal to 1,2, or 3.

Please Help

	* This program implements a NIM game with normal 
   * win rule.  The game starts with three piles 
   * of sticks each containing between 1 and 50 sticks 
   * which are represented with a “*”. The number of 
   * sticks in each pile is specified by the players. 
   * The two players take turns removing any positive 
   * number of sticks from a pile that is not empty.   
   * The player that takes the last stick wins.  The 
   * program refreshes the number of sticks in each pile 
   * after every turn and prints out graphically the number 
   * of sticks in each pile.  The program checks for several 
   * different errors throughout. 
   * 
 */  
   
 #include <stdio.h>  
 #include <stdlib.h>  
   
 #define MSG_0 0  
 #define MSG_1 1  
 #define MSG_2 2  
 #define MSG_3 3  
 #define MSG_4 4  
 #define MSG_5 5  
 #define MSG_6 6  
 #define MSG_7 7  
 #define MSG_8 8  
 #define MSG_9 9  
   
 void printPiles (int pile1, int pile2, int pile3);  
 int printMsg (int msgCode, int player);  
   
 /* Main function	*/  
 int main () {  
   
   
	 /* declare variables */  
		 #define EMPTY 0  
		 int pile1;  
		 int pile2;  
		 int pile3;  
		 int A;  
		 int pile;  
		 int player;  
		 int condition;  
		 int flag;  
   
	 /* implement the game */  
		 player = 1;  
		 printMsg (MSG_0, 1);  
		flag = scanf("%d %d %d", &pile1, &pile2, &pile3);  
		   
		   if(flag != 3) 
	   {  
				 printMsg (MSG_5, 1);  
				 exit(1);  
		   }  
		 
	condition = 1;  
		 while(condition == 1)  
	{ 
		 if(pile1 > 50 || pile1 < 1)  
			 printMsg (MSG_6, 1);  
			   
		 if(pile2 > 50 || pile2 < 1)  
			 printMsg (MSG_6, 1);	  
		   
		 if(pile3 > 50 || pile3 < 1)  
			 printMsg (MSG_6, 1);  
   
		 else  
			 condition = 0;  
		   
   	}
		   
   
		 printPiles(pile1, pile2, pile3);  
   
   
	 if (pile1 > 0 || pile2 > 0 || pile3 >0) {  
		 if (player == 1)
	 {  
			 printMsg (MSG_1, player);  
			 player = player + 1;  
		 }  
   
		 else 
	{  
			 printMsg (MSG_1, 2);  
			 player = player - 1;  
		 }  
   
		 printMsg (MSG_2, 1);  
		 flag = scanf("%d", &pile);  
		
	 if (flag == 0) 
	{	  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  

		 else printMsg (MSG_3, 1);  
		 
	flag = scanf("%d", &A);  

		 if (flag == 0)  
		 {  
			 printMsg (MSG_5, 1);  
			 exit(1);  
		 }  
		   
 /**********************************************************************************************************/		  
		 if (pile == 1)  
		 {  
				 if (pile1 != 0)  
				 {  
					 pile1 = pile1 - A;  
					 if (pile1 <= 0)  
						 pile1 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
				  
   
		 else if (pile == 2)  
		 {  
				 if (pile2 != 0)  
				 {  
					 pile2 = pile2 - A;  
					 if (pile1 <= 0)  
						 pile2 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
		   
		 else if (pile == 3)  
		 {  
				 if (pile3 != 0)  
				 {  
					 pile3 = pile3 - A;  
					 if (pile3 <= 0)  
						 pile3 = 0;  
				 }  
				   
				 else  
					 printMsg (MSG_8, 1);  
   
		 }  
   
		 else	
			  printMsg (MSG_7, 1);  
	 }	  
	   
	 else  
	 {  
		 if (player == 1)  
			 player = player + 1;  
		 else  
			 player = player - 1;  
		   
		 printMsg (MSG_1, 1);  
			 exit(1);  
	 }	  
		   
   
   
   return 0;  
 }  
   
 /* Function: printPiles() 
 * This function visually prints out the three piles with asterisks 
 * Inputs: the number of sticks in pile1, pile2, pile3 
 * Output: none 
 */  
 void printPiles (int pile1, int pile2, int pile3) {  
   /* declare variables */  
   
 int counter;	
   
   
   
   /* write your printing code here */  
   
		 counter = pile1;  
		 printf("Pile #1: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;				   
		 }  
   
		 counter = pile2;  
		 printf("\nPile #2: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }  
   
		 counter = pile3;  
		 printf("\nPile #3: ");  
		 while (counter > 0)  
		 {  
			 printf("*");  
			 counter = counter - 1;  
		 }		  
   
   
 }  
   
 /* Printing messages: 
 * This function prints out a message corresponding to a message code.   
 * Input: the message code, the player ID for the outputs for a particular player. 
 * Output: 0 if no error, 1 if error. 
 */  
 int printMsg (int msgCode, int player)  
 {  
   
   if ( msgCode < 0 || msgCode > 9)  
	 return 1;  
   if ( msgCode == 1 || msgCode == 4 )  
	 if ( player < 1 || player > 2 )  
	   return 1;  
   
   switch (msgCode)  
   {  
	 case 0: printf("Enter pile sizes (separated by spaces): "); break;  
	 case 1: printf("\nPlayer %d's turn:\n", player); break;  
	 case 2: printf("Pick a pile #: "); break;  
	 case 3: printf("Remove how many sticks? "); break;  
	 case 4: printf("Congratulations! Player %d is the winner!\n", player); break;  
	 case 5: printf("Invalid input, end of game!\n"); break;  
	 case 6: printf("Invalid pile sizes. Try again.\n"); break;  
	 case 7: printf("Invalid pile. Try again.\n"); break;  
	 case 8: printf("That pile is empty. Try again.\n"); break;  
	 case 9: printf("You have to take at least one stick!\n"); break;  
	 default: break;  
   }  
   return 0;  
 } 



Was This Post Helpful? 0
  • +
  • -

#13 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: First C program, NIM gane

Posted 12 March 2008 - 09:20 PM

There don't seem to be any loops in here that would make it go back to the beginning. So, it asks for the number of sticks, changes the value of the pile you suggested, and then exits. There are no print statements after your pile math, so it looks like it is skipping the statements.

You'll need to add a loop with an endgame condition to make it go back to the beginning of the program.

Good Luck!

Per
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1