- 積分
- 215
- 最後登入
- 1970-1-1
- 閱讀權限
- 30
- 積分
- 215
- 帖子
- 精華
升級
57.5%
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- const int h=9,w=9,mt=10;//設定高,寬,地雷數量
- int mine[h+2][w+2],t=0,hi=0,wi=0;
- char state[h+2][w+2] ;
- void initialgame();
- void printgmaefield();
- void openspace(int hic,int wic);
- void checksp(int hic,int wic);
- void cler();
- void randm();
- void con();
- int main(){
- srand((unsigned) time(NULL));
- char k,re;
- int f=0,one=0;
- fflush(stdin);
- for(int i=0;i<h+2;i++)
- for(int j=0;j<w+2;j++)
- state[i][j]='u';
- printf("-----------------Program Name: Minesweeper-----------------n");
- printf("--------------------Author:Chiayin Wang--------------------n");
- initialgame();
- while(1){
- if(state[hi][wi]=='c'&&mine[hi][wi]==0){
- checksp(hi-1,wi-1);
- }
- printgmaefield();
- if(state[hi][wi]=='c'&&mine[hi][wi]==-1){//踩到地雷
- printf("你踩到地雷了!!!n");
- printf("-------------------------Game Over-------------------------n");
- printf("--------------------Author:Chiayin Wang--------------------n");
- printf("要重新開始嗎?(輸入Y/y重新開始 其他鍵結束!!!)n");
- scanf("%c",&re);
- if(re=='y'||re=='Y')main();
- return 0;
- }
- f=0;
- for(int p=1;p<h+1;p++)//檢查剩餘地雷
- for(int q=1;q<w+1;q++)
- if(state[p][q]=='c'&&mine[p][q]!=-1)f++;
- if(((h*w)-f)==mt){
- printf("恭喜獲勝!!!n");
- printf("-------------------------Game Over-------------------------n");
- printf("--------------------Author:Chiayin Wang--------------------n");
- printf("要重新開始嗎?(輸入Y/y重新開始 其他鍵結束!!!)n");
- fflush(stdin);
- scanf("%c",&re);
- if(re=='y'||re=='Y')main();
- else return 0;
- }
- printf("n");
- printf("請輸入欲操作之座標(如A1或B2):");
- scanf("%c%d",&hi,&wi);
- if(hi>='A'&&hi<='Z')hi-='A'-1;
- else if(hi>='a'&&hi<='z') hi-='a'-1;
- if(state[hi][wi]=='c'){
- printf("資訊:操作無效!!!n該格已經被打開!n");
- }
- else{
- printf("請輸入動作(C/c打開)(F/f插旗)(U/u解除插旗):");
- scanf("n%c",&k);
- printf("n");
- if(k>='A'&&k<='Z')k+=32;
- if(state[hi][wi]=='f'&&k=='c')printf("資訊:操作無效!!!n該格已經被插旗封印!n");
- else state[hi][wi]=k;
- }
-
- fflush(stdin);
- }
- return 0;
- }
- void play(){
- }
- void initialgame(){//前置作業
- cler();
- randm();
- con();
- }
- void cler(){//清空地雷圖層
- for(int i=0;i<h+2;i++){
- for(int j=0;j<w+2;j++){
- mine[i][j]=0;}
- }
- }
- void randm(){//產生地雷
- int hr=0,wr=0,t=0;
- for(int i=1;i<(mt+1);i++){
- hr=rand()%h+1;
- wr=rand()%w+1;
- mine[hr][wr]=-1;
- }
- for(int i=1;i<h+1;i++){
- for(int j=1;j<w+1;j++){
- if(mine[i][j]==-1){
- t++;
- }
- }
- }
- if(t<mt){
- cler();
- randm();
- }
- }
- void printgmaefield(){//印出表格
- for(int i=1;i<w+1;i++)printf(" %d",i);
- printf("n ┌");
- for(int i=1;i<w;i++)printf("─┬");
- printf("─┐n");
- for(int i=0;i<h;i++){
- printf("%c│",i+65);
- for(int j=0;j<w;j++){
- if(state[i+1][j+1]=='u'){
- printf("■│");
- }
- if(state[i+1][j+1]=='f'){
- printf("◤│");
- }
- if(state[i+1][j+1]=='c'&&mine[i+1][j+1]==0){
- printf(" │");
- }else if(state[i+1][j+1]=='c'&&mine[i+1][j+1]==-1){
- printf("●│");
- }else if(state[i+1][j+1]=='c')
- {
- printf("%2d│",mine[i+1][j+1]);
- }
- }
- printf("n");
- if(i<h-1){
- printf(" ├");
- for(int j=0;j<w-1;j++)
- {
- printf("─┼");
- }
- printf("─┤n");
- }
- }
- printf(" └");
- for(int i=1;i<w;i++)printf("─┴");
- printf("─┘n");
- }
- void con(){//計算炸彈數量
- for(int i=0;i<h+2;i++){
- for(int j=0;j<w+2;j++){
- if(mine[i][j]==-1){
- if(mine[i+1][j]!=-1){
- mine[i+1][j]++;
- }
- if(mine[i][j+1]!=-1){
- mine[i][j+1]++;
- }
- if(mine[i-1][j]!=-1){
- mine[i-1][j]++;
- }
- if(mine[i][j-1]!=-1){
- mine[i][j-1]++;
- }
- if(mine[i+1][j+1]!=-1){
- mine[i+1][j+1]++;
- }
- if(mine[i-1][j-1]!=-1){
- mine[i-1][j-1]++;
- }
- if(mine[i+1][j-1]!=-1){
- mine[i+1][j-1]++;
- }
- if(mine[i-1][j+1]!=-1){
- mine[i-1][j+1]++;
- }
- }
- }
- }
- }
- void checksp(int hic,int wic){//檢查空白
- int tt=0;
- openspace(hic,wic);
- while(h*w){
- for(int i=0;i<h+1;i++){
- for(int j=0;j<w+1;j++){
- if(state[i+1][j+1]=='c'&&mine[i+1][j+1]==0)openspace(i,j);
-
- }
- }
- tt++;
- }
- }
- void openspace(int hic, int wic){//展開空白
- state[hic+2][wic+1]='c';
- state[hic][wic+1]='c';
- state[hic+1][wic+2]='c';
- state[hic+1][wic]='c';
- state[hic+2][wic+2]='c';
- state[hic][wic+2]='c';
- state[hic+2][wic]='c';
- state[hic][wic]='c';
- }
複製代碼 |
本帖子中包含更多資源
您需要 登入 才可以下載或查看,沒有帳號?註冊
x
|