]> git.cworth.org Git - kub/blobdiff - kub.c
Added beginnings of card_compare and added to card_group_is_run(still incomplete)
[kub] / kub.c
diff --git a/kub.c b/kub.c
index cb2b8d9dbb0f2c19ab9425b173856c9b23e9c319..fc5c8b688225c55b9a1061e287bfcd804d3c0e4e 100644 (file)
--- a/kub.c
+++ b/kub.c
@@ -71,6 +71,36 @@ static void player_init(player_t *player)
     card_group_init(&player->hand);
 }
 
+
+static int card_compare(card_t *card_one, card_t *card_two)
+{
+    if (card_one.number < card_two.number)
+       return -1;
+    if (card_one.number == card_two.number)
+       return 0;
+    if (card_one.number > card_two.number)
+       return 1;
+}
+
+static int card_group_is_run(card_group_t *card_group)
+{
+    if (card_group->num_cards > 13 || card_group->num_cards < 3)
+    {
+       return 0;
+    }
+    for (i = 0; i < card_group->num_cards - 1; ++i)
+       if(card_group->cards[i].color != card_group->cards[i + 1].color)
+       {
+           return 0;
+       }    
+    for (i = 0; i < card_group->num_cards - 1; ++i)
+       if(card_group->cards[i].number != card_group->cards[i + 1].number -1)
+       {
+           return 0;
+       }
+    return 1;    
+}
+
 static int card_group_is_run(card_group_t *card_group)
 {
     int i;
@@ -94,13 +124,13 @@ static int card_group_is_run(card_group_t *card_group)
            lowest = card_group->cards[i].number;
        }
     }
-    if (highest - lowest = card_group->num_cards - 1)
+    if (highest - lowest != card_group->num_cards - 1)
     {
-       return 1;
+       return 0;
     }
+    return 1;
 }
 
-
 static int card_group_is_set(card_group_t *card_group)
 {
     int i;
@@ -129,24 +159,36 @@ static int card_group_is_set(card_group_t *card_group)
     return 1;
 }
 
-#if 0
-static void deck_deal(deck_t *deck, game_t *game)
+static void deck_deal(game_t *game, deck_t *deck)
 {
     card_t temp;
     int rand_card;
-    int i, j;
-    for (i = 0; i < PLAYERS; ++i)
+    int i, j, newline;
+    
+    printf ("How many players(1-4) should I deal in? ");
+    game->num_players = getchar();
+    if (game->num_players == EOF)
+    {
+       printf ("\nGoodbye.\n");
+       exit (1);
+    }
+    newline = getchar();   
+    game->num_players -= '0';
+    
+    for (i = 0; i < game->num_players; ++i)
     {
-       for (j = 0; j < 13; ++j)
+       for (j = 0; j < 14; ++j)
        {
-           rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
+           rand_card = ((deck->num_cards + 1.0) * rand()) / (RAND_MAX + 1.0);
            temp = deck->cards[rand_card];
-           deck->cards[rand_card] = deck->cards[last];
-           game->players[i]->hand->cards[j] = temp;
+           deck->cards[rand_card] = deck->cards[deck->num_cards - 1];
+           game->players[i].hand.cards[j] = temp;
+           deck->num_cards -= 1;
+           game->players[i].hand.num_cards += 1;
        }
     }
+    printf ("Game dealt for %d player(s)\n", game->num_players);
 }
-#endif
 
 static void deck_init(deck_t *deck)
 {
@@ -176,8 +218,8 @@ static void deck_shuffle(deck_t *deck)
     {
        rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
        temp = deck->cards[rand_card];
-       deck->cards[rand_card] = deck->cards[last];
-       deck->cards[last] = temp;
+       deck->cards[rand_card] = deck->cards[last - 1];
+       deck->cards[last - 1] = temp;
     }
 }
 
@@ -197,6 +239,15 @@ static void deck_print(deck_t *deck)
     printf ("There are %d tiles in the deck\n" , deck->num_cards);
 }
 
+static void hand_print(game_t *game)
+{
+    int i;
+    for (i = 0; i < game->players[0].hand.num_cards; ++i)
+    {
+       card_print(game->players[0].hand.cards[i]);
+    }
+}
+
 static void game_init(game_t *game)
 {
     int i;
@@ -221,7 +272,9 @@ int main(void)
     
     game_init(&game);
     deck_print(&game.deck);
-    
+    deck_deal(&game, &game.deck);
+    hand_print(&game);
+    deck_print(&game.deck);
     return 0;
     
 }