C语言——初阶三子棋

目录

一、在test.c文件主函数实现游戏逻辑

二、在game.h文件中声明头文件和函数声明

三、在game.c文件中实现声明的函数

1.游戏主体逻辑

2.将棋盘初始化为空格

3.打印棋盘

4.玩家下棋

5.电脑下棋

6.判断是否赢了

7.判断棋盘是否满载

四、程序运行效果图

 五、完整代码


一、在test.c文件主函数实现游戏逻辑

#define _CRT_SECURE_NO_WARNINGS 1;
#include"game.h"
//游戏菜单的生成
void menu()
{
	printf("**********************\n");
	printf("****  1.start  *******\n");
	printf("****  0.exit   *******\n");
	printf("**********************\n");
}
int main()
{
	srand((unsigned)time(NULL));//时间戳,用于电脑随机生成坐标
	int input;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("开始游戏\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入有误,清重新输入\n");
		}

	} while (input);
	return 0;
}

二、在game.h文件中声明头文件和函数声明

#define ROW 3//行
#define COL 3//列
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//进入游戏
void game();
//初始化棋盘
void init_borad(char borad[ROW][COL], int row, int col);
//打印棋盘
void print_borad(char borad[ROW][COL], int row, int col);
//玩家下棋
void player_chess(char borad[ROW][COL]);
//电脑下棋
void computer_chess(char borad[ROW][COL]);
//判断是否赢了
char is_victory(char borad[ROW][COL]);
//判断棋盘是否满载
int is_full(char borad[ROW][COL]);

三、在game.c文件中实现声明的函数

1.游戏主体逻辑

void game()
{
	//int row, col;
	char borad[ROW][COL];//创建棋盘
	init_borad(borad,ROW,COL);
	print_borad(borad, ROW, COL);
	while (1)
	{
		player_chess(borad);
		print_borad(borad, ROW, COL);
		if (is_victory(borad) == '*')
		{
			printf("玩家胜利\n");
			break;
		}
		if ( is_full(borad) == 1)
		{
			printf("平局\n");
			break;
		}
		computer_chess(borad);
		print_borad(borad, ROW, COL);
		if (is_victory(borad) == '#')
		{
			printf("电脑胜利\n");
			break;
		}
		if (is_victory(borad) == 'q' && is_full(borad) == 1)
		{
			printf("平局\n");
			break;
		}

	}
	
}

2.将棋盘初始化为空格

void init_borad(char borad[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			borad[i][j] = ' ';
		}
	}
}

3.打印棋盘

void print_borad(char borad[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ",borad[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}			
		}
		printf("\n");
		if (i < row - 1)
		{
			for (int j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

4.玩家下棋

void player_chess(char borad[ROW][COL])
{
	int x, y;
	printf("请玩家输入棋子位置:");
	scanf("%d %d", &x, &y);
	if ((x <= ROW && x >=1) && (y >= 1 && y <= COL))
	{
		if (borad[x-1][y-1] == ' ')
		{
			borad[x-1][y-1] = '*';
		}
		else
		{
			printf("该位置已被占领\n");
			player_chess(borad);
		}
	}
	else
	{
		printf("玩家输入坐标不合理,请重新输入\n");
		player_chess(borad);
	}
}

5.电脑下棋

void computer_chess(char borad[ROW][COL])
{
	int x = rand() % ROW;//随机生成0-2的横坐标
	int y = rand() % COL;//随机生成0-2的纵坐标
	if (borad[x][y] == ' '&&borad[x][y]!='*')
	{
		printf("机器人下棋:%d %d\n", x + 1, y + 1);
		borad[x][y] = '#';
	}
	else
	{
		computer_chess(borad);
	}

}

6.判断是否赢了

char is_victory(char borad[ROW][COL])
{
	//三行的判断
	for (int i=0; i < ROW; i++)
	{
		if (borad[i][0] == borad[i][1]&& borad[i][0] == borad[i][2] && borad[i][0] != ' ')
		{
			if (borad[i][0] == '*')
			{
				return '*';
			}
			else if (borad[i][0] == '#')
			{
				return '#';
			}
		}
	}
	//三列的判断
	for (int j=0; j < COL; j++)
	{
		if (borad[0][j]==borad[1][j]&&borad[0][j] == borad[2][j]&& borad[0][j] != ' ')
		{
			if (borad[0][j] == '*')
			{
				return '*';
			}
			else if (borad[0][j] == '#')
			{
				return '#';
			}
		}
	}
	//左上到右下的判断
	if (borad[0][0] == borad[1][1]&&borad[0][0] == borad[2][2])
	{
		if (borad[0][0] == '*')
		{
			return '*';
		}
		else if (borad[0][0] == '#')
		{
			return '#';
		}
	}
	//左下到右上的判断
	if (borad[0][2] == borad[1][1] &&borad[0][2] == borad[2][0])
	{
		if (borad[0][2] == '*')
		{
			return '*';
		}
		else if (borad[0][2] == '#')
		{
			return '#';
		}
	}

	return 'q';
}

7.判断棋盘是否满载

int is_full(char borad[ROW][COL])
{
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			if (borad[i][j] == ' ');
			return 0;//满载
		}
	}
	return 1;//没有满载
}

四、程序运行效果图

 五、完整代码

//test.c文件

#define _CRT_SECURE_NO_WARNINGS 1;
#include"game.h"
//游戏菜单的生成
void menu()
{
	printf("**********************\n");
	printf("****  1.start  *******\n");
	printf("****  0.exit   *******\n");
	printf("**********************\n");
}
int main()
{
	srand((unsigned)time(NULL));//时间戳,用于电脑随机生成坐标
	int input;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("开始游戏\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入有误,清重新输入\n");
		}

	} while (input);
	return 0;
}


