您的位置:首页 > 其它

第一个让我觉得有点小激动的扫雷程序

2016-12-27 21:38 351 查看
刚刚学完一个扫雷程序,虽然写的有点挫(好像很挫

),但是还是想贴出来看看   激励自己

等写完三子棋或者五子棋,也要发出来



1.game.h

#ifndef __GAME_H__
#define __GAME_H__

#define ROWS 10
#define COLS 10
#define DEFAULT_COUNT 20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

void init_board(char arr[ROWS + 2][COLS + 2], int row, int col, char ch);
void display(char arr[ROWS + 2][COLS + 2], int row, int col);
void set_mine(char arr[ROWS + 2][COLS + 2], int row, int col);
void sweep(char mine[ROWS+2][COLS+2], char show[ROWS+2][COLS+2]);
int get_mine(char mine[ROWS+2][COLS+2], int x, int y);

#endif //__GAME_H__

2.test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "game.h"

void menu()
{
printf("*******************************\n");
printf("******  1.play   0.exit  ******\n");
printf("*******************************\n");
}
void game()
{
char mine[ROWS + 2][COLS + 2] = { 0 };
char show[ROWS + 2][COLS + 2] = { 0 };
init_board(mine, ROWS + 2, COLS + 2, '0');
init_board(show, ROWS + 2, COLS + 2, '*');
//display(mine, ROWS + 2, COLS + 2);
display(show, ROWS + 2, COLS + 2);
set_mine(mine, ROWS + 2, COLS + 2);
//display(mine, ROWS + 2, COLS + 2);
sweep(mine, show);
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误,请重新选择:<\n");
break;
}
} while (input);
system("pause\n");
return 0;
}



3.game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void init_board(char arr[ROWS + 2][COLS + 2], int row, int col,char ch)
{
//int i = 0;
//int j = 0;
//for (i = 1; i < row; i++)
//{
// for (j = 1; j < col; j++)
// {
// arr[i][j] = '0';
// }
//}
memset(arr, ch, row*col);
}
int get_mine_num(int x, int y)
{
return rand() % (y - x + 1) + x;
}
void set_mine(char arr[ROWS + 2][COLS + 2], int row, int col)
{
int x = 0;
int y = 0;
int count = DEFAULT_COUNT;
srand((unsigned)time(NULL));
while (count)
{
x = get_mine_num(1, 10);
y = get_mine_num(1, 10);
if (arr[x][y] != '1')
{
arr[x][y] = '1';
count--;
}

}

}
void display(char arr[ROWS + 2][COLS + 2], int row, int col)
{
int i = 0;
int j = 0;
printf(" ");
for (i = 0; i < col - 2; i++)
{
printf("%d ", i + 1);
}
printf("\n");

for (i = 0; i < row - 2; i++)
{
printf("%2d ", i + 1);
for (j = 0; j < col - 2; j++)
{
printf("%c ", arr[i + 1][j + 1]);
}
printf("\n");
}
}
void sweep(char mine[ROWS + 2][COLS + 2], char show[ROWS + 2][COLS + 2])
{
int x = 0;
int y = 0;
int count = 0;
while (count != (ROWS*COLS - DEFAULT_COUNT))
{
printf("请输入坐标:>\n");
scanf("%d %d", &x, &y);
if (mine[x][y] == '1')
{
printf("哎呀,踩雷了:<\n");
break;
}
else
{
int ret = get_mine(mine, x, y);
show[x][y] = ret + '0';
display(show,ROWS+2,COLS+2);
//count++;
}
}
if (count == ROWS*COLS - DEFAULT_COUNT)
printf("恭喜你,你赢了!\n");
display(mine, ROWS + 2, COLS + 2);
}
int get_mine(char mine[ROWS + 2][COLS + 2], int x, int y)
{
return (mine[x + 1][y + 1] - '0') +
(mine[x + 1][y - 1] - '0') +
(mine[x - 1][y + 1] - '0') +
(mine[x - 1][y - 1] - '0') +
(mine[x + 1][y] - '0') +
(mine[x - 1][y] - '0') +
(mine[x][y + 1] - '0') +
(mine[x][y - 1] - '0');
/*int count = 0;
if (mine[x + 1][y + 1] == '1')count++;
if (mine[x + 1][y - 1] == '1')count++;
if (mine[x + 1][y] == '1')count++;

if (mine[x - 1][y + 1] == '1')count++;
if (mine[x - 1][y - 1] == '1')count++;
if (mine[x - 1][y] == '1')count++;

if (mine[x][y + 1] == '1')count++;
if (mine[x][y - 1] == '1')count++;

return count;*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: