c语言写的简单三子棋

三子棋游戏
在一个工程里面创建三个文件,一个头文件(.h)两个源文件(.c)。

1.主文件

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void menu()
{
	printf("**********************************\n");
	printf("**********   1. play   ***********\n");
	printf("**********   0. exit   ***********\n");
	printf("**********************************\n");
}
void game()
{
	char c;
	int count = ROW * COL;
	//创建棋盘(数组)存放数据
	char board[ROW][COL];
	//初始化数组(函数调用)
	Initboard(board, ROW, COL);
	//打印数组里面的内容
	Displayboard(board, ROW, COL);
	while (1)
	{
		//玩家下棋
		Player(board, ROW, COL);
		//判断玩家是否胜利
		c = who_win(board, ROW, COL);
		count--;
		if (c == '*')
		{
			printf("玩家赢!\n");
			break;
		}
		else if (count == 0)
		{
			printf("平局!\n");
			break;
		}
		//电脑下棋
		Computer(board, ROW, COL);
		c = who_win(board, ROW, COL);
		count--;
		if (c == '#')
		{
			printf("玩家赢!\n");
			break;
		}
		else if (count == 0)
		{
			printf("平局!\n");
			break;
		}
	}
}

void text()
{
	int input;
	menu();
	do
	{
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入错误!\n");
			break;
		}
	} while (input);
}
int main()
{
	srand((unsigned int)time(NULL));
	text();
	return 0;
}

2.头文件

#pragma once


///*这里放项目所需的头文件和函数的声明*/

#include
#include
#include
#define ROW 3
#define COL 3

// 初始化数组
void Initboard(char board[ROW][COL], int row, int col);
//打印数组
void Displayboard(char board[ROW][COL], int row, int col);
//玩家下棋
void Player(char board[ROW][COL], int row, int col);
//电脑下棋
void Computer(char board[ROW][COL], int row, int col);
//判断胜者
char who_win(char board[ROW][COL], int row, int col);

3.函数文件

#define _CRT_SECURE_NO_WARNINGS 1


///*这里放函数的实现(即每个函数的功能)*/
#include "game.h"

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

void Displayboard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ", board[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(char board[ROW][COL], int row, int col)
{
	int x, y;
	while (1)
	{
		printf("玩家下棋:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= col && y >= 1 && y <= row)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x-1][y-1] = '*';
				Displayboard(board, ROW, COL);
				break;
			}
			else
				printf("此位置已有棋子,请重新输入!\n");
		}
		else
		{
			printf("输入坐标范围错误!\n");
		}
	}
}

void Computer(char board[ROW][COL], int row, int col)
{
	printf("电脑下棋:>\n");
	while (1)
	{
		int x = rand() % col + 1;
		int y = rand() % row + 1;
		if (board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '#';
			Displayboard(board, ROW, COL);
			break;
		}
	}
}

char who_win(char board[ROW][COL], int row, int col)
{
	//判断行
	for (int i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}
	//判断列
	for (int i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}
	//判断对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	return '0';

}

你可能感兴趣的:(c/c++,c语言)