[LCP]senet.c - returning a ptr to a function
Nick Croft
nicko at acay.com.au
Thu Jul 26 12:29:07 UTC 2001
Hi,
I'm learning C from one of those books that promise a lot in 21 days. One
thing I've learnt from this series is that there's plenty of help getting
started, but the exercises and advice thin out a bit when the going gets tough.
To consolidate my learning I've set myself the goal of writing an ascii game
based on a simple board game - Senet. Instead of sticks this game uses 4
throwing-sticks. Landing one way up gives 0, the other way up gives 1. The four
sticks are totalled, with 4x0 counting as a six. The character of the game
derives from the resulting chances, 2 most often, then 1 or 3, then 4 or 6.
Try as I might, I can't turn the function `sticks' in the following inelegant
piece into a for-loop with an array. I keep getting the address returned.
Unfortunately chapter 14 of the 21 day book has no answers to the few
exercises on returning pointers to an array.
______________________________________________
senet.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int sticks (void);
int x;
int main ()
{
x = sticks();
printf("\nThrow value: %d. \n", x);
return 0;
}
int sticks (void)
{
int throw1, throw2, throw3, throw4, total;
srand(time(NULL));
throw1 = (int) (2.0*rand()/(RAND_MAX+1.0));;
throw2 = (int) (2.0*rand()/(RAND_MAX+1.0));;
throw3 = (int) (2.0*rand()/(RAND_MAX+1.0));;
throw4 = (int) (2.0*rand()/(RAND_MAX+1.0));;
total = throw1 + throw2 + throw3 + throw4;
if (total == 0) { total = 6;
}
return total;
}
___________________________________________________
I'd love to have an nice for-loop and an array instead of the throw1-2-3-4
business.
Sorry for the simplicity, but it's where I'm at. Maybe while there's not
much traffic on the list you might care to help.
Thanks,
Nick
_______________________________________________
linuxcprogramming mailing list
linuxcprogramming at lists.linux.org.au
http://lists.linux.org.au/listinfo/linuxcprogramming
More information about the linuxCprogramming
mailing list