c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)

初学c语言,也是第一次文章,主要是记录一下我的学习经历 

c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)_第1张图片这次用了三个文件:game.h   、  game.c  和  test.c。test.c是主程序(程序框架在这里写,运行也在这),game.c是函数程序(将主程序的一些功能挪到这里用函数实现,使主程序更加清晰,这里每个函数负责不同功能。方便阅读), game.h是头文件(用与声明函数和一些程序要用到的库)

 这是头文件程序内容:

//头文件

#pragma once

#include 
#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 PlayerMove(char board[ROW][COL], int row, int col);

//电脑下棋
//找没有下棋的位置随机下
void ComputerMove(char board[ROW][COL], int row, int col);

//判断输赢
char WinJudg(char board[ROW][COL], int row, int col);
//
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
//

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

 这里是主程序的内容:

//主程序文件


//*********************************************实现三子棋(井字棋)**********************************************
//




//test.c  //测试游戏的逻辑
//
//game.c  //游戏代码实现
//game.h  //游戏代码声明




#define _CRT_SECURE_NO_WARNINGS
#include 
#include "game.h"


void menu()
{
	printf("**********************************\n");
	printf("***********   五子棋   ***********\n");
	printf("**********************************\n");
	printf("********1.play      0.exit********\n");
	printf("**********************************\n");
}

void game()
{
	char ret = 0;
	char board[ROW][COL] = { 0 };
	//初始化棋盘
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	//下棋
	while (1)
	{
		//玩家下
		PlayerMove(board, ROW, COL);
		system("cls");
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = WinJudg(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		//电脑下
		ComputerMove(board, ROW, COL);
		Sleep(100);
		system("cls");
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = WinJudg(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢\n");
	}
	else
	{
		printf("平局\n");
	}
}

int main()
{
	srand((unsigned int)time(NULL));           //使用rand前提设置
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		system("cls");
		switch (input)
		{
		case 0:
			printf("退出游戏\n");
			break;
		case 1:
			game();
			break;
		default:
			printf("选择错误,请重新输入\n");
			break;
		}
	} while (input);
}

 这里是函数文件内容:

//函数文件

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"


void InitBoard(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 DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0;i < row;i++)
	{
		//打印数据
		//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		int j = 0;
		for (j = 0;j < col ;j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		//打印分割信息
		//printf("---|---|---\n");
		if (i < row - 1)
		{
			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("请输入坐标(如:输入 “ 1 3 ”  表示第一行第三列下棋):>\n");
		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");
		}
	}
}


void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑下棋:>\n");

	int x = 0;
	int y = 0;
	int i = 0;
	int j = 0;
	char k = '0';
	char p = '0';

	if (board[1][1] == ' ')
	{
		board[1][1] = '#';
		goto end;
	}

	if (board[1][1] != ' ')
	{
		if (board[0][0] == ' ')
		{
			board[0][0] = '#';
			goto end;
		}	
	}


	//只对井字棋有作用
	
	
	//先进攻!
	for (i = 0;i < row;i++)
	{
		for (j = 0;j < col;j++)
		{
			if (board[i][j] == ' ')
			{
				board[i][j] = '#';
				p = WinJudg(board, ROW, COL);
				if (p == '#')
				{
					board[i][j] = '#';
					goto end;
				}
				else
				{
					board[i][j] = ' ';
				}
			}
		}
	}


	//后防守
	for (i = 0;i < row;i++)
	{
		for (j = 0;j < col;j++)
		{
			if (board[i][j] == ' ')
			{
				board[i][j] = '*';
				p = WinJudg(board, ROW, COL);
				if (p == '*')
				{
					board[i][j] = '#';
					goto end;
				}
				else
				{
					board[i][j] = ' ';
				}
			}
		}
	}



	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
end:;
}


char WinJudg(char board[ROW][COL], int row, int col)
{
	先判断行,再判断列,再判断对角线
	行  判断
	//int i = 0;
	//for (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];
	//	}
	//}
	列  判断
	//int j = 0;
	//for (j = 0;j < col; j++)
	//{
	//	if ((board[0][j] == board[1][j]) && (board[1][j] == board[2][j]) && (board[1][j] != ' '))
	//	{
	//		return board[1][j];
	//	}
	//}
	对角线判断
	//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];
	//}

	//能够适用不止3行3列的判断代码
	int i = 0;
	int j = 0;

	//判断行
	for (i = 0;i < row;i++)
	{
		j = 0;
		while ((board[i][j] == board[i][j + 1])&&(board[i][1]!=' '))
		{
			if ((j == col - 2))
			{
				return board[i][1];
			}
			j++;
		}
	}


	//判断列
	for (j = 0;j< col;j++)
	{
		i = 0;
		while ((board[i][j] == board[i+1][j]) && (board[1][j] != ' '))
		{
			if ((i == row - 2))
			{
				return board[1][j];
			}
			i++;
		}
	}


	//判断对角线
	for (i = 1;i < row;i++)
	{
		if (board[i][i]!=board[i-1][i-1])
		{
			break;
		}
		else if(board[i][i]==' ')
		{
			break;
		}
		else if(i==row-1)
		{
			return board[i][i];
		}
	}
	for (i = 1;i < row;i++)
	{
		if (board[i][row- 1- i] != board[i - 1][row - i])
		{
			break;
		}
		else if (board[i][row-1-i] == ' ')
		{
			break;
		}
		else if (i==row-1)
		{
			return board[i][row - 1 - i];
		}
	}

	//判断平局
	if (IsFull(board,row,col))
	{
		return 'Q';
	}

	//游戏继续
	return 'C';
}

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

看看效果吧

c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)_第2张图片c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)_第3张图片c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)_第4张图片

c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)_第5张图片c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)_第6张图片c语言实现井字棋(新手都能看懂,因为这就是我刚学C写的)_第7张图片 

你可能感兴趣的:(算法)