Skip to main content
gamedev.net
🔒 Locked

cookiebox

Started by donoboso 3 replies 400+ views
Original Post
donoboso
donoboso
I looked in an older programming book and in this book while loops where explained by cookies. So i came up with this little programming game. (Maybe there was a cookie game before, i dunno) Target: The code below has to become long and somehow mad. You can add or change anything you want:

// definitions
#define COOKIEBOX_SIZE 10 //There are 10 cookies in the box


// includes
#include <stdio.h>


// globals
int cookienum=0;


// functions
int Take_Cookie(void)
{
    cookienum++;
}


int Box_Is_Empty(void)
{
    if(cookienum > COOKIEBOX_SIZE)
        return 0;
    
    return 1;
}


int main(void)
{
    while(1)
    {
        Take_Cookie();
        
        if(Box_Is_Empty())
            break;
    }
    
    return 0;
}




CodeMunkie
CodeMunkie
It might be more intuitive to set cookienum to COOKIEBOX_SIZE at the start, and then decrement cookienum, then check to see if it is 0 in Box_is_Empty. It accomplishes the same thing, but when you take a cookie, you would expect the number of cookies to decrease. Also,

const int COOKIEBOX_SIZE = 10; //There are 10 cookies in the box

#define makes baby Jesus cry.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
twix
twix
Okay, I'll play.
! #define COOKIEBOX_SIZE 10 // There are 10 cookies in the box------! #define COOKIEBOX_SIZE ((cookienum++ > Take_Cookie() + 5) ? 3 : (cookienum++ - 3 < Box_Is_Empty())) // omfg

How many cookies are there, then?
donoboso
donoboso
I'm not sure, because i've forgotten the return in Take_Cookie.
It was supposed to return 1 but i think that you thought, that it returns cookienum.

Well im really not sure^^
twix
twix
Since this program is apparently in C, an int-returning function without a return statement will return 0 by default. (Or so I seem to recall, although I've never actually used a C compiler. [grin])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.