-2

I want to create a game minesweeper using visual studio. Project visual c++ Win32 Console Application. Source file C++ file(.cpp) This is my source code

#define _CRT_SECURE_NO_WARNINGS

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

int menu()//menampilkan menu
{
    int x;
    printf("\nThe Minesweeper for You\n\n");
    printf("<<Main Menu>>\n\n");
    printf("1.Start\n");
    printf("2.Exit\n\n");
    printf("Masukkan pilihan : ");
    scanf("%i", &x);
    while ((x<1) || (x>2))
    {
        printf("Pilihan Invalid !\n");
        printf("Masukkan pilihan : \n");
        scanf("%i", &x);
    }
    printf("\n");
    return x;
}

void cek(int board[15][15], int revealed[15][15], int x, int y)//mengecek saat board[x][y] berisi 0
{
    //Kamus lokal
    int i, j;
    revealed[x][y] = 1;

    //Algoritma
    for (i = x - 1; i<x + 2; i++)
    {
        for (j = y - 1; j<y + 2; j++)
        {
            if (i >= 0 && j >= 0 && i<15 && j<15)
            {
                if (revealed[i][j] == 0 && board[i][j] != 0)
                {
                    revealed[i][j] = 1;
                }
            }
        }
    }
    for (i = x - 1; i<x + 2; i++)
    {
        for (j = y - 1; j<y + 2; j++)
        {
            if (i >= 0 && j >= 0 && i<15 && j<15)
            {
                if (revealed[i][j] == 0 && board[i][j] == 0)
                {
                    cek(board, revealed, i, j);
                }
            }
        }
    }


}



int klik(int board[15][15], int revealed[15][15], char kondisi[15][15])
{
    //Kamus lokal
    int a, b, x, y, q, z;

    //Algoritma
    do {
        printf("X: ");
        scanf("%i", &y);
        printf("y: ");
        scanf("%i", &x);
    } while (x<1 || y<1 || x>15 || y>15);

    x--; y--;//konfersi ke matriks yg dari 0 sampai 14
    if (kondisi[x][y] == 'F')
    {
    }
    else if (board[x][y] == 9)
    {
        for (a = 0; a<15; a++)for (b = 0; b<15; b++)
        {
            if (board[a][b] == 9)revealed[a][b] = 1;
        }
        z = 1;

    }
    else if (board[x][y] == 0)
    {
        revealed[x][y] = 1; //revealed 1 terbuka
        cek(board, revealed, x, y);
    }
    else{ revealed[x][y] = 1; }

    for (a = 0; a<15; a++)
    {
        for (b = 0; b<15; b++)
        if (revealed[a][b] == 1)
        {
            switch (board[a][b])
            {
            case 0: kondisi[a][b] = '0'; break;
            case 1: kondisi[a][b] = '1'; break;
            case 2: kondisi[a][b] = '2'; break;
            case 3: kondisi[a][b] = '3'; break;
            case 4: kondisi[a][b] = '4'; break;
            case 5: kondisi[a][b] = '5'; break;
            case 6: kondisi[a][b] = '6'; break;
            case 7: kondisi[a][b] = '7'; break;
            case 8: kondisi[a][b] = '8'; break;
            case 9: kondisi[a][b] = '#'; break;
            }

        }

    }

    q = jumlhbuka(revealed, kondisi);

    if (z == 1){ return 1; }
    else if (q >= 200){ z = 2; }
    else{ z = 0; }
    return z;

}

int jumlhbuka(int revealed[15][15], char kondisi[15][15])
{
    int a, b, q = 0;
    for (a = 0; a<15; a++)
    {
        for (b = 0; b<15; b++)
        {
            if (revealed[a][b] == 1 && kondisi[a][b] != '#'){ q = q + 1; }
        }
    }
    return q;
}


void flag(char kondisi[15][15], int revealed[15][15])
{
    //Kamus lokal
    int x, y;
    //Algoritma
    do {
        printf("X: ");
        scanf("%i", &y);
        printf("y: ");
        scanf("%i", &x);
    } while (x<1 || y<1 || x>15 || y>15);
    if (revealed[x - 1][y - 1] == 0)//board = 0 tertutup
    {
        kondisi[x - 1][y - 1] = 'F';

    }
}

