您好,欢迎来到画鸵萌宠网。
搜索
您的当前位置:首页C语言的扫雷简化版,rabbitmq集群同步原理

C语言的扫雷简化版,rabbitmq集群同步原理

来源:画鸵萌宠网

先将数组初始化,以便后面雷的随机排放

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)

{

int i, j;

for (i = 0; i < rows; i++)

{

for (j = 0; j < cols; j++)

{

board[i][j] = set;

}

}

}

显示雷盘


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

打印雷盘,让雷盘显示在玩家眼中,同时将列数和行数打印出来以方便玩家选择坐标

void DisplayBoard(char board[ROWS][COLS], int row, int col)

{

int i,j;

printf("--------------------\n");

for (int i = 0; i <= col; i++)

{

printf("%d ",i);

}

printf("\n");

for (i=1;i<=row;i++)

{

printf("%d ",i);

for (j = 1; j <= col; j++)

{

printf("%c ",board[i][j]);

}

printf("\n");

}

printf("--------------------\n");

}

电脑随机布置雷


电脑随机布置雷,如果在坐标上有雷就会重新选择坐标

void SetMine(char board[ROWS][COLS], int row, int col)

{

int count = EASY_COUNT;

while (count)

{

int x = rand() % row + 1;

int y = rand() % col + 1;

if (board[x][y] != ‘1’)

{

board[x][y] = ‘1’;

count–;

}

}

}

排查雷


玩家输入一个坐标,看一个mine数组在这个坐标上是否是雷如果是就游戏结束

如果不是就检查周围雷的个数,再将该坐标周围雷的个数放置在数组show里面

int GetMineCount(char mine[ROWS][COLS], int x, int y)

{

return (mine[x - 1][y] +

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 - 1][y + 1] - 8 * ‘0’);

}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)

{

int win = 0;

int sum = row * col - EASY_COUNT;

int x=0, y=0;

while (win < sum)

{

printf(“请选择坐标>”);

scanf("%d %d",&x,&y);

if (x >= 1 && x <= row && y >= 1 && y <= col)

{

if (mine[x][y] == ‘1’)

{

printf(“哈哈哈,你被炸死了\n”);

DisplayBoard(mine, ROW, COL);

break;

}

else

{

int count = GetMineCount(mine, x, y);

show[x][y] = count + ‘0’;

DisplayBoard(show, ROW, COL);

win++;

}

}

else

{

printf(“输入的坐标错误\n”);

}

}

if (win == sum)

{

printf(“恭喜你,排雷成功\n”);

DisplayBoard(mine, ROW, COL);

}

}

总代码如下:


#pragma once

#include <stdlib.h>

#include <time.h>

#include<stdio.h>

#define ROW 9

#define COL 9

#define ROWS ROW+2

#define COLS COL+2

#define EASY_COUNT 10

//初始化雷盘

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

//显示雷盘

void DisplayBoard(char board[ROWS][COLS], int row, int col);

//布置雷

void SetMine(char board[ROWS][COLS], int row, int col);

//排查雷

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

//排查二维数组mine[x][y]周围雷的数目返回雷的个数

int GetMineCount(char mine[ROWS][COLS], int x, int y);

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)

{

int i, j;

for (i = 0; i < rows; i++)

{

for (j = 0; j < cols; j++)

{

board[i][j] = set;

}

}

}

int GetMineCount(char mine[ROWS][COLS], int x, int y)

{

return (mine[x - 1][y] +

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 - 1][y + 1] - 8 * ‘0’);

}

void DisplayBoard(char board[ROWS][COLS], int row, int col)

{

int i,j;

printf("--------------------\n");

for (int i = 0; i <= col; i++)

{

printf("%d ",i);

}

printf("\n");

for (i=1;i<=row;i++)

{

printf("%d ",i);

for (j = 1; j <= col; j++)

{

printf("%c ",board[i][j]);

}

printf("\n");

}

printf("--------------------\n");

}

void SetMine(char board[ROWS][COLS], int row, int col)

{

int count = EASY_COUNT;

while (count)

{

int x = rand() % row + 1;

int y = rand() % col + 1;

if (board[x][y] != ‘1’)

{

board[x][y] = ‘1’;

count–;

}

}

}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)

{

int win = 0;

int sum = row * col - EASY_COUNT;

int x=0, y=0;

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo8.com 版权所有 湘ICP备2023022238号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务