Home > front end >  (C) toothpick game has me stuck in a LOT of places
(C) toothpick game has me stuck in a LOT of places

Time:10-07

I've tried so many different things but cant organize the structure properly to get the game actually working I have the shell and functions layered out, but cant properly implement my defined functions into the sections where they are needed.

   #define ROUNDS 3
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void greeting();//display welcome message to user
int playRound(int round); //play one round
int humanPick(); //retrieve the user's guess
int computerPick(int choice, int leftover); //computer makes its pick
int leftOnTable(int toothpicks, int taken); //calculate number of toothpicks left
void winnerAnnouncment(int user); //overall winner of round announcement

int main()
{
    void greeting();{
        printf("Welcome to the Toothpick Game!\n");
        printf("Here are the rules.\n");
        printf("There are currently 31 toothpics 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");
    }
    
    
    for(int x = 0; x < ROUNDS;   x)
    {
        int result = playRound(x   1); //call playRound and assign result the value function returns
        
        void winnerAnnouncement(int user){

            if (user == )
        } 
    
    }
    
    printf("********************************************************\n");
    printf("Thank you for playing!\n");
    return 0;
}

int playRound(int round)
{
    printf("Welcome to a new round %d!\n", round);
    printf("You may go first!\n");
    
    int toothpicks = 31; //number of toothpicks to start with
    //int taken;
    
    
    int leftOnTable(int toothpicks, int taken);{
        int taken;
        while(toothpicks > 0){
            toothpicks = toothpicks - taken;
            return toothpicks;
        }
           
            
    }
    //loop that keeps track of toothpicks until respective no more toothpicks left.
    
    while(toothpicks != 0)
    {
        printf("There are currently %d toothpicks left.\n", toothpicks);
        printf("How many toothpicks are you going to take off the table?");
        printf("Pick a number between 1 , 2 , and 3.\n");
        scanf("%d", &userChoice);
        int humanPick()
        {
            if (userChoice >= 1 && userChoice <= 3)
                return userChoice;
            if(userChoice < 1 || userChoice > 3)
                return 0;
        }

        int computerPick(int choice, int leftover)
        {
            if (toothpicks > 4)
                choice = 4 - leftover;
            if (toothpicks = 2 || 3 || 4)
                choice = 
            if (toothpicks = 1)
                choice = toothpicks;
                

        }
         
        
        return toothpicks; //terminates loop 
    }
    
    return 0; 
}

CodePudding user response:

You do not want to be defining functions inside of other functions. Although some compilers allow that as an extension, it is not part of the language. But, even if you are using a compiler that allows it, your syntax is wrong. Consider:

int leftOnTable(int toothpicks, int taken);{
    int taken;
    while(toothpicks > 0){
        toothpicks = toothpicks - taken;
        return toothpicks;
    }
       

In the body of another function, the semi-colon after int taken) ends the declaration of the function, and the {...} after that is not a function definition but is just a block of commands to be executed in the enclosing function. You want to write this (outside of any enclosing function) as:

int
leftOnTable(int toothpicks, int taken)
{
    ... 
}

and then call it as:

int
main(void)
{
    ...
    int toothpicks = 31;
    int taken = humanPick();
    ...
    toothpicks = leftOnTable(toothpicks, taken);
    ...
}

Your attempt to define humanPick() seems incorrect. Getting user input is notoriously difficult, and scanf is almost always the wrong tool. (See http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html). By "almost always", I am being conservative and I actually mean that it is absolutely never the right choice. But, if you want to use scanf, you might try something like:

int
humanPick(int toothpicks)
{
    int userChoice;
    printf("There are currently %d toothpicks left.\n", toothpicks);
    puts("How many toothpicks are you going to take off the table?");
    puts("Pick a number between 1, 2 and 3.");

    int rv;
    while( 1 != (rv = scanf("m", &userChoice))
        || userChoice > 3 || userChoice < 1 )
    {
        char *msg = "Invalid input.";
        switch( rv ){
        case EOF:
            puts("Terminating.  Thanks for playing!");
            exit(0);
        case 1:
            msg = "Choice must be 1, 2, or 3";
            /* Fall thru */
        default:
            fprintf(stderr, "%s. Try again\n", msg);
            while( (rv = getchar()) != EOF && rv != '\n' ){
                ;
            }
        }
    }
    return userChoice;
}

Be aware that this function changes the function prototype, and this version take toothpicks as a parameter.

Note two things about the usage of scanf here that are absolutely essential. You must always check the value returned by scanf, and you must always use a width modifier on the conversion specifier in the format string. If you do not understand what this means, you should stop using scanf. Without a width modifier, the behavior is undefined for certain inputs. Note that using a width modifier on %d means that you cannot reliably scan the full range of values that can be stored in an integer, and this is just one more reason to avoid using scanf.

CodePudding user response:

"but cant organize the structure properly"

This section will work the way you have it, but I would place the #define below the #includes:

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

#define ROUNDS 3

Your prototype section looks okay, but its idiomatic in declarations to use (void) rather than ()

void greeting(void);                        //display welcome message to user
int playRound(int round);                   //play one round
int humanPick(void);                        //retrieve the user's guess
int computerPick(int choice, int leftover); //computer makes its pick
int leftOnTable(int toothpicks, int taken); //calculate number of toothpicks left
void winnerAnnouncment(int user);           //overall winner of round announcement

The highest number of compile errors have to do with using nested functions. The C language does not support them, and it is not a good idea IMO to use extensions that allow them. The form of the program should be shaped like this:

int main(void)
{
    // declare variables and call functions here
    greeting(); 
    //other constructs & function calls here.

    return 0;
}

//Functions definitions should include 1 definition for each prototype:
void greeting(void)
{
    printf("Welcome to the Toothpick Game!\n");
    printf("Here are the rules.\n");
    printf("There are currently 31 toothpics 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");
}

int playRound(int round)
{
    //content here (but your current content has issues)
    return something;
}

int humanPick(void)
{
    //content here
    return something;
}

int computerPick(int choice, int leftover)
{
    //content here
    return something;
}
...and so on.

Note also I see you are using = to do comparisons. = is an assignment operator, for example toothpicks = 2 means assign the value 2 to the variable toothpicks. If you want to check equality, then use ==, eg if(toothpicks == 2){do something}.

An additional general note about function implementation and usage. Functions are seen in different forms:

  • prototype - is a declaration in the code that instructs the compiler about the data type of the function, arguments and parameter list. example: int add(int a, int b);

  • definition - The signature of a function is presented in the same way as that of its prototype but includes {...} enclosures which contain the body of executable code the function is designed to execute during run-time. Example:

    int add(int a, int b)
    {
        return a   b;
    }
  • calling a function - When a function is called, its signature, including the decorations around its name are not shown, but instead contain objects that are of the same type as specified in the function formal signature. For example, inside main(), or some other function:
    int sum = 0;
    int a = 10;
    int b = 20;
    sub = add(a, b); 

Keep in mind that syntax errors will always be flagged by a good compiler, and prevent an executable from being created. But the number and type of warning messages you see at compile time is settable, and depend to some extent on how you have your compiler set. Setting the warnings on your compiler to a strict enough setting will output messages to warn of syntax errors, or possible mis-use of operators, or regarding function definition / usage and often suggest how to correct. if using GCC for example set compiler to use -Wall.

  •  Tags:  
  • c
  • Related