三个小游戏的实现

三子棋的实现

#pragma once
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#define ROW 3
#define COL 3

void init_board(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
void play_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include "games.h"
void init_board(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0; i < row; i++) {
		int j = 0;
		for (j = 0; j < col; j++) {
			board[i][j] = ' ';
		}
	}
}
void print_board(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0; i < row; i++) {

		//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);		
		//if (i < row - 1) {
			//printf("---|---|---\n");
		//}
		int j = 0;
		for (j = 0; j < col; j++) {
			printf(" %c ", board[i][j]);
			if (j < col - 1) {
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1) {
			for (j = 0; j < col; j++) {
				 printf("---");
				if (j < col - 1) {
					printf("|");
				}
			}
			printf("\n");
		}
	}
}
void play_move(char board[ROW][COL], int row, int col) {
	printf("玩家下棋->");
	while (1) {
		int x = 0;
		int y = 0;
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (board[x - 1][y - 1] == ' ') {
				board[x - 1][y - 1] = '*';
				break;
			}
			else {
				printf("坐标被占用,重新输入->");
			}
		}
		else {
			printf("非法坐标");
		}
	}
}
void computer_move(char board[ROW][COL], int row, int col) {
	printf("电脑下棋->\n");
	while (1) {
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ') {
			board[x][y] = '#';
			break;
		}
	}
}
int is_full(char board[ROW][COL], int row, int col) {
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++) {
		for (j = 0; j < col; j++) {
			if (board[i][j] == ' ') {
				return 0;
			}
		}
	}
	return 1;
}
char is_win(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0; i < row; i++) {
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
			return board[i][0];
		}
	}
	for (i = 0; i < col; i++) {
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
			return board[0][i];
		}
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (is_full(board, row, col) == 1) {
		return 'Q';
	}
	return 'C';
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include "games.h"
void menu() {
	printf("**********************\n");
	printf("******* 1 - play *****\n");
	printf("******* 0- exit  *****\n");
	printf("**********************\n");
}
void game() {
	char ret = 0;
	char board[ROW][COL];
	//初始化棋盘
	init_board(board, ROW, COL);
	print_board(board, ROW, COL);
	while (1) {
		play_move(board, ROW, COL);
		print_board(board, ROW, COL);
		//判断输赢
		ret = is_win(board, ROW, COL);
		if (ret != 'C') {
			break;
		}
		computer_move(board, ROW, COL);
		print_board(board, ROW, COL);
		//判断输赢
		ret = is_win(board, ROW, COL);
		if (ret != 'C') {
			break;
		}
   }
	if (ret == "#") {
		printf("电脑赢\n");
	}
	else if (ret == '*') {
		printf("玩家赢\n");
	}
	else {
		printf("平局\n");
	}
	
}
void test() {
	srand((unsigned int)time(NULL));
	int input = 0;
	do {
		menu();
		printf("请选择->");
		scanf("%d",&input);
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏");
			break;
		default :
			printf("选择错误,重新输入\n");
			break;
		}
	} while (input);
}
int main() {
	test();
}

扫雷的实现

