标题:做了个扫雷,发现我最后要显示地雷位置的出现了问题,我觉得问题应该是出在 ...
只看楼主
卷毛的企鹅
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-8-7
得分:0 
做了如下修改,求技术指正。。。

程序代码:
void displaynum(char field[], int num, int row, int col)
{            
            if(num==0)
            {
                field[row*MAX_PADDED_COL+col]=SPACE; //centre position
                if(field[row*MAX_PADDED_COL+col-1] == STAR)                
                field[row*MAX_PADDED_COL+col-1]=SPACE; //left                 
                if(field[row*MAX_PADDED_COL+col+1] == STAR)
                field[row*MAX_PADDED_COL+col+1]=SPACE; //right
                if(field[row*MAX_PADDED_COL+col-MAX_PADDED_COL] == STAR)
                field[row*MAX_PADDED_COL+col-MAX_PADDED_COL]=SPACE;//top
                if(field[row*MAX_PADDED_COL+col+MAX_PADDED_COL] == STAR)
                field[row*MAX_PADDED_COL+col+MAX_PADDED_COL]=SPACE; //bottom
                if(field[row*MAX_PADDED_COL+col-MAX_PADDED_COL-1] == STAR)
                field[row*MAX_PADDED_COL+col-MAX_PADDED_COL-1]=SPACE;  //left top
                if(field[row*MAX_PADDED_COL+col-MAX_PADDED_COL+1] == STAR)
                field[row*MAX_PADDED_COL+col-MAX_PADDED_COL+1]=SPACE;  //right top
                if(field[row*MAX_PADDED_COL+col+MAX_PADDED_COL+1] == STAR)
                field[row*MAX_PADDED_COL+col+MAX_PADDED_COL+1]=SPACE;  //right bottom
                if(field[row*MAX_PADDED_COL+col+MAX_PADDED_COL-1]==STAR)
                field[row*MAX_PADDED_COL+col+MAX_PADDED_COL-1]=SPACE;   //left bottom
            }
            else if(num==1)
            {
                field[row*MAX_PADDED_COL+col]='1';
            }
            else if(num==2)
            {
                field[row*MAX_PADDED_COL+col]='2';
            }
            else if(num==3)
            {
                field[row*MAX_PADDED_COL+col]='3';
            }
            else if(num==4)
            {
                field[row*MAX_PADDED_COL+col]='4';
            }
            else if(num==5)
            {
                field[row*MAX_PADDED_COL+col]='5';
            }
            else if(num==6)
            {
                field[row*MAX_PADDED_COL+col]='6';
            }
            else if(num==7)
            {
                field[row*MAX_PADDED_COL+col]='7';
            }
            else
            {
                field[row*MAX_PADDED_COL+col]='8';
            }
}
void start_game(void)
{
    char field[MAX_PADDED_ROW * MAX_PADDED_COL];
    char mine[MAX_PADDED_ROW * MAX_PADDED_COL] = {0};
    int  row, col, a, i, j,b,checkstar;

    initgrid(field, mine, MAX_PADDED_ROW, MAX_PADDED_COL);
    mineposition(mine, MAX_PADDED_ROW, MAX_PADDED_COL);
    row=0;
    col=0;
    checkstar = 0;
    for(;;)
    {
        
        printgrid(field,MAX_PADDED_ROW,MAX_PADDED_COL);
        if(mine[row*MAX_PADDED_COL+col] == MINE)
        {
            printf("You hit a mine!! Game over...\n");
            break;
        }
        
        if(checkstar==MAX_MINES)
        {
            printf("Congratulations! You win!\n");
            break;
        }
        // begin: to be deleted: for testing purpose
        for (i=0;i<MAX_PADDED_ROW;i++)
        {
            for (j = 0; j < MAX_PADDED_COL; j++)
            {
                if (mine[i * MAX_PADDED_COL + j] == MINE)
                {
                    printf("[%d][%d]\t", /*(i * MAX_PADDED_COL + j) / MAX_PADDED_COL*/i, /*(i * MAX_PADDED_COL + j) % MAX_PADDED_COL)*/j);
                }
            }
        }
        // end: to be deleted: for testing purpose
    
        printf("\nEnter <-1 -1> to exit game...\n\n");
        printf("row and column <[row] [col]>':\n");
        scanf("%d %d",&row,&col);
        if (row == -1 && col == -1)
        {
            printf("\nExiting from game...\n");            
            break;
        }
        else if (((row < 1 )||( row > ROW)) && ((col<1)||(col >COL)))
        {
            printf("\nInvalid tile position! Please enter row and column again...\n\n");
        }
        else
        {
            if(mine[row*MAX_PADDED_COL+col] != MINE)
            {

                a = checknearbymines(mine, row,col);
                displaynum(field, a, row, col);
            }
            else
            {                
                for (i=0;i<MAX_PADDED_ROW;i++)
                {
                    for (j = 0; j < MAX_PADDED_COL; j++)
                    {
                        if (mine[i * MAX_PADDED_COL + j] == MINE)
                        {                            
                            field[i * MAX_PADDED_COL + j]= 'Y';                            
                        }
                    }
                }
                field[row*MAX_PADDED_COL+col] = 'X';                                            
            }        
        }
        checkstar=0;
        for(b=0;b<MAX_PADDED_ROW*MAX_PADDED_COL;b++)
        {
            if(field[b]==STAR)
            {
                checkstar++;
            }
        }
        system("cls");
    }
        
}
2013-08-09 03:35
卷毛的企鹅
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-8-7
得分:0 
又做了一些小小的改进。。

