C语言三子棋

首先先了解三子棋规则

1.双方一人一手
2.不可下在已经下过的地方
3.三子练成及判赢或棋盘下满判平局

开始编程

1.编程可按照其规则编写,分析其规则可建立三个模块(头文件game.h,游戏文件game.c和测试test.c)
测试文件test.c开始进行选择
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void menu()
{
	printf("**************************\n");
	printf("*****   1.开始游戏   *****\n");
	printf("*****   0.退出游戏   *****\n");
	printf("**************************\n");
}
建立空的3*3二维数组进行存放棋子
void box_jl(char box[3][3])
{
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			box[i][j] = ' ';
		}
	}
}
主函数中包含了选择和下棋函数Playgame,人下棋函数myplay,电脑下棋函数complay 以及判断函数win,判满函数full,通过两个int判断函数的返回值来判断
int main()
{
	int true = 0;
	char ret = 0;
	do
	{
		char box[3][3] = { 0 };
		box_jl(box);
		menu();
		printf("请选择->");
		scanf("%d", &true);
		switch (true)
		{
		case 1:
			Playgame(box, 3, 3);
			while (1)
			{
				myplay(box, 3, 3);
				Playgame(box, 3, 3);
				ret = win(box, 3, 3);
				if (ret != ' ')
					break;
				ret = full(box, 3, 3);
				if (ret != ' ')
					break;
				printf("电脑走:\n");
				complay(box, 3, 3);
				Playgame(box, 3, 3);
				ret = win(box, 3, 3);
				if (ret != ' ')
					break;
				ret = full(box, 3, 3);
				if(ret != ' ')
					break;
			}
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入错误,重新输入\n");
			break;
		}
		if (ret == '*')
			printf("玩家胜\n");
		if (ret == '#')
			printf("电脑胜\n");
		if (ret == 'q')
			printf("平局\n");
	} while (true);
	system("pause");
	return 0;
}
2. 游戏函数game.c
建立棋盘
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void Playgame(char box[3][3], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			printf(" %c ", box[i][j]);
			if (j < row - 1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
		}
		printf("\n");
	}
}
人下棋
其中包含了判断是否可以下棋,可下则由" "变 " * "
void myplay(char box[3][3], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("输入你要下的坐标->");
	scanf("%d%d", &x,&y);
	if (x > 0 && x < 4 && y > 0 && y < 4)
	{
		if (box[x - 1][y - 1] ==  ' ')
		{
			box[x - 1][y - 1] = '*';
		}
		else
			printf("坐标有误,请重新输入\n");
	}
	else
		printf("坐标有误,请重新输入\n");
}
同样的电脑下棋
电脑下棋通过产生随机函数来确定下的位置
void complay(char box[3][3], int row, int col)
{
	int a = 0;
	int b = 0;
	while (1)
	{
		srand((unsigned)time(NULL));
		a = rand() % row + 1;
		b = rand() % col + 1;
		if (box[a - 1][b - 1] == ' ')
		{
			box[a - 1][b - 1] = '#';
			break;
		}
	}
}
判断输赢则一竖列或者一横行或者两条对角线
int win(char box[3][3], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		if (box[i][0] == '*'&& box[i][1] == '*'&& box[i][2] == '*')
			return '*';
		if (box[i][0] == '#'&& box[i][1] == '#' && box[i][2] == '#')
			return '#';
	}
	for(j = 0;j < col;j++)
	{
		if (box[0][j] == '*'&& box[1][j] == '*'&& box[2][j] == '*')
			return '*';
		if (box[0][j] == '#' && box[1][j] == '#'&& box[2][j] == '#')
			return '#';
	}
	if (box[0][0] == '*'&& box[1][1] == '*'&& box[2][2] == '*')
		return '*';
	if (box[0][0] == '#'&& box[1][1] == '#'&& box[2][2] == '#')
		return '#';
	if (box[0][2] == '*'&& box[1][1] == '*'&& box[2][0] == '*')
		return '*';
	if (box[0][2] == '#'&& box[1][1] == '#'&& box[2][0] == '#')
		return '#';
	return ' ';
}
判满函数
int full(char box[3][3], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (box[i][j] == ' ')
				return ' ';
		}
	}
	return 'q';
}
最后头文件game.h
    
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<Windows.h>

void Playgame(char box[3][3], int row, int col);
void you(char box[3][3], int row, int col);
void mayplay(char box[3][3], int row, int col);
void complay(char box[3][3], int row, int col);
int win(char box[3][3], int row, int col);
int full(char box[3][3], int row, int col);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值