Thread: Minesweeper

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Minesweeper

    Im not done with the game yet, but so far when it prints, the numbering of the rows isnt lined up properly. Ive tried several different ways, but I cant seem to figure it out, yet it seems it should be an easy fix.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_ROW 100
    #define MAX_COL 100
    
    char game[MAX_ROW][MAX_COL] = {0};
    unsigned char display[MAX_ROW][MAX_COL] = {0};
    int m, n;              
    unsigned char difficulty = 1;       
    int i, j;              
    int r, c; 
    int x, y;                
    unsigned char hit_a_mine = 0;       
    int main (int argc, char * argv[]) {
     
     if (argc != 4) {
      printf("Invalid\n");
     } else {
        m = atoi(argv[1]);
        n = atoi(argv[2]);
     }
     play();
     
    }   
     
    
    int play() {
     for (x = 0; x < m; x++) {
      for (y = 0; y < n; y++) {
       game[x][y] = '.';
      }
     }
     for (i = 0; i < 10; i++) {
     x = rand() % m;
     y = rand() % n;
     game[x][y] = '*';
     }
     for (y = 0; y < n; y++) {
      printf("%3s%d", " ", y);
     }
     for (x = 0; x < m; x++) {
      printf("\n%d\n", x);
      for (y = 0; y < n; y++) {
       printf("%3s%c", " ", game[x][y]);
      }
      printf("\n");
     }
     //printf("Enter row and column: ");
     //scanf("%d %d", &r, &c);
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    I understand that the new line im inserting after every row is why its not lining up with game[x][y]. But I didnt want to crunch the game into a small space. I would like to evenly space out everything to read easily.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Is it simply because you have a \n at the end of this printf?
    Code:
     printf("\n%d\n", x);
    Other problems:

    It's always best to use local instead of global variables when you can.

    You're accepting 3 command-line arguments from argv in main, but only using 2 of them. Also, if argc != 4, you print "Invalid" but then continue on with the game. You should exit.

    Don't make play return an int for no reason. Make it void. And you should have it's prototype before main.

    You should return 0 at the end of main.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Thanks oogabooga. Im not quite finished with the game, but the third argument I havent used is for the difficulty level. I'll probably do that last since that will be the easiest thing to do. And yes, the new line im inserting after every row is definitely why its not lining up, but I couldnt figure out how to space the numbering of the rows and columns evenly without the new line there. I guess my question is, how should I insert the new line.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I see. Here's a quick rewrite of your program with local variables and (perhaps) somewhat better variable names and better spacing. I decided to interpret your 3rd parameter as the number of mines for now (you can change that).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_ROWS 40
    #define MAX_COLS 40
    
    void play(int cols, int rows, int num_mines);
    
    int main (int argc, char * argv[]) {
        int cols, rows, num_mines;
        if (argc != 4) {
            printf("Invalid parameters.\n");
            return 1;
        } else {
            cols = atoi(argv[1]);
            rows = atoi(argv[2]);
            // You should check that cols and rows are not out-of-range.
            num_mines = atoi(argv[3]);
        }
        play(cols, rows, num_mines);
        return 0;
    }
     
    void play(int cols, int rows, int num_mines) {
        char game[MAX_ROWS][MAX_COLS];
        int x, y, i;
    
        memset(game, '.', MAX_ROW * MAX_COL);
    
        for (i = 0; i < num_mines; i++) {
            do {
                x = rand() % cols;
                y = rand() % rows;
            } while (game[y][x] == '*');
            game[y][x] = '*';
        }
    
        printf("  ");
        for (x = 0; x < cols; x++) {
            printf(" %2d", x);
        }
    
        for (y = 0; y < rows; y++) {
            printf("\n%2d", y);
            for (x = 0; x < cols; x++) {
                printf("  %c", game[y][x]);
            }
            printf("\n");
        }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minesweeper
    By monkey20 in forum C Programming
    Replies: 2
    Last Post: 04-28-2011, 01:31 PM
  2. minesweeper
    By mark69 in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 10:28 AM
  3. Minesweeper AI
    By Shakti in forum Contests Board
    Replies: 34
    Last Post: 05-13-2005, 07:24 PM
  4. minesweeper
    By sameintheend01 in forum C++ Programming
    Replies: 1
    Last Post: 02-18-2003, 09:50 AM
  5. Minesweeper
    By PJYelton in forum Game Programming
    Replies: 2
    Last Post: 12-22-2002, 10:58 AM