程序代码:
void start_game(void)
{
    char field[MAX_PADDED_ROW * MAX_PADDED_COL];
    char mine[MAX_PADDED_ROW * MAX_PADDED_COL] = {0};
    int  row, col, a, i, j,b,checkstar;

    initgrid(field, mine, MAX_PADDED_ROW, MAX_PADDED_COL);
    mineposition(mine, MAX_PADDED_ROW, MAX_PADDED_COL);
    row=0;
    col=0;
    checkstar = 0;
    printgrid(field,MAX_PADDED_ROW,MAX_PADDED_COL);
    for(;;)
    {
        if(mine[row*MAX_PADDED_COL+col] == MINE)
        {
            printf("You hit a mine!! Game over...\n");
            break;
        }
        
        if(checkstar==MAX_MINES)
        {
            printf("Congratulations!! You have successfully completed the game!\n");
            break;
        }
        // begin: to be deleted: for testing purpose
        for (i=0;i<MAX_PADDED_ROW;i++)
        {
            for (j = 0; j < MAX_PADDED_COL; j++)
            {
                if (mine[i * MAX_PADDED_COL + j] == MINE)
                {
                    printf("[%d][%d]\t", /*(i * MAX_PADDED_COL + j) / MAX_PADDED_COL*/i, /*(i * MAX_PADDED_COL + j) % MAX_PADDED_COL)*/j);
                }
            }
        }
        // end: to be deleted: for testing purpose
    
        printf("\nEnter <-1 -1> to exit game...\n\n");
        printf("row and column <[row] [col]>':\n");
        scanf("%d %d",&row,&col);
        if (row == -1 && col == -1)
        {
            printf("\nExiting from game...\n");            
            break;
        }
        else if (((row < 1 )||( row > ROW)) || ((col<1)||(col >COL)))
        {
            printf("\nInvalid tile position! Please enter row and column again...\n\n");
            continue;
        }
        else
        {
            if(mine[row*MAX_PADDED_COL+col] != MINE)
            {
                a = checknearbymines(mine, row,col);
                displaynum(field, a, row, col);
            }
            else
            {                
                for (i=0;i<MAX_PADDED_ROW;i++)
                {
                    for (j = 0; j < MAX_PADDED_COL; j++)
                    {
                        if (mine[i * MAX_PADDED_COL + j] == MINE)
                        {                            
                            field[i * MAX_PADDED_COL + j]= 'Y';                            
                        }
                    }
                }
                field[row*MAX_PADDED_COL+col] = 'X';                                            
            }        
        }
        checkstar=0;
        for(b=0;b<MAX_PADDED_ROW*MAX_PADDED_COL;b++)
        {
            if(field[b]==STAR)
            {
                checkstar++;
            }
        }
        system("cls");
        printgrid(field,MAX_PADDED_ROW,MAX_PADDED_COL);  // owc
    }
        
}
2013-08-09 17:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-418770-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058273 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved