I want a counter to add each time matrix[yVal][xVal] == 5 - c

I have the following code:
do{
int health = 3;
int xVal;
int yVal;
printf("Enter an x value from 0 to 9\n");
scanf("%d", &xVal);
printf("You entered '%d'\n",xVal);
printf("Enter a y value from 0 to 9\n");
scanf("%d", &yVal );
printf("You entered '%d'\n",yVal);
printf("Value at '%d', '%d' is '%d'\n", yVal , xVal, matrix[yVal][xVal]);
if ( matrix[yVal][xVal] == 5 )
{
health = health - 1;
printf("Ouch... You have found a 5 and lost a point\n If you lose %d more, you will lose.\n", health);
}
if (health == 0)
{
isGameOver = true;
printf("You have lost.\n");
}
if ( matrix[yVal][xVal] == 9 )
{
isGameOver = true;
printf("You have won.\n");
}
} while(isGameOver != true);
The problem comes each time I find a five in the array, it only subtracts from the health counter once.
If I find 2 fives, it will stay as a 2 rather than counting down to one, the whole point of my game is that if you find 3 "5"s, you lose.

The problem is you are declaring health = 3 inside the loop, so each time it loops it resets it to 3. Move it to before the loop (ie before the "do" statement)

Related

all functions being skipped and code is running without them

I have fixed my code style and have updated what it looks like, but it's skipping all the rounds and inputs I want to make
If I formatted the returns wrong(Which is what I'm thinking), or I didn't nest my functions properly, those are the only issues I can imagine would be wrong
#define ROUNDS 3
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
//Greets the user at the start of the round
void greeting();{
printf("Welcome to the Toothpick Game!\n");
printf("Here are the rules.\n");
printf("There are currently 31 toothpicks on the table.\n");
printf("You and I will each get a turn to pick either 1, 2, or 3 toothpick off the table.\n");
printf("The player that gets to puck the last toothpicks looses the game!\n");
printf("Sounds easy right? Well lets see if you can beat me!\n");
printf("Ready to play?... Here we go!\n");
} //display welcome message to user
for(int x = 0; x < ROUNDS; ++x)
{
int result = playRound(x + 1); //call playRound and assign result the value function returns
void winnerAnnouncment(int user);{//overall winner of round announcement
}
}
printf("********************************************************\n");
printf("Thank you for playing!\n");
return 0;
}
int playRound(int round)
{
int toothpicks = 31; //number of toothpicks to start with
int winner, taken, choice, leftover;
printf("Welcome to a new round %d!\n", round);
printf("You may go first!\n");
int leftOnTable(int toothpicks, int taken);{ //calculate number of toothpicks left
toothpicks = toothpicks - taken;
while(toothpicks != 0)//loop to control playing of the game
{
int humanPick();{ //retrieve the user's guess
int userchoice;
printf("How many toothpicks do you want to take? ");
scanf("%d", &taken);
printf("Okay... You took %d off the table", taken);
if (toothpicks = 1)
winner = 0;
int computerPick(int choice, int leftover);{ //computer makes its pick
if (toothpicks > 4){//Caculates what the computer will take based off of the users choice
choice = 4 - userchoice;
toothpicks = toothpicks - choice;
leftover = choice;
printf("I am taking %d toothpicks off the table.", choice);
}
if(toothpicks = 2 || 3 || 4)//calculates how many toothpicks the computer will take to leave one left on the table
if (toothpicks == 2){
choice = toothpicks - 1;
leftover = 1;
printf("I am taking %d toothpicks off the table.", choice);
}
if (toothpicks == 3){
choice = toothpicks - 1;
printf("I am taking %d toothpicks off the table.", choice);
}
if (toothpicks == 4){
choice = toothpicks - 1;
leftover = 1;
printf("I am taking %d toothpicks off the table.", choice);
}
if (toothpicks = 1){
choice = 1;
leftover = 0;
toothpicks = leftover;
winner = 1;
printf("I will take the last toothpick.");
}
}
}
return toothpicks;
}
}
return round;
}
Multiple times throughout the code used is an assignment operator = instead of the == likely intended.
In addition, the following:
userchoice is never initialized to a value.
Nested functions aren't supported by the C standard.
Numerous semicolons are found before the function definitions (e.g. void greeting();{...}).
This declares, but doesn't define the function. The following is code that executes in a new code block (new scope).
Incorrect || operator usage in conditionals (Broken:toothpicks == 2 || 3 || 4 Fixed: toothpicks == 2 || toothpicks == 3 || toothpicks == 4).
This condition would always be true. The conditional toothpicks == 2 is tested, if this proves untrue the conditional 3 is tested. 3 is true, and so this condition is always true.
The functions leftOnTable and greeting are never called.
Additional unused variables include: result and winner
Other logical errors may exist.
This should answer, if only partially, why the program isn't working as expected. Perhaps with more information about why nested functions are being used and the intended functionality of the program, I could provide a working code fix to every present issue.

