第一个让我觉得有点小激动的扫雷程序
2016-12-27 21:38
351 查看
刚刚学完一个扫雷程序,虽然写的有点挫(好像很挫
),但是还是想贴出来看看 激励自己
等写完三子棋或者五子棋,也要发出来
#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;*/
}
),但是还是想贴出来看看 激励自己
等写完三子棋或者五子棋,也要发出来
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;*/
}
相关文章推荐
- 自己做的觉得好玩的第一个JAVA程序
- 心情灰常激动,第一个汇编程序烧入Nand Flash,点亮 led1
- 终于开始写第一个博客了,哈哈,有点小激动,记录一下listview回滚到顶部,老是忘
- 第一个用java-swing写的 图形界面程序-扫雷
- 我的第一个程序------有点曲折
- 我觉得这样有点乱, 如果真的想搞下去,是否需要一个独立的网站?[乐色.Net]
- 程序人生--我有点累了
- 用Eclipse MyEclipse WebLogic8.1开发第一个Web程序
- 我的第一个socket程序!
- JSP与JavaMail(3)---试着写第一个邮件发送程序
- 是第一个程序的加强版本,加入了BOSS功能
- SWT学习笔记(3)——第一个程序
- 我的第一个PHP程序--用户登录页面
- PHP第一个小程序
- 扫雷程序思想讲解
- 我写的扫雷程序,增加了新功能哦!有源代码(delphi, c++)
- mono:第一个gnome程序
- 我的第一个JSP(Hello World)程序
- mono:第一个gtk#程序
- mono:第一个gtk#程序