项目代码,使用教程:GitHub - Dusongg/program_lobby: 大一程序设计 C/C++:基于EasyX的图形化程序设计,实现通讯录、扫雷、迷宫游戏的应用平台(含实验报告与答辩ppt)
目录
一、扫雷
功能简介:1. 棋盘设置 2. 设置模式 3. 红旗标志 4. 递归展开(DFS)
扫雷
二、迷宫
功能介绍: 1. 二维矩阵设置 2. 键盘输入 3. 自动求解(回溯)
迷宫
三、通讯录
功能介绍:1. 设计模板 2. 添加、删除、查找 3. 文件操作 4. 用户栏信息展开 5. 排序与优化
可视化通讯录
四、核心代码
1. Interface.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#undef UNICODE
#undef _UNICODE
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <easyx.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <graphics.h>
#include <windows.h>
#include <unordered_map>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
//1. Minesweeper
#define ROW 10
#define COL 10
#define ROWS ROW+2
#define COLS COL+2
#define MODE_EAZY 10
#define MODE_MID 15
#define MODE_HARD 20
void InitBoard(char Board[ROWS][COLS], int rows, int cols, int set);
void SetBoard(char Board[ROWS][COLS], int row, int col, int mode);
void StartToPlay(char inBoard[ROWS][COLS], char outBoard[ROWS][COLS], int row, int col, int mode, int* keep_playing);
//2. Mazegame
#define mROW 21
#define mCOL 29
void MazeGame();
//3. Contact
#define NAME_MAX 16
#define SEX_MAX 8
#define ADD_MAX 16
#define TELE_MAX 16
#define KEY_MAX 16
#define CON_MAX 32
#define AVA_MAX 32
typedef struct PeoInfo //通过学号定位
{
long long stu_number; //学号
char name[NAME_MAX];
char key[KEY_MAX]; //密码
int age;
char sex[SEX_MAX];
char address[ADD_MAX];
char tele[TELE_MAX];
char contact[CON_MAX]; //该学生的通讯录
char avatar[AVA_MAX]; //头像
}IP;
void ContactInter(IP info, unordered_map<long long, IP> ID, long long* saveId);
//基于map的所有用户管理
void LoadStudentsID(unordered_map<long long, IP>& ID);
void SaveStudentsID(long long* id, IP* info);
void DelstudentsID(long long del);
2.Minesweeper.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Interface.h"
void InitBoard(char Board[ROWS][COLS], int rows, int cols, int set) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Board[i][j] = set;
}
}
}
//踩雷之后展示整个棋盘
void DisPlayBoardGra(char Board[ROWS][COLS], int r, int c) {
for (int i = 1; i < ROWS-1; i++) {
for (int j = 1; j < COLS-1; j++) {
if (i == r && j == c) continue;
if (Board[i][j] - '0' == 1) {
IMAGE bomb;
loadimage(&bomb, "image_minesweeper\\bomb.jpg");
putimage(100 + (j - 1) * 40, 100 + (i - 1) * 40, &bomb);
}
else {
IMAGE blank2;
loadimage(&blank2, "image_minesweeper\\blank.jpg");
putimage(100 + (j - 1) * 40, 100 + (i - 1) * 40, &blank2);
}
Sleep(75);
}
}
}
//随机放置mode个数的雷到底层棋盘中
void SetBoard(char Board[ROWS][COLS], int row, int col, int mode){
int count = mode;
while (count){
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Board[x][y] != '1'){
Board[x][y] = '1';
count--;
}
}
}
//计算周围雷的数量
int Count(char inBoard[ROWS][COLS], int x, int y){
return (inBoard[x - 1][y - 1] + inBoard[x - 1][y] + inBoard[x][y - 1] +
inBoard[x + 1][y - 1] + inBoard[x + 1][y] + inBoard[x + 1][y + 1] +
inBoard[x][y + 1] + inBoard[x - 1][y + 1] - 8 * '0');
}
void LookForRay(char inBoard[ROWS][COLS], char outBoard[ROWS][COLS], int x, int y, int* win){
if (outBoard[x][y] == 'F') return; //递归到红旗返回
if (inBoard[x][y] == '0')
*win -= 1; //记录win
char sum = Count(inBoard, x, y); //计算周围雷的个数:越界问题?
outBoard[x][y] = sum + '0';
//显示该点周围雷的个数
char sum_str[30];
sprintf(sum_str, "image_minesweeper\\%d.jpg", sum); // 用sprintf合并字符串,通过文件相对位置找到对应图片
IMAGE number;
loadimage(&number, sum_str);
putimage(100 + (y - 1) * 40, 100 + (x - 1) * 40, &number);
//该坐标周围没有雷,想周围递归
if (outBoard[x][y] == '0'){
IMAGE blank;
loadimage(&blank, "image_minesweeper\\blank.jpg");
putimage(100 + (y - 1) * 40, 100 + (x - 1) * 40, &blank);
if (x - 1 >= 1 && x - 1 <= ROW && y - 1 >= 1 && y - 1 <= COL && outBoard[x - 1][y - 1] == '*') //递归条件:递归坐标未被排点,
LookForRay(inBoard, outBoard, x - 1, y - 1, win); //win为地址
if (x - 1 >= 1 && x - 1 <= ROW && y >= 1 && y <= COL && outBoard[x - 1][y] == '*')
LookForRay(inBoard, outBoard, x - 1, y, win);
if (x - 1 >= 1 && x - 1 <= ROW && y + 1 >= 1 && y + 1 <= COL && outBoard[x - 1][y + 1] == '*')
LookForRay(inBoard, outBoard, x - 1, y + 1, win);
if (x >= 1 && x <= ROW && y - 1 >= 1 && y - 1 <= COL && outBoard[x][y - 1] == '*')
LookForRay(inBoard, outBoard, x, y - 1, win);
if (x >= 1 && x <= ROW && y + 1 >= 1 && y + 1 <= COL && outBoard[x][y + 1] == '*')
LookForRay(inBoard, outBoard, x, y + 1, win);
if (x + 1 >= 1 && x + 1 <= ROW && y - 1 >= 1 && y - 1 <= COL && outBoard[x + 1][y - 1] == '*')
LookForRay(inBoard, outBoard, x + 1, y - 1, win);
if (x + 1 >= 1 && x + 1 <= R

该项目包含C/C++编程的三个应用:基于EasyX库的扫雷游戏,实现了不同难度级别;迷宫游戏,支持自动求解;以及一个通讯录系统,支持添加、删除和查找联系人。代码中还包含了实验报告和答辩PPT的相关资源。
最低0.47元/天 解锁文章
1579

被折叠的 条评论
为什么被折叠?



