This article has shared a simple version of the C language mine-sweeping game for your reference. The specific content is as follows
#pragma once #include <> #include <> #include <> #include <> #define ROW 12 #define COL 12 #define NUMS 20 #pragma warning(disable:4996) void Menu(); void Game();
#include "" void Menu() { printf("###########################\n"); printf("## 2. Exit ##\n"); printf("###########################\n"); printf("Please enter#"); } void SetMines(char board[][COL], int row, int col) { int num = NUMS; while (num) { int x = rand() % 10 + 1; int y = rand() % 10 + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } } int GetNums(char board[][COL], int row, int col, int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + \ board[x - 1][y + 1] + board[x][y + 1] + \ board[x + 1][y + 1] + board[x + 1][y] + \ board[x + 1][y - 1] + board[x][y - 1] - 8 * '0'; } void ShowBoard(char board[][COL], int row, int col) { printf(" "); for (int i = 1; i < col - 1; i++) { printf(" %2d ", i); } printf("\n"); printf("-------------------------------------------\n"); for (int i = 1; i < row - 1; i++) { printf("%2d|", i); for (int j = 1; j < col - 1; j++) { printf(" %c |", board[i][j]); } printf("\n"); printf("-------------------------------------------\n"); } } void Game() { system("cls"); srand((unsigned long)time(NULL)); char show_board[ROW][COL]; char mine_board[ROW][COL]; memset(show_board, '*', sizeof(show_board)); memset(mine_board, '0', sizeof(mine_board)); SetMines(mine_board, ROW, COL); int count = (ROW - 2) * (COL - 2) - NUMS; int x = 0; int y = 0; do { ShowBoard(show_board, ROW, COL); printf("Please enter coordinates#"); scanf("%d %d", &x, &y); if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) { printf("The input position is out of bounds, please re-enter!\n"); continue; } if (show_board[x][y] != '*') { printf("This location has been excluded!\n"); continue; } if (mine_board[x][y] == '1') { break; } int num = GetNums(mine_board, ROW, COL, x, y); show_board[x][y] = num + '0'; count--; system("cls"); } while (count > 0); if (count > 0) { printf("You were blown to death!"); ShowBoard(mine_board, ROW, COL); } else { printf("Congratulations, you passed the game!\n"); } }
#include "" void Menu() { printf("###########################\n"); printf("## 2. Exit ##\n"); printf("###########################\n"); printf("Please enter#"); } void SetMines(char board[][COL], int row, int col) { int num = NUMS; while (num) { int x = rand() % 10 + 1; int y = rand() % 10 + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } } int GetNums(char board[][COL], int row, int col, int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + \ board[x - 1][y + 1] + board[x][y + 1] + \ board[x + 1][y + 1] + board[x + 1][y] + \ board[x + 1][y - 1] + board[x][y - 1] - 8 * '0'; } void ShowBoard(char board[][COL], int row, int col) { printf(" "); for (int i = 1; i < col - 1; i++) { printf(" %2d ", i); } printf("\n"); printf("-------------------------------------------\n"); for (int i = 1; i < row - 1; i++) { printf("%2d|", i); for (int j = 1; j < col - 1; j++) { printf(" %c |", board[i][j]); } printf("\n"); printf("-------------------------------------------\n"); } } void Game() { system("cls"); srand((unsigned long)time(NULL)); char show_board[ROW][COL]; char mine_board[ROW][COL]; memset(show_board, '*', sizeof(show_board)); memset(mine_board, '0', sizeof(mine_board)); SetMines(mine_board, ROW, COL); int count = (ROW - 2) * (COL - 2) - NUMS; int x = 0; int y = 0; do { ShowBoard(show_board, ROW, COL); printf("Please enter coordinates#"); scanf("%d %d", &x, &y); if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) { printf("The input position is out of bounds, please re-enter!\n"); continue; } if (show_board[x][y] != '*') { printf("This location has been excluded!\n"); continue; } if (mine_board[x][y] == '1') { break; } int num = GetNums(mine_board, ROW, COL, x, y); show_board[x][y] = num + '0'; count--; system("cls"); } while (count > 0); if (count > 0) { printf("You were blown to death!"); ShowBoard(mine_board, ROW, COL); } else { printf("Congratulations, you passed the game!\n"); } }
More interesting classic game implementation topics, share with you:
Summary of C++ classic games
Summary of classic python games
python Tetris game collection
JavaScript classic game
Summary of classic java games
Summary of classic javascript games
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.