//game.h文件

#pragma once
#define ROW 3//行
#define COL 3//列
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//进入游戏
void game();
//初始化棋盘
void init_borad(char borad[ROW][COL], int row, int col);
//打印棋盘
void print_borad(char borad[ROW][COL], int row, int col);
//玩家下棋
void player_chess(char borad[ROW][COL]);
//电脑下棋
void computer_chess(char borad[ROW][COL]);
//判断是否赢了
char is_victory(char borad[ROW][COL]);
//判断棋盘是否满载
int is_full(char borad[ROW][COL]);


//game.c文件
#define _CRT_SECURE_NO_WARNINGS 1;
#include "game.h"

void game()
{
	//int row, col;
	char borad[ROW][COL];//创建棋盘
	init_borad(borad,ROW,COL);
	print_borad(borad, ROW, COL);
	while (1)
	{
		player_chess(borad);
		print_borad(borad, ROW, COL);
		if (is_victory(borad) == '*')
		{
			printf("玩家胜利\n");
			break;
		}
		if ( is_full(borad) == 1)
		{
			printf("平局\n");
			break;
		}
		computer_chess(borad);
		print_borad(borad, ROW, COL);
		if (is_victory(borad) == '#')
		{
			printf("电脑胜利\n");
			break;
		}
		if (is_victory(borad) == 'q' && is_full(borad) == 1)
		{
			printf("平局\n");
			break;
		}

	}
	
}

void init_borad(char borad[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			borad[i][j] = ' ';
		}
	}
}
void print_borad(char borad[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ",borad[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}			
		}
		printf("\n");
		if (i < row - 1)
		{
			for (int j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

void player_chess(char borad[ROW][COL])
{
	int x, y;
	printf("请玩家输入棋子位置:");
	scanf("%d %d", &x, &y);
	if ((x <= ROW && x >=1) && (y >= 1 && y <= COL))
	{
		if (borad[x-1][y-1] == ' ')
		{
			borad[x-1][y-1] = '*';
		}
		else
		{
			printf("该位置已被占领\n");
			player_chess(borad);
		}
	}
	else
	{
		printf("玩家输入坐标不合理,请重新输入\n");
		player_chess(borad);
	}
}

void computer_chess(char borad[ROW][COL])
{
	int x = rand() % ROW;//随机生成0-2的横坐标
	int y = rand() % COL;//随机生成0-2的纵坐标
	if (borad[x][y] == ' '&&borad[x][y]!='*')
	{
		printf("机器人下棋:%d %d\n", x + 1, y + 1);
		borad[x][y] = '#';
	}
	else
	{
		computer_chess(borad);
	}

}

char is_victory(char borad[ROW][COL])
{
	//三行的判断
	for (int i=0; i < ROW; i++)
	{
		if (borad[i][0] == borad[i][1]&& borad[i][0] == borad[i][2] && borad[i][0] != ' ')
		{
			if (borad[i][0] == '*')
			{
				return '*';
			}
			else if (borad[i][0] == '#')
			{
				return '#';
			}
		}
	}
	//三列的判断
	for (int j=0; j < COL; j++)
	{
		if (borad[0][j]==borad[1][j]&&borad[0][j] == borad[2][j]&& borad[0][j] != ' ')
		{
			if (borad[0][j] == '*')
			{
				return '*';
			}
			else if (borad[0][j] == '#')
			{
				return '#';
			}
		}
	}
	//左上到右下的判断
	if (borad[0][0] == borad[1][1]&&borad[0][0] == borad[2][2])
	{
		if (borad[0][0] == '*')
		{
			return '*';
		}
		else if (borad[0][0] == '#')
		{
			return '#';
		}
	}
	//左下到右上的判断
	if (borad[0][2] == borad[1][1] &&borad[0][2] == borad[2][0])
	{
		if (borad[0][2] == '*')
		{
			return '*';
		}
		else if (borad[0][2] == '#')
		{
			return '#';
		}
	}

	return 'q';
}

int is_full(char borad[ROW][COL])
{
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			if (borad[i][j] == ' ');
			return 0;//满载
		}
	}
	return 1;//没有满载
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值