Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,227 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,052 people online right now. Registration is fast and FREE... Join Now!




First C program, NIM gane

2 Pages V  1 2 >  
Reply to this topicStart new topic

First C program, NIM gane

bpatzin2
post 12 Mar, 2008 - 04:24 PM
Post #1


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


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.


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);
        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;
}
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 12 Mar, 2008 - 04:34 PM
Post #2


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


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 Mar, 2008 - 04:35 PM
User is offlineProfile CardPM

Go to the top of the page

bpatzin2
post 12 Mar, 2008 - 04:39 PM
Post #3


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 12 Mar, 2008 - 04:49 PM
Post #4


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


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.

cpp

/* 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 smile.gif
User is offlineProfile CardPM

Go to the top of the page

bpatzin2
post 12 Mar, 2008 - 05:01 PM
Post #5


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


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)
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 12 Mar, 2008 - 05:12 PM
Post #6


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


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

if(player == 1)
User is offlineProfile CardPM

Go to the top of the page

bpatzin2
post 12 Mar, 2008 - 05:14 PM
Post #7


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


Thanks, you're a lifesaver.
User is offlineProfile CardPM

Go to the top of the page

bpatzin2
post 12 Mar, 2008 - 06:24 PM
Post #8


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


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.

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);  
         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;  
}
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 12 Mar, 2008 - 06:32 PM
Post #9


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


Please don't post duplicate topics.

*topics merged*
User is offlineProfile CardPM

Go to the top of the page

bpatzin2
post 12 Mar, 2008 - 06:54 PM
Post #10


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

bpatzin2
post 12 Mar, 2008 - 07:24 PM
Post #11


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


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:

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);  
     &