Con éxito

Notas

Error

Session expiration Your session is going to expireClick here to extend

Presupuesto

Proyecto pequeño <800

Publicado el

27/11/18 16:19

Este proyecto ha expirado

Publica un proyecto similar y recibe ofertas sin compromiso alguno.

¡Publica un proyecto similar ahora!

Descripción

I need someone to correct and make better this project. It is about making minesweeper in C code. We have already made the code but there are some mistakes. Also we are not allowed to use global variables.

/* Includes */

#include

#include

#include

#include


/* Definitions */

#define BOARD_SIZE 5

#define TRUE 0

#define FALSE 1


/* Global Variables */

char board[BOARD_SIZE][BOARD_SIZE];

char game_board[BOARD_SIZE][BOARD_SIZE];

int lost = 0;


/* Including functions */

void buildBoard();

void setMinesInBoard();

int playGame();

void printBoard();

void checkForMines(int, int);

void printFullboard();

int checkForNearbyMines(int, int);




/* Main function, plays game until user have lost or won */

int main ()

{

    buildBoard();

    setMinesInBoard();

    do

    {

        playGame();

        printBoard();

    } while(lost != 1); // While the user hasn't lost, loop.

    printFullboard(); //Prints the board where the mines are

    return 0;

}


/* Build game board for user input */

void buildBoard() {

    int i, j;

    int row, col;

    

    // Assigns char '#' for all board elements

    

    for(i = 0; i < BOARD_SIZE; i++)

    {

        {

            for(j = 0; j < BOARD_SIZE; j++)

                game_board[i][j] = '#';

        }

    }

    //--------------------------------

    // Prints board

    //--------------------------------

    for(col = 0; col < BOARD_SIZE; col++)

    {

        printf("%d ", col + 1);

    }

    printf("\n\n");

    

    for(row = 0; row < BOARD_SIZE; row++)

    {

        for(col = 0; col < BOARD_SIZE; col++)

        {

            printf("%c ", game_board[row][col]);

        }

        printf(" %d ", row + 1);

        printf("\n");

    }

}

/* Void function that creates random places in the array for mines */

void setMinesInBoard()

{

    int i, j, random;

    srand(time(NULL));

        

    for (i = 0; i < 4; i++)

    {

        random = rand() % (BOARD_SIZE);

        board[random][i] = 'M';

    }

    for (j = 0; j < 2; j++)

    {

        random = rand() % (BOARD_SIZE);

        board[random][j] = 'M';

    }

    

}

/* Int function that plays the game */

int playGame()