#pragma once
#include<stdio.h>
#define EASY_COUNT 15
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void init_board(char board[ROWS][COLS], int rows, int cols,char ret);
void display_board(char show[ROWS][COLS], int row, int col);
void set_board(char mine[ROWS][COLS], int row, int col);
void find_board(char mine[ROWS][COLS],char show[ROWS][COLS],int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(char board[ROWS][COLS], int rows, int cols,char ret) {
	int i = 0;
	for (i = 0; i < rows; i++) {
		int j = 0;
		for (j = 0; j < cols; j++) {
			board[i][j] = ret;
		}
	}
}
void display_board(char show[ROWS][COLS], int row, int col) {
	int i = 0;
	for (i = 0; i <= col; i++) {
		printf("%d ", i);
	}
	printf("\n");
	int j = 0;
	for (i = 1; i <= row; i++) {
		printf("%d ", i);
		for (j = 1; j <=col; j++) {
			printf("%c ", show[i][j]);
		}
		printf("\n");
	}
}
void set_board(char mine[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int count = EASY_COUNT;
	while (count) {
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0') {
			mine[x][y] = '1';
		}
		count--;
	}
}
int get_mine_count(char mine[ROWS][COLS], int x, int y) {
	return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x + 1][y + 1] + mine[x][y - 1] + mine[x][y + 1]
		+ mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x - 1][y] - 8 * '0';
}
void find_board(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row * col - EASY_COUNT) {
		printf("请输入你要排查的坐标->");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x<=row && y >= 0 && y<=col) {
			if (show[x][y] != '*') {
				printf("该坐标已经排查过了,重新输入->\n");
				continue;
			}
			else if (mine[x][y] == '1') {
				printf("很遗憾,你被炸死了,游戏结束\n");
				break;
			}
			else {
				int n = get_mine_count(mine, x, y);
				show[x][y] = n+'0';
				display_board(show, ROW, COL);
				win++;
			}
			
		}
		else {
			printf("非法坐标,请重新输入->\n");
		}
	}
	if (win == row + col - EASY_COUNT) {
		printf("排雷成功");
	}
}

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include "game.h"
void menu() {
	printf("**************************\n");
	printf("********1 - play *********\n");
	printf("********0 - exit *********\n");
	printf("**************************\n");
}
void game() {
	//初始化棋盘
	srand((unsigned int)time(NULL));
	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	init_board(mine, ROWS, COLS, '0');
	init_board(show, ROWS, COLS, '*');
	display_board(show, ROW, COL);
	//布置雷
	set_board(mine, ROW, COL);
	//display_board(mine, ROW, COL);
	//排查雷的数目
	find_board(mine, show, ROW, COL);
}
int main() {
	int input = 0;
	
	do {
		menu();
		printf("请选择->");
		scanf("%d", &input);
		switch (input) {
		case 1:
			printf("开始游戏->\n");
			game();
			break;
		case 0:
			printf("退出游戏->\n1 ");
			break;
		default:
			printf("选择错误,重新选择->\n");
			break;
		}
	} while (input);
}

通讯录的实现

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define NAME_MAX 20
#define SEX_MAX  10
#define DEFAULT_SZ 3//初始化容量
#define size 2
typedef struct stu {
	char name[NAME_MAX];
	int age;
	float score;
	char sex[SEX_MAX];
}postal;
typedef struct s {
	postal* ptf;
	int sz;
	//初始化容量,后面增加的
	int capacity;
}Contact;
void InitContact(Contact* pc);
void AddContact(Contact* pc);
void ShowContact(Contact* pc);
void SearchContact(Contact* pc);
void DelContact(Contact* pc);
void ModifyContact(Contact* pc);
void DestoryContact(Contact* pc);
void SaveContact(Contact* pc);
void LoadContact(Contact* pc);

