C区扫雷艇

C区扫雷艇,c,time,C,Time,游戏运行得非常完美,但我想添加一个计时器,在游戏结束时测量并显示玩家的游戏持续时间 该代码是一款经典的扫雷游戏,用户在游戏中给出他想从棋盘上发现的点的坐标,游戏是用c语言制作的,而不是用c语言 游戏包括清除二维排列中所有不隐藏地雷的方块。有些方块会有一个随机地雷,而其他方块则不会。用户被要求在二维排列中放置一个盒子,如果他没有找到地雷,他将继续游戏直到获胜,否则他将完成游戏,用户将失败 #包括 #包括 #包括 #定义刷新刷新(标准输入) 无效难度(void); 虚空初学者(虚空); 无效中间物

游戏运行得非常完美,但我想添加一个计时器,在游戏结束时测量并显示玩家的游戏持续时间

该代码是一款经典的扫雷游戏,用户在游戏中给出他想从棋盘上发现的点的坐标,游戏是用c语言制作的,而不是用c语言

游戏包括清除二维排列中所有不隐藏地雷的方块。有些方块会有一个随机地雷,而其他方块则不会。用户被要求在二维排列中放置一个盒子,如果他没有找到地雷,他将继续游戏直到获胜,否则他将完成游戏,用户将失败


