game2.h
#ifndef __GAME2__H__
#define __GAME2_H__
#define ROWS 9
#define LOCS 9
int init_arr(char arr[ROWS][LOCS], int row, int loc);
void dis_play(char arr[ROWS][LOCS], int row, int loc);
void Init_iay(char arr[ROWS][LOCS], int row, int loc);
int playgame(char arr1[ROWS][LOCS], char arr2[ROWS][LOCS], int no_count);
#endif
game.2c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game2.h"
#include"string.h"
#include"stdlib.h"
#include"time.h"
#include"stdio.h"
int init_arr(char arr[ROWS][LOCS], int row, int loc)
{
int i, j,count=0,temp=0;
for (i = 0; i < row; i++)
{
do
{
temp = 0;
for (j = 0; j < loc; j++)
{
arr[i][j] = rand() % 5;
if (arr[i][j]==3)
{
count++;
temp++;
}
}
} while (temp==0||temp==9);
}
return count;
}
void Init_iay(char arr[ROWS][LOCS], int row, int loc)
{
memset(arr,'*' ,row*loc*sizeof(arr[0][0]));
}
void dis_play(char arr[ROWS][LOCS], int row, int loc)
{
int i, j;
for (i = 0; i < row; i++)
{
printf(" |%d", i + 1);
}
printf("\n");
for (i = 0; i < row; i++)
{
printf("_____________________________________\n");
printf("%d ", i+1);
for (j = 0; j < loc; j++)
{
printf("| %c ", arr[i][j]);
}
printf("|\n");
}
printf("_____________________________________\n");
}
int playgame(char arr1[ROWS][LOCS], char arr2[ROWS][LOCS] ,int no_count)
{
int x, y;
printf("输入你选择的坐标》");
scanf("%d%d", &x, &y);
if (arr2[x-1][y-1] == ' ')
{
printf("已经选过此坐标,请重新选择坐标\n");
scanf("%d%d", &x, &y);
}
if (arr1[x-1][y-1]== 3)
{
printf("踩中雷区\n\a");
arr2[x - 1][y - 1] = '@';
return 0;
}
else
{
arr2[x-1][y-1] = ' ';
no_count=no_count-1;
}
if (no_count == 0)
printf("玩家挑战成功");
return no_count;
}
test.2
#define _CRT_SECURE_NO_WARNINGS 1
#include"stdio.h"
#include"game2.h"
#include"string.h"
#include"time.h"
#include"stdlib.h"
void play()
{
int no_count,count;
srand((unsigned int) time(NULL));
char arr1[ROWS][LOCS] = { 0 };
char arr2[ROWS][LOCS] = { 0 };
count=init_arr(arr1, ROWS, LOCS);
no_count = 81 - count;
Init_iay(arr2, ROWS, LOCS);
dis_play(arr2, ROWS, LOCS);
do
{
no_count = playgame(arr1, arr2, no_count);
dis_play(arr2, ROWS, LOCS);
} while (no_count);
}
mean()
{
printf("**********************\n");
printf("****play.1 exit.0*****\n");
printf("**********************\n");
}
void test()
{
int input;
do
{
mean();
scanf("%d", &input);
switch (input)
{
case 1:
play();
break;
case 0:
printf("游戏结束\n");
break;
default :
printf("选择错误,请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}