COMS 3101-3: Programming Languages (C)

For your class project, you will be designing the Minesweeper game in C.

Your program will give 3 choices to the user:

  1. Easy (8*8 grid, 6 mines)

  2. Medium (12*12 grid, 14 mines)

  3. Hard (16*16 grid, 25 mines)

Based on the user's choice, your program will dynamically create the grid. It will randomly place mines in the grid (not seen by the user) using the rand() function.

The grid will be displayed to the user.

The user will be able to do the following:

  1. Open a location

  2. Mark a location as flagged

  3. Unmark a flagged location

The user will input (x,y) position with his choice.

By opening a location, the program will expose the number of mines in its neighborhood (surrounding 8 locations). If the user opens a location that has no (0) neighboring mines, then the program should (recursively) open its neighbors. In other words, an opened location with number 0 should always have all of its neigbors opened.

If the user opens a location that contains a mine, then he/she loses the game.

The user may mark a location as a viable location for a mine by flagging it. He/she may unflag the location at any time. An opened location cannot be changed to flagged.

Everytime the user makes a choice, the program should compute and print the updated status of the grid on the screen. You may use your own (user-friendly) conventions for your output as well as the input.

No gui is required but a fancy console-based interface will fetch you extra credit. The main focus should be on the game engine rather than the interface.

The user wins the game when the mine locations have been flagged correctly and all other locations have been opened.

Finally, your program should print the time take taken by the user to solve the game. Use the gettimeofday() function. The time calculated should be the time elapsed between the moment the grid is initialized and the end of the game.

        struct timeval time1, time2;
        int diffsec;
        gettimeofday(&time1, NULL);

        /* computation */

        gettimeofday(&time2, NULL);
        diffsec = time2.tv_sec - time1.tv_sec;
        printf("elapsed time: %d", diffsec);

Happy Coding!