博客
关于我
c语言学习小结_05_扫雷小游戏和三子棋小游戏
阅读量:84 次
发布时间:2019-02-25

本文共 6059 字,大约阅读时间需要 20 分钟。

一、 main.c是小游戏的主要的核心逻辑代码,内部的实现细节在saolei.c中。使用多文件结构实现。
//main.c#include "saolei.h"int main(){   	int quit = 0;	int select = 0;	while (!quit){   		Menu();		scanf("%d", &select);		switch (select){   		case 1:			Game();			printf("要不要再来一把!\n");			break;		case 2:			quit = 1;			break;		default:			printf("输入错误!请重新输入!");			break;		}	}	printf("ByeBye!\n");	system("pause");	return 0;}
//saolei.h#ifndef _SAOLEI_H#define _SAOLEI_H#include
#include
#include
#include
#pragma warning(disable:4996)#define ROW 7#define COL 7#define NUMS 5void Menu();void Game();#endif
//saolei.c#include "saolei.h"void Menu(){   	printf("************************\n");	printf("****1、Play  2 Exit*****\n");	printf("please input your select:");	}static void ShowLine(int nums){   	printf("---");	for (int i = 0; i < nums; i++){   		printf("-");	}	printf("\n");}void ShowBoard(char show_board[][COL], int row, int col){   	printf("   ");	for (int i = 1; i < row - 1; i++){   		printf(" %d  ", i);	}	printf("\n");	ShowLine(2 * col + col + 4);	for (int i = 1; i < row - 1; i++){   		printf("%2d|", i);		for (int j = 1; j < col - 1; j++){   			printf(" %c |", show_board[i][j]);		}		printf("\n");		ShowLine(2 * col + col + 4);	}}void SetMines(char mine_board[][COL], int row, int col){   	int count = NUMS;	while (count){   		int x = rand() % 5 + 1;		int y = rand() % 5 + 1;		if (mine_board[x][y] == '0'){   			mine_board[x][y] = '1';			count--;		}	}}int GetMines(char mine_board[][COL], int row, int col, int x, int y){   	return mine_board[x - 1][y - 1] + mine_board[x - 1][y] + mine_board[x - 1][y + 1] + \		mine_board[x][y - 1] + mine_board[x][y + 1] + mine_board[x + 1][y - 1] + \		mine_board[x + 1][y] + mine_board[x + 1][y + 1] - 8 * '0';}void Game(){   	char show_board[ROW][COL];	char mine_board[ROW][COL];	//初始化两个二维数组,* 和 ‘0’	memset(show_board, '*', sizeof(show_board));	memset(mine_board, '0', sizeof(mine_board));	srand((unsigned long)time(NULL));	SetMines(mine_board,ROW,COL);//随机设置雷到mine_board			int count = (ROW - 2)*(COL - 2) - NUMS;	int x = 0;	int y = 0;	do{   		//开始扫雷		ShowBoard(show_board, ROW, COL);		printf("请输入位置:");		scanf("%d %d", &x, &y);		if (x<1 || x>10 || y<1 || y>10){   			printf("输入越界请重新输入!\n");			continue;		}		if (show_board[x][y] != '*'){   			printf("这个位置已经排过雷了,请重新输入!!\n");			continue;		}		if (mine_board[x][y] == '1'){   			break;//炸死了		}		//走到这里说明没有雷		//需要统计这个位置周围点的坐标		int num = GetMines(mine_board,ROW,COL,x, y);//获得周围雷的个数		//修改show_board		show_board[x][y] = num + '0';		count--;		system("cls");	} while (count > 0);	//根据count可以判断是否是炸死的	if (count > 0){   		printf("你被炸死了!\n");	}	else {   		printf("你赢了!\n");	}	printf("下面是雷区的排布!\n");	ShowBoard(mine_board, ROW, COL);}

二、

main.c是小游戏的主要的核心逻辑代码,内部的实现细节在sanziqi.c中。使用多文件结构实现。

// main.c#include "japplet.h"//三子棋游戏int main(){   	int quit = 0;	int select = 0;	while (!quit){   		Menu();//菜单		scanf("%d", &select);		switch (select)		{   		case 1:			Game();			printf("要不要再来一把!\n");			break;		case 2:			quit = 1;			break;		default:			printf("你输入有误!请重新输入!");			break;		}	}	printf("ByeBye!\n");	system("pause");	return 0;}
//japplet.h#ifndef _GAME_H_#define _GAME_H_#include
#include
#include
#pragma warning(disable:4996)//3*3的棋盘#define ROW 3#define COL 3//玩家和电脑的棋子#define PLAYER_CHESS 'X'#define COMPUTER_CHESS 'O'#define PLAYING 'P'#define DRAW 'D'//函数的声明void Menu();void Game();#endif
//japplet.c#include "japplet.h"//函数的定义void Menu(){   	printf("*****************************\n");	printf("*******1、play   2、exit ****\n");	printf("*****************************\n");	printf("please input your select:");}static void PlayerMove(char board[][COL], int row, int col){   	int x = 0;	int y = 0;	int quit = 0;	while (!quit){   		printf("请下棋(输入落子的位置):");		scanf("%d %d", &x, &y);		//验证落子的位置是否不妥		if (x<1 || x>3 || y<1 || y>3){   			printf("输入位置有误!请重新输入!");			continue;		}		if (board[x - 1][y - 1] != ' '){   			printf("你输入的位置已经有棋子了!请重新输入!");			continue;		}		board[x - 1][y - 1] = PLAYER_CHESS;		quit = 1;	}}static char Judge(char board[][COL], int row, int col){   	//检测行是否三子连珠	for (int 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 (int 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[0][0] != ' '){   		return board[0][0];	}	//检测负对角线是否三子连珠	if (board[0][2] == board[1][1] && \		board[1][1] == board[2][0] && \		board[1][1] != ' '){   		return board[0][2];	}	//还在游戏中并没有下满,并且还没有三子连珠,程序能运行到这里说明前面都不符合	for (int i = 0; i < row; i++){   		for (int j = 0; j < col; j++){   			if (board[i][j] == ' '){   				return PLAYING;			}		}	}	return DRAW;}static void ComputerMove(char board[][COL], int row, int col){   	while (1){   		int x = rand() % row;		int y = rand() % col;		if (board[x][y] == ' '){   			board[x][y] = COMPUTER_CHESS;			break;		}	}}void ShowBoard(char board[][COL], int row, int col){   	printf("  | 1 | 2 | 3 |\n");	printf("---------------\n");	for (int i = 0; i < row; i++){   		printf("%d |", i + 1);		for (int j = 0; j < col; j++){   			printf(" %c |", board[i][j]);		}		printf("\n---------------\n");	}}//游戏的主要逻辑void Game(){   	srand((unsigned long)time(NULL));//随机数种子	 //棋盘的定义和初始化	char board[ROW][COL];	memset(board, ' ', sizeof(board));	char result = 'x';	do{   		ShowBoard(board,ROW,COL);		PlayerMove(board,ROW,COL);		result = Judge(board, ROW, COL);		if (result != PLAYING){   			//证明此时的结果只有胜负和平局(反正就是已经出结果了)			break;		}		ComputerMove(board, ROW, COL);		result = Judge(board, ROW, COL);		if (result != PLAYING){   			//证明此时的结果只有胜负和平局(反正就是已经出结果了)			break;		}	} while (1);	if (PLAYER_CHESS==result){   		printf("恭喜你,你赢了!\n");	}	else if (COMPUTER_CHESS == result){   		printf("很遗憾你输了!\n");	}	else{   		printf("平局!\n");	}	printf("¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥\n");	ShowBoard(board, ROW, COL);}

转载地址:http://rfp.baihongyu.com/

你可能感兴趣的文章
Mysql 纵表转换为横表
查看>>
mysql 编译安装 window篇
查看>>
mysql 网络目录_联机目录数据库
查看>>
MySQL 聚簇索引&&二级索引&&辅助索引
查看>>
Mysql 脏页 脏读 脏数据
查看>>
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>
Mysql 表分区
查看>>
mysql 表的操作
查看>>
mysql 视图,视图更新删除
查看>>
MySQL 触发器
查看>>
mysql 让所有IP访问数据库
查看>>
mysql 记录的增删改查
查看>>
MySQL 设置数据库的隔离级别
查看>>
MySQL 证明为什么用limit时,offset很大会影响性能
查看>>
Mysql 语句操作索引SQL语句
查看>>
MySQL 误操作后数据恢复(update,delete忘加where条件)
查看>>
MySQL 调优/优化的 101 个建议!
查看>>
mysql 转义字符用法_MySql 转义字符的使用说明
查看>>