Advertisement
lacedemonian

Scrabble.c

Oct 14th, 2024
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6.  
  7. struct Entry{
  8.  char word[32];
  9.  struct Entry* next;
  10. };
  11.  
  12. //GLOBAL VARIABLES
  13. char buffer[32];
  14. int guessDist[26];
  15. int handDist[26];
  16. struct Entry* wordList = NULL; //Head node for the dictionary
  17. //At least 6 characters
  18. //Easy Start option? Also make sure it has a minimum of high-frequency letters?
  19.  
  20. int init(){
  21.  srand(time(NULL));
  22.  //FILE* dictionary = fopen("dictionary.txt","r");
  23.  //if(dictionary==NULL){
  24.  // dictionary = fopen("2of12.txt","r"); //I'd prefer to call the dictionary file 'dictionary.txt', but i assume you're going to test it with the default filename
  25.  //}
  26.  //if(dictionary==NULL){
  27.  // printf("No Dictionary Found!");
  28.  //}
  29.  return 0; //Placeholder
  30. }
  31.  
  32. void displayWorld(){
  33.  printf("--------------------------\n"); //Placeholder
  34. }
  35.  
  36. void getLetterDistribution(char *word, int *dist){
  37.  for(int i=0;i<26;i++){
  38.   printf("%c",i+65);
  39.  }
  40.  printf("\n");
  41.  for(int i=0;i<26;i++){
  42.   int j = 0;
  43.   do{
  44.    if(word[j] == i+65){ //The capital letters begin at 65 in the ascii table
  45.     dist[i]++; //Increase the count for that letter
  46.    }
  47.    j++;
  48.   }while(word[j] != '\0'); //Wait for end of word
  49.   printf("%d",dist[i]);
  50.  }
  51.  printf("%ls\n",dist);
  52. }
  53.  
  54. int compareCounts(int *tryWord, int *hand){ //Compare two distribution arrays
  55.  int playable = 1; //Start by assuming the word can be played.
  56.  int letter = 0;
  57.  while(playable == 1 && letter < 26){ //26 elements in each array
  58.   if(tryWord[letter]>hand[letter]){
  59.    playable = 0; //If at any point we each a letter that isn't on hand, cut it off there.
  60.   }
  61.   letter++;
  62.  }
  63.  return playable; //Return 1 for playable and 0 for unplayable
  64. }
  65.  
  66. void acceptInput(){
  67.  printf("Enter a Guess: ");
  68.  fgets(buffer, sizeof(buffer), stdin);
  69.  if(buffer != NULL) { //Ensure there's something to read
  70.   int i = 0;
  71.   while(buffer[i] != '\0'){ //Loops through each char in sequence until it hits the null term
  72.    //printf("%d",buffer[i]);
  73.    buffer[i] = toupper((unsigned char)buffer[i]); //Replaces the char with its uppercase
  74.    i++;
  75.   }
  76.   printf("%s",buffer);
  77.   //*guess = *buffer;
  78.   //printf("%s\n",guess);
  79.  } else {
  80.   printf("Invalid Entry\n");
  81.   acceptInput(); //Retry input
  82.  }
  83. }
  84.  
  85. void teardown(){ //Teardown is ONE word!
  86.  printf("All Done\n");
  87. }
  88.  
  89. int isDone(){
  90.  return 1; //crude implementation of boolean logic: 1 or 0
  91. }
  92.  
  93. void gameLoop(){
  94.  char testHand[12] = "ACEROLAORIO\0"; //TEST INVOCATIONS
  95.  int testHandDist[26];
  96.  do{
  97.   displayWorld();
  98.   printf("Hand: %s\n",testHand);
  99.   acceptInput();
  100.   getLetterDistribution(testHand,handDist);
  101.   //printf("%s\n",buffer);
  102.   getLetterDistribution(buffer,guessDist);
  103.   printf("%d\n",compareCounts(guessDist,handDist));//TEST INVOCATIONS
  104.  }while(isDone()!=1);
  105. }
  106.  
  107. int main(){
  108.  init();
  109.  gameLoop();
  110.  teardown();
  111.  return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement