C语言-扫雷游戏
頭文件
#ifndef __MINE_H__
#define __MINE_H__#define LINE 10
#define LIST 10
#define ROWS 6
#define COWS 6int game(char UserBoard[LINE+2][LIST+2], char PlayerBoard[LINE][LIST]);
void PrintBoard(char Playerboard[LINE][LIST]);
void MineLay(char UserBoard[LINE + 2][LIST + 2]);
void PrintUser(char UserBoard[LINE + 2][LIST + 2]);
int MineClear(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST]);
void Blast(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST]);
int Counter(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST], int x, int y);#endif   //mine.h
函數文件
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include"mine.h"void MineLay(char UserBoard[LINE + 2][LIST + 2])
{int x = 0, y = 0,i = 0,j = 0;char m = '0';printf("**************************\n");printf("******* a: 十個雷 ********\n");printf("******* b: 二十雷 ********\n");printf("**************************\n");printf("請選擇難度>:");fflush(stdin);scanf("%c",&m);printf("\n");if ('a' == m){j = 10;}elsej = 20;while (i < j){x = rand() % 10 + 1;y = rand() % 10 + 1;if ('0' == UserBoard[x][y]){UserBoard[x][y] = '1';i++;}}}void Blast(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST])//全局輸了
{int i = 0, j = 0;for (i = 1; i <=LINE; i++){for (j = 1; j <= LIST; j++){if ('1' == UserBoard[i][j]){PlayerBoard[i - 1][j - 1] = '#';}if ('*' == PlayerBoard[i - 1][j - 1])    //{                                       //PlayerBoard[i - 1][j - 1] = ' ';      //}                                          //}}
}void PrintBoard(char Playerboard[LINE][LIST])
{int i = 0, j = 0;printf("  0 1 2 3 4 5 6 7 8 9 10\n");printf("------------------------\n");for (i = 0; i < LINE; i++){printf("%2d| ",i+1);for (j = 0; j < LIST; j++){printf("%c ",Playerboard[i][j]);}printf("\n");}printf("\n");
}int MineClear(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST])
{int x = 0, y = 0,num = 0,win = 0,i =0,j = 0,m = 0,n = 0,p = 0,q = 0,a = 0,b = 0;while (1){printf("請輸入掃雷坐標>:");scanf("%d%d", &x, &y);a = x;b = y;i = x;j = y;m = x;n = y;p = x;q = y;if ((x > 0 && x <= 10) && (y > 0 && y <= 10)){if ('1' == UserBoard[x][y]){PlayerBoard[x-1][y-1] = '#';Blast(UserBoard, PlayerBoard);PrintBoard(PlayerBoard);printf("啊!!!!你被炸死啦!!!!\n");return 0;}if ('0' == UserBoard[x][y]){for (a = x; a >= 1; a--,b = y)//向上向左{if (0 != Counter(UserBoard, PlayerBoard, a, b))break;for (b = y; b >= 1; b--)   自加自減和for循環的初值一定要注意哦,我一開始的代碼是	for (a = x; a >= 1 && '0' == UserBoard[a][b]; a--)for(; b >= 1 && '0' == UserBoard[a][b]; b--)此時{                                                        //此時,當a--時,b的值已經是上次的0了,所以下面的每次都不執行,因為判斷條件是b>=1if (0 != Counter(UserBoard, PlayerBoard, a, b))break;}}for (i = x; i >= 1; i--,j = y)//向上向右數{if (0 != Counter(UserBoard, PlayerBoard, i, j))break;for (j = y; j <= LIST; j++)     //for循環里面盡量不要既執行又判斷{if (0 != Counter(UserBoard, PlayerBoard, i, j))break;}}for (m = x; m <= LINE; m++,n = y)//向下向左{if (0 != Counter(UserBoard, PlayerBoard, m, n))break;for (n = y; n >= 1; n--){if (0 != Counter(UserBoard, PlayerBoard, m, n))break;}				}for (p = x; p <= LINE; p++,q = y)//向下向右{if (0 != Counter(UserBoard, PlayerBoard, p, q))break;for (q = y; q <= 4; q++){if (0 != Counter(UserBoard, PlayerBoard, p, q))break;}				}PrintBoard(PlayerBoard);//PrintUser(UserBoard);  win = 0;                 //一開始的時候這個地方沒有寫win = 0;這樣就出現了一個問題,就是,上次輸入一個坐標循環的時候win已經有大于0的值,導致循環完的最終結果不能小于3for (x = 0; x < LINE; x++){for (y = 0; y < LIST;y++)if ('*' == PlayerBoard[x][y]){win++;	}}if (win <= 3){for (x = 0; x < LINE; x++){for (y = 0; y < LIST; y++)if ('*' == PlayerBoard[x][y]){PlayerBoard[x][y] = '#';}}printf("掃雷成功,你贏了!!!\n");return 0;}}}}
}int Counter(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST], int x, int y)
{int num = 0;num = (UserBoard[x - 1][y - 1] - '0') +(UserBoard[x - 1][y] - '0') +(UserBoard[x - 1][y + 1] - '0') +(UserBoard[x][y - 1] - '0') +(UserBoard[x][y + 1] - '0') +(UserBoard[x + 1][y - 1] - '0') +(UserBoard[x + 1][y] - '0') +(UserBoard[x + 1][y + 1] - '0');if (0 == num){PlayerBoard[x - 1][y - 1] = '0';}else{PlayerBoard[x - 1][y - 1] = num + '0';}return num;
}int game(char UserBoard[LINE+2][LIST+2], char PlayerBoard[LINE][LIST])
{/*int x = 0, y = 0;int i = 0, j = 0;*///PrintBoard(PlayerBoard);MineLay(UserBoard);PrintBoard(PlayerBoard);//PrintUser(UserBoard);                           ///return  MineClear(UserBoard, PlayerBoard);return 0;
}void PrintUser(char UserBoard[LINE + 2][LIST + 2])
{int i = 0, j = 0;printf("   0 1 2 3 4 5 6 7 8 9 10\n");for (i = 0; i < LINE + 2; i++){printf("%2d ", i );for (j = 0; j < LIST + 2; j++){printf("%c ", UserBoard[i][j]);}printf("\n");}printf("\n");
}
 
 
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#include"mine.h"
//1.第一次掃雷,掃一大片2.玩家選擇難度3.插標記void menu()
{printf("***********************\n");printf("******  1.play  *******\n");printf("******  0.exit  *******\n");printf("***********************\n");printf("請選擇相應的數字>:");
}int main()
{char PlayerBoard[LINE][LIST];char UserBoard[LINE + 2][LIST + 2];int input = 1, a = 0;srand((unsigned int)time(NULL));while (input){memset(PlayerBoard, '*', sizeof(char)*LINE*LIST);memset(UserBoard, '0', sizeof(char)*(LINE+2)*(LIST+2));menu();scanf("%d", &input);printf("\n");switch (input){case 1:a = game(UserBoard, PlayerBoard);break;case 0:break;    //此處break的作用是結束switchdefault:printf("你的輸入不符合要求,請重新輸入>:"); break;}if (0 == input){break;      //此處break的作用是跳出while循環,即結束游戲}}printf("游戲結束,歡迎再次使用!!!\n");system("pause");return 0;
}
總結
                            
                        - 上一篇: 求一个好听的环保主题名字!
 - 下一篇: 输卵管堵塞好不好治疗