#define _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"
//void InitContact(Contact* pc) {
//	pc->sz = 0;
//	//我是这样想的,ptf是一个指针类型,存放好几个内存的数据,但是得释放
//	pc->ptf = (postal*)malloc(sizeof(postal) * (pc->sz+1));
//}
//实现动态内存初始化数组
void InitContact(Contact* pc) {
	pc->sz = 0;
	pc->ptf = (postal*)calloc(DEFAULT_SZ, sizeof(postal));
	pc->capacity = DEFAULT_SZ;
	LoadContact(pc);
}
void check_capacity(Contact* pc) {
	assert(pc);
	if (pc->capacity == pc->sz) {
	
		postal* pf = (postal*)realloc(pc->ptf, (pc->capacity + size) * sizeof(postal));
		if (pf == NULL) {
			perror("realloc");
		}
		pc->ptf = pf;
		pc->capacity += size;
	}
      printf("开辟成功\n");
}
void AddContact(Contact* pc) {
	assert(pc);
	printf("输入年纪->");
	scanf("%d", &(pc->ptf[pc->sz].age));
	printf("输入名字->");
	scanf("%s", pc->ptf[pc->sz].name);
	printf("输入分数->");
	scanf("%f", &(pc->ptf[pc->sz].score));
	printf("输入性别->");
	scanf("%s", pc->ptf[pc->sz].sex);
	pc->sz++;
	if (pc->capacity == pc->sz) {
		check_capacity(pc);
	}
}
void ShowContact(Contact* pc) {
	printf("%-20s\t%-15s\t%-15s\t%-15s\n", "年纪", "姓名", "分数", "性别");
	int i = 0;
	int pos = 0;
	for (i = 0; i < pc->sz; i++) {
		pos = i;
		printf("%-20d\t%-15s\t%-15f\t%-15s\n", pc->ptf[pos].age, pc->ptf[pos].name,
			pc->ptf[pos].score, pc->ptf[pos].sex);
	}
}
int is_isist(char name[], Contact* pc) {
	int i = 0;
	for (i = 0; i < pc->sz; i++) {
		if (strcmp(pc->ptf[i].name, name) == 0) {
			return i;
		}
	}
	return -1;
}
void SearchContact(Contact* pc) {
	char name[NAME_MAX] = { 0 };
	printf("输入你要查找的姓名->");

	scanf("%s", name);
	int pos = is_isist(name, pc);
	if (pos == -1) {
		printf("查找的人不存在");
	}
	else {
		printf("%-20s\t%-15s\t%-15s\t%-15s\n", "年纪", "姓名", "分数", "性别");
		printf("%-20d\t%-15s\t%-15f\t%-15s\n", pc->ptf[pos].age, pc->ptf[pos].name,
			pc->ptf[pos].score, pc->ptf[pos].sex);
	}
}
void DelContact(Contact* pc) {
	char name[NAME_MAX];
	printf("请输入你要删除的姓名->");
	scanf("%s", name);
	int pos = is_isist(name, pc);
	if (pos == -1) {
		printf("删除的人不存在");
	}
	else {
		int i = 0;
		for (i = pos; i < pc->sz-1; i++) {
			//假如有10个元素,当pos等于9的时候,第十个元素替代他
			pc->ptf[pos] = pc->ptf[pos + 1];
		}
		pc->sz--;
	}
	printf("删除成功!\n");
}
void ModifyContact(Contact* pc) {
	char name[NAME_MAX];
	printf("请输入你要修改的姓名->");
	scanf("%s", name);
	int pos = is_isist(name, pc);
	if (pos == -1) {
		printf("修改的人不存在");
	}
	else {
		printf("输入年纪->");
		scanf("%d", &(pc->ptf[pos].age));
		printf("输入名字->");
		scanf("%s", pc->ptf[pos].name);
		printf("输入分数->");
		scanf("%f", &(pc->ptf[pos].score));
		printf("输入性别->");
		scanf("%s", pc->ptf[pos].sex);
	}
	printf("修改成功!\n");
}
void DestoryContact(Contact* pc) {
	free(pc->ptf);
	pc->ptf = NULL;
	pc = NULL;
	pc->capacity = 0;
	pc->sz = 0;
}
void SaveContact(Contact* pc) {
	int i = 0;
	assert(pc);
	FILE* pf = fopen("contact.txt", "wb");
	if (pf == NULL) {
		perror("fopen");
		return;
	}

	for (i = 0; i < pc->sz; i++) {
		fwrite(pc->ptf, sizeof(postal), 1, pf);
	}
	printf("保存成功!");
	fclose(pf);
	pf = NULL;
}
void LoadContact(Contact* pc) {
	//读文件
	//1.打开文件
	FILE* ptr = fopen("contact.txt", "rb");
	if (ptr == NULL) {
		perror("fopen");
	}
	else {
		//2.读数据
		postal tmp = {0};
		int i = 0;
		while (fread(&tmp, sizeof(postal), 1, ptr)&&i<pc->sz) {
			check_capacity(pc);
			pc->ptf[i] = tmp;
			//pc->sz++;
			i++;
		}
		fclose(ptr);
		ptr = NULL;
	}
}

这最后的数据读取和写入可能有些问题,由于比较忙,没有来得及改,大家可以自己研究一下

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include "Contact.h"
void menu() {
	printf("***************************\n");
	printf("************6 - free*******\n");
	printf("******* 0-exit 2-show******\n");
	printf("***** 3-Search 4-distory***\n");
	printf("***** 1-Add 5-Modify********\n");
	printf("***************************\n");
}
int main() {
	int input = 0;
	printf("请输入你要选择的数字->");
	scanf("%d", &input);
	Contact con;
	InitContact(&con);
	do {
		menu();	
		scanf("%d", &input);
		switch (input) {
		case 1:
			AddContact(&con);
			break;
		case 2:
			ShowContact(&con);
			break;
		case 3:
			SearchContact(&con);
			break;
		case 4:
			DelContact(&con);
			break;
		case 5:
			ModifyContact(&con);
			break;
		case 0:
			SaveContact(&con);
			DestoryContact(&con);
			printf("退出游戏\n");
			break;
		default :
			printf("选择错误,重新选择->");
			break;
		}
	} while (input);
}

由于国庆回家后有点忙,文章就是提供了一下代码,有问题的话可以自己探讨和研究,回学校后我会纠正这些问题的

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值