【【C语言康复训练-3】】

C语言康复训练3

head.c

#define _CRT_SECURE_NO_WARNINGS  1
#include
#include
#include
#include"project.h"


int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1 : game(); break;
		case 0 : printf("爱玩玩,不玩滚\n"); break;
		default: printf("请重新输入\n"); break;
		}
	} while (input);
	return 0;
}

project,c

#define _CRT_SECURE_NO_WARNINGS  1
#include
#include
#include
#include"project.h"

//目录设计
void menu()
{
	printf("**********************\n");
	printf("**********************\n");
	printf("******1.开始游戏******\n");
	printf("******0.退出游戏******\n");
	printf("**********************\n");
	printf("**********************\n");
}



//游戏主体设计
void game()
{
	char ret = 0;
	//先设计三子棋的棋盘大小
	//行列 
	char board[hang][lie];
	//定义完成之后先初始化该数组
	chushihua(board, hang, lie);
	xianshi(board, hang, lie);
	//接下来用while循环判断两次 玩家胜利 电脑胜利 然后是胜利基础
	while (1)
	{
		wanjiadong(board, hang, lie);
		xianshi(board, hang, lie);
		ret = panduan(board, hang, lie);
		if (ret != 'j')
		{
			break;
		}
		diannaodong(board, hang, lie);
		xianshi(board, hang, lie);
		ret = panduan(board, hang, lie);
		if (ret != 'j')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢\n");
	}
	else if (ret == 'p')
	{
		printf("平局\n");
	}
}




//把三行三列全部初始化为空白
void chushihua(char board[hang][lie], int Hang, int Lie)
{
	int i = 0;
	int j = 0;
	for (i = 0;i < Hang;i++)
	{
		for (j = 0;j < Lie;j++)
		{
			board[i][j] = ' ';
		}
	}
}

//显示效果分析
//我想做到 假设1是有0是没有
// 1 | 0 | 0 (0行)
//---|---|---
// 1 | 0 | 0 
//---|---|---
// 1 | 0 | 0 
//效果是每输入一行数字就再补一行横线
//显示模块
void xianshi(char board[hang][lie], int Hang, int Lie)
{
	int i = 0;
	for (i = 0;i < hang;i++)
	{
		int j = 0;
		for (j = 0;j < lie;j++)
		{
			printf(" %c ", board[i][j]);
			if (j < lie - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < hang - 1)
		{
			for (j = 0; j < lie; j++)
			{
				printf("---");
				if (j < lie - 1)

					printf("|");
			}
			printf("\n");

			//同理对于上面以列为标准附加”---"
		}

	}
}




//玩家动作模块
void wanjiadong(char board[hang][lie], int Hang, int Lie)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("请输入想要输入的坐标;\n");
		scanf("%d""%d", &x, &y);
		if (x >= 1 && x <= Hang && y >= 1 && y <= Lie)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("该坐标被占用\n");
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
}





//电脑动模块
void diannaodong(char board[hang][lie], int Hang, int Lie)
{
	//电脑动需要生成一个随机数
	int x = 0;
	int y = 0;
	printf("电脑开始动了\n");
	while (1)
	{
		x = rand() % Hang;
		y = rand() % Lie;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}







//判断模块写好就行了
char panduan(char board[hang][lie], int Hang, int Lie)
{
	/// 玩家赢  *电脑赢   #平局  (p)  pingju 继续 (j) jixu
	// sorry 上面这个不太好判断
	//我无法直接判断是电脑赢还是我自己赢还需要转,直接用# *
	int i = 0;
	//同行同列 对角线算赢
	for (i = 0; i < hang; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][0];//行
		}
	}
	for (i = 0; i < lie; i++)
	{
		if (board[0][i] == board[2][i] && board[2][i] == board[1][i] && board[1][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[2][0] && board[2][0] == board[1][1] && board[1][1] != ' ')
	{
		return board[1][1];//右对角线
	}
	if (1 == shibushiman(board, Hang, Lie))
	{
		return 'p';
	}
	return 'j';
}






int shibushiman(char board[hang][lie], int Hang, int Lie)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < hang; i++)
	{
		for (j = 0; j < lie; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;
			}
		}
	}
	return 1;
}

project.h

#pragma once
#define hang 3
#define lie  3
void menu();
void game();
void chushihua(char board[hang][lie], int Hang, int Lie);
void xianshi(char board[hang][lie], int Hang, int Lie);
void wanjiadong(char board[hang][lie], int Hang, int Lie);
void diannaodong(char board[hang][lie], int Hang, int Lie);
char panduan(char board[hang][lie], int Hang, int Lie);
int shibushiman(char board[hang][lie], int Hang, int Lie);

你可能感兴趣的:(萌新的C语言学习,c语言,开发语言)