#pragma once #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define Easy_count 80 #define ROWS ROW+2 #define COLS COL+2 void InitBoard(char Board[ROWS][COLS], int rows, int cols,char set); void Display(char Board[ROWS][COLS], int row, int col); void Setmine(char Board[ROWS][COLS], int row, int col); void findmine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col);
#include "game.h" void InitBoard(char Board[ROWS][COLS], int rows, int cols,char set) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols;j++) { Board[i][j] = set; } } } void Display(char Board[ROWS][COLS], int row, int col) { int i, j; //打印列號 for (i = 0; i <= col; i++) { printf("%d ", i); }printf("\n"); for (i = 1; i <= row; i++) { printf("%d ",i); for (j = 1; j <= col; j++) { printf("%c ", Board[i][j]); } printf("\n"); } } void Setmine(char Board[ROWS][COLS], int row, int col) { int count = Easy_count; while (count) { int x = rand()% row + 1; int y = rand() % col + 1; if (Board[x][y] == '0') { Board[x][y] = '1'; count--; } } } int getmine(char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1]-8*'0'; } void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x, y; int win = 0; while (win<row*col-Easy_count) { printf("請輸入排查座標:"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { //座標合法 if (mine[x][y] == '1') { printf("很遺憾,死了\n"); Display(mine, row, col); break; } else { //非雷 計算周圍幾個雷 int count=getmine(mine, x, y); show[x][y] = count + '0'; Display(show, row, col); win++; } } else { printf("輸入非法從新輸\n"); } } if (win == row * col - Easy_count) { printf("恭喜你成功"); Display(mine, row, col); } }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include "game.h" void menu() { printf("********************************\n"); printf("***1.玩遊戲****2.退出***********\n"); printf("********************************\n"); } void game() { //1 佈置好的雷的信息 char mine[ROWS][COLS] = {0}; //2 排查出雷的信息 char show [ROWS][COLS] = { 0 }; //初始化 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); //打印棋盤 //Display(mine, ROW, COL); Display(show, ROW, COL); //佈置雷 Setmine(mine, ROW, COL); Display(mine, ROW, COL); //掃雷 findmine(mine,show,ROW,COL); } void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("請選擇:"); scanf("%d", &input); switch (input) { case 1: game(); break; case 2: printf("退出遊戲,結束\n"); break; default: printf("選擇錯誤,從新選擇\n"); break; } } while (input); } int main() { test(); return 0; }