c语言三子棋简单实现

三子棋

test.c —— 测试游戏
game.h —— 游戏函数的声明
game.c —— 游戏函数的实现

一、test.c (测试游戏)

#include"game.h"

void menu()
{
	printf("***********************\n");
	printf("******* 1.play ********\n");
	printf("******* 0.eixt ********\n");
	printf("***********************\n");
}

void game()
{
	char board[ROW][COL] = { 0 };
	//初始化棋盘
	InitBoard(board,ROW,COL);
	//打印棋盘
	DisPlayBoard(board, ROW, COL);
	
	char ret = 0;
	while(1)
	{
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisPlayBoard(board, ROW, COL);
		//判断输赢
		ret=Judge(board, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisPlayBoard(board, ROW, COL);
		//判断输赢
		ret = Judge(board, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
	}
	if (ret == 'd')
	{
		printf("平局\n");
	}
	else if (ret == '*')
	{
		printf("玩家胜利\n");
	}
	else if (ret == '#')
	{
		printf("电脑胜利\n");
	}

}

int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("eixt\n");
			break;
		default:
			printf("Re enter\n");
		}
	} while (input);
	return 0;
}

二、game.h(游戏函数的声明)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.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);

char Judge(char board[ROW][COL], int row, int col);

int Empty(char board[ROW][COL], int row, int col);

三、game.c(游戏函数的实现)

1.棋盘的初始化

void InitBoard(char board[ROW][COL], int row, int col)
{
	memset(&board[0][0],' ', sizeof(char) * row * col);//以字节为单位设置内存
}

2.棋盘的打印

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 < row - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		for (int j = 0; j < col; j++)
		{
			printf("---");
			if(j<col-1)
			printf("|");
		}
		printf("\n");
	}
}

3.玩家下棋

void PlayerMove(char board[ROW][COL], int row, int col)
{
	while(1)
	{
		int x1, y1 = 0;
		printf("玩家下棋\n");
		printf("请输入坐标:\n");
		scanf("%d %d", &x1, &y1);
		int x = x1 - 1;
		int y = y1 - 1;
		if (x >= 0 && x < row && y >= 0 && y < row)
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '*';
				break;
			}
			else
			{
				printf("该坐标以落子,请重新选择\n");
			}
		}
		else
		{
			printf("坐标超出棋盘限制,请重新选择\n");
		}
	}
}

4.电脑下棋

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;
		}
	}
}

5.判断棋盘中是否还有棋子

int Empty(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return 1;
			}
		}
	}
	return 0;
}

6.判断输赢

char Judge(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0
				&& i - 1 < row && j - 1<col && i + 1< row && j + 1 < col 
				&& board[i][j] == board[i - 1][j - 1] && board[i][j] == board[i + 1][j + 1] && board[i][j]!=' ')//  \方向
			{
				return board[i][j];
			}
			if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0 
				&& i - 1 < row && j - 1 < col && i + 1 < row && j + 1 < col 
				&& board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')// /方向
			{
				return board[i][j];
			}
			if (j - 1 >= 0 && j + 1 >= 0 
				&& j - 1 < col && j + 1 < col 
				&& board[i][j] == board[i][j + 1] && board[i][j] == board[i][j - 1] && board[i][j] != ' ')// —方向
			{
				return board[i][j];
			}
			if (i + 1 >= 0 && i - 1 >= 0 
				&& i - 1 < row && i + 1 < row 
				&& board[i][j] == board[i+1][j] && board[i][j] == board[i-1][j] && board[i][j] != ' ')// | 方向
			{
				return board[i][j];
			}
		}
	}
	if (Empty(board, row, col) == 1)//如果棋盘不满
	{
		return 'c';
	}
	return 'd';//平局
}

四、完整代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.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);

char Judge(char board[ROW][COL], int row, int col);

int Empty(char board[ROW][COL], int row, int col);

void InitBoard(char board[ROW][COL], int row, int col)
{
	memset(&board[0][0],' ', sizeof(char) * row * col);//以字节为单位设置内存
}

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 < row - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		for (int j = 0; j < col; j++)
		{
			printf("---");
			if(j<col-1)
			printf("|");
		}
		printf("\n");
	}
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
	while(1)
	{
		int x1, y1 = 0;
		printf("玩家下棋\n");
		printf("请输入坐标:\n");
		scanf("%d %d", &x1, &y1);
		int x = x1 - 1;
		int y = y1 - 1;
		if (x >= 0 && x < row && y >= 0 && y < row)
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '*';
				break;
			}
			else
			{
				printf("该坐标以落子,请重新选择\n");
			}
		}
		else
		{
			printf("坐标超出棋盘限制,请重新选择\n");
		}
	}
}

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;
		}
	}
}

int Empty(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return 1;
			}
		}
	}
	return 0;
}

char Judge(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0
				&& i - 1 < row && j - 1<col && i + 1< row && j + 1 < col 
				&& board[i][j] == board[i - 1][j - 1] && board[i][j] == board[i + 1][j + 1] && board[i][j]!=' ')//  \方向
			{
				return board[i][j];
			}
			if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0 
				&& i - 1 < row && j - 1 < col && i + 1 < row && j + 1 < col 
				&& board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')// /方向
			{
				return board[i][j];
			}
			if (j - 1 >= 0 && j + 1 >= 0 
				&& j - 1 < col && j + 1 < col 
				&& board[i][j] == board[i][j + 1] && board[i][j] == board[i][j - 1] && board[i][j] != ' ')// —方向
			{
				return board[i][j];
			}
			if (i + 1 >= 0 && i - 1 >= 0 
				&& i - 1 < row && i + 1 < row 
				&& board[i][j] == board[i+1][j] && board[i][j] == board[i-1][j] && board[i][j] != ' ')// | 方向
			{
				return board[i][j];
			}
		}
	}
	if (Empty(board, row, col) == 1)//如果棋盘不满
	{
		return 'c';
	}
	return 'd';//平局
}
void menu()
{
	printf("***********************\n");
	printf("******* 1.play ********\n");
	printf("******* 0.eixt ********\n");
	printf("***********************\n");
}

void game()
{
	char board[ROW][COL] = { 0 };
	//初始化棋盘
	InitBoard(board,ROW,COL);
	//打印棋盘
	DisPlayBoard(board, ROW, COL);
	
	char ret = 0;
	while(1)
	{
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisPlayBoard(board, ROW, COL);
		//判断输赢
		ret=Judge(board, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisPlayBoard(board, ROW, COL);
		//判断输赢
		ret = Judge(board, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
	}
	if (ret == 'd')
	{
		printf("平局\n");
	}
	else if (ret == '*')
	{
		printf("玩家胜利\n");
	}
	else if (ret == '#')
	{
		printf("电脑胜利\n");
	}

}

int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("eixt\n");
			break;
		default:
			printf("Re enter\n");
		}
	} while (input);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值