Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//3 AI's and 1 player game
typedef struct player Player;
struct player {
char alias[25]; //player name
char *nCards[26]; //cards the player has
Player *next;
};
int TCards; //total cards
Player *newn(void); //new node (player)
//int cards (void);
int dist (void); //distributing
int main (void) {
dist();
return 0;
}
int dist (void) {
char *cards[] = {"2c","3c","4c","5c","6c","7c","8c","9c","Tc","JC","QC","KC","AC",
"2s","3s","4s","5s","6s","7s","8s","9s","Ts","JS","QS","KS","AS",
"2h","3h","4h","5h","6h","7h","8h","9h","Th","JH","QH","KH","AH",
"2d","3d","4d","5d","6d","7d","8d","9d","Td","JD","QD","KD","AD"};
char *cards2[] = {"2c","3c","4c","5c","6c","7c","8c","9c","Tc","JC","QC","KC","AC",
"2s","3s","4s","5s","6s","7s","8s","9s","Ts","JS","QS","KS","AS",
"2h","3h","4h","5h","6h","7h","8h","9h","Th","JH","QH","KH","AH",
"2d","3d","4d","5d","6d","7d","8d","9d","Td","JD","QD","KD","AD"};
/*char *p_dist[52]; //distributed cards are also copied into this array, so no recopying
char *p_dist2[52];*/ //second array to handle doubles to prevent infinite loop from less-card distribution
int i = 0; //indexer of array from distributor
int j = 0;
int arr_choice; //value can be 0 or 1, if 0, then take from cards[], if 1, from cards2[]
Player *n_player; //node for new players
Player *user; //human
Player *ai_1;
Player *ai_2;
Player *ai_3;
puts("Creating AI's and User...");
// THINGS FINALLY STOPPED GIVING SEG FAULTS, CHECK SIZE OF ALLOCATION
n_player = newn();
user = n_player;
n_player = newn();
ai_1 = n_player;
n_player = newn();
ai_2 = n_player;
n_player = newn();
ai_3 = n_player;
puts("\nEnter your name");
scanf("%25s", user->alias);
puts("\nEnter 1st AI's name");
scanf("%25s", ai_1->alias);
puts("Enter 2nd AI's name");
scanf("%25s", ai_2->alias);
puts("Enter 3rd AI's name");
scanf("%25s", ai_3->alias);
printf("\n\nDistributing cards to %s...\n\n", user->alias);
srand ( time(NULL) ); //seed
TCards = 0; //indexer for controlling cards that go to user
//////////////////*THIS IS THE DISTRIBUTION LOOP FOR THE HUMAN USER*/////////////////
for (TCards=0; TCards < 25; TCards++){ //104 / 4 = 26 cards per user
arr_choice = rand()/(int) (((unsigned)RAND_MAX + 1)/ 2);
if(arr_choice == 0) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
while(strcmp(cards[i], "0") == 0) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
}
user->nCards[j] = malloc(strlen(cards[i])*sizeof(char*));
strcpy(user->nCards[j], cards[i]);
cards[i] = "0";
j++;
}
else if(arr_choice == 1) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
while(strcmp(cards2[i], "0") == 0) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
}
user->nCards[j] = malloc(strlen(cards2[i])*sizeof(char*));
strcpy(user->nCards[j], cards2[i]);
cards[i] = "0";
j++;
}
}
int b;
for (b=0;b<25;b++){
printf("%s\n", user->nCards[b]);
}
/////////////////////*THIS IS THE DISTRIBUTION LOOP FOR AI 1////////////////////
printf("\n\nDistributing cards to %s...\n\n", ai_1->alias);
j = 0;
for (TCards=0; TCards < 25; TCards++){ //104 / 4 = 26 cards per user
arr_choice = rand()/(int) (((unsigned)RAND_MAX + 1)/ 2);
if(arr_choice == 0) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
while(strcmp(cards[i], "0") == 0) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
}
ai_1->nCards[j] = malloc(strlen(cards[i])*sizeof(char*));
strcpy(ai_1->nCards[j], cards[i]);
cards[i] = "0";
j++;
}
else if(arr_choice == 1) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
while(strcmp(cards2[i], "0") == 0) {
i = rand()/(int) (((unsigned)RAND_MAX + 1)/ 53);
}
ai_1->nCards[j] = malloc(strlen(cards2[i])*sizeof(char*));
strcpy(ai_1->nCards[j], cards2[i]);
cards[i] = "0";
j++;
}
}
for (b=0;b<25;b++){
printf("%s\n", ai_1->nCards[b]);
}
return 0;
}
Player *newn(void) {
Player *player;
player = (Player*)malloc(sizeof(Player));
return (player);
}