Permainan Tebak Kata Berbasis C++

Permainan ini bernama Blindwords. Terinspirasi oleh permainan Hangman, namun tidak ada animasi stickman digantung yang legendaris itu. Blindwords hanya mengimplementasi tebak katanya saja. Program ini dibuat untuk menyelesaikan tugas akhir semester satu mata kuliah Algoritma dan Struktur Data I jurusan Ilmu Komputer dan Sistem Informasi, Sekolah Vokasi Universitas Gadjah Mada.

Kode Sumber

/*

    BLINDWORDS
    -------------
    You don't have permission to reuse this source code in academic or commercial purposes.
    (C) 2016

    Made by:
        Salim Romadhon
        Nanda Septyaningrum
        Iva Faradilla F
        Arum Puspitasari  
        Nur Haryani
        Diyan Ayu Hari Murni T
        Aji Setyo A

*/

#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>

using namespace std;

string clean(string s) {    // remove repeating character(s) in a string
    string out = "";
    for (int i = 0; (unsigned) i < s.length(); i++) {
        bool exist = 0;
        for (int j = i+1; (unsigned) j < s.length(); j++) {
            if (s[i] == s[j]) {
                exist = 1;
            }
        }
        if (exist == 0) out += s[i];
    }
    return out;
}

bool doesExist(char c, string s) {
    bool exist = 0;
    for (int i = 0; (unsigned) i < s.length() && exist == 0; i++) {
        if (c == s[i]) {
            exist = 1;
        }
    }
    return exist;
}

string addChar(char c, string a, string b) {        
    for (int i = 0; (unsigned) i < b.length(); i++) {
        if (c == b[i]) {
            a[i] = c;
        }
    }
    return a;
}

string makeSpace(int x) {
    string s = "";
    for (int i = 0; i < x; i++) {
        s += "*";
    }
    return s;
}

void game() {

    // source: http://stackoverflow.com/questions/1647557/ifstream-how-to-tell-if-specified-file-doesnt-exist
    ifstream myFile("BLINDWORDS.txt");
    if (myFile.fail()) {
        cout << "THE FILE `BLINDWORDS.txt` DOESN'T EXIST. PLEASE MAKE IT FIRST BEFORE CONTINUING TO PLAY THIS GAME." << endl << endl
             << "-------------"                                                                                      << endl << endl
             << "Make some lines like the example below."                                                            << endl << endl
             << "WORD\tclue" << endl << "WORD\tthis-is-a-clue" << endl << "WORD\tanother-clue"                                                 << endl;
    } else {

        cout << endl << "WELCOME TO BLINDWORDS!\n\nYou have to guess characters to be a nice word based on the clues given.\nYou have 5 times tolerance at each word if you wrong.\nPlease use your `CAPS LOCK`.\nHappy playing. Good luck :)" << endl << endl;

        // source: http://stackoverflow.com/questions/17392218/how-to-parse-string-file-txt-into-array-with-c
        std::ifstream in("BLINDWORDS.txt");

        // initialize the vector from the values in the file:
        std::vector<string> words(( std::istream_iterator<string>(in)), std::istream_iterator<string>() );

        string s;

        int score     = 0;
        bool gameOver = 0;

        for (int i = 0, number = 1; (unsigned) i < words.size() && gameOver == 0; i += 2) {

            cout << "-------------"    << endl
                 << number++           << endl
                 << "-------------"    << endl                        // display the question number
                 << "SCORE: " << score << endl
                 << "-------------"    << endl
                                       << endl;

            bool match   = 0;                                         // stop when word is complete

            string space = makeSpace(words[i].length());              // make a space to fill

            int wrongs   = 0;                                         // count wrongs

            cout << words[i+1] << endl << endl;                       // the clue

            cout << space << endl;                                    // the space, stars

            while (wrongs < 6 && match == 0) {
                cin >> s;

                s = clean(s);

                for (int j = 0; (unsigned) j < s.length(); j++) {
                    if (doesExist(s[j], words[i]) == 1) {
                        space = addChar(s[j], space, words[i]);
                    } else {
                        wrongs++;
                    }
                }

                cout << space << endl;

                if (space == words[i]) {
                    match = 1;
                    score += 10-2*wrongs;
                    cout << endl << endl;
                } else if (wrongs >= 6) {
                    gameOver = 1;
                    cout << endl << endl;
                }
            }
        }

        cout << "GAME OVER!"       << endl
             << "-------------"    << endl
             << "SCORE: " << score << endl
             << endl               << endl;

    }
}

int main() {
    game();
    return 0;
}

Cara Penggunaan

Gunakan perangkat lunak IDE seperti Dev-C++, Code::Blocks, atau Geany untuk melakukan bangun (compile and build) kode program C++ tersebut. Setelah itu, buat berkas BLINDWORDS.txt pada direktori (folder) di mana berkas aplikasi yang sudah dibangun berada. Berkas ini berfungsi untuk menyimpan pertanyaan yang akan diproses oleh program Blindwords.

Setelah semua langkah di atas dilakukan, Blindwords dapat dijalankan melalui IDE, atau klik ganda pada berkas .exe hasil bangun, atau jika Anda menggunakan Linux ketikkan ./<application-name> dari terminal.

Isi Berkas BLINDWORDS.txt

Berikut ini adalah contoh isi berkas yang valid.

CPLUSPLUS       bahasa-pemrograman
KOMPUTER        mesin-canggih
PROSESOR        otak
CPU             komputer
KEYBOARD        ketik
MOUSE           pointer
PRINTER         cetak
SPEAKER         suara
MOTHERBOARD     papan
LAN             kabel
ROUTER          jaringan
BUS             jaringan
RAID            disk
SATA            disk
ATA             disk
IDE             DevC++
DATABASE        basis
FORMULA         fungsi
PROGRAM         fungsi
ALGORITMA       cara
COMPILER        susun
PERULANGAN      loop
PERCABANGAN     if
OVERCLOCK       prosesor
PLATTER         disk
HARDDISK        disk
SSD             disk
SSHD            disk
RAM             memori
BOOLEAN         y/t

Lisensi

Program ini boleh digunakan untuk dipelajari namun tidak untuk diplagiasi sebagai pemenuhan tugas-tugas akademik.

Pembaharuan 8 September 2018. Melihat lagi pos ini, ternyata kode ini di beberapa bagian kurang efektif. Silakan cari dan perbaiki sendiri :)

⚑ Tanggapan