C语言 三子棋游戏

Posted 爱学习的大鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 三子棋游戏相关的知识,希望对你有一定的参考价值。

test.c

#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()
{
	int set = 0;
	char board[ROW][COL];//3*3的棋盘空间
	Initboard(board, ROW, COL);//初始化棋盘
	Displayboard(board, ROW, COL);//显示棋盘
	while (1)
	{
		Playermove(board, ROW, COL);//玩家下棋
		//1为继续
		//0为平局
		//*为玩家获胜
		//#为电脑获胜
		set=Who_win(board, ROW, COL);//判断谁赢了
		if (set != 1)//判断是否继续
			break;
		Computermove(board, ROW, COL);//电脑下棋
		set = Who_win(board, ROW, COL);
		if (set != 1)
			break;
	}
	if (set == \'*\')
		printf("玩家获胜\\n");
	if (set == \'#\')
		printf("电脑获胜\\n");
	if (set == 0)
		printf("打成平局\\n");
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));//生成随机数
	do
	{
		menu();//进入游戏游戏菜单
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出成功");
			break;
		default:
			printf("输入错误,请重新输入:\\n");
			break;
		}
	} while (input);

	return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char board[ROW][COL], int row, int col)//初始化棋盘
{
	int x = 0;
	int y = 0;
	for (x = 0; x < row; x++)
	{
		for (y = 0; y < col; y++)
		{
			board[x][y] = \' \';
		}
	}
}

void Displayboard(char board[ROW][COL], int row, int col)//显示棋盘
{
	int x = 0;
	int y = 0;
	for (x = 0; x < row; x++)
	{
		for (y = 0; y < col; y++)
		{
			printf(" %c ", board[x][y]);
			if (y < col - 1)
				printf("|");
		}
		printf("\\n");
		if (x < row - 1)
		{
			for (y = 0; y < col; y++)
			{
				printf("---");
				if (y < col - 1)
					printf("|");
			}
			printf("\\n");
		}
	}
}

void Playermove(char board[ROW][COL], int row, int col)//玩家下棋
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("玩家下棋:");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (board[x - 1][y - 1] == \' \')
			{
				board[x - 1][y - 1] = \'*\';
				break;
			}
			else
				printf("已被占用,请重新输入\\n");
		}
		else
			printf("输入不在范围内,请重新输入\\n");
	}
	Displayboard(board, ROW, COL);

}

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

int Who_win(char board[ROW][COL], int row, int col)//判断谁赢了
{
	int x = 0;
	int y = 0;
	for (x = 0; x < row; x++)
	{
		if (board[x][0] == board[x][1] && board[x][0] == board[x][2] && board[x][0] != \' \')//判断三行是否相等
			return	board[x][0];

	}
	for (y = 0; y < col; y++)
	{
		if (board[0][y] == board[1][y] && board[0][y] == board[2][y] && board[0][y] != \' \')//判断三列是否相等
			return	board[0][y];
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != \' \')//判断对角是否相等
		return board[1][1];
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != \' \')
		return board[1][1];
	for (y = 0; y < col; y++)
	{
		for (x = 0; x < row; x++)//判断是否继续游戏
		{
			if (board[x][y] == \' \')
				return	1;
		}
	}
	return 0;
}

game.h

#define _CRT_SECURE_NO_WARNINGS 1

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

// 符号定义
#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 Playermove(char board[ROW][COL], int row, int col);//玩家下棋
void Computermove(char board[ROW][COL], int row, int col);//电脑下棋
int Who_win(char board[ROW][COL], int row, int col);//判断谁赢了

运行结果:

以上是关于C语言 三子棋游戏的主要内容,如果未能解决你的问题,请参考以下文章

C语言实现三子棋游戏

三子棋(C语言实现)

C语言实现游戏三子棋

用C语言实现三子棋游戏(附上思路+项目展示+源代码)

C语言实现三子棋(井字棋)

C语言实现三子棋