diff options
| author | Mario Kilies <MarioKilies@GMX.net> | 2011-01-04 03:41:55 +0100 |
|---|---|---|
| committer | Mario Kilies <MarioKilies@GMX.net> | 2011-01-04 03:41:55 +0100 |
| commit | 81bbb4b5fce757d0be8166cc44d2a15a2d9f247b (patch) | |
| tree | a32f6db9e5ee54bd0e8e1068dbbc5ba0dbd4fa23 /src | |
| parent | baee729cd17c59253319ae8281f49e49abd76644 (diff) | |
cardpoints(): Rewritten code that is more readable.
Changed return values from int to uint32_t.
Added display initialization to main().
Diffstat (limited to 'src')
| -rw-r--r-- | src/common.c | 34 | ||||
| -rw-r--r-- | src/common.h | 16 | ||||
| -rw-r--r-- | src/main.c | 13 |
3 files changed, 37 insertions, 26 deletions
diff --git a/src/common.c b/src/common.c index d676f12..89841c4 100644 --- a/src/common.c +++ b/src/common.c @@ -1,31 +1,25 @@ #include "common.h" +#include <assert.h> -int cardpoints(card c) +uint32_t cardpoints(const card c) { - int rem = c % 10; - int div = c / 10; - - if(rem == div) - return (rem==5)?7:5; - - if(rem == 5) - return 2; - else if(rem == 0) - return 3; - - return 1; + assert(c > 0 && c <= 105); + if (55 == c) return 7; // Card 55 is worth of 7 points + if (c % 11 == 0) return 5; // Cards that are multiples of 11 are worth 5 points + if (c % 10 == 0) return 3; // ... 10 are worth 3 points + if (c % 5 == 0) return 2; // ... 5 are worth 2 points + return 1; // All other cards are worth 1 point } -int stackpoints(cardstack s) +uint32_t stackpoints(const cardstack s) { - int points = 0; - for(int i=0; i<MAX_CARDSTACK_SIZE; i++) + uint32_t points = 0; + + for(uint32_t i = 0; i < MAX_CARDSTACK_SIZE; i++) { - card cur = s[i]; - if(cur > 0) - points += cardpoints(cur); + if(s[i] > 0) + points += cardpoints(s[i]); } return points; } - diff --git a/src/common.h b/src/common.h index 13fcf7c..5bd31f7 100644 --- a/src/common.h +++ b/src/common.h @@ -1,10 +1,16 @@ +#ifndef OXEN_COMMON_H +#define OXEN_COMMON_H + +#include <stdint.h> + #define MAX_CARDSTACK_SIZE 5 -#define MAX_CARDHAND_SIZE 10 +#define MAX_HAND_SIZE 10 typedef char card; -typedef card cardstack[5]; -typedef card hand[10]; +typedef card cardstack[MAX_CARDSTACK_SIZE]; +typedef card hand[MAX_HAND_SIZE]; -int cardpoints(card c); -int stackpoints(cardstack s); +uint32_t cardpoints(const card c); +uint32_t stackpoints(const cardstack s); +#endif // OXEN_COMMON_H @@ -1,9 +1,20 @@ #include <stdio.h> #include <stdlib.h> +#include <unistd.h> +#include <curses.h> +#include "display.h" #include "common.h" int main(int argc, char **argv) { - printf("55:%d, 42:%d, 11:%d, 100:%d, 85:%d\n", cardpoints(55), cardpoints(42), cardpoints(11), cardpoints(100), cardpoints(85)); + display_init(); + mvprintw(12, 40, "oxen"); + refresh(); + sleep(2); + display_fini(); + + for (int i = 1; i <= 105; i++) + printf("%d:%d\n", i, cardpoints(i)); + return EXIT_SUCCESS; } |
