扫雷

来源:互联网 发布:描述设置数据有效性 编辑:程序博客网 时间:2024/06/15 08:41

扫雷的c代码文件

main.c


#define _CRT_SECURE_NO_WARNINGS 0
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include"game.h"
void menu()
{
 printf("*********************************\n");
 printf("********* 0:  退  出  **********\n");
 printf("********* 1:  开  始  **********\n");
 printf("*********************************\n");
}
void game()
{
 int k = 0;
 char play[ROWS][LOWS];
 char arr[ROWS][LOWS];
 int i = 0;
 int m = 0;
 srand((unsigned int)time(NULL));
 init(play, ROWS, LOWS);
 display_chushi(arr); 
 display_board(arr, ROWS, LOWS);
 computer_bulei(play, ROWS, LOWS);
 computer_shulei(play, ROWS, LOWS);
 //display_board(play, ROWS, LOWS);
 while (1)
 {
      /* computer_jh(play,arr);*/
    i = computer_jh(play, arr);
    if ((m == 0)&&(i==0))
    {
     game();
    }
  
    if (i == 0)
    {
     break;
    }
    display_board(arr, ROWS, LOWS);
   
   /* k = computer_jh(play, arr);*/
    if (i == 0)
    {
     break;
    }
    if ((display_win(arr,ROWS,LOWS)) == 90)
    {
     break;
    }
 }
 
 
}
int main()
{
 int input = 0;
 do
 {
  menu();
  printf("请输入选项—>:");
  scanf("%d", &input);
  switch (input)
  {
  case 0:
   break;
  case 1:
   game();
   break;
  default:
   break;
  }
 } while (input);
}


game.h

#ifndef __GAME_H__
#define __GAME__H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define ROWS 11
#define LOWS 11
#define TYS  48
void init(char play[ROWS][LOWS], int row, int low);
void display_board(char play[ROWS][LOWS], int row, int low);
void computer_bulei(char play[ROWS][LOWS], int row, int low);
void computer_shulei(char play[ROWS][LOWS], int row, int low);
int  computer_jh(char play[ROWS][LOWS], char arr[ROWS][LOWS]);
void display_chushi(char arr[ROWS][LOWS]);
int display_win(char arr[ROWS][LOWS], int row, int low);
#endif //__GAME__H__


game.c


#define _CRT_SECURE_NO_WARNINGS 0
#include"game.h"
void init(char play[ROWS][LOWS], int row, int low)
{
 memset(play, ' ', row*low*sizeof(char));
}
void display_chushi(char arr[ROWS][LOWS])
{
 int i = 0;
 int j = 0;
 for ( i = 1; i < 11; i++)
 {
  for (j = 1; j < 11; j++)
  {
   arr[i][j] = '?';
  }
 }
}
void display_board(char play[ROWS][LOWS], int row, int low)
{
 int i = 0;
 system("cls");
 printf("    1   2   3   4   5   6   7   8   9   10 \n");
 printf("  |---|---|---|---|---|---|---|---|---|---|\n");
 for (i = 1; i < row; i++)
 {
  printf("%2d| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", i, play[i][1], play[i][2], play[i][3], play[i][4], play[i][5], play[i][6], play[i][7], play[i][8], play[i][9],play[i][10]);
  printf("  |---|---|---|---|---|---|---|---|---|---|\n");
 }
}
void computer_bulei(char play[ROWS][LOWS], int row, int low)
{
 int i = 0;
 int j = 0;
 int x = 0;
 int y = 0;
 int m = 0;
 int k = 10;
 for (m = 0; m < k; m++)
 {
  x = rand() % 10 + 1;
  y = rand() % 10 + 1;
  if ((x>0) && (x<11) && (y>0) && (y < 11) && (play[x][y] != '*'))
  {
   play[x][y] = '*';
  }
  else
  {
   k++;
  }
 }
}
void computer_shulei(char play[ROWS][LOWS], int row, int low)
{
 int i = 0;
 int j = 0;
 int k = 0;
 for (i = 1; i < row; i++)
 {
  for (j = 1; j < low; j++)
  {
   if ((play[i][j] != '*'))
   {
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
     (play[i][j - 1] == '*')                                + (play[i][j + 1] == '*') +
     (play[i + 1][j-1] == '*') + (play[i+1][j] == '*') + (play[i+1][j+1] == '*') == 1)
    {
     play[i][j] = '1';
    }
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
     (play[i][j - 1] == '*') + (play[i][j + 1] == '*') +
     (play[i + 1][j - 1] == '*') + (play[i + 1][j] == '*') + (play[i + 1][j + 1] == '*') == 2)
    {
     play[i][j] = '2';
    }
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
     (play[i][j - 1] == '*') + (play[i][j + 1] == '*') +
     (play[i + 1][j - 1] == '*') + (play[i + 1][j] == '*') + (play[i + 1][j + 1] == '*') == 3)
    {
     play[i][j] = '3';
    }
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
     (play[i][j - 1] == '*') + (play[i][j + 1] == '*') +
     (play[i + 1][j - 1] == '*') + (play[i + 1][j] == '*') + (play[i + 1][j + 1] == '*') == 4)
    {
     play[i][j] = '4';
    }
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
     (play[i][j - 1] == '*') + (play[i][j + 1] == '*') +
     (play[i + 1][j - 1] == '*') + (play[i + 1][j] == '*') + (play[i + 1][j + 1] == '*') == 5)
    {
     play[i][j] = '5';
    }
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
     (play[i][j - 1] == '*') + (play[i][j + 1] == '*') +
     (play[i + 1][j - 1] == '*') + (play[i + 1][j] == '*') + (play[i + 1][j + 1] == '*') == 6)
    {
     play[i][j] = '6';
    }
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
      (play[i][j - 1] == '*') + (play[i][j + 1] == '*') +
      (play[i + 1][j - 1] == '*') + (play[i + 1][j] == '*') + (play[i + 1][j + 1] == '*') == 7)
     {
      play[i][j] = '7';
     }
    if ((play[i - 1][j - 1] == '*') + (play[i - 1][j] == '*') + (play[i - 1][j + 1] == '*') +
     (play[i][j - 1] == '*') + (play[i][j + 1] == '*') +
     (play[i + 1][j - 1] == '*') + (play[i + 1][j] == '*') + (play[i + 1][j + 1] == '*') == 8)
    {
     play[i][j] = '8';
    }
    }
   }
  }
 } 
 
