三子棋【开源代码】

2024-06-04 12:52:28 浏览数 (2)

为实现三子棋的基本功能,我们把这个项目拆分成三个部分:test.c,game.c, game.h

源文件:test.c内部存放整体框架;game.c内部存放具体函数功能的实现

头文件:game.h内部存放函数的声明

程序源代码:

test.c

代码语言:javascript复制
#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 ret = 0;
	//存储数据--创建一个二维数组
	char board[ROW][COL];
	//初始化
	InitiaBoard(board, ROW, COL);
	//打印棋盘
	Display(board, ROW, COL);
	while (1)
	{
		//玩家走
		PlayerMove(board,ROW,COL);
		Display(board, ROW, COL);
		//判断输赢
		ret=Iswin(board, ROW, COL);
		if (ret != 'C')
			break;

		//电脑走
		ComMove(board, ROW, COL);
		Display(board, ROW, COL);
		//判断输赢
		Iswin(board, ROW, COL);
		if (ret != 'C')
			break;
		
}
	//玩家赢——返回*,电脑赢——返回#,平局——返回Q,继续——返回C
	if (ret == '*')
	{
		printf("玩家赢了n");
	}
	else if (ret == '#')
	{
		printf("电脑赢了n");
	}
	else
	{
		printf("平局n");
	}
	Display(board, ROW, COL);
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch(input)
		{
		case 1:
			printf("三子棋游戏n");
			game();
			break;
		case 0:
			printf("退出游戏n");
			break;
		default:
			printf("输入错误,请重新输入n");
			break;
		}
	} while (input);

	return 0;
}

 game.h

代码语言:javascript复制
#include<stdio.h>
#include<time.h>
#include<stdlib.h>


//符号的定义 
#define ROW 3
#define COL 3



//函数的声明
//初始化棋盘
void InitiaBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void Display(char board[ROW][COL],int row,int col);
//玩家走
void PlayerMove(char board[ROW][COL], int row, int col);
//电脑走
void ComMove(char board[ROW][COL], int row, int col);


//判断输赢
char Iswin(char board[ROW][COL], int row, int col);

 game.c

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitiaBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i  )
	{
		for (j = 0; j < col; j  )
		{
			board[i][j] = ' ';
		}
	}
}
void Display(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i  )
	{
		//printf(" %c | %c | %c n", board[a][0], board[a][1],board[a][2]);
		int j = 0;
		for (j = 0; j < col; j  )
		{
			printf(" %c ",board[i][j]);
			if(j<col-1)
			   printf("|");
		}
		printf("n");
		if(i < row-1)
		{
			int j = 0;
			for (j = 0; j < col; j  )
			{
				printf("---");
				if (j < col-1)
					printf("|");
			}
			printf("n");
		}
	}
}
void PlayerMove (char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家走n");
	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("坐标非法,请重新输入");
		}
	}
}
//电脑走

void ComMove(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 Isfull(char board[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 (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
char Iswin(char board[ROW][COL], int row, int col)
{
	int a = 0;
	for (a = 0; a < row; a  )
	{
		if (board[a][0] == board[a][1] && board[a][1] == board[a][2]&&board[a][0]!=' ')
			return board[a][0];
	}
	for (a = 0; a < col; a  )
	{
		if (board[0][a] == board[1][a] && board[1][a] == board[2][a] && board[0][a] != ' ')
			return board[0][a];
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
	{
		return board[0][0];
	}
	if (board[2][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[2][0];
	}
	//判断平局——满了返回1,不满返回零
	int ret = Isfull(board, row, col);
	if (ret == 1)
	{
		return 'Q';
	}
	return 'C';
}

0 人点赞