Tugas Game Tebakan (C++)

>>> Source Code <<<
Tugas Buat Game Tebakan Bahasa Inggris (C++)

#include <stdio.h>
#include <string.h>
#include <time.h>

#define FILENAME "database.txt"       
#define MAX_LEN 80                 
#define WON_MSG "Anda Menang...!\n"      
#define LOST_MSG "Anda Kalah...!\n"     


void menu(void);                   
void display_contact(void);        
void draw_gallow(int, int, int);   
void draw_word(void);              
void draw_alphabet(void);          
void prompt(void);                 
void check_win(void);              
void update_wrong_right(void);     
void random_word(void);            


int diff;                         
char guess;                       
char display[MAX_LEN];             
char display_alpha[27];            
int section;                      
char word[MAX_LEN];                
int chances;                       
int correct;                       
int wrong;                         
int blanks;                        
int old_blanks;                   
int len;                           
int i;                             

int main()
{
    system("CLS");               
    menu();                        

    random_word();               
  
 
    len = strlen(word);
    for (i = 0; i < len; i++) display[i] = '-';
    display[i] = '\0';
  
 
    strcpy(display_alpha, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  

    correct = 0;
    wrong = 0;
    old_blanks = len;
  

    while (1) {
        system("CLS");                  
        switch (diff) {                 
            case 0:                      
                section = wrong * 1;
                break;
            case 1:
                section = wrong * 2;
                break;
            case 2:
                section = wrong * 3;
                break;
            default:
                printf("Error: primary loop.\n");
        }
        display_contact();
        draw_gallow(section, 30, 1);
        draw_word();
        draw_alphabet();
        check_win();
        prompt();
        update_wrong_right();
    }


    system("PAUSE");
    return(0);
}


void menu(void)
{
    char choice;
   
    while (1) {
        printf("                       ********** GAME TEBAKAN - NRP GANJIL**********\n");
        printf("                ********** Tebakan bahasa inggris binatang (animal) **********\n\n");
        printf("                               >>>Ristu Sadewa<<<\n");      
        printf("                                >>>7410030855<<<\n\n");      
        printf("\tPilih level yang anda inginkan : \n\n");
        printf("\t<E>\t-\tEasy / Mudah\n");
        printf("\t<M>\t-\tMedium / Menengah\n");
        printf("\t<H>\t-\tHard / Sulit\n");
        printf("\t<Q>\t-\tQuit / Keluar\n");
        printf("\n\n");
        printf("Masukkan pilihan anda : ");
        choice = getch();
  
        switch (choice) {
            case 'q':    
            case 'Q':
                exit(0);
            case 'e':    
            case 'E':
                diff = 0;
                chances = 12;
                break;
            case 'm':    
            case 'M':
                diff = 1;
                chances = 8;
                break;
            case 'h':    
            case 'H':
                diff = 2;
                chances = 4;
                break;
            default:
                system("CLS");
                continue;
        }
        break;
    }
}


void display_contact(void)
{
    printf("D3-PJJ\n\n\n");
}


void draw_gallow(int section, int x, int y)
{
    int i;
  

    char v_line  = (char) 186; 
    char h_line = (char) 205; 
    char b_cross = (char) 202;
    char t_cross = (char) 203;
    char head    = (char) 1;
  

    for (i = 0; i < y; i++) printf("\n");
  
  
    if (section >= 4) {
        for (i = 0; i < x; i++) printf(" ");
        printf("[");
        for (i = 0; i < 4; i++) printf("%c", h_line);
        printf("%c", t_cross);
        for (i = 0; i < 8; i++) printf("%c", h_line);
        if (section >= 6) {
             for (i = 0; i < 8; i++) printf("%c", h_line);
             printf("]");
        }
        printf("\n");
    } else {
        printf("\n");     
    }
  
    if (section >= 3) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        printf(" ");
        if (section >= 5) {
            printf("//");
            for (i = 0; i < 9; i++) printf(" ");
            if (section >= 7) {
                 printf(":");
            }
        }
        printf("\n");
    } else {
        printf("\n");
    }

    if (section >= 3) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        if (section >= 5) {
            printf("//");
            for (i = 0; i < 10; i++) printf(" ");
            if (section >= 8) {
                printf(":");
            }
        }
        printf("\n");
    } else {
        printf("\n");
    }

  
    if (section >= 3) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        if (section >= 5) {
            printf("/");
            for (i = 0; i < 11; i++) printf(" ");
            if (section >= 9) {
                printf("%c", head);
            }
        }
        printf("\n");
    } else {
        printf("\n");
    }  

    if (section >= 3) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        for (i = 0; i < 11; i++) printf(" ");
        if (section >= 10) {
            printf("/A\\");
        }
        printf("\n");
    } else {
        printf("\n");
    }  

  
    if (section >= 2) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        for (i = 0; i < 12; i++) printf(" ");
        if (section >= 11) {
            printf("V");
        }
        printf("\n");
    } else {
        printf("\n");
    }

    if (section >= 2) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        for (i = 0; i < 11; i++) printf(" ");
        if (section >= 12) {
            printf("/ \\");
        }
        printf("\n");
    } else {
        printf("\n");
    }

    if (section >= 2) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        printf("\n");
    } else {
        printf("\n");
    }

    if (section >= 2) {
        for (i = 0; i < x; i++) printf(" ");
        for (i = 0; i < 5; i++) printf(" ");
        printf("%c", v_line);
        printf("\n");
    } else {
        printf("\n");
    }

  
    if (section >= 1) {
        for (i = 0; i < x; i++) printf(" ");
        printf("[");
        for (i = 0; i < 4; i++) printf("%c", h_line);
        printf("%c", b_cross);
        for (i = 0; i < 10; i++) printf("%c", h_line);
        printf("]");
        printf("\n");
    } else {
        printf("\n");     
    }
  
    printf("\n");

}


void draw_word(void)
{
    int length;       
    int flag = 0;     
    int length2;      
    int width = 80;   
    int i;            

 
 
    for (i = 0; word[i] != '\0'; i++) {
        if (word[i] == guess) {
            display[i] = guess;
        }
    }
  

    length = strlen(display);
    length2 = length * 2;     
    width -= length2;
    width /= 2;
  

    for (i = 0; i < width; i++) printf(" ");
    for (i = 0; display[i] != '\0'; i++) printf("%c ", display[i]);
    printf("\n\n");
}


void draw_alphabet(void)
{
    int i;
   

    for (i = 0; display_alpha[i] != '\0'; i++) {
        if (tolower(display_alpha[i]) == guess) {
            display_alpha[i] = '_';
        }  
    }
  

    printf("              ");
  
    for (i = 0; display_alpha[i] != '\0'; i++) printf("%c ", display_alpha[i]);
    printf("\n\n");
}


void check_win(void)
{
    char choice;
   
    if (correct == strlen(word)) {         // Pemain menang
        printf("%s\n", WON_MSG);
        printf("Apakah anda ingin bermain dengan level yang lebih tinggi(Y/N)? ");
        choice = getch();
      
        switch (choice) {
            case 'y':       
            case 'Y':
                main();
                break;
            case 'n':       
            case 'N':
                exit(0);
                break;
            default:
                printf("Error: in check_win().\n");
        }
    } else {
        if (chances <= 0) {                // pemain kalah
            printf("%s\n", LOST_MSG);
            printf("Jawabnya adalah : %s\n", word);
            printf("Apakah anda ingin mencoba kembali (Y/N)? ");
            choice = getch();
          
            switch (choice) {
                case 'y':       
                case 'Y':
                    main();
                    break;
                case 'n':       
                case 'N':
                    exit(0);
                    break;
                default:
                    printf("Error: in check_win().\n");
            }
        }
    }
}


void prompt(void)
{
    printf("Level:   ");
    switch (diff) {
        case 0:
            printf("Easy\n");
            break;
        case 1:
            printf("Medium\n");
            break;
        case 2:
            printf("Hard\n");
            break;
        default:
            printf("Error: in prompt(), displaying difficulty.\n");
    }
    printf("Chances: %d\n\n", chances);
    printf("Guess letter: ");
    guess = getch();
}


void update_wrong_right(void)
{
    int i;
    int blanks = 0;        
  

    for (i = 0; word[i] != '\0'; i++) {
        if (word[i] == guess) {
            display[i] = guess;
        }
    }
  

    for (i = 0; display[i] != '\0'; i++) {
        if (display[i] == '-') blanks++;  
    }
  

    if (blanks == old_blanks) {
        wrong++;
        chances--;         
    }
  

  
    if ((old_blanks-1) == blanks) {
        correct++;
    } else if ((old_blanks-2) == blanks) {
        correct += 2;
    } else if ((old_blanks-3) == blanks) {
        correct += 3;
    } else if ((old_blanks-4) == blanks) {
        correct += 4;  
    } else if ((old_blanks-5) == blanks) {
        correct += 5;  
    } else if ((old_blanks-6) == blanks) {
        correct += 6;  
    } else if ((old_blanks-7) == blanks) {
        correct += 7;  
    } else if ((old_blanks-8) == blanks) {
        correct += 8;  
    }
  

    old_blanks = blanks;
}


void random_word(void)
{
    int word_count = 0;   
    char buff[MAX_LEN];   
    int i = 0;
    int x = 0;
  
    FILE *fp;
  
    fp = fopen(FILENAME, "r");
  
    if (fp == NULL) {
        fprintf(stderr, "Error: opening file %s\n", FILENAME);
        exit(1);
    }
  

    while (fgets(buff, MAX_LEN, fp) != NULL) word_count++;
    fclose(fp);
  
    fp = fopen(FILENAME, "r");
    // mengacak nomorss
    srand(time(NULL));
    x = rand() % word_count;

    while (fgets(word, MAX_LEN, fp) != NULL) {
          i++;
          if (i == x) break;
    }

    word[strlen(word)-1] = '\0';

    fclose(fp);
}
Setelah itu, untuk memanggil database untuk library tebakannya, buatlah sebuah file berekstensi .txt
dan kasih nama file database.txt. jangan lupa, disimpan 1 folder dengan file .c nya..
File database tersebut digunakan untuk menyimpan soal.

Dah, gitu aja, aku mau bobok dulu... ngantuk coy

6 comments

Bisa om...
kemarin itu saya pake buat tugas dari dosen..
Situ pake C++ V.Berapa?

Reply

isi file database.txt nya gimana ya?

Reply

untuk database.txt nya isikan kata-kata untuk soalnya.
misalnya :

rumah
mobil
patung
kopi
asbak

dan lain-lain,

Thanks dah mampir...

Reply

Post a Comment