18,356
社区成员
发帖
与我相关
我的任务
分享
#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 11
#define COLS 11
#define MAX 10
#define ROW (ROWS-2)
#define COL (COLS-2)
void init_board(char mine[ROWS][COLS],char set);
void set_mine(char mine[ROWS][COLS]);
void display(char mine[ROWS][COLS]);
int get_mine_count(char mine[ROWS][COLS], int row, int col);
void showblank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#endif//__GAME_H__
#include"game.h"
void init_board(char mine[ROWS][COLS], char set)
{
memset(mine, set, ROWS*COLS*sizeof(mine[0][0]));
}
void set_mine(char mine[ROWS][COLS])
{
int count = MAX;
while (count > 0)
{
int x = rand() % ROW + 1;
int y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void display(char mine[ROWS][COLS])
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i <= ROW; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= ROW; i++)
{
printf("%d ",i);
for (j = 1; j <= COL; j++)
{
printf("%c ",mine[i][j]);
}
printf("\n");
}
}
int get_mine_count(char mine[ROWS][COLS],int x,int y)
{
return mine[x - 1][y - 1] + mine[x][y - 1] +
mine[x + 1][y - 1] + mine[x + 1][y] +
mine[x + 1][y + 1] + mine[x][y + 1] + mine[x ][y - 1]
+ mine[x - 1][y] - 8 * '0';
}
void showblank( char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
int set = mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';
show[x][y] = set+'0';
if (show[x][y] == 0)
{
if (x <= ROW&&x >= 1 && y <= COL&&y >= 1 && '1' == show[x][y])
{
showblank(mine, show, x - 1, y - 1);
showblank(mine, show, x - 1, y);
showblank(mine, show, x - 1, y + 1);
showblank(mine, show, x, y - 1);
showblank(mine, show, x, y + 1);
showblank(mine, show, x + 1, y - 1);
showblank(mine, show, x + 1, y);
showblank(mine, show, x + 1, y + 1);
}
}
}
#include "game.h"
#include <stdio.h>
#pragma warning (disable:4996)
void menu()
{
printf("*********************************\n");
printf("**********1.play 0.exit*********\n");
printf("*********************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
init_board(mine,'0');
init_board(show, '*');
set_mine(mine);
display(show);
int x = 0;
int y = 0;
int count = 0;
while (count < ROW*COL - MAX)
{
printf("你认为哪颗不是雷:\n");
scanf("%d%d", &x, &y);
if ((x >= 1) && (x <= ROW) && (y >= 1) && (y <= COL))
{
if (mine[x][y] == '1')
{
printf("炸死了!\n");
display(mine);
break;
}
else
{
int set = get_mine_count(mine, x, y);
show[x][y] = set + '0';
showblank(mine, show, x, y);
count++;
display(show);
}
}
else
{
printf("坐标有误,请重新输入!\n");
}
}
if (count == ROW * COL - MAX)
{
printf("排雷完毕,危险解除\n");
display(mine);
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择开始还是结束:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
case 0:
break;
default:
printf("选择有误,请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ROWS 11
#define COLS 11
#define MAX 10
#define ROW (ROWS-2)
#define COL (COLS-2)
void digui(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y);
void init_board(char mine[ROWS][COLS], char set)
{
memset(mine, set, ROWS*COLS*sizeof(mine[0][0]));
}
void set_mine(char mine[ROWS][COLS])
{
int count = MAX;
while (count > 0)
{
int x = rand() % ROW;
int y = rand() % COL;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void display(char mine[ROWS][COLS])
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i <= ROW; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= ROW; i++)
{
printf("%d ",i);
for (j = 1; j <= COL; j++)
{
printf("%c ",mine[i - 1][j-1]);
}
printf("\n");
}
}
int get_mine_count(char mine[ROWS][COLS],int x,int y)
{
int nCount = 0;
////////////////////////////////////////
if (((x - 1) >= 0) && ((y - 1) >= 0))
{
if (mine[x-1][y - 1] == '1')
{
nCount++;
}
}
if ((x - 1) >= 0)
{
if (mine[x-1][y] == '1')
{
nCount++;
}
}
if (((x - 1) >= 0) && ((y + 1) < COL))
{
if (mine[x-1][y+1] == '1')
{
nCount++;
}
}
////////////////////////////////////////
if (((y - 1) >= 0))
{
if (mine[x][y - 1] == '1')
{
nCount++;
}
}
if (((y + 1) < COL))
{
if (mine[x][y+1] == '1')
{
nCount++;
}
}
////////////////////////////////////////
////////////////////////////////////////
if (((x + 1) < ROWS) && ((y - 1) >= 0))
{
if (mine[x+1][y - 1] == '1')
{
nCount++;
}
}
if (((x + 1) < ROWS))
{
if (mine[x+1][y] == '1')
{
nCount++;
}
}
if (((x + 1) < ROWS) && ((y + 1) < COL))
{
if (mine[x+1][y+1] == '1')
{
nCount++;
}
}
return nCount;
}
void showblank( char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
if (x < ROW&&x >= 0 && y < COL&&y >= 0 && '0' == mine[x][y] && '*' == show[x][y])
{
int set = get_mine_count(mine, x, y);
show[x][y] = set + '0';
if (set == 0)
{
digui(mine, show, x, y);
}
}
}
void digui(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
showblank(mine, show, x - 1, y - 1);
showblank(mine, show, x - 1, y);
showblank(mine, show, x - 1, y + 1);
showblank(mine, show, x, y - 1);
showblank(mine, show, x, y + 1);
showblank(mine, show, x + 1, y - 1);
showblank(mine, show, x + 1, y);
showblank(mine, show, x + 1, y + 1);
}
void menu()
{
printf("*********************************\n");
printf("**********1.play 0.exit*********\n");
printf("*********************************\n");
}
bool isEnd(char show[ROWS][COLS])
{
int nCount = 0;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (show[i][j] == '*')
{
nCount++;
}
}
}
if (nCount == MAX)
{
return true;
}
else
{
return false;
}
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
init_board(mine,'0');
init_board(show, '*');
set_mine(mine);
display(show);
int x = 0;
int y = 0;
int count = 0;
while (!isEnd(show))
{
printf("你认为哪颗不是雷:\n");
scanf("%d%d", &x, &y);
if ((x >= 1) && (x <= ROW) && (y >= 1) && (y <= COL))
{
x = x - 1;
y = y - 1;
if (mine[x][y] == '1')
{
printf("炸死了!\n");
display(mine);
return;
}
else
{
int set = get_mine_count(mine, x, y);
show[x][y] = set + '0';
digui(mine, show, x, y);
count++;
display(show);
}
}
else
{
printf("坐标有误,请重新输入!\n");
}
}
printf("排雷完毕,危险解除\n");
display(mine);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择开始还是结束:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
case 0:
break;
default:
printf("选择有误,请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
改吐了 void step0(int xx,int yy) {
byte n;
if (A[xx+yy*10]!=4) return;
n=0;
if (xx>=1 && yy>=1) n+=B[(xx-1)+(yy-1)*10];
if ( yy>=1) n+=B[(xx )+(yy-1)*10];
if (xx<=8 && yy>=1) n+=B[(xx+1)+(yy-1)*10];
if (xx>=1 ) n+=B[(xx-1)+(yy )*10];
if (xx<=8 ) n+=B[(xx+1)+(yy )*10];
if (xx>=1 && yy<=8) n+=B[(xx-1)+(yy+1)*10];
if ( yy<=8) n+=B[(xx )+(yy+1)*10];
if (xx<=8 && yy<=8) n+=B[(xx+1)+(yy+1)*10];
A[xx+yy*10]=(byte)(n+5);
if (n==0) {
if (xx>=1 && yy>=1) step0(xx-1,yy-1);
if ( yy>=1) step0(xx ,yy-1);
if (xx<=8 && yy>=1) step0(xx+1,yy-1);
if (xx>=1 ) step0(xx-1,yy );
if (xx<=8 ) step0(xx+1,yy );
if (xx>=1 && yy<=8) step0(xx-1,yy+1);
if ( yy<=8) step0(xx ,yy+1);
if (xx<=8 && yy<=8) step0(xx+1,yy+1);
}
}