game.c
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"//初始化void init_board(char mine[ROWS][COLS], int rows, int cols, charset)
{int i = 0;int j = 0;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){mine[i][j] = set;}}
}
//打印棋盘void print_board(char mine[ROWS][COLS], int row, int col)
{int i = 0;int j = 0;for (j = 0; j <= col; j++){printf("%d ", j);}printf("\n");for (i = 1; i <= row; i++){printf("%d ", i);for (j = 1; j <= col; j++){printf("%c ", mine[i][j]);}printf("\n");}printf("\n");
}
//置雷void set_mine(char mine[ROWS][COLS], int row, int col)
{int count = EASY_COUNT;while (count){int x = rand() % 9 + 1;int y = rand() % 9 + 1;if (mine[x][y] == '0'){mine[x][y] = '1';count--;}}
}//移雷void move_mine(char mine[ROWS][COLS], int row, int col)
{while (1){int x = rand() % 9 + 1;int y = rand() % 9 + 1;if (mine[x][y] == '0'){mine[x][y] = '1';break;}}
}// 统计周围雷的个数int get_mine_count(char mine[ROWS][COLS], int x, int y)
{int num = 0;num = (mine[x - 1][y - 1] - '0') + (mine[x - 1][y] - '0')+ (mine[x - 1][y + 1] - '0') + (mine[x][y + 1] - '0')+ (mine[x + 1][y + 1] - '0') + (mine[x + 1][y] - '0')+ (mine[x + 1][y - 1] - '0') + (mine[x][y - 1] - '0');return num;
}//展开void reveal(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{int flag = get_mine_count(mine, x, y);if (flag == 0){show[x][y] = '0';if (x - 1 > 0 && y - 1 > 0 && show[x - 1][y - 1] == '*')reveal(mine, show, x - 1, y - 1, ROWS, COLS);if (x - 1 > 0 && y > 0 && show[x - 1][y] == '*')reveal(mine, show, x - 1, y, ROWS, COLS);if (x - 1 > 0 && y + 1 <= COL && show[x - 1][y + 1] == '*')reveal(mine, show, x - 1, y + 1, ROWS, COLS);if (x > 0 && y - 1 > 0 && show[x][y - 1] == '*')reveal(mine, show, x, y - 1, ROWS, COLS);if (x > 0 && y + 1 <= COL && show[x][y + 1] == '*')reveal(mine, show, x, y + 1, ROWS, COLS);if (x + 1 <= ROW && y - 1 > 0 && show[x + 1][y - 1] == '*')reveal(mine, show, x + 1, y - 1, ROWS, COLS);if (x + 1 <= ROW && y > 0 && show[x + 1][y] == '*')reveal(mine, show, x + 1, y, ROWS, COLS);if (x + 1 > 0 && y + 1 > 0 && show[x + 1][y + 1] == '*')reveal(mine, show, x + 1, y + 1, ROWS, COLS);}elseshow[x][y] = get_mine_count(mine, x, y) + '0';
}//排雷void clearn_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int num)
{int x = 0;int y = 0;int win = 0;int count = 0;while (win < COL*ROW-EASY_COUNT){printf("请输入坐标:>");scanf("%d,%d", &x,&y);count++;if (x >= 1 && x <= 9 && y >= 1 && y <= 9){if (count == 1 && mine[x][y] == '1'){move_mine(mine, row, col);//移雷print_board(mine, ROW, COL);}if (mine[x][y] == '1' && count != 1){printf("很遗憾,被炸死\n");print_board(mine, ROW, COL);break;}else{show[x][y] = get_mine_count(mine, x, y) + '0';reveal(mine, show, x, y);print_board(show, row, col);win++;}}else{printf("坐标有误, 请重新输入\n");}}if (win == ROW * COL - EASY_COUNT){printf("排雷成功\n");print_board(mine, row, col);}
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1#ifndef __GAME_H__#define __GAME_H__#include<stdio.h>#include<stdlib.h>#include<time.h>#define ROW 9#define COL 9#define ROWS ROW + 2#define COLS COL + 2#define EASY_COUNT 10void init_board(char mine[ROWS][COLS], int rows, int cols, charset);
void print_board(char mine[ROWS][COLS], int row, int col);
void set_mine(char mine[ROWS][COLS], int row, int col);
void clearn_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int get_mine_count(char mine[ROWS][COLS], int x, int y);
void move_mine(char mine[ROWS][COLS], int row, int col);
void reveal(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);#endif//__GAME_H__
【题目描述】
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several diff…
栈桢
首先来看一段代码
#include<stdio.h>
int add(int x, int y)
{int z x y;return z;
}
int main()
{int a 10;int b 20;int ret add(a, b);printf("ret %d\n",ret);return 0;
} 此处是为了给a,b分别开辟空间,这时栈桢如图所示 两条push命令将a,b变…
http://blog.csdn.net/lanxuezaipiao/article/details/41603883 导读 最近在补看《C Primer Plus》第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑。C面试过程中,很多面试官都喜欢问智能指针相…
【题目描述】
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j;…
【题目描述】 HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between ax …