Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- //for anagram
- char ana1[]="listen";
- char ana2[]="earth";
- char ana3[]="binary";
- //for caesar-cipher
- char ciph1[]="There is a secret code";
- char ciph2[]="Attack at dawn";
- char ciph3[]="Meet me at the park";
- //for word-guessing
- char word1[]="program";
- char word2[]="network";
- char word3[]="science";
- int stlen(char str[]);
- void stcpy(char str[],char str1[]);
- int stcompare(char str1[],char str2[]);
- int show_menu();
- void shuffle(char result[], char str[]);
- void encrypt(char str[],int shift);
- void decrypt(char str[],int shift);
- int main()
- {
- srand(time(NULL));
- int r=rand() % 4;
- int x= show_menu();
- if(x==2)
- {
- return 0;
- }
- //anagram challenge
- {
- printf("Starting Anagram Challenge...\n");
- char chosen[50];
- if(r==1)
- {
- stcpy(chosen,ana1);
- }
- else if(r==2)
- {
- stcpy(chosen,ana2);
- }
- else
- {
- stcpy(chosen,ana3);
- }
- char shuffled_word[50];
- shuffle(shuffled_word,chosen);
- printf("Scrambled word: %s\n",shuffled_word);
- int anagram_attempt=1;
- while(anagram_attempt<=3)
- {
- char guess[50];
- printf("Your guess: ");
- scanf("%s",guess);
- if(stcompare(guess,chosen)==1)
- {
- printf("Correct! You solved it in %d attempt(s)\n",anagram_attempt);
- break;
- }
- else
- {
- printf("Incorrect! ");
- if(anagram_attempt<3)
- {
- printf("Try again.\n");
- }
- else
- {
- printf("Correct word: %s\n",chosen);
- }
- anagram_attempt++;
- }
- }
- }
- //caesar-cipher challenge
- {
- int shift=0;
- while(shift==0)
- {
- shift= rand() % 10;
- }
- printf("Starting Caesar Cipher Challenge...\n");
- char chosen_phrase[50];
- if(r==1)
- {
- stcpy(chosen_phrase,ciph1);
- }
- else if(r==2)
- {
- stcpy(chosen_phrase,ciph2);
- }
- else
- {
- stcpy(chosen_phrase,ciph3);
- }
- char encrypted[200];
- stcpy(encrypted,chosen_phrase);
- encrypt(encrypted,shift);
- printf("Encrypted phrase: %s\n",encrypted);
- int cipher_attempt=1;
- while(cipher_attempt<=3)
- {
- char guess_phrase[200];
- getchar();
- printf("Your guess: ");
- scanf("%[^\n]",guess_phrase);
- if(stcompare(guess_phrase,chosen_phrase)==1)
- {
- printf("Correct! You decrypted it in %d attempt(s)\n",cipher_attempt);
- break;
- }
- else
- {
- printf("Incorrect! ");
- if(cipher_attempt<3)
- {
- printf("Try again.\n");
- }
- else
- {
- printf("Correct phrase: %s\n",chosen_phrase);
- }
- cipher_attempt++;
- }
- }
- }
- }
- int show_menu()
- {
- printf("Welcome to the Game World!\n");
- printf("1. Start Game\n");
- printf("2. Exit\n");
- int choice;
- printf("Enter your choice: ");
- scanf("%d",&choice);
- return choice;
- }
- int stlen(char str[])
- {
- int i,size=0;
- for(i=0;str[i];i++)
- {
- size++;
- }
- return size;
- }
- void stcpy(char str[],char str1[])
- {
- int i;
- for(i=0;str1[i];i++)
- {
- str[i]=str1[i];
- }
- str[stlen(str1)]='\0';
- }
- //pass two strings so the first is a shuffled version of the second
- void shuffle(char result[], char str[])
- {
- int n=stlen(str);
- stcpy(result,str);
- int i,j;
- srand(time(NULL));
- for(i=n-1;i>=0;i--)
- {
- j= rand() % (i+1);
- char temp=result[i];
- result[i]=result[j];
- result[j]=temp;
- }
- }
- //returns 0 if the strings dont exactly match
- int stcompare(char str1[],char str2[])
- {
- if(stlen(str1)!=stlen(str2))
- {
- return 0;
- }
- int i;
- for(i=0;str1[i];i++)
- {
- if(str1[i]!=str2[i])
- {
- return 0;
- }
- }
- return 1;
- }
- //shifts each character in str by "shift" forward and wraps around
- void encrypt(char str[],int shift)
- {
- int i,temp;
- for(i=0;str[i];i++)
- {
- if(str[i]!=' ')
- {
- if(str[i]>='a'&&str[i]<='z')
- {
- str[i] = ((str[i] - 'a' + shift) % 26) + 'a';
- }
- else if(str[i]>='A' && str[i]<='Z')
- {
- str[i] = ((str[i] - 'A' + shift) % 26) + 'A';
- }
- }
- }
- }
- void decrypt(char str[],int shift)
- {
- int i,temp;
- for(i=0;str[i];i++)
- {
- if(str[i]!=' ')
- {
- if(str[i]>='a'&&str[i]<='z')
- {
- str[i] = ((str[i] - 'a' - shift) % 26) + 'a';
- }
- else if(str[i]>='A' && str[i]<='Z')
- {
- str[i] = ((str[i] - 'A' - shift) % 26) + 'A';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement