Untitled
unknown
plain_text
a year ago
8.5 kB
7
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int exits=0,points=0,challenge=0,randomizer;
char anagram_1st[]="listen",anagram_2nd[]="earth",anagram_3rd[]="binary";
char cipher_1st[]="There is a secret code",cipher_2nd[]="Attack at dawn",cipher_3rd[]="Meet me at the park";
char word_1st[]="program",word_2nd[]="network",word_3rd[]="science";
void show_menu()
{
int n;
printf("Welcome to the Game World!\n1. Start Game\n2. Exit\nEnter your choice: ");
scanf("%d",&n);
printf("\n");
if(n==2)
{
printf("Game Over!\nYour total score: 0 points\n");
exits=1;
}
}
void score(int n)
{
points+=n;
}
void completion()
{
challenge+=1;
}
void random_number()
{
srand(time(0));
randomizer=rand()%3;
}
void scramble(char string[])
{
int count=0,j=0,k;
while(string[j]!='\0')
{
count++;
j++;
}
char str_1st=string[0],str_last=string[count-1];
do{
k=0;
while(k<count)
{
int pos1=rand()%count;
int pos2=rand()%count;
if(pos1!=pos2)
{
char temp=string[pos1];
string[pos1]=string[pos2];
string[pos2]=temp;
}
k++;
}
}while(string[0]==str_1st || string[count-1]==str_last);
}
void caesar_encrypt(char string[])
{
int shift = 3,i;
for (i=0; string[i]!='\0'; i++)
{
if (string[i]>='A' && string[i]<= 'Z')
{
string[i] = 'A' + (string[i]-'A'+shift)%26;
}
if (string[i]>='a' && string[i]<='z')
{
string[i] = 'a' + (string[i]-'a'+shift)%26;
}
}
}
void caesar_decrypt(char string[])
{
int shift=3,i;
for (i=0; string[i]!='\0'; i++)
{
if (string[i]>='A' && string[i]<='Z')
{
string[i] = 'A' + (string[i]-'A'-shift+26)%26;
}
if (string[i]>='a' && string[i]<='z')
{
string[i] = 'a' + (string[i]-'a'-shift+26)%26;
}
}
}
void stringcopy(char string1[],char string2[])
{
int i=0;
while(string1[i]!='\0')
{
string2[i]=string1[i];
i++;
}
string2[i]='\0';
}
int stringcompare(char string1[], char string2[])
{
int i=0,k=1;
while(string1[i]!='\0')
{
if(string1[i]!=string2[i])
{
k=0;
break;
}
i++;
}
return k;
}
int stringlength(char string1[])
{
int count=0,i=0;
while(string1[i]!='\0')
{
count++;
i++;
}
return count;
}
void checksubstring(char string1[], char string2[])
{
int count1=0,count2=0,i=0,j=0;
while(string1[i]!='\0')
{
count1++;
i++;
}
while(string2[j]!='\0')
{
count2++;
j++;
}
int p,n,mil,bool=0;
if(count2>count1) printf("No\n");
else
{
for(i=0; string1[i]!='\0'; i++)
{
if(string1[i]==string2[0])
{
p=i+1,n=1,mil=1;
while(string1[p]==string2[n])
{
mil++;
p++;
n++;
}
}
if(mil==count2)
{
bool=1;
break;
}
}
}
if(bool==1) printf("Yes\n");
else printf("No\n");
}
void anagram_play(char anagram[])
{
printf("Starting Anagram Challenge...\n");
int cha=stringlength(anagram);
char copy[cha],what[30];;
stringcopy(anagram,copy);
scramble(anagram);
int runs=1;
printf("Scrambled word: %s\n",anagram);
while(runs<4)
{
printf("Your guess: ");
scanf("%s", what);
if(stringcompare(copy,what)==1)
{
printf("Correct! You solved it in %d attempt(s)\n\n",runs);
score(10*(3-runs)+10);
completion();
break;
}
else
{
runs++;
if(runs>3) break;
printf("Incorrect! Try again.\n");
}
}
if(runs>3) printf("You have failed! Correct answer was: %s\n\n",copy);
}
void caesar_cipher(char cipher[])
{
printf("Starting Caesar Cipher Challenge...\n");
int attempts=1;
char guess[100];
caesar_encrypt(cipher);
printf("Encrypted phrase: %s\n",cipher);
caesar_decrypt(cipher);
getchar();
while(attempts<4)
{
printf("Your guess: ");
fgets(guess,sizeof(guess),stdin);
if(stringcompare(cipher,guess)==0)
{
if(attempts==3) break;
printf("Incorrect! Try again.\n");
}
else
{
printf("Correct! You decrypted it in %d attempt(s).\n\n",attempts);
score(10*(3-attempts)+10);
completion();
break;
}
attempts++;
}
if(attempts==3 && stringcompare(cipher,guess)==0)
{
printf("You have failed! Correct answer was: %s\n\n",cipher);
}
}
void word_guessing(char word[])
{
printf("Starting Word Guessing Challenge...\n");
int key,time2=0,time3=0,trys=1,utility=0;
char shoot[100];
printf("Hint: %c%c%c______\n",word[0],word[1],word[2]);
printf("Select an option: 1. Write Answer 2. Check Substring 3. Check Length\n");
while(trys<4)
{
scanf("%d",&key);
if(key==2)
{
if(time2==0)
{
printf("Enter substring: ");
char sub[100];
scanf("%s",sub);
checksubstring(word,sub);
utility++;
score(-2);
}
else
{
printf("Error: Utility function already used.\n");
}
time2++;
}
if(key==3)
{
if(time3==0)
{
int userlength;
printf("Enter length: ");
scanf("%d",&userlength);
if(userlength==stringlength(word)) printf("Yes\n");
else printf("No\n");
utility++;
score(-2);
}
else
{
printf("Error: Utility function already used.\n");
}
time3++;
}
if(key==1)
{
printf("Enter your guess: ");
scanf("%s",shoot);
if(stringcompare(word,shoot)==0)
{
if(trys==3) break;
printf("Incorrect! Try again.\n");
}
else
{
if(utility==1)
printf("Correct! You guessed it in %d attempt(s) using one utility function.\n\n",trys);
else if(utility==2)
printf("Correct! You guessed it in %d attempt(s) using two utility functions.\n\n",trys);
else
{
printf("Correct! You guessed it in %d attempt(s) without any utility function.\n\n",trys);
}
score(10*(3-trys)+10);
completion();
break;
}
trys++;
}
}
if(trys==3 && stringcompare(word,shoot)==0) printf("You have failed!\nCorrect answer was: %s\n\n",word);
}
int main()
{
show_menu();
if(exits==1) return 0;
random_number();
if(randomizer==0)
anagram_play(anagram_1st);
else if(randomizer==1)
anagram_play(anagram_2nd);
else anagram_play(anagram_3rd);
// printf("random num was: %d\n",randomizer);
random_number();
if(randomizer==0)
caesar_cipher(cipher_1st);
else if(randomizer==1)
caesar_cipher(cipher_2nd);
else caesar_cipher(cipher_3rd);
// printf("random num was: %d\n",randomizer);
random_number();
if(randomizer==0)
word_guessing(word_1st);
else if(randomizer==1)
word_guessing(word_2nd);
else word_guessing(word_3rd);
// printf("random num was: %d\n",randomizer);
if(challenge==3) score(5);
printf("Game Over!\nYour total score: %d points",points);
return 0;
}
Editor is loading...
Leave a Comment