c语言小游戏扫雷
头文件
#pragma once #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define easy_count 10 #include<stdio.h> #include<stdlib.h> #include<time.h> void start__board(char board[ROWS][COLS], int rows, int cols, char rew); void start__qipan(char board[ROWS][COLS], int row, int col); void start__mine(char mine[ROWS][COLS], int row, int col); void find__mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
实现个个小功能的源文件
#include"game.h" void start__board(char board[ROWS][COLS], int rows, int cols, char rew) { int i = 0; int j = 0; for (int i = 0; i <= rows; i++) { for (int j = 0; j <= cols; j++) { board[i][j] = rew; } } } void start__qipan(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (int i = 0; i <= row; i++) { printf("%d ", i); } printf("\n"); for (int i = 1; i <= row; i++) { printf("%d ", i); for (int j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("\n"); } void start__mine(char mine[ROWS][COLS], int row, int col) { int count = 10; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } int count__mine(char mine[ROWS][COLS], int x, int y) { return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'); } void find__mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int sum = 0; int x, y; int win = 0; while (win < row * col - easy_count) { printf("请输入要排雷的坐标:->"); scanf_s("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (show[x][y] == '*') { if (mine[x][y] == '1') { printf("很遗憾,你被炸死了\n"); start__qipan(mine, ROW, COL); break; } else { int sum = count__mine(mine, x, y); show[x][y] = sum + '0'; start__qipan(show, ROW, COL); win++; } } else { printf("改坐标已经被排查过了"); } } else { printf("坐标非法,请重新输入"); } if (win = row * col - easy_count) { printf("恭喜你排雷成功\n"); start__qipan(mine, ROW, COL); } } }
主函数文件
#include"game.h" void menu() { printf("***************************\n"); printf("********* 1.play ********\n"); printf("********* 0.exit ********\n"); printf("***************************\n"); } void game() { char mine[ROWS][COLS]; char show[ROWS][COLS]; start__board(mine,ROWS,COLS,'0'); start__board(show, ROW, COL, '*'); //start__qipan(mine, ROW, COL); //start__qipan(show, ROW, COL); start__mine(mine, ROW, COL); start__qipan(show, ROW, COL); find__mine(mine, show, ROW, COL); } int main() { srand((unsigned int)time(NULL)); int m; do { menu(); printf("请选择:->\n"); scanf_s("%d", &m); switch (m) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择:\n"); break; } } while (m); return 0; }