Translate

29 Kasım 2015 Pazar

My Second Game: Hangman in C Programming


     Uzun zaman önce yaptığım kelime oyununu paylaşmam için şimdi vakit bulabildim. Özellikle dizi(array) konusunu pekiştirmek için güzel bir çalışma. Kelimeleri İngilizce olarak belirledim fakat siz Türkçe seçebilirsiniz, aynı zamanda dizi boyutlarını arttırarak daha fazla kelime ile oynayabilirsiniz. Programa görsellik eklemedim siz isteğinize göre ekleyebilirsiniz.

     Programı yazarken dikkat etmeniz gerekenler:
  • Oyunun sürekli devam edebilmesi çıkan kelimeyi diziden çıkartmalısınız ki tekrar aynı kelime gelmesin.
  • Yanlış yapılan harf sayısını sayarken dikkatli olmalısınız, döngüden doğru zamanda çıkması gerekir.
  • Seçilen harf kelimenin tümünde aranmalı.
  • Sorulan kelime rastgele gelmesi gerekir, hep aynı sırada gelirse bir anlamı olmaz.
   
     Programı anlamanız açısından yorum satırları ekledim. Bu program bir bonus sorusuydu, önceliğiniz kendi algoritmalarınızla bir yere bakmadan yapmanız ve takıldığınız yerlerde buradan faydalanmanız olmalıdır.


/*
Name : Berkay Saydam
ID :
Purpose of Program :  Hangman game .
*/

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
#include<conio.h>

int wordpull( void ); // a function for choose random word

main(){
int i=0,word=0,a=1,j=0,lose=0,found=0,exit=5,win=0;


const char* word_database[20][20] = {"literature", "scholar",
"enormous", "influence", "publication", "pioneer", "telephone",
"orchestra", "teacher", "member", "final", "phonograph",
"executed", "oldest", "people", "requiring", "screwdriver",
"intelligent", "different", "striking"};
int l[20]={10,7,8,9,11,7,9,9,7,6,5,10,8,6,6,9,11,11,9,8};  //length of words, they can take with library command .
char unique[15];                         
char quest[15];
char c;

do{       // loop for play again
lose=0;
found=0;
exit=5;
do{  // an algorithm for pull word that don't pull before.
do{

word = wordpull( );
if(word_database[0][word]!="NULL"){
a=1;
word_database[1][word]=word_database[0][word];
}
else{
                a=0;
}
}while(a==0);
word_database[0][word]="NULL";
}while(word_database[1][word]=="NULL");

strcpy(unique,word_database[1][word]); // chosen word assigning to unique array.

for (i=0; unique[i]!='\0'; i++){

      quest[i]=unique[i];     //unique assigning to quest
      unique[i]='*';         // and unique is being '*'
       
}
do{
c='\0';
printf("\nWord : %s \nPlease enter character : ",unique);  // Taking character from user
c=getche();   // This character assigning to C variable

for(i=0;i<l[word];i++){
               
if(quest[i]==c){               // Entering character are founding in word.
                unique[i]=quest[i];
                found++;                  // If there is , found will be increased.
}
}

                if(found==0){
                               lose++;              // If there isn't , lose will be increased.
                              
                }
               
                found=0;     //found is being 0 to using again another trial.
               
                if(strchr(unique,'*')=='\0'){              //If user found all character of word, exit will be equal to 6. ( Can be another value, not important )
                exit=6;
                printf("\nYou are win, word was %s , Congrulations.\n",unique);
    }
               
               
               
                if(lose==5){                     // If lose equal to 5, user haven't any chance, game over.
                for(i=0;i<l[word];i++){          // To appear correct word.
                unique[i]=quest[i];
               
}
                printf("\nGame Over ! Word was %s",unique);
}
   

}while(lose!=5 && exit!=6);              // If user haven't any chance or guess right, while loop will be end.

if(exit==6){                            // Guess right number equal to win variable.
                win++;
}

}while(lose!=5);

printf("\nYour score : %d",win);

return 0;
}

int wordpull( void ){
int word;
srand ( time ( NULL ) );
word = rand( ) % 20; // random word assigning to word variable.
return word;
}