#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 10
int row, col;
int n;
int count = 0;
void initial_screen(char mimesweep, char chess);
int isplay(char choose);
void play(char minesweep, char chess2);
void set_mine(char mine);
int get_mine_count(char mine);
void gamescreen(char** minesweep);
void search_mine(char **minesweep, char **mine);
void sign_mine(char **mine, char minesweep);
void input();
char screen(int n,char chess);
int main() {
char **minesweep=NULL;
char choose;
char **mine=NULL;
printf("Do you want to play mine-sweeping game ?:<'y' for yes;'n' for 'no'> ");
while (1) {
rewind(stdin);
choose = getchar();
if (choose == 'y' || choose == 'n')break;
else printf("Please input a new word!\n");
}
//initial_screen(minesweep, '');
if (isplay(choose)) {
printf("the number of the screen of the game:");
while (1) {
scanf_s("%d", &n);
if (n > 0)break;
else printf("please input a new number.\n");
}
minesweep = screen(n,'');
mine = screen(n,'0');
gamescreen(minesweep);
set_mine(mine);
play(minesweep, 'X');
gamescreen(minesweep);
search_mine(minesweep, mine);
}
else printf("OK,you will qiut the game .See you next time!\n");
return 0;
}
void initial_screen(char minesweep, char chess)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
minesweep[i][j] = chess;
}
}
}
void gamescreen(char minesweep) {
int i, j;
printf("=====================\n");
printf(" ");
for (i = 0; i < n; i++) {
printf("%d ", i);
}
putchar('\n');
for (i = 0; i < n; i++) {
printf("%d ", i);
for (j = 0; j < n; j++) {
printf("%c ", minesweep[i][j]);
}
putchar('\n');
}
printf("=====================\n");
}
void search_mine(char** minesweep, char** mine)
{
int win = 0;
char chess;
while (win < n * n- n) {
if (mine[row][col] == '1') {
printf("sorry,you meet the mine.\n");
printf("game over!\n");
gamescreen(mine);
break;
}
else if (mine[row][col] == '0') {
printf("surrounding mines are %d\n", get_mine_count(mine));
printf("whether you want to sign the mine:<'y' for yes,'n' for no>: ");
while (1) {
rewind(stdin);
chess = getchar();
if (chess == 'y' || chess == 'n')break;
else printf("Please input a new word!\n");
}
if (chess == 'y') {
sign_mine(mine, minesweep);
gamescreen(minesweep);
putchar('\n');
if (count == n) {
printf("Congratulation!you win the game!\n");
gamescreen(mine);
break;
}
}
else if (chess == 'n')
putchar('\n');
play(minesweep, 'X');
gamescreen(minesweep);
win++;
}
}
if (win == n * n - n) {
printf("Congratulation!you win the game!\n");
}
}
void sign_mine(char** mine, char** minesweep)
{
int row1, col1;
while (1) {
rewind(stdin);
printf("row1=");
scanf_s("%d", &row1);
if (0 <= row1 && row1 <= n- 1)break;
else printf("Wrong number.please enter the new number again.\n");
}
while (1) {
rewind(stdin);
printf("col1=");
scanf_s("%d", &col1);
if (0 <= col1 && col1 <= n - 1)break;
else printf("Wrong number.please enter the new number again.\n");
}
while (1) {
if (minesweep[row1][col1] == '_')
{
minesweep[row1][col1] = '@';
break;
}
else {
printf("There is already a chess at (%d,%d)!Try again!\n", row, col);
printf("row=");
rewind(stdin);
scanf_s("%d", &row);
printf("col=");
rewind(stdin);
scanf_s("%d", &col);
}
}
if (mine[row1][col1] == '1')count++;
}
void input()
{
while (1) {
rewind(stdin);
printf("row=");
scanf_s("%d", &row);
if (0 <= row && row <= n - 1)break;
else printf("Wrong number.please enter the new number again.\n");
}
while (1) {
rewind(stdin);
printf("col=");
scanf_s("%d", &col);
if (0 <= col && col <= n - 1)break;
else printf("Wrong number.please enter the new number again.\n");
}
}
int isplay(char choose)
{
switch (choose) {
case 'y':return 1;
case 'n':return 0;
}
}
void play(char **minesweep, char chess2)
{
input();
while (1) {
if (minesweep[row][col] == '_')
{
minesweep[row][col] = chess2;
break;
}
else {
printf("There is already a chess at (%d,%d)!Try again!\n", row, col);
input();
}
}
}
void set_mine(char **mine)
{
srand((unsigned int)time(NULL));
initial_screen(mine, '0');
int count = 0;
while (count < n) {
int x = rand() % n;
int y = rand() % n;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count++;
}
else continue;
}
}
int get_mine_count(char mine)
{
if (row == 0 && col > 0 && col < n - 1)
return(mine[row][col - 1] + mine[row][col + 1]
+ mine[row + 1][col - 1] + mine[row + 1][col] + mine[row + 1][col + 1] - 5 * '0');
else if (row > 0 && row < n - 1 && col == 0)
return(mine[row - 1][col] + mine[row - 1][col + 1] + mine[row][col + 1] + mine[row + 1][col] + mine[row + 1][col + 1] - 5 * '0');
else if (row == n - 1 && col > 0 && col < n - 1)
return(mine[row][col - 1] + mine[row][col + 1]
+ mine[row - 1][col - 1] + mine[row - 1][col] + mine[row - 1][col + 1] - 5 * '0');
else if (row > 0 && row < n - 1 && col == n - 1)
return(mine[row - 1][col] + mine[row + 1][col]
+ mine[row - 1][col - 1] + mine[row][col - 1] + mine[row + 1][col - 1] - 5 * '0');
else if (row == 0 && col == 0)
return(mine[row][col + 1] + mine[row + 1][col] + mine[row + 1][col + 1] - 3 * '0');
else if (row == 0 && col == n - 1)
return(mine[row][col - 1] + mine[row + 1][col] + mine[row + 1][col - 1] - 3 * '0');
else if (row == n - 1 && col == n - 1)
return(mine[row - 1][col] + mine[row][col - 1] + mine[row - 1][col - 1] - 3 * '0');
else if (row == n - 1 && col == 0)
return(mine[row - 1][col] + mine[row][col + 1] + mine[row - 1][col + 1] - 3 * '0');
else if (row > 0 && row < n - 1 && col > 0 && col < n - 1)
return(mine[row - 1][col - 1] + mine[row - 1][col] + mine[row - 1][col + 1]
+ mine[row][col - 1] + mine[row][col + 1]
+ mine[row + 1][col - 1] + mine[row + 1][col] + mine[row + 1][col + 1] - 8 * '0');
}
char screen(int n,char chess) {
char** S;
char i, j;
S = (char**)malloc(sizeof(char*) * (n));
if (S) {
for (i = 0; i < n; i++)
S[i] = (char*)malloc(sizeof(char) * (n));
for (i = 0; i < n; i++) {
if (S[i]) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
S[i][j] = chess;
}
}
return S;
for (i = 0; i < n; i++)
free(S[i]);
free(S);
}
else return NULL;
}
}
else return NULL;
}
警告 C6386 写入 "S" 时缓冲区溢出。