【问题标题】:Minesweeper in C [closed]C中的扫雷[关闭]
【发布时间】:2018-10-23 13:31:38
【问题描述】:

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

代码是一个经典的扫雷游戏,用户给出他想从棋盘上发现的点的坐标,游戏是用c而不是C#制作的。

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


#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);
}

【问题讨论】:

  • 你必须展示你的一些代码。否则,我们将无法真正为您提供帮助。很抱歉在这个网站上这样欢迎你,但这就是 StackOverflow 的工作原理!
  • 问题是什么?
  • @RyoShiina 很抱歉,这是我第一次发帖,无法输入代码。
  • @DeiDei 很抱歉,这是我第一次发帖,无法输入代码。
  • 尝试time(获取时间)和difftime(计算两次之间的差异)。

标签: c time


【解决方案1】:

使用函数clock()获取时刻的时钟,然后使用宏CLOCKS_PER_SEC计算已经过去了多少秒。像这样:

int timeInSeconds;
clock_t start = clock(), end,total;

 ...

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

【讨论】:

  • 是什么让 clock() 比 time() 更好?顺便说一句:时间已经包含并使用了
  • 如何在这段代码中实现它?
  • clock 不是答案。 clock返回processor time used的数量,与wall-clock time不同。
猜你喜欢
  • 1970-01-01
  • 2016-03-25
  • 2021-03-17
  • 2021-06-10
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多