{

    int r_selection = 0, c_selection = 0,

    nearbymines = 0, nearbymines2 = 0,

    nearbymines3 = 0, nearbymines4 = 0,

    nearbymines5 = 0, nearbymines6 = 0,

    nearbymines7 = 0, nearbymines8 = 0,

    i = 0;

    

    //----------------------------------------------

    // Recieves data from the user, first the row

    // number, then the column number...

    // There are other ways to do this, but

    // this one seemed easiest.

    //----------------------------------------------

    do {

        printf("Row--> ");

        scanf("%d", &r_selection);

        printf("Col--> ");

        scanf("%d", &c_selection);

        

    } while(r_selection < 1 || r_selection > BOARD_SIZE || c_selection < 1 || r_selection > BOARD_SIZE);

    

    /* Checks for any invalid input statements from user. */

    checkForMines(r_selection - 1, c_selection - 1);

    

    if(lost == 1)

        return -1;

    

    // Checks for nearby mines at every direction from user

    // input location. Assigns that location the number

    // of mines found nearby, updating the board.

    nearbymines = checkForNearbyMines(r_selection - 1, c_selection - 1);

    game_board[r_selection - 1][c_selection - 1] = (char)( ((int)'0') + nearbymines );

    

    if(nearbymines == 0)

    {

        if(c_selection != BOARD_SIZE)

        {

            i = 0;

            while(nearbymines == 0 && (c_selection - 1 + i) < BOARD_SIZE)

            {

                /* This is checking elements to the right */

                nearbymines = checkForNearbyMines(r_selection - 1, (c_selection - 1 + i));

                if(nearbymines != -1)

                {

                    game_board[r_selection - 1][(c_selection - 1) + i] = (char) ( ((int)'0') + nearbymines );

                    i++;

                }

            }

            if(r_selection != 1)

            {

                i = 0;

                while(nearbymines5 == 0 && (c_selection - 1 + i) < BOARD_SIZE && (r_selection - 1 - i) > 0)

                {

                    /* This is checking elements to the diagonal-uright */

                    nearbymines5 = checkForNearbyMines((r_selection - 1 - i), (c_selection - 1 + i));

                    if(nearbymines5 != -1)

                    {

                        game_board[(r_selection - 1) - i][(c_selection - 1) + i] = (char) ( ((int)'0') + nearbymines5);

                        i++;

                    }

                }

            }

            if(r_selection != BOARD_SIZE)

            {

                i = 0;

                while(nearbymines6 == 0 && (r_selection - 1 + i) < BOARD_SIZE && (c_selection - 1 + i) < BOARD_SIZE )

                {

                    // This is checking elements to the diagonal-dright

                    nearbymines6 = checkForNearbyMines((r_selection - 1 + i), (c_selection - 1 + i));

                    if(nearbymines != -1)

                    {

                        game_board[(r_selection - 1) + i][(c_selection - 1) + i] = (char) ( ((int)'0') + nearbymines6);

                        i++;

                    }

                }

            }

        }

        

        if(r_selection != BOARD_SIZE)

        {

            i = 0;

            while(nearbymines2 == 0 && (r_selection - 1 + i) < BOARD_SIZE)

            {

                // This is checking elements heading down

                nearbymines2 = checkForNearbyMines((r_selection - 1 + i), c_selection - 1);

                if(nearbymines2 != -1)

                {

                    game_board[(r_selection - 1) + i][c_selection - 1] = (char) ( ((int)'0') + nearbymines2 );

                    i++;

                }

            }

            

            if(c_selection != BOARD_SIZE)

            {

                i = 0;

                while(nearbymines7 == 0 && (r_selection - 1 + i) < BOARD_SIZE && (c_selection - 1 - i) > 0)

                {

                    /* This is checking elements to the diagonal-dleft */

                    nearbymines7 = checkForNearbyMines((r_selection - 1 + i), (c_selection - 1 - i));

                    if(nearbymines != -1)

                    {

                        game_board[(r_selection - 1) + i][(c_selection - 1) - i] = (char) ( ((int)'0') + nearbymines7);

                        i++;

                    }

                }

            }

        }

        

        if(r_selection != 1)

        {

            i = 0;

            while(nearbymines3 == 0 && (r_selection - i) > 0)

            {

                /* This is checking elements heading up */

                nearbymines3 = checkForNearbyMines((r_selection - 1 - i), c_selection - 1);

                if(nearbymines3 != -1)

                {

                    game_board[(r_selection - 1) - i][c_selection - 1] = (char) ( ((int)'0') + nearbymines3 );

                    i++;

                }

            }

            if(c_selection != BOARD_SIZE)

            {

                while(nearbymines8 == 0 && (c_selection - 1 - i) > 0 && (r_selection - 1 - i) > 0)

                {

                    /* This is checking elements to the diagonal-uleft */

                    nearbymines8 = checkForNearbyMines((r_selection - 1 - i), (c_selection - 1 - i));

                    if(nearbymines8 != -1)

                    {

                        game_board[(r_selection - 1) - i][(c_selection - 1) - i] = (char) ( ((int)'0') + nearbymines8);

                        i++;

                    }

                }

            }

        }

        

        if(c_selection != 1)

        {

            i = 0;

            while(nearbymines4 == 0 && (c_selection - i) > 0)

            {

                /* This is checking elements to the left */

                nearbymines4 = checkForNearbyMines(r_selection - 1, (c_selection - 1 - i));

                if(nearbymines4 != -1)

                {

                    game_board[r_selection - 1][(c_selection - 1) - i] = (char) ( ((int)'0') + nearbymines4 );

                    i++;

                }

            }

        }

    }

    return 0;

}


//Prints the game board with rows and columns

void printBoard()

