c语言之三子棋

一个头文件game.h,两个c文件game.c和test.c

头文件game.h

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW		5
#define COL		5

//初始化棋盘函数
void InitBoard(int row, int col, char arr[ROW][COL]);

//打印棋盘函数
void DisplayBoard(int row, int col, char arr[ROW][COL]);

//下棋函数
void PlayGame(int row, int col, char arr[ROW][COL]);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//初始化棋盘函数
void InitBoard(int row, int col,char arr[ROW][COL])
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			arr[i][j] = ' ';
		}
	}
}

//打印棋盘函数
void DisplayBoard(int row, int col, char arr[ROW][COL])
{
	int i = 0;
	int j = 0;
	printf("   ");
	for (j = 0; j < col; j++)
	{
		printf(" %2d ",j);
	}
	printf("\n");
	for (i = 0; i < row; i++)
	{
		printf("   ");
		for (j = 0; j < col; j++)
		{
			if (j < col - 1)
				printf("|---");
			else
				printf("|---|");
		}
		printf("\n");
		printf("%2d ", i);
		for (j = 0; j < col; j++)
		{
			if (j < col - 1)
				printf("| %c ", arr[i][j]);
			else
				printf("| %c |",arr[i][j]);
		}
		printf("\n");

	}
	printf("   ");
	for (j = 0; j < col; j++)
	{
		if (j < col - 1)
			printf("|---");
		else
			printf("|---|");
	}
	printf("\n");
}

//判断输赢函数
int Judge(int row, int col, char arr[ROW][COL])
{
	int i = 1;
	int j = 1;
	int ret = 0;
	int isfull = 1;
	for (i = 1; i < row - 1; i++)
	{
		for (j = 1; j < col - 1; j++)
		{
			if ((arr[i][j] == '*' && arr[i - 1][j] == '*' && arr[i + 1][j] == '*') || (arr[i][j] == '*' && arr[i][j - 1] == '*' && arr[i][j + 1] == '*') || (arr[i][j] == '*' && arr[i - 1][j - 1] == '*' && arr[i + 1][j + 1] == '*') || (arr[i][j] == '*' && arr[i - 1][j + 1] == '*' && arr[i + 1][j - 1] == '*'))ret = 1;
			else if ((arr[i][j] == '#' && arr[i - 1][j] == '#' && arr[i + 1][j] == '#') || (arr[i][j] == '#' && arr[i][j - 1] == '#' && arr[i][j + 1] == '#') || (arr[i][j] == '#' && arr[i - 1][j - 1] == '#' && arr[i + 1][j + 1] == '#') || (arr[i][j] == '#' && arr[i - 1][j + 1] == '#' && arr[i + 1][j - 1] == '#'))ret = 2;
				
		}
	}
	for (i = 0; i < row; i++)
	{
		for (j=0;j<col;j++)
		{
			if (arr[i][j] == ' ')
			{
				isfull = 0;
			}
		}
	}
	if (isfull == 1)
		ret = 3;
	return ret;
}

//下棋函数
void PlayGame(int row,int col,char arr[ROW][COL])
{
	srand((unsigned int)time(NULL));
	int  result = 0 ;
	int player = 0 ;
	while (result == 0)
	{
		switch (player)
		{
			//玩家选择
		case 0:
			printf("请选择您的坐标:");
			int x = 0;
			int y = 0;
			scanf("%d %d", &x, &y);
			if (arr[x][y] == ' ')
			{
				arr[x][y] = '*';
				player = !player;
			}
			else
			{
				printf("选择错误,请重新选择\n");
				Sleep(1000);
			}
			break;

		case 1:
			//机器选择
			x = rand() % row;
			y = rand() % col;
			if (arr[x][y] == ' ')
			{
				arr[x][y] = '#';
				player = !player;
			}
			break;
		}
		
		system("cls");
		DisplayBoard(row, col, arr);
		result =Judge(row, col, arr);
	}
	if (result == 1) printf("You Win\n");
	else if (result == 2) printf("You Lost\n");
	else if (result == 3) printf("平局\n");
}

主函数test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
	printf("************************\n");
	printf("********* 1.start*******\n");
	printf("********** 0.esit ******\n");
	printf("************************\n");
}

void game()
{
	printf("三子棋\n");
	//定义棋盘
	char Board[ROW][COL] = { 0 };
	//初始化棋盘
	InitBoard(ROW, COL, Board);
	//打印棋盘
	DisplayBoard(ROW,COL,Board);
	//下棋函数
	PlayGame(ROW, COL, Board);
}

void test()
{

	int input = 0;
	do
	{
		menu();
		printf("请选择:");
		scanf("%d",&input);
		switch (input)
		{
			case 1: game(); break;
			case 0:printf("您已退出\n"); break;
			default:printf("选择错误,请重新选择\n"); break;
		}
	} while (input);


}
int main()
{
	test();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值