C语言学习笔记(4)

C语言学习笔记(4)

笔记内容:

  1. 多文件三子棋小游戏;
  2. 多文件扫雷小游戏;

一、三子棋
1.头文件

#ifndef _GAME_H_
#define _GAME_H_

#include<stdio.h>
#include<windows.h>
#include<time.h>
#pragma warning(disable:4996)

void Menu();
void Game();

#define ROW 3
#define COL 3
#define P_COLOR 'X'
#define C_COLOR 'O'

#endif

2.游戏源文件

#include "game.h"

void Menu()
{
	printf("############################\n");
	printf("##1.Play            2.Exit##\n");
	printf("############################\n");
	printf("Please Enter:");
}

void DrawBoard(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 PlayMove(char board[][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1){
		printf("Please Enter Your Pos:");
		scanf("%d %d", &x, &y);
		if (x<1 || x>3 || y<1 || y>3){
			printf("Pos Error!\n");
			continue;
		}
		if (board[x - 1][y - 1] != ' '){
			printf("Chress Exist!\n");
			continue;
		}
		board[x - 1][y - 1] = P_COLOR;
		return;
	}
}
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[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];
	}
	for (int i = 0; i < row;i++){
		for (int j = 0; j < col;j++){
			if (board[i][j] == ' ')
				return 'N';
		}
	}
	return 'P';
}
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] = C_COLOR;
			break;
		}
	}
}

void Game()
{
	srand((unsigned long)time(NULL));
	char board[ROW][COL];
	memset(board, ' ', sizeof(board));
	char win = '\0';
	do{
		DrawBoard(board,ROW,COL);
		PlayMove(board, ROW, COL);
		win = Judge(board, ROW, COL);
		if (win != 'N'){
			break;
		}
		ComputerMove(board, ROW, COL);
		Judge(board, ROW, COL);
		if (win != 'N'){
			break;
		}
	} while (1);
	switch (win){
	case P_COLOR:
		printf("you win!\n");
		break;
	case C_COLOR:
		printf("you lose!\n");
		break;
	case 'P':
		printf("平局\n");
		break;
	default:
		break;
	}
	DrawBoard(board, ROW, COL);
}

3.主源文件

#include "game.h"

int main()
{
	int quit = 0;
	while (quit!=1){
		Menu();
		int select = 0;
		scanf("%d", &select);
		switch (select){
		case 1:
			Game();
			break;
		case 2:
			quit = 1;
			printf("quit!");
			break;
		default:
			printf("Enter Error! Please Try Again!\n");
		}
	}
	printf("ByeBye!\n");
	system("pause");
	return 0;
}

二、扫雷
1.头文件

#ifndef _GAME_H_
#define _GAME_H_

#include<stdio.h>
#include<windows.h>
#include<string.h>
#include<time.h>
#pragma warning(disable:4996)

void Menu();
void Game();

#define ROW 12
#define	COL 12
#define NUMS 20

#endif

2.游戏源文件

#include "game.h"

void Menu()
{
	printf("#######################################\n");
	printf("### 1.开始 ################## 2.退出 ##\n");
	printf("#######################################\n");
	printf("请选择:");
}

void ShowBoard(char board[][COL], int row, int col)
{
	printf("   ");
	for (int i = 1; i < col - 1; i++){
		printf(" %d  ",i);
	}
	printf("\n   ----------------------------------------\n");
	for (int i = 1; i < row - 1; i++){
		printf("%2d|", i);
		for (int j = 1; j < col - 1; j++){
			printf(" %c |",board[i][j]);
		}
		printf("\n   ----------------------------------------\n");
	}
}

void SetMines(char board[][COL], int row, int col)
{
	int num = NUMS;
	while (num){
		int x = rand() % 10 + 1;
		int y = rand() % 10 + 1;
		if (board[x][y] == '0'){
			board[x][y] = '1';
			num--;
		}
	}
}

int Getnum(char board[][COL], int row, int col, int x, int y)
{
	return board[x - 1][y - 1] + board[x - 1][y] + \
		   board[x - 1][y + 1] + board[x][y + 1] + \
		   board[x + 1][y + 1] + board[x + 1][y] + \
		   board[x + 1][y - 1] + board[x][y - 1] - 8 * '0';
}

void Game()
{
	srand((unsigned long)time(NULL));
	char show_board[ROW][COL];
	char mine_board[ROW][COL];
	memset(show_board, '*', sizeof(show_board));
	memset(mine_board, '0', sizeof(mine_board));

	SetMines(mine_board, ROW, COL);
	int x = 0;
	int y = 0;
	int count = (ROW - 2)*(COL - 2) - NUMS;
	do{
		system("cls");
		ShowBoard(show_board, ROW, COL);
		printf("请输入坐标:");
		scanf("%d %d", &x, &y);
		if (x<1 || x>ROW - 2 || y<1 || y>COL - 2){
			printf("输入有误,请重新输入!\n");
			continue;
		}
		if (show_board[x][y] != '*'){
			printf("位置已输入过!\n");
			continue;
		}
		if (mine_board[x][y] == '1'){
			break;
		}
		int num = Getnum(mine_board, ROW, COL, x, y);
		show_board[x][y] = num + '0';
		count--;
	} while (count > 0);
	if (count > 0){
		printf("你被炸死了!\n");
	}
	else{
		printf("恭喜你,通过游戏!\n");
	}
}

3.主源文件

#include "game.h"

int main()
{
	int quit = 0;
	int select = 0;
	while (quit != 1){
		Menu();
		scanf("%d", &select);
		switch (select){
		case 1:
			Game();
			break;
		case 2:
			quit = 1;
			break;
		default:
			printf("输入有误,请重新选择:");
			break;
		}
	}
	printf("ByeBye!");
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值