void rflag(char kondisi[15][15], int revealed[15][15])
{
    //Kamus lokal
    int x, y;

    //Algoritma
    do {
        printf("X: ");
        scanf("%i", &y);
        printf("y: ");
        scanf("%i", &x);
    } while (x<1 || y<1 || x>15 || y>15);

    if (kondisi[x - 1][y - 1] == 'F')       //jika kondisinya sedang flag
    {
        kondisi[x - 1][y - 1] = '_';
    }
}

void random(int A[15][15])
{
    //Kamus lokal
    srand((unsigned)time(NULL));
    int a, b, c = 0;

    //Algoritma
    for (a = 0; a<15; a++){ for (b = 0; b<15; b++){ A[a][b] = 0; } }
    while (c<25)
    {
        for (a = 0; a<15 && c<25; a++)
        {
            for (b = 0; b<15 && c<25; b++)
            {
                if (A[a][b] != 9)
                {
                    A[a][b] = rand() % 10;
                    if (A[a][b] == 9){ c++; }
                    else{ A[a][b] = 0; }
                }
            }
        }
    }
}


void PasangBom(int board[15][15])//mengisi board dengan 0-9. 9 maka berisi bom dan 0 maka kosong
{
    //Kamus lokal
    int x, y, a, i, j; a = 25;

    //Algoritma
    random(board);//isi board dengan 9 sebagai bom dan yang lain 0;
    for (x = 0; x<15; x++)
    for (y = 0; y<15; y++)
    if (board[y][x] == 9)
    {
        if ((y - 1) >= 0)
        if (board[y - 1][x] != 9)
            board[y - 1][x]++;
        if ((y - 1) >= 0 && (x - 1) >= 0)
        if (board[y - 1][x - 1] != 9)
            board[y - 1][x - 1]++;
        if ((x - 1) >= 0)
        if (board[y][x - 1] != 9)
            board[y][x - 1]++;
        if ((y + 1) < 15)
        if (board[y + 1][x] != 9)
            board[y + 1][x]++;
        if ((y + 1) < 15 && (x + 1) < 15)
        if (board[y + 1][x + 1] != 9)
            board[y + 1][x + 1]++;
        if ((x + 1) < 15)
        if (board[y][x + 1] != 9)
            board[y][x + 1]++;
        if ((y - 1) >= 0 && (x + 1) < 15)
        if (board[y - 1][x + 1] != 9)
            board[y - 1][x + 1]++;
        if ((y + 1) < 15 && (x - 1) >= 0)
        if (board[y + 1][x - 1] != 9)
            board[y + 1][x - 1]++;
    }

}



void Tampilkar(char x[15][15])
{
    //Kamus lokal
    int a = 0;
    int b = 0;

    //Algoritma
    printf(" y\\x");
    while (b<15)
    {
        if (b<9){ printf("_%i__", b + 1); }
        else{ printf("%i__", b + 1); }
        b = b + 1;
    }
    printf("\n");
    while (a<15)
    {
        b = 0;
        if (a<9){ printf(" %i", a + 1); }
        else{ printf("%i", a + 1); }
        while (b<15){ printf("_|_%c", x[a][b]); b = b + 1; }
        printf("_|\n");
        a = a + 1;
    }
    printf("\n");
}

void Tampilang(int x[15][15])
{
    //Kamus lokal
    int a = 0;
    int b = 0;

    //Algoritma
    printf(" y\\x");
    while (b<15)
    {
        if (b<9){ printf("_%i__", b + 1); }
        else{ printf("%i__", b + 1); }
        b = b + 1;
    }
    printf("\n");
    while (a<15)
    {
        b = 0;
        if (a<9){ printf(" %i", a + 1); }
        else{ printf("%i", a + 1); }
        while (b<15){ printf(" | %i", x[a][b]); b = b + 1; }
        printf(" |\n");
        a = a + 1;
    }
    printf("\n");
}