int computer_jh(char play[ROWS][LOWS], char arr[ROWS][LOWS])
{
 int x = 0;
 int y = 0;
 int i = 0;
 int j = 0;
 int m = 0;
 int n = 0;
 printf("请输入坐标(x y)—>:");
 scanf("%d%d", &x, &y);
 arr[x][y] = play[x][y];
 m = x;
 n = y;
    if (arr[x][y] == '*')
 {
  //printf("遗憾!你输了!!\n");
  return 0;
 }
 else if (arr[x][y]==' ')
     {  
  for (x = m; x < 11; ++x)
  {
   if (arr[x][n] != ' ')
   {
    for (i = x, j = y; j < 11; ++j)
    {
     if (play[i][j] != '*')
     {
      arr[i][j] = play[i][j];
     }
     else
     {
      break;
     }
    }
    for (i = x, j = y; j > 0; --j)
    {
     if (play[i][j] != '*')
     {
      arr[i][j] = play[i][j];
     }
     else
     {
      break;
     }
    }
   }
   else if ((arr[i][j] == '1') && (arr[i][j] == '2') && (arr[i][j] == '3') && (arr[i][j] == '4')
    && (arr[i][j] == '5') && (arr[i][j] == '6') && (arr[i][j] == '7') && (arr[i][j] == '8'))
   {
    arr[x][n] = play[x][n];
    break;
   }
   else
   {
    break;
   }
  }
  for (x = m; x > 0; --x)
  {
   if (arr[x][n] != ' ')
   {
    for (i = x, j = y; j < 11; ++j)
    {
     if (play[i][j] != '*')
     {
      arr[i][j] = play[i][j];
     }
     else
     {
      break;
     }
    }
    for (i = x, j = y; j > 0; --j)
    {
     if (play[i][j] != '*')
     {
      arr[i][j] = play[i][j];
     }
     else
     {
      break;
     }
    }
   }
   else if ((arr[i][j] == '1') && (arr[i][j] == '2') && (arr[i][j] == '3') && (arr[i][j] == '4')
    && (arr[i][j] == '5') && (arr[i][j] == '6') && (arr[i][j] == '7') && (arr[i][j] == '8'))
   {
    arr[x][n] = play[x][n];
    break;
   }
   else
   {
    break;
   }
  }
   for (y = n; y < 11; ++y)
   {
    if (arr[m][y] != ' ')
    {
     for (i = x, j = y; i < 11; ++i)
     {
      if (play[i][j] != '*')
      {
       arr[i][j] = play[i][j];
      }
      else
      {
       break;
      }
     }
     for (i = x, j = y; i > 0; --i)
     {
      if (play[i][j] != '*')
      {
       arr[i][j] = play[i][j];
      }
      else
      {
       break;
      }
     }
    }
    else if ((arr[i][j] == '1') && (arr[i][j] == '2') && (arr[i][j] == '3') && (arr[i][j] == '4')
     && (arr[i][j] == '5') && (arr[i][j] == '6') && (arr[i][j] == '7') && (arr[i][j] == '8'))
    {
     arr[m][y] = play[m][y];
     break;
    }
    else
    {
     break;
    }
   }
  
 for (y = n; y > 0; --y)
 {
  if (arr[m][y] != ' ')
  {
   for (i = x, j = y; i < 11; ++i)
   {
    if (play[i][j] != '*')
    {
     arr[i][j] = play[i][j];
    }
    else
    {
     break;
    }
   }
   for (i = x, j = y; i > 0; --i)
   {
    if (play[i][j] != '*')
    {
     arr[i][j] = play[i][j];
    }
    else
    {
     break;
    }
   }
  }
  else if ((arr[i][j] == '1') && (arr[i][j] == '2') && (arr[i][j] == '3') && (arr[i][j] == '4')
   && (arr[i][j] == '5') && (arr[i][j] == '6') && (arr[i][j] == '7') && (arr[i][j] == '8'))
  {
   arr[m][y] = play[m][y];
   break;
  }
  else
  {
   break;
  }
   }
  } else if((arr[i][j] == '1') && (arr[i][j] == '2') && (arr[i][j] == '3') && (arr[i][j] == '4')
  && (arr[i][j] == '5') && (arr[i][j] == '6') && (arr[i][j] == '7') && (arr[i][j] == '8'))
  {
      arr[x][y] = play[x][y];
     }
  return 2;
 }
 


int display_win(char arr[ROWS][LOWS], int row, int low)
{
 int i = 0;
 int j = 0;
 int k = 0;
 for (i = 1; i < 11; i++)
 {
  for (j = 1; j < 11; j++)
  {
   if ((arr[i][j] != '1') && (arr[i][j] != '2')&&(arr[i][j] != '3') && (arr[i][j] != '4')
    && (arr[i][j] != '5') && (arr[i][j] != '6')&&(arr[i][j] != '7') && (arr[i][j] != '8')
    && (arr[i][j] != ' '))
   {
    k++;
   }
  }
 }
 return k;
}



原创粉丝点击