C program - Faulty scores output

C program. IDE used is Xcode v12.2.
The problem I am facing:
Faulty scores output. For each round, the program should output the highest and lowest scores and the average score if the player chooses to do so.
Scores output after playing for 1 round:
Enter 'S' to show results
Enter 'P' to play another round
Enter 'R' to return to the main menu
S
Round 0 score: 92/100
Highest score: 92/100
Lowest score: 92/100
Average score:inf
****** Player: MAX ******
Scores output after playing for 2 rounds:
Enter 'S' to show results
Enter 'P' to play another round
Enter 'R' to return to the main menu
S
Round 0 score: 95/100
Highest score: 95/100
Lowest score: 92/100
Average score:inf
****** Player: MAX ******
Questions:
Why is 'Round 1' shown as 'Round 0'? And what does the 'inf' mean in the average score section? How do I turn 'inf' into a numerical output? After 2 rounds, the output 'Round 2' is still shown as 'Round 0' and the 'Average score' did not change to a numerical output.
What I have tried:
void quiz(char name[])
{
// function created to enclose quiz functionality apart from instructions
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
float avg = 0;
int i, j, g = 0;
//char x;
struct struc test[MAX_TESTS];
srand((unsigned) time(NULL));
for (;;)
{
rounds++;
for (i = 0; i < MAX_TESTS; i++) // generate all questions
{
ctm_i(&test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
//if question is already present
ctm_i(&test[i]); //then re-generate
}
//int ig = getchar();
char x;
x = getchar();
printf("\n Are you ready? Press Enter key to continue. ");
fflush(stdin);
while (x != '\n') {}
while (getchar() != '\n') {}
//getchar();
for (i = 1; i <= 5; i++)
{
printf( " *******************************************************************"
"**"
"***********\n");
printf( " ..................................................................."
".."
"...........\n");
}
// Take quiz
for (i = 0; i < MAX_TESTS; i++)
tcm_i(&test[i], i);
printf(" End\n\n");
bool done = false;
bool unsure = true;
bool showS = true;
while (unsure)
{
unsure = false;
puts("\n");
if (showS)
{
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
//puts(" Enter 'Q' to quit");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
myread("%c", &choice);
if (choice == 'r' || choice == 'R')
{
done = true;
}
else if (choice == 'S' || choice == 's')
{
showS = false;
// calculate total score for current round
g = 0;
for (i = 0; i < MAX_TESTS; i++)
{
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
avg = allScore / rounds; //average of all rounds
if (g > highest)
{
highest = g;
}
if (g < lowest)
{
lowest = g;
}
if (rounds == 1)
{
printf(" Final score: %d/100\n", g); //display round score
printf(" ******Player: %s ******\n", name);
}
else
{
//puts("Whoops! Looks like highest/lowest have not been adjusted!");
printf(" Round %d score: %d/100\n", rounds, g); //display round score
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score: %d/100\n", lowest);
printf(" Average score: %f\n", avg);
printf(" ******Player: %s ******\n", name);
}
unsure = true;
//getchar();
}
else if (choice == 'P' || choice == 'p')
{
g = 0;
for (i = 0; i < MAX_TESTS; i++)
{
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
if (g > highest)
{
highest = g;
}
if (g < lowest)
{
lowest = g;
}
}
else
{
puts(" Invalid input!");
unsure = true;
}
}
if (done)
break;
}
}
Why is 'Round 1' shown as 'Round 0'?
The value of variable rounds is printed as the round number. If the round number always prints as 0 then it follows that rounds always has value zero at the point where that printf() call is made. And indeed, a look at all the appearances of that variable in your code shows that it is initialized to zero in its declaration, and then never modified.
And what does the 'inf' mean in
the average score section?
It means "infinity", and before even looking at your code, I would take it as a sign that you have somewhere used the result of dividing a non-zero floating-point number by zero.
In particular, the problem seems to be the same one as the previous: you use rounds as the divisor in your average score computation, and it is always zero.
How do I turn 'inf' into a numerical
output?
Change the faulty computation into a correct one. Properly maintaining the value of the rounds variable may be enough.
After 2 rounds, the output 'Round 2' is still shown as 'Round
0' and the 'Average score' did not change to a numerical output.
That's consistent with my observations above.

Logical Operators in C causing issue with my loop?

I am making a game for an assignment where there are 2 players and each turn the player rolls 2 die. When I ask the player if they want to roll again everything goes south from there. I'm confused because even if I change my rollAgain character to 'n' it still plays my loop where I want it to run only if rollAgain is 'y' or 'Y'. This is my first program in C, I have only programmed in Java previously. I feel like I must be checking the logical operators incorrectly in my while loop but idk what I'm doing wrong, I've looked over all the code several times now.
Here is my code:
#include <stdio.h>
#include <stdlib.h> // NULL constant, srand() & rand() functions
#include <time.h> // access your computer’s clock time
// function prototype statements
int rollDice();
int calcPoints(int die1, int die2);
char checkOne(int die1, int die2);
int main()
{
int p1Score = 0;
int p2Score = 0;
int turnTotal = 0;
char rollAgain = 'Y';
char rolled1 = 'N';
int die1;
int die2;
printf("Welcome to the Pig Game. Each turn the player rolls two dice repeatedly until a single 1 is rolled or the player holds.\n\n");
printf("If a single 1 is rolled all points are lost. If the player holds, all earned points are kept.\n");
printf("If double 1's are rolled 25 points are earned, if any other doubles are rolled the value is doubled.\n");
printf("Player 1 has an advantage because they get to go first, the youngest player gets to be Player 1.\n\n");
printf("First player to 100 points wins, let the games begin!!!\n\n");
do
{
//----------------------P1's turn starts----------------------------------------
while ((rollAgain == 'Y' || rollAgain == 'y') && rolled1 == 'N')
{
die1 = rollDice(); //roll die 1
die2 = rollDice(); //roll die 2
printf("Player 1 rolls %d, and %d\n", die1, die2);
rolled1 = checkOne(die1, die2); //check if turn ending 1 is rolled if they did the below while loop will not run and the current while loop will end.
if(rolled1 == 'N') //if a single 1 wasn't rolled calculate total for the turn
{
turnTotal += calcPoints(die1, die2);
printf("Player 1 your turn total so far is %d. Would you like to roll again?\n", turnTotal);
fflush(stdin);
scanf_s("%c", &rollAgain);
}
}
//-------------------------P1's turn is now over---------------------------------
if (rolled1 == 'Y') //turnTotal isn't added to p1Score
{
printf("Sorry Player 1 your turn is over because you rolled a 1 on a single die :(\n");
printf("Your total score is now: %d.\n", p1Score);
}
else //player1 must have held
{
printf("Congratulations on scoring %d point this turn Player 1!\n", turnTotal);
p1Score += turnTotal;
printf("Your total score is now: %d.\n", p1Score);
}
//reset variables to start p2s turn
turnTotal = 0;
rolled1 = 'N';
rollAgain = 'Y';
//----------------------P2's turn starts----------------------------------------
while ((rollAgain == 'Y' || rollAgain == 'y') && rolled1 == 'N' && p1Score < 100) //player2's turn should not start if player 1 has already won the game so we must check p1's score as well
{
int die1 = rollDice(); //roll die 1
int die2 = rollDice(); //roll die 2
printf("Player 2 rolls %d, and %d\n", die1, die2);
rolled1 = checkOne(die1, die2); //check if turn ending 1 is rolled if it was the below while loop will not run and the current while loop will end.
if(rolled1 == 'N') //if a single 1 wasn't rolled calculate total for the turn
{
turnTotal += calcPoints(die1, die2);
printf("Player 2 your turn total so far is %d. Would you like to roll again?\n", turnTotal);
fflush(stdin);
scanf_s("%c", &rollAgain);
}
}
//-------------------------P2's turn is now over---------------------------------
if (rolled1 == 'Y' && p1Score < 100) //turnTotal isn't added to p1Score
{
printf("Sorry Player 2 your turn is over because you rolled a 1 on a single die :(\n");
printf("Your total score is now: %d.\n", p2Score);
}
if (rolled1 == 'N' && p1Score < 100) //player2 must have held, use an if statement rather than else because we don't want this code to play if p1 has already won
{
printf("Congratulations on scoring %d point this turn Player 2!\n", turnTotal);
p2Score += turnTotal;
printf("Your total score is now: %d.\n", p2Score);
}
//reset variables for p1's turn
turnTotal = 0;
rolled1 = 'N';
rollAgain = 'Y';
//if either player's score is > 100 the game is now over so the game ending code will now play
} while (p1Score < 100 && p2Score < 100);
if (p1Score >= 100) //player 1 has won, congratulate them
printf("Congratulations Player 1, you have won the game! Pat yourself on the back!\n");
else //player 1 didn't win so player 2 must have won since there are no draws
printf("Congratulations Player 2, you have won the game! You are awesome!\n");
return 0; //end main
}
//function rollDice rolles the dice for the player using a random number between 1-6.
int rollDice()
{
// seed the random number generator using the computers clock
srand(time(0));
//generate a random number between 1-6
int num = (rand() % 6) + 1;
return num;
}
//function calcPoints that calculates how many points the player earned.
int calcPoints(int die1, int die2)
{
int points;
if (die1 == die2) //check if doubles were rolled
{
//snake eyes = 25 points
if (die1 == 1)
points = 25;
//regular doubles = the value on the dice *2
else
points = (die1 + die2) * 2;
}
else //doubled weren't rolled
points = die1 + die2;
return points;
}
//function checkOne that checks if the player rolled a single 1 during their turn.
char checkOne(int die1, int die2)
{
if (die1 == 1 && die2 != 1)
return 'Y';
else if (die1 != 1 && die2 == 1)
return 'Y';
else
return 'N';
}
The players turn is supposed to end if a single 1 is rolled.
I would really appreciate any help I'm not sure where I am going wrong. The code just has a mind of its own after it asks if I want to roll again.
Player 1 rolls 2, and 2
Player 1 your turn total so far is 8. Would you like to roll again?
y
Player 1 rolls 3, and 3
Player 1 your turn total so far is 20. Would you like to roll again?
Congratulations on scoring 20 point this turn Player 1!
Your total score is now: 20.
Player 2 rolls 3, and 3
Player 2 your turn total so far is 12. Would you like to roll again?
this is the output I get if I say yes to rolling again. At the end of this output it allows me to input if I want to roll again.
Player 1 rolls 3, and 3
Player 1 your turn total so far is 12. Would you like to roll again?
n
Congratulations on scoring 12 point this turn Player 1!
Your total score is now: 12.
Player 2 rolls 1, and 1
Player 2 your turn total so far is 25. Would you like to roll again?
Congratulations on scoring 25 point this turn Player 2!
Your total score is now: 25.
Player 1 rolls 1, and 1
Player 1 your turn total so far is 25. Would you like to roll again?
this is the output I get if I say no to rolling again. At the end of this output it allows me to input if I want to roll again.
There are multiple classic issues in your code:
fflush(stdin); has undefined behavior. Just remove this statement, it does not consume the pending input in stdin.
scanf_s("%c", &rollAgain); will read the pending newline in the input stream. To skip it, you can just add a space in the format string and you must test if scanf_s succeeded to avoid undefined behavior at end of file:
if (scanf_s(" %c", &rollAgain) != 1) {
exit(1); // unexpected end of file or some other error
}
You should not reinitialize the random number generator every time you roll a die, just once at the beginning of the program (move the srand() function call to the beginning of the main function, and pass it clock() that varies much more rapidly than time(NULL)):
//function rollDice rolls the die for the player using a random number between 1-6.
int rollDice() {
//generate a random number between 1-6
return (rand() % 6) + 1;
}

Factoring Pointer Error in C programming

So i'm having trouble making a program that asks the user to enter a number and then using that number I must increase the value of the pointer towards two_count and three_count. These are counter the factors of two's and three's in the number entered.
For example if the user input 2, then the program should spit out
"There have been 1 factor of 2 and 0 factors of 3"
Then the user can input 0 to exit program
What I have so far is
include <stdio.h>
void main()
{
int* two_count;
int* three_count;
int num;
while(two_count >= 0 || three_count >= 0)
{
printf("Enter a number: \n");
scanf("%d", &num);
if(num % 2)
{
two_count++;
}
else if(num % 3)
{
three_count++;
}
else if(num == 0)
{
printf("Thank you for playing, enjoy your day!\n");
break;
}
printf("So far, there have been %d factors of 2 and %d factors of 3\n", two_count, three_count);
}
}
Thanks!
If you want to use pointers, you can do it like this
int two_count = 0;
int* two_count_ptr = &two_count;
int three_count = 0;
int* three_count_ptr = &three_count;
Then, for retrieving value and incrementing you would need to dereference the pointer
while(*two_count_ptr >= 0 || *three_count_ptr >= 0)
(*two_count_ptr)++;
Hope this helps.

C: Creating a game, but I'm getting an off by one error and an infinite loop error in certain cases

I'm in an intro to C class, and my professor has assigned us to code a game called "tea party" for our current assignment. I've finished coding the game, and it works for the most part, but there are some certain kinks that I can't seem to work out.
The rules of the game are simple: two players take turns spinning the spinner (emulated by entering "0" and pressing enter), and collecting all 7 items for the tea party. The first player to get all 7 items wins. The only catch is that you cannot collect a sandwich, fruit, or dessert unless you have a plate first. If you land on the LOSE A PIECE square, you must give up one of your pieces. Both of the errors come from the lose a piece instance in the game, so I'm thinking the error must originate from the "get_lost_piece" function.
One of them is that the pieces from the "player" array are numbered oddly in that they are 1 value higher than they should be. The other error is that when a player tries to remove their plate while they have an item that requires the plate, it should print out "Sorry, it is bad manners to eat without a plate. Enter another choice:", but instead, I get an infinite loop that reads "You lost item 1."
Here is the code I have:
#include <stdio.h>
#include <time.h>
#define SLOW_MODE 1
#define NUMPLAYERS 2
#define NUMPIECES 7
#define MAXLEN 20
#define NO_WINNER -1
const char CHOICES[NUMPIECES+1][MAXLEN] = {"PLATE", "NAPKIN", "TEA CUP", "CREAM AND SUGAR", "SANDWICH", "FRUIT", "DESSERT", "LOSE A PIECE"};
void update_player(int player[], int square);
int get_lost_piece(int player[]);
int search(int piece_list[], int choice);
int get_spin();
void init_player(int player[]);
int get_winner(int players[][NUMPIECES]);
int get_next_player(int player_num);
int count_pieces(int player[]);
void print_player(int player[], int player_num);
int main() {
srand(time(0));
int players[NUMPLAYERS][NUMPIECES];
// Initialize each player in the game.
int i;
for (i=0; i<NUMPLAYERS; i++)
init_player(players[i]);
int player_number = 0;
// Play until we get a winner.
int status = get_winner(players);
while (status == NO_WINNER) {
int dummy;
// In slow mode, we stop before every spin.
if (SLOW_MODE) {
printf("Player %d, it is your turn. Type 0 and enter to spin.\n", player_number+1);
scanf("%d", &dummy);
}
// Get the current player's spin and print out her pieces.
int square = get_spin();
printf("Player %d, have landed on the square %s.\n", player_number+1, CHOICES[square]);
update_player(players[player_number], square);
print_player(players[player_number], player_number+1);
// Update the game status.
player_number = get_next_player(player_number);
status = get_winner(players);
printf("\n\n");
}
printf("Congrats player %d, you win!\n", status+1);
return 0;
}
// Pre-conditions: player stores the contents of one player and square is in between 0 and 7, inclusive.
// Post-conditions: The turn for player will be executed with the given square selected.
void update_player(int player[], int square) {
if (square == 7) {
if (count_pieces(player) == 0)
{
printf("There is no piece for you to lose. Lucky you, sort of.\n");
return;
}
else{
int q;
q = get_lost_piece(player);
player[q]= 0;
}
return;
}
player[square]=search(player, square);
// Restricted by having no plate!
if (player[0] == 0) {
if(square == 4 || square == 5 ||square == 6){
printf("Sorry, you can't obtain that item because you don't have a plate yet.\n");}
else{
if (player[square] == 0){
player[square]++;
}
}
}
// Process a regular case, where the player already has a plate.
else {
if (player[square] == 0){
player[square]++;
}
}
}
// Pre-conditions: player stores the contents of one player that has at least one piece.
// Post-conditions: Executes asking a player which item they want to lose, and reprompts them
// until they give a valid answer.
int get_lost_piece(int player[]) {
int choice = -1;
// Loop until a valid piece choice is made.
while (1) {
if (choice == -1){
printf("Which piece would you like to lose?\n");
print_player(player,choice);
scanf("%d", &choice);}
if (player[choice] == 0 && choice < 7 && choice >= 0){
printf("Sorry, that was not one of the choices");
scanf("%d", &choice);
}
if (player[0] == 1 && choice == 4 || choice == 5 ||choice == 6){
printf("Sorry, it is bad manners to eat without a plate. Enter another choice:\n");
scanf("%d", &choice);
}
else{
printf("You lost piece %d\n", choice);
}
}
return choice;
}
// Pre-conditions: piece_list stores the contents of one player
// Post-conditions: Returns 1 if choice is in between 0 and 6, inclusive and corresponds to
// an item in the piece_list. Returns 0 if choice is not valid or if the
// piece_list doesn't contain it.
int search(int piece_list[], int choice) {
int i;
for (i=0; i<NUMPIECES; i++){
if(piece_list[i]==choice){
return 1;}
else {return 0;}
}
}
// Pre-condition: None
// Post-condition: Returns a random value in between 0 and 7, inclusive.
int get_spin() {
return rand() % 8;
}
// Pre-condition: None
// Post-condition: Initializes a player to have no pieces.
void init_player(int player[]) {
int j;
for (j=0; j< NUMPIECES; j++)
player[j]=0;
}
// Pre-condition: players stores the current states of each player in the tea party game.
// Post-condition: If a player has won the game, their 0-based player number is returned.
// In the case of no winners, -1 is returned.
int get_winner(int players[][NUMPIECES]) {
int i =0;
for (i=0; i<NUMPLAYERS; i++){
if(count_pieces(players[i]) == NUMPIECES) {
return i;}
}
return -1;
}
// Pre-condition: 0 <= player_num < NUMPLAYERS
// Post-condition: Returns the number of the next player, in numerical order, with
// a wrap-around to the beginning after the last player's turn.
int get_next_player(int player_num) {
player_num++;
if (player_num == NUMPLAYERS){
player_num = 0;
}
return player_num;
}
// Pre-conditions: player stores the contents of one player
// Post-conditions: Returns the number of pieces that player has.
int count_pieces(int player[]) {
int y, counter;
counter = 0;
for ( y = 0; y < 7; y++){
if(player[y] == 1){
counter++;
}
}
return counter;
}
// Pre-conditions: player stores the contents of one player and player_num is that
// player's 1-based player number.
// Post-conditions: Prints out each item the player has, numbered with the numerical
// "codes" for each item.
void print_player(int player[], int player_num) {
int i;
printf("\n");
printf("Player %d\n", player_num);
for (i=0; i < 7; i++){
if (player[i] == 1){
printf("%d. %s\n", i + 1, CHOICES[i]);
}
}
}
Thanks for the help in advance. I get the feeling that the solution is staring me right in the face, but after spending a couple of days on this, I'm having difficulty spotting the problem.
need #include <stdlib.h> for rand(), srand()
add player_number parameter to get_lost_piece() and pass it to print_player.
The following is just one idea for refactoring. It could be done many different ways.
Get input once at the start of the loop.
Use continue keyword in each if statement to redo the loop.
It works fine when I change the logic of get_lost_piece() to:
while...
get choice
if choice < 1 || choice > 7 "1-7 please..." continue
if player[choice - 1] == 0 "sorry..." continue
if choice == 1 (and player[] contains foods) "bad manners..." continue
return choice - 1;
Helpful hints
Loop should be limited and needs to give player a quit option.
Check out FAQ entry: scanf() returns errors, leaves stuff on input stream.
Test early, test often.
Turn up compiler warnings (as suggested by catcall)

Resources