void start()
{
    //Kamus lokal
    int a, b, x, y, z, jbuka;
    int jk = 0;//jumlah klik,
    int dead = 0;//0 masih main, 1 kalah, 2 menang , 4 keluar(stop)
    int board[15][15];
    int revealed[15][15];
    char kondisi[15][15];

    //Algoritma
    PasangBom(board);
    for (a = 0; a<15; a++)for (b = 0; b<15; b++)revealed[a][b] = 0;//mengkosongkan parameter buka
    for (a = 0; a<15; a++)for (b = 0; b<15; b++)kondisi[a][b] = '_';//mengkosongkan tampilan awal
    Tampilkar(kondisi);
    while (dead == 0)
    {
        do{
            printf("1.Klik\n");
            printf("2.Flag\n");
            printf("3.Remove Flag\n");
            printf("4.Stop\n");
            printf("Masukkan pilihan : ");
            scanf("%i", &z);
        } while (z<1 || (z>4 && z != 2357));
        switch (z)
        {
        case 1:
            dead = klik(board, revealed, kondisi);
            jk++;
            break;

        case 2:
            flag(kondisi, revealed);
            jk++;
            break;

        case 3:
            rflag(kondisi, revealed);
            jk++;
            break;

        case 4:
            system("cls");
            dead = 4;
            break;

        case 2357: //Cheat untuk melihat seluruh isi bom di minesweeper bila user memasukkan angka 2357
            Tampilang(board);
            break;
        }
        if (z != 4)
            Tampilkar(kondisi);
    }


    jbuka = jumlhbuka(revealed, kondisi);
    if (dead == 1)
    {
        printf("\nANDA MENGENAI BOMB!!! \n\n");
    }
    else if (dead == 2)
    {
        printf("\nANDA MENANG!!! :)\n\n");

    }

    system("cls");

}


int main()
{
    int a, x;
    a = 1;

    while (a == 1)
    {
        printf("========================================\n");
        printf("  #     # # #   # ##### ##### SWEEPER\n");
        printf("  ##   ## # ##  # #     #     SWEEPER\n");
        printf("  # # # # # # # # ##### ##### SWEEPER\n");
        printf("  #  #  # # #  ## #         # SWEEPER\n");
        printf("  #     # # #   # ##### ##### SWEEPER\n");
        printf("========================================\n\n");
        x = menu();
        switch (x){
        case 1:
            system("cls");
            start();
            break;

        case 2:
            system("cls");
            a = 0;
            printf("\nThanks for Playing Our Game!!!\n\n");
        }
    }
}

And i get this error Error C3861: 'jumlhbuka': identifier not found I try googling how to fix C3861 snd try the solution still failed :( Please help me to fix it, i hope someone can fix it Thanks guys :)

  • Put `int jumlhbuka(int revealed[15][15], char kondisi[15][15]);` above `int klik(int board[15][15], int revealed[15][15], char kondisi[15][15]) {` – drescherjm Jun 02 '19 at 13:27
  • or move the definition of `jumlhbuka` before the definition of `klik` – bruno Jun 02 '19 at 13:29
  • 2
    I think we can close off-topic "simple typographical error." – bruno Jun 02 '19 at 13:34
  • @drescherjm after running i get error Run-time Check Failure #3 - The variable 'z' is being used without being initialized – Fahim 10969 Jun 02 '19 at 13:36
  • 1
    @Fahim10969 That's a different error entirely. –  Jun 02 '19 at 13:38
  • @Fahim10969 this is an other problem, I encourage you to delete that question (simple error/fixing) and to open a new one with enough details – bruno Jun 02 '19 at 13:39
  • @bruno Maybe resolved in a way that's not helpful to other people (maybe, could still be helpful), but I don't think it's a "simple typographical error." –  Jun 02 '19 at 13:40
  • 1
    @Chipster I do not think, just to move a definition before an other or add a declaration is not complicated – bruno Jun 02 '19 at 13:40
  • ***The variable 'z' is being used without being initialized*** The debugger told you exactly what this new problem was. There are code paths in your function where `z` is never set a value but then used in an if () and returned. – drescherjm Jun 02 '19 at 13:46

1 Answers1

1

Just add a prototype line of the function to the top of the file:

int jumlhbuka(int revealed[15][15], char kondisi[15][15]);

That way, it can find jumlhbuka() before the actual implementation. All together, it will look something like this:

#define _CRT_SECURE_NO_WARNINGS

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

// added line. Should do the trick
int jumlhbuka(int revealed[15][15], char kondisi[15][15]);

int menu()//menampilkan menu
{
// ...

Or, alternatively, you can switch the order of jumlhbuka() and klik().