Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the text file is a n by n table which i already know 'n' and it is 50 max. so the table will look something like this:
##*
#*#
*##
i have created hidden_board[50][50] so what to do is copy the data from text file to the array and for '#' put the number 10 and for '*' put the number 11.
i have complicated things a lot but this is what i have got so far:
it is so messy an inefficient and does not work and when i print the array it shows:
10 11 0
10 11 0
0 11 0
i don't know where the zeros came from. can someone tell me the correct way to do this?

What I have tried:

C++
char my_board_dummy[n*n];
    int my_board_dummy1[n*n];
    int i=0;
    FILE* file;
    if ((file=fopen("minesweeper.txt" , "r"))) {
        char c;
        while ((c=fgetc(file))!=EOF)
            my_board_dummy[i++]=c;
        for (int i=0; i<n*n; ++i){
            if (my_board_dummy[i]=='#')
                my_board_dummy1[i]=10;
            else if (my_board_dummy[i]=='*')
                my_board_dummy1[i]=11;
        }
        for (int i=0; i<n*n; ++i){
            hidden_board[i/n][i%n-1]=my_board_dummy1[i];
        }
Posted
Updated 12-Jan-19 4:30am
v2
Comments
Richard MacCutchan 12-Jan-19 9:44am    
Why are you using a dummy array in the first place? As you read each line of text, set the relevant entries in the array to the values 10, or 11 depending on which character you read in. You also need to ensure that your array is exactly the same size as there are elements in the data file.

1 solution

You don't need two loops, you can do it with one:
while ((c=fgetc(file))!=EOF)
    {
    my_board_dummy[i++] = c == '#' ? 10 : 11;
    }
But... if your input file has lines, you would be better off doing two things:
1) Store the value of "n" in the file as the first line, and allocate exactly enough space for for the board when you read the size from the file. Make the board a two dimensional array, and you have an x and y coordinate to access each location. That makes the rest of your code a lot easier to work with.
2) Use two nested for loops instead of a while:
2.1) the output loop reads a whole line of text from the file, incrementing y.
2.2) the inner loop processes each line and converts the characters to board values, incrementing x. For each location you then have an x and y location to fill.

But ... minesweeper games aren't usually file based: you would normally fill a board with "empty" squares, then use a random number generator to select "n" locations that are mines and replace the empty square in each location with a mine value.

I'd also suggest that it's a good idea to make your board 2 squares larger in each direction than it needs to be, and fill that new area with "empty" squares. You then work with X and Y coordinates from 1 to n inclusive (instead of 0 to n - 1) and can access the user coordinate plus and minus one in both directions. That means when you are "looking for empty space" (a middle click in Windows Minesweeper) you don't have to check for edges at all, and that makes your code a lot cleaner.
 
Share this answer
 
Comments
average_angela 12-Jan-19 10:57am    
hey, thanks for the feedback. i figured out the problem: since there are several lines in the text file there is a null character at the end of each line and those were causing the trouble. i fixed it and now it works:
int hidden_board[50][50];
FILE* file;
if ((file=fopen("minesweeper.txt" , "r"))) {
char c;
for (int i=0; i<n; ++i){
for (int j=0; j<n; ++j){
c= fgetc(file);
if (c=='#'){
hidden_board[i][j]= 10;
}
else if (c=='*')
hidden_board[i][j]= 11;
else
--j;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900