This program is a NIM game. It seems to work perfectly until the line of ***'s I added to show where the mistake seems to be. I need help finding my mistake. I think it might be with my integer "pile'". I am not sure if I used scanf incorrectly or what else the error could possibly be. 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);
flag = scanf("%d %d %d", &pile1, &pile2, &pile3);
if(flag != 3)
{
printMsg (MSG_5, 1);
exit(1);
}
condition = 1;
if(condition == 1)
{
if(pile1 > 50 || pile1 < 1)
printMsg (MSG_6, 1);
else if(pile2 > 50 || pile2 < 1)
printMsg (MSG_6, 1);
else if(pile3 > 50 || pile3 < 1)
printMsg (MSG_6, 1);
else
condition = 0;
}
else
printMsg (MSG_7, 1);
if (pile1 > 0 || pile2 > 0 || pile3 >0)
{
printPiles(pile1, pile2, pile3);
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
{
if (player == 1)
player = player + 1;
else
player = player - 1;
printMsg (MSG_1, 1);
exit(1);
}
}
else
{
if (player == 1)
player = player + 1;
else
player = player - 1;
printMsg (MSG_4, player);
}
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;
}