Advertisement
Guest User

Untitled

a guest
May 30th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_LENGTH 100
  5. #define ROW_LENGTH
  6. #define EMPTY_SQUARE -1
  7. #define NOT_FINISH -2
  8. #define FINISH_SUCCESS 0
  9. #define FINISH_FAILURE -3
  10. #define BOARD_SIZE 9
  11. #define FILLED 1
  12. #define FAIL -1
  13. #define MAX_POSSIBILITIES 8
  14. typedef struct SquareCord //struct for square cords
  15. {
  16. short row;
  17. short col;
  18. }Cords;
  19.  
  20. typedef struct CordsListNode{ //struct for square cords list nodes
  21. Cords square;
  22. struct CordsNode* next;
  23. }CordsNode;
  24.  
  25. typedef struct CordsList //struct for square cords list
  26. {
  27. CordsNode* head;
  28. CordsNode* tail;
  29. }cordList;
  30.  
  31. typedef struct RandomSudokoBoard //Random sudoko board struct
  32. {
  33. int N;
  34. cordList list;
  35. }rndBoard;
  36.  
  37. typedef struct _Array
  38. {
  39. short *arr;
  40. unsigned short size;
  41. } Array;
  42.  
  43. typedef struct _Player //Player struct
  44. {
  45. char* playerName;
  46. int sudokuBoard[9][9];
  47. Array*** possibilitiesArray;
  48. }player;
  49.  
  50. typedef struct PlayerList //Player list struct
  51. {
  52. playerNode* head;
  53. playerNode* tail;
  54. }playerList;
  55.  
  56. typedef struct PlayerListNode //Player list node struct
  57. {
  58. player* Player;
  59. struct playerNode* next;
  60. }playerNode;
  61.  
  62. typedef struct WinnersList
  63. {
  64. playerList winlist;
  65. }winLst;
  66.  
  67. typedef struct ActivePlayerList
  68. {
  69. playerList activePlayers;
  70. }activeList;
  71.  
  72. typedef struct ActivePlayerArray
  73. {
  74. player** playersArr;
  75. }activeArr;
  76.  
  77.  
  78. char* getPlayerName(int currPlayer);
  79.  
  80. int main(void)
  81. {
  82. int playerNum;
  83. int currPlyr = 0;
  84. activeList activePlayerList;
  85.  
  86. makeEmptyPlayerList(&(activePlayerList.activePlayers));
  87.  
  88. printf("Please enter the number of players: ");
  89. scanf("%d\n", &playerNum);
  90.  
  91. while (currPlyr < playerNum)
  92. {
  93.  
  94. }
  95.  
  96.  
  97. }
  98.  
  99. void makeEmptyPlayerList(playerList *lst)
  100. {
  101. lst->head = NULL;
  102. lst->tail = NULL;
  103. }
  104.  
  105. void AddToEmptyList(playerList *list, playerNode * cell_to_add)
  106. {
  107. list->head = list->tail = cell_to_add;
  108. }
  109.  
  110. playerNode* createNewPlayerListNode(playerNode * next)
  111. {
  112. int i, j;
  113. playerNode* res;
  114. res = (playerNode*)malloc(sizeof(playerNode));
  115. res->Player = (player*)malloc(sizeof(player));
  116.  
  117. for (i = 0; i < 9; i++)
  118. for (j = 0; j < 9; j++)
  119. res->Player->sudokuBoard[i][j] = EMPTY_SQUARE;
  120. res->next = next;
  121. return res;
  122. }
  123.  
  124. char* getPlayerName(int currPlayer) //this function recieves input from the user,allocates a string that contains the input entered by the user and then returns the starting address of the string
  125. {
  126. char* str; //holds the current string
  127. char ch; //holds the current character
  128. int logSize = 0; //holds the logical size
  129. int phsSize = 1; //holds the physical size
  130.  
  131. str = (char*)malloc(phsSize*sizeof(char)); //Allocates an array for 'str'
  132.  
  133. if (str == NULL)//if allocation fails the program will shut down
  134. {
  135. sprintf(stderr, "Allocation failure! program shutting down...");
  136. exit(-1);
  137. }
  138.  
  139. str[phsSize] = '\0'; //add's '\0' at the end of the string to mark its end
  140.  
  141. printf("Please enter the name of player number %d: ", currPlayer + 1);
  142.  
  143. ch = getchar(); //gets a character into 'ch'
  144.  
  145. while (ch != '\n')
  146. {
  147. if (logSize + 1 == phsSize) //checks if the string's logicalsize+1 (for '\0') reaches the end of the size allocated to it
  148. {
  149. phsSize *= 2;
  150. str = (char*)realloc(str, phsSize *sizeof(char)); //Reallocates 'str',increasing its size
  151.  
  152. if (str == NULL) //checks if reallocation failed,if so the program will shut down
  153. {
  154. sprintf(stderr, "Allocation failure! program shutting down...");
  155. exit(-1);
  156. }
  157. }
  158.  
  159. str[logSize] = ch;
  160. str[logSize + 1] = '\0';
  161. logSize++;
  162. ch = getchar();
  163. }
  164.  
  165. str[logSize + 1] = '\0';
  166.  
  167. if (logSize < phsSize)//checks if theres excess positions in 'str' that aren't being used and if so the string will be reallocated
  168. {
  169. str = (char*)realloc(str, (logSize + 1)*sizeof(char)); //Reallocates 'str',decreasing its size
  170.  
  171. if (str == NULL) //checks if reallocation failed,if so the program will shut down
  172. {
  173. sprintf(stderr, "Allocation failure! program shutting down...");
  174. exit(-1);
  175. }
  176. }
  177.  
  178. if (logSize>0) //checks the string contains atleast 1 character
  179. return(str);
  180. else
  181. return(NULL);
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement