mine sweeper code help..

This is a discussion on mine sweeper code help.. within the C Programming forums, part of the General Programming Boards category; I have a 2D array i put a '*' char in 0,1 cell. My program reads the board(2d array) from ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    mine sweeper code help..

    I have a 2D array i put a '*' char in 0,1 cell.
    My program reads the board(2d array) from 1..size
    so i subtract 1 from each of the "uncover" input coordinate.

    If i dont step on a mine it replaces the value of the cell with the number of the
    surrounding cells which has '*' in them.

    i wrote this simple case code to demonstrate it
    it should print the board with '*' in board[1][0] and '1' in board[0][0]

    but it shows me a blank array .
    why??
    Code:
    #include <stdio.h>
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    int unCover(char board[N][N],int size,int x,int y);
    int main(){
        int index,kndex;
        int x;
    char board[N][N];
     for(index=0;index<N;index++){
         for(kndex=0;kndex<N;kndex++){
             board[index][kndex]=' ';
         }
    }
    
    board[1][0]='*';
    x=unCover(board,5,1,1);
    }
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost");
       board[y-1][x-1]=='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
      if (board[y][x]!='*'){
         
          if (((y-2)>0) && ((x-2)>0) && (board[y-2][x-2]=='*')){ //counting how many mines surrounds the cell
           count++;
          }
    
            if (((y-2)>0) && ((x-1)>0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>0) && ((x)<size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>0) && ((x-2)>0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x]=(char)count;
           printBoard(board, size);
       return 1;
        }
     }
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }
    Last edited by transgalactic2; 01-01-2009 at 05:32 AM.

  2. #2
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Code:
    int unCover(char board[N][N],int size,int x,int y){
    // ...
           board[y-1][x]=(char)count;
    }
    You assign the count to the wrong index, plus casting the count may not be suitable here. Try:
    Code:
    board[y-1][x-1]='0'+count;
    Instead.

    Next thing are your if statements that check whether or not a surrounding index is out of boundaries. Remember that array index begins at 0.
    For example, one of your if statements is as follows:
    Code:
    if (((y)<size) && ((x-1) > 0) && (board[y][x-1]=='*')){
    Try changing all of the inequalities with zeros to something like:
    Code:
    if (((y)<size) && ((x-1) >= 0) && (board[y][x-1]=='*')){

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did your changes
    its not working .
    if the user prints to uncover 1,1 then because its counting from 1 to size
    the the actual coordinate is 0,0.
    also the column number is X and the row number is y the input goes like x,y
    so "y" in the rows and "x" in cols.
    it need to print '1' on the 0,0 cell and show the astrix in board[1][0].
    Code:
    #include <stdio.h>
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    int unCover(char board[N][N],int size,int x,int y);
    int main(){
        int index,kndex;
        int x;
    char board[N][N];
     for(index=0;index<N;index++){
         for(kndex=0;kndex<N;kndex++){
             board[index][kndex]=' ';
         }
    }
    
    board[1][0]='*';
    x=unCover(board,5,1,1);
    
    return 0;
    }
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost");
       board[y-1][x-1]=='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
      if (board[y][x]!='*'){
        //if  (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<=size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x]='0' +count;
           printBoard(board, size);
       return 1;
        }
     }
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }

  4. #4
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    You again assign the count to the wrong index:
    Code:
    board[y-1][x]='0'+count;

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok it prints the number in the right place.
    why it doesnt print the astrix bellow it??

    Code:
    #include <stdio.h>
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    int unCover(char board[N][N],int size,int x,int y);
    int main(){
        int index,kndex;
        int x;
    char board[N][N];
     for(index=0;index<N;index++){
         for(kndex=0;kndex<N;kndex++){
             board[index][kndex]=' ';
         }
    }
    
    board[1][0]='*';
    x=unCover(board,5,1,1);
    
    return 0;
    }
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost");
       board[y-1][x-1]=='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
      if (board[y][x]!='*'){
        //if  (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<=size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x-1]='0' +count;
           printBoard(board, size);
       return 1;
        }
     }
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }
    Last edited by transgalactic2; 01-01-2009 at 07:23 AM.

  6. #6
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    On line 42:
    Code:
            if (((y-2)>=0) && ((x)<=size) && (board[y-2][x]=='*')){
    You do not want to reach size as that will be beyond the boundary.

    Regardless of the above, it does print an asterisk on on my computer.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I did your fix
    and it worked correctly.
    but when i put an astrix on
    board[1][0]='*';
    board[1][1]='*';
    board[0][1]='*';
    (surrounded it by astrixes) it needs to show 3 on 0,0 ant the astrixes

    but its not showing anything at all
    ??
    Code:
    #include <stdio.h>
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    int unCover(char board[N][N],int size,int x,int y);
    int main(){
        int index,kndex;
        int x;
    char board[N][N];
     for(index=0;index<N;index++){
         for(kndex=0;kndex<N;kndex++){
             board[index][kndex]=' ';
         }
    }
    
    board[1][0]='*';
    board[1][1]='*';
    board[0][1]='*';
    x=unCover(board,5,1,1);
    
    return 0;
    }
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost");
       board[y-1][x-1]=='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
      if (board[y][x]!='*'){
        //if  (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x-1]='0' +count;
           printBoard(board, size);
       return 1;
        }
     }
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }

  8. #8
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Check line number 35:
    Code:
      if (board[y][x]!='*'){
    And tell me if you notice anything wrong.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok i fixed it.
    i entered a case where i choose to step on a mine
    its supposed to enter X on this spot and print the board.(with that X on this spot)
    but its not doing that
    why??

    Code:
    #include <stdio.h>
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    int unCover(char board[N][N],int size,int x,int y);
    int main(){
        int index,kndex;
        int x;
    char board[N][N];
     for(index=0;index<N;index++){
         for(kndex=0;kndex<N;kndex++){
             board[index][kndex]=' ';
         }
    }
    
    
    board[4][4]='*';
    
     x=unCover(board,5,5,5);
    
    
    return 0;
    }
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost");
       board[y-1][x-1]=='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
    
        //if  (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x-1]='0' +count;
           printBoard(board, size);
       return 1;
    
     }
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }
    Last edited by transgalactic2; 01-01-2009 at 09:08 AM.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I solved it .
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mine sweeper help..
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 01-04-2009, 11:18 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21