{

    int row, col;

    

    system("cls");

    for(col = 0; col < BOARD_SIZE; col++)

        printf("%d ", col + 1);

    

    printf("\n\n");

    for(row = 0; row < BOARD_SIZE; row++)

    {

        for(col = 0; col < BOARD_SIZE; col++)

        {

            printf("%c ", game_board[row][col]);

        }

        printf(" %d ", row + 1);

        printf("\n");

    }

}



//Prints if loser hit a mine, that it has lost

void checkForMines(int r_select, int c_select)

{

    if(board[r_select][c_select] == 'M')

    {

        printf("\nYou've hit a mine! You lose!\n");

        getchar(); getchar();

        lost = 1;

    }

}


/* Printw the full board showing where the mines aremines */

void printFullboard()

{

    int row, col;

    

    system("cls");

    for(col = 0; col < BOARD_SIZE; col++)

        printf("%d ", col + 1);

    

    printf("\n");

    for(row = 0; row < BOARD_SIZE; row++)

    {

        for(col = 0; col < BOARD_SIZE; col++)

        {

            printf("%c ", board[row][col]);

        }

        printf(" %d ", row + 1);

        printf("\n");

    }

}


// Another function to find nearby mines

int checkForNearbyMines(int r_select, int c_select)

{

    int nearby_mine_count = 0;

    

    if(board[r_select][c_select] == 'M')

    {

        return -1;

    }

    // Check for mines below and to the right.

    if(r_select < (BOARD_SIZE - 1) && c_select < (BOARD_SIZE - 1))

    {

        // Check for mine below

        if(board[r_select + 1][c_select] == 'M')

            nearby_mine_count++;

        // Check for mine to the right.

        if(board[r_select][c_select + 1] == 'M')

            nearby_mine_count++;

        // Check for mine diagonal-dright.

        if(board[r_select + 1][c_select + 1] == 'M')

            nearby_mine_count++;

        

        // Check whether the columns to the left can be checked

        if(c_select != 0)

        {

            // Check for mine diagonal-dleft

            if(board[r_select + 1][c_select - 1] == 'M')

                nearby_mine_count++;

            // Check for mine to the left

            if(board[r_select][c_select - 1] == 'M')

                nearby_mine_count++;

        }

        // Check whether the rows above can be checked

        if(r_select != 0)

        {

            // Check for mine above

            if(board[r_select - 1][c_select] == 'M')

                nearby_mine_count++;

            // Check for mine diagonal-uright

            if(board[r_select - 1][c_select + 1] == 'M')

                nearby_mine_count++;

            // Check whether columns to the left can be checked

            if(c_select != 0)

            {

                // Check for mine diagonal-uleft

                if(board[r_select - 1][c_select - 1] == 'M')

                    nearby_mine_count++;

            }

        }

    }

    // Check if selection is in last row

    if(r_select == (BOARD_SIZE - 1) && c_select != (BOARD_SIZE - 1))

    {

        // Check for mine above

        if(board[r_select - 1][c_select] == 'M')

            nearby_mine_count++;

        // Check for mine diagonal-uright

        if(board[r_select - 1][c_select + 1] == 'M')

            nearby_mine_count++;

    }

    // Check if selection is in last column

    if(c_select == (BOARD_SIZE - 1) && r_select != (BOARD_SIZE - 1))

    {

        // Check for mine to the left

        if(board[r_select][c_select - 1] == 'M')

            nearby_mine_count++;

        // Check for mine diagonal-dleft

        if(board[r_select + 1][c_select - 1] == 'M')

            nearby_mine_count++;

    }

    // Check whether selection is last in element

    if(r_select == (BOARD_SIZE - 1) && c_select == (BOARD_SIZE - 1))

    {

        // Check for mine to the left

        if(board[r_select][c_select - 1] == 'M')

            nearby_mine_count++;

        // Check for mine diagonal-dleft

        if(board[r_select - 1][c_select - 1] == 'M')

            nearby_mine_count++;

        // Check for mine above

        if(board[r_select - 1][c_select] == 'M')

            nearby_mine_count++;

    }

    

    return nearby_mine_count;

}