小项目 : C语言实现三子棋小游戏[拓展为多子棋]

game.h

#ifndef __GAME_H__
#define __GAME_H__
#include 
#include 
#include 

#define ROW 5
#define COL 5

void GAME(char bor[ROW][COL],int row, int col);
void print_bor(char bor[ROW][COL], int row, int col);
void People(char bor[ROW][COL], int row, int col);
void Computer(char bor[ROW][COL], int row, int col);
int Is_win(char bor[ROW][COL], int row, int col);
int IF_FULL(char bor[ROW][COL], int row, int col);
void Init(char bor[ROW][COL], int row, int col);




#endif __GAME_H__
game.c

#include "game.h"
void Init(char bor[ROW][COL], int row, int col) //初始化数组,使数组每个字符都为空格
{
	memset(bor, ' ', col*row*sizeof(char));
}
void menu() //打印菜单
{
	printf("*************************\n");
	printf("*** 1:PLAY     0:OUT  ***\n");
	printf("*************************\n");
}
void print_bor(char bor[ROW][COL], int row, int col) //打印数组
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (j == col - 1)
				printf(" %c ", bor[i][j]);
			else
				printf(" %c |", bor[i][j]);
		}
		printf("\n");

		for (j = 0; j < col; j++)
		{
			if (i < row - 1)
			printf("--- ");
		}
		printf("\n");
	}
}
void People(char bor[ROW][COL], int row, int col) //人下棋,显示为#
{
	int x = 0;
	int y = 0;
	printf("请输入一个坐标:>");
	while (1)
	{
		scanf("%d%d", &x, &y);
		if ((x > 0) && (x <= row) && (y > 0) && (y <= col))
		{
			if (bor[x - 1][y - 1]==' ')
			{
				bor[x - 1][y - 1] = '#';
				break;
			}
			else
			{
				printf("下标有误,请重新输入:>");
			}
		}
		else
			printf("下标有误,请重新输入:>");
	}
}
void Computer(char bor[ROW][COL], int row, int col) //电脑走,产生随机数,显示为$
{
	int x = 0;
	int y = 0;
	printf("电脑走:\n");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (bor[x][y]==' ')
		{
			bor[x][y] = '$';
			break;
		}
	}
}
int Is_win(char bor[ROW][COL], int row, int col) //判断输赢,返回#则玩家获胜,返回$则电脑获胜。
{
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = 0; i < row; i++)
	{
		count = 0;
		for (j = 0; j < col-1; j++)
		{
			if ( (bor[i][j] == bor[i][j + 1]) && ( bor[i][j] != ' ') )
				count++;
			else break;
			if (count == row - 1)
				return bor[i][1];
		}
	}
	for (i = 0; i < col; i++)
	{
		count = 0;
		for (j = 0; j < col-1; j++)
		{
			if ((bor[j][i] == bor[j + 1][i]) && (bor[j][i] != ' '))
				count++;
			else break;
			if (count == row - 1)
				return bor[j][1];
		}
	}
	for (i = 0; i < row-1; i++)
	{
		count = 0;
		if ((bor[i][i] == bor[i + 1][i + 1]) && (bor[0][0] != ' '))
			count++;
		else break;
		if (count == row - 1)
			return bor[0][0];
	}
	for (i = 0; i < row-1; i++)
	{
		count = 0;
		if ((bor[i][row - 1 - i] == bor[i + 1][row - 2 - i]) && (bor[row-1][0] != ' '))
			count++;
		else break;
		if (count == row - 1)
			return bor[row-1][0];
	}
	return 0;
}
int IF_FULL(char bor[ROW][COL], int row, int col) //判断棋满,返回f
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (bor[i][j]==' ')
				return 1;
		}
	}
	return 'f';
}

test,c

#include "game.h"


void GAME(char bor[ROW][COL], int row, int col)
{
	int g = 0;
	int ret = 0;
	print_bor(bor, row, col);
	while (1)
	{
		People(bor, row, col);
		print_bor(bor, row, col);
		ret = Is_win(bor, row, col);
		if (ret == '#' || ret == '$')
			break;
		ret = IF_FULL(bor, row, col);
		if (ret == 'f')
			break;
		Computer(bor, row, col);
		print_bor(bor, row, col);
		ret = Is_win(bor, row, col);
		if (ret == '#' || ret == '$')
			break;
		ret = IF_FULL(bor, row, col);
		if (ret == 'f')
			break;
	}
	if (ret == '#')
	{
		printf("玩家获胜!\n");
	}
	if (ret == '$')
	{
		printf("电脑获胜!\n");
	}
	if (ret == 'f')
	{
		printf("平局!\n");
	}
}
int main()
{
	char bor[ROW][COL] = { 0 };
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		Init(bor, ROW, COL);
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			GAME(bor, ROW, COL);
			break;
		case 0:
			break;
		default: printf("请输入1/0:\n");
			break;
		}
	} while (input);
	system("pause");
	return 0;
}


你可能感兴趣的:(小项目 : C语言实现三子棋小游戏[拓展为多子棋])