#包括
#包括
#包括
#定义刷新刷新(标准输入)
无效难度(void);
虚空初学者(虚空);
无效中间物(void);
无效专家(无效);
空洞雷场_发生器(空洞);
无效打印_雷场(无效);
无效猜测(void);
空臂(void);
无效打印\最终\雷场(无效);
无效赢(无效);
再次无效播放(无效);
无效游戏结束(无效);
int x,y;
int M,N;
浮差;
int total_mines=0;
int=0;
国际雷场[30][30]//这个二维数组包含所有的地雷、数字和空格
内布拉克尤雷场[30][30]//其中包含满是“|-|”字符的雷区
国际最终雷场[30][30];
int main()
{
printf(“\t\t距离扫雷舰\n”);
难度();
返回0;
}
无效难度(void)//用于选择难度级别的函数
{
差异=0;
而((diff!=1)和&(diff!=2)和&(diff!=3)和&(diff!=4))
{
printf(“\t\t为自定义游戏选择难度等级(1-3)或4:”;
scanf(“%f”、&diff);
脸红
如果((diff!=1)&&(diff!=2)&&(diff!=3)&(diff!=4))
{
printf(“\t\t请输入1、2、3或4\n”);
}
}   
if(diff==1)//if、else if和else语句,每个语句都有各自的难度
{
初学者();
}
否则如果(差异==2)
{
中间产物();
}
否则如果(差异==3)
{
专家();
}
否则如果(差异==4)
{
习惯();
}
}
void初学者(void)//为雷区提供“初学者”网格和地雷
{
M=9;
N=9;
矿井总数=10;
雷场_发生器();
猜();
}
void intermediate(void)//为雷区提供“intermediate”网格和地雷
{
M=16;
N=16;
矿井总数=40;
雷场_发生器();
猜();
}
void expert(void)//为雷区提供“专家”网格大小和地雷
{
M=16;
N=30;
矿井总数=99;
雷场_发生器();
猜();
}
作废海关(作废)
{
M=0;
N=0;
矿井总数=0;
printf(“\t\t请输入所需尺寸的大小\n”);
printf(“\t\t第一个值:\n”);
scanf(“%d”、&M);
printf(“\t\t第二个值:\n”);
scanf(“%d”和“&N”);
printf(“\t\t您要分配给董事会的矿山数量:\n”);
scanf(“%d”和总矿山);
雷场_发生器();
猜();
}
void雷场\ u生成器(void)//生成雷场的函数
{
int i=0,j=0;
srand(time(NULL));//启动随机编号生成器
同时(J#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define FLUSH fflush(stdin)

void difficulty( void );
void beginner( void );
void intermediate( void );
void expert( void );
void minefield_generator( void );
void print_minefield( void );
void guess( void );
void boom( void );
void print_final_minefield( void );
void win( void );
void play_again( void );
void game_over( void );

int x, y;
int M, N;
float diff;
int total_mines = 0;
int mines = 0;
int minefield[30][30];                                      //This 2-D array contains all of the mines, numbers and blank spaces
int blank_minefield[30][30];                                //This contains the minefield full of '|-|' characters
int final_minefield[30][30];

int main()
{
    printf("\t\tWelcome to Minesweeper\n");
    difficulty();
    return 0;
}

void difficulty( void )                                     //Function for choosing the difficulty level
{
    diff = 0;
    while( (diff != 1) && (diff != 2) && (diff != 3) && (diff != 4))        
    {
        printf("\t\tChoose a difficulty level(1-3) or 4 for a custom game:");   
        scanf("%f", &diff);
        FLUSH;
        if( (diff != 1) && (diff != 2) && (diff != 3) && (diff != 4))
        {
            printf("\t\tPlease enter either 1, 2, 3 or 4\n");
        }
    }   

    if( diff == 1 )                                         //If, else if and else statements that each go to the respective difficulty
    {
        beginner();
    }
    else if( diff == 2 )
    {
        intermediate();
    }
    else if( diff == 3 )
    {
        expert();
    }
    else if( diff == 4)
    {
        custom();
    }
}

void beginner( void )                                       //Gives the minefield the 'beginner' grid and mines
{
    M = 9;
    N = 9;
    total_mines = 10;
    minefield_generator();
    guess();
}

void intermediate( void )                                   //Gives the minefield the 'intermediate' grid and mines
{
    M = 16;
    N = 16;
    total_mines = 40;
    minefield_generator();
    guess();
}

void expert( void )                                         //Gives the minefield the 'expert' grid size and mines
{
    M = 16;
    N = 30;
    total_mines = 99;
    minefield_generator();
    guess();
}

void custom( void )
{
    M = 0;
    N = 0;
    total_mines = 0;
    printf("\t\tPlease enter the size of the dimensions you want\n");
    printf("\t\tFirst value:\n");
    scanf("%d", &M);
    printf("\t\tSecond value:\n");
    scanf("%d", &N);
    printf("\t\tNumber of mines you want to assign to the board:\n");
    scanf("%d", &total_mines);
    minefield_generator();
    guess();
}

void minefield_generator( void )                            //Function that generates the minefield
{
    int i = 0, j = 0;

    srand( time( NULL ) );                                  //Starts the random no. generator

    while( j < N )                                          //Nested loop for making the blank minefield and final minefield
    {
        while( i < M)
        {
            minefield[i][j] = '-';
            blank_minefield[i][j] = minefield[i][j];        
            final_minefield[i][j] = minefield[i][j];        
            i++;
        }
        i = 0;
        j++;
    }
    mines = 0;
    while( mines < total_mines )                            //Randomly generates the mines into the minefield
    {
        i = rand()%(M);
        j = rand()%(N);
        if( minefield[i][j] != '*')                         //If statement that checks if there is a mine already there and doesn't place a mine if there is
        {
            minefield[i][j] = '*';
            final_minefield[i][j] = minefield[i][j];
            mines++;
        }
    }
    i = 0;
    j = 0;

    while( j < N )                                          //While loop that generates the numbers for any adjacent mines
    {
        while( i < M)
        {
            if( minefield[i][j] != '*')
            {
                minefield[i][j] = 0;
            }   
            if((minefield[i-1][j-1] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            if((minefield[i-1][j] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            if((minefield[i][j-1] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            if((minefield[i-1][j+1] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            if((minefield[i+1][j-1] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            if((minefield[i+1][j] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            if((minefield[i][j+1] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            if((minefield[i+1][j+1] == '*') && (minefield[i][j] != '*'))
            {
                minefield[i][j]++;
            }
            i++;
        }
        i = 0;
        j++;
    }
    i = 0;
    j = 0;
}

void print_minefield(void)                                  // This function prints the minefield
{
    int i = 0, j = 0, z = 0;
    while( z < M )                                          // This while loop prints out the line of co-ordinates along the x axis of the minefield
    {
        if( z == 0 )
        {
            printf("\t");
        }
        printf("|%d|\t", z);
        z++;
    }
    printf("\n\n");

    while( j < N )                                          // Loop that prints out each character in the minefield                         
    {   
        printf("|%d|\t", j);
        while( i < M)
        {
            if( blank_minefield[i][j] == '-')
            {
                printf("|%c|\t", blank_minefield[i][j]);

            }
            else if( minefield[i][j] == 0 )                 // This changes any spaces with values of zero to the character 'B'
            {
                blank_minefield[i][j] = 'B';                
                printf("|%c|\t", blank_minefield[i][j]);
            }
            else
            {
                printf("|%d|\t", blank_minefield[i][j]);

            }
            i++;
        }
        printf("\n");
        i = 0;
        j++;
    }
}


void guess( void )
{
    int q = 0, i=0, j=0, match=0;
    print_minefield();
    while( j < N )                                          // While loop for testing whether or not the user has cleared the minefield
    {
        while( i < M )
        {
            if(minefield[i][j] == blank_minefield[i][j])
            {
                match++;
            }
            i++;
        }
        i = 0;
        j++;
    }
    if( match == (( M * N ) - total_mines))                 // If the user has cleared the minefield, the win() function is run
    {
        win();
    }
    printf("\nEnter the x value, then a space, then the y value:");
    scanf("%d %d", &x, &y);                                 // Reading in the co-ordinates for the guess
    FLUSH;
    if( (x >= M) || (x < 0) || (y < 0) || (y >= N) )
    {
        printf("\nPlease enter a value inside the grid\n");
        guess();
    }
    if( minefield[x][y] == '*' )                            // Runs the boom() function if the user selects a mine
    {
        boom();
    }
    if( blank_minefield[x][y] != '-' )
    {
        printf("\nPlease enter a value that has not already been entered\n");
        guess();
    }

    else                                                // Checks if the adjacent spaces are blank, then changes the values in the blank_minefield array. Because they are changed, they will now print out in the print_minefield function
    {
        blank_minefield[x][y] = minefield[x][y];
        if( minefield[x][y] == 0 )
        {
            if( minefield[x-1][y-1] == 0 )
            {
                blank_minefield[x-1][y] = minefield[x-1][y];
            }
            if( minefield[x-1][y] == 0 )
            {
                blank_minefield[x-1][y] = minefield[x-1][y];
            }
            if( minefield[x][y-1] == 0 )
            {
                blank_minefield[x][y-1] = minefield[x][y-1];
            }
            if( minefield[x-1][y+1] == 0 )
            {
                blank_minefield[x-1][y+1] = minefield[x-1][y+1];
            }
            if( minefield[x+1][y-1] == 0 )
            {
                blank_minefield[x+1][y-1] = minefield[x+1][y-1];
            }
            if( minefield[x+1][y] == 0 )
            {
                blank_minefield[x+1][y] = minefield[x+1][y];
            }
            if( minefield[x][y+1] == 0 )
            {
                blank_minefield[x][y+1] = minefield[x][y+1];
            }
            if( minefield[x+1][y+1] == 0 )
            {
                blank_minefield[x+1][y+1] = minefield[x+1][y+1];
            }
        }
        guess();
    }
}

void boom( void )                                       // Runs the print_final_minefield function, then the play_again function                    
{
    print_final_minefield();
    printf("\n\t\tYou hit a mine at %d,%d\n\t\tYOU LOSE!!!!", x, y);
    play_again();
}

void print_final_minefield( void )                      // Prints the minefield, showing where all of the mines are placed
{
    int i = 0, j = 0, z = 0;
    while( z < M )
    {
        if( z == 0 )
        {
            printf("\t");
        }
        printf("|%d|\t", z);
        z++;
    }
    printf("\n\n");

    while( j < N )
    {   
        printf("|%d|\t", j);
        while( i < M)
        {
            printf("|%c|\t", final_minefield[i][j]);
            i++;
        }
        printf("\n");
        i = 0;
        j++;
    }
}

void win( void )                                        // Runs the play_again function
{
    printf("\n\n\n\t\t\tYOU WIN!!!!!\n\n\n");
    play_again();
}

void play_again( void )                                 // Gives the user the option to play again
{
    char option[2];
    printf("\n\t\tWould you like to play again(Y/N)?:");
    scanf("%c", &option[0]);
    FLUSH;
    if((option[0] == 'Y') || (option[0] == 'y'))        // Restarts the program from after the welcome message
    {
        difficulty();
    }
    else if( (option[0] == 'N') || (option[0] == 'n'))
    {
        game_over();
    }
    else
    {
        printf("Please enter either Y or N");
        play_again();
    }
}

void game_over( void )                                  // Ends the program
{
    printf("\n\n\t\tGame Over");
    exit(1);
}
int timeInSeconds;
clock_t start = clock(), end,total;

 ...

end = clock();
total = end - start;
timeInSeconds = total/CLOCKS_PER_SEC;