編寫程式—實現掃雷遊戲
阿新 • • 發佈:2018-11-10
編寫程式,實現掃雷遊戲
程式程式碼如下:
game.h
#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 11
#define COLS 11
#define ROW (ROWS-2)
#define COL (COLS-2)
#define COUNT 10
void InitBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols);
void DisplayBoard(char show[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS ][COLS], int count, int row, int col, int x, int y);
void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
int UnknownNum(char show[ROWS][COLS], int row, int col);
#endif//__GAME_H__
game.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"
void InitBoard(char mine[ROWS][COLS], char show[ROWS][COLS],int rows, int cols)
{
memset(mine, '0', rows*cols*sizeof(mine[0][0]));
memset(show, '*', rows*cols*sizeof(show[0][0]));
}
void SetMine(char mine[ROWS][COLS], int count, int row, int col,int x,int y)
{
int i = 0;
while (count)
{
int X = rand() % row + 1 ;
int Y = rand() % col + 1;
if ((X != x) && (Y != y) && (mine[X][Y] == '0'))
{
mine[X][Y] = '1';
count--;
}
}
}
void DisplayBoard(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("%d ", i);
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", show[i][j]);
}
printf("\n");
}
}
void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if ((mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
+ mine[x][y - 1] + mine[x][y] + mine[x][y + 1]
+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]
- 8 * '0') == '0')
{
show[x][y] = ' ';
if ((x - 1 >= 1) && (x - 1 <= ROW) && (y - 1 >= 1) && (y - 1 <= COL) && (show[x - 1][y - 1] == '*'))
GetMineCount(mine, show, x - 1, y - 1);
if (((x - 1 >= 1) && (x - 1 <= ROW)) && ((y >= 1) && (y <= COL)) && (show[x - 1][y] == '*'))
GetMineCount(mine, show, x - 1, y);
if (((x - 1 >= 1) && (x - 1 <= ROW)) && ((y + 1 >= 1) && (y + 1 <= COL)) && (show[x - 1][y + 1] == '*'))
GetMineCount(mine, show, x - 1, y + 1);
if (((x >= 1) && (x <= ROW)) && ((y - 1 >= 1) && (y - 1 <= COL)) && (show[x][y - 1] == '*'))
GetMineCount(mine, show, x, y - 1);
if (((x >= 1) && (x <= ROW)) && ((y + 1 >= 1) && (y + 1 <= COL)) && (show[x][y + 1] == '*'))
GetMineCount(mine, show, x, y + 1);
if (((x + 1 >= 1) && (x + 1 <= ROW)) && ((y - 1 >= 1) && (y - 1 <= COL)) && (show[x + 1][y - 1] == '*'))
GetMineCount(mine, show, x + 1, y - 1);
if (((x + 1 >= 1) && (x + 1 <= ROW)) && ((y >= 1) && (y <= COL)) && (show[x + 1][y] == '*'))
GetMineCount(mine, show, x + 1, y);
if (((x + 1 >= 1) && (x + 1 <= ROW)) && ((y + 1 >= 1) && (y + 1 <= COL)) && (show[x + 1][y + 1] == '*'))
GetMineCount(mine, show, x + 1, y + 1);
}
else
{
show[x][y] = mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
+ mine[x][y - 1] + mine[x][y] + mine[x][y + 1]
+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]
- 8 * '0';
}
}
int UnknownNum(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
{
count++;
}
}
}
return count;
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
void menu()
{
printf("********************\n");
printf("***** 1.Play *****\n");
printf("***** 0.Exit *****\n");
printf("********************\n");
}
void game()
{
char mine[ROWS][COLS] = {0};
char show[ROWS][COLS] = {0};
int x = 0;
int y = 0;
int safe = ROW * COL - COUNT;
InitBoard(mine, show, ROWS, COLS);//初始化棋盤
DisplayBoard(show, ROW, COL);//列印棋盤
while (safe)
{
printf("請輸入一個座標:");
scanf("%d%d", &x, &y);
if (safe == ROW * COL - COUNT)
{
SetMine(mine, COUNT, ROW, COL, x, y);//佈置地雷
}
if ((x >= 1) && (x <= ROW) && (y >= 1) && (y <= COL))
{
if (mine[x][y] == '1')
{
DisplayBoard(mine, ROW, COL);
printf("很遺憾,你被炸死了,祝下次好運!\n");
break;
}
else
{
GetMineCount(mine, show, x, y);
}
safe = UnknownNum(show, ROW, COL) - 10;
DisplayBoard(show, ROW, COL);
}
else
{
printf("座標有誤\n");
}
}
if (safe == 0)
{
printf("恭喜你獲得“掃雷新兵”稱號\n");
}
}
void test()
{
int input = 0;
do
{
menu();
printf("請選擇:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出程式\n");
break;
default:
printf("選擇錯誤,請重新選擇\n");
break;
}
} while (input);
}
int main()
{
srand((unsigned int)time(NULL));
test();
system("pause");
return 0;
}
程式執行結果如下: