C语言三子棋

#define _CRT_SECURE_NO_WARNINGS 1
#include  
#include  
#include  

#define ROWS 3 
#define COLS 3 
//函数声明
void init_board(char board[ROWS][COLS], int row, int col); 
void display_board(char board[ROWS][COLS], int row, int col); 
void player_move(char board[ROWS][COLS], int row, int col); 
void computer_move(char board[ROWS][COLS], int row, int col); 
int check_win(char board[ROWS][COLS], int row, int col);
int count_board(char board[ROWS][COLS], int row, int col);

int count_board(char board[ROWS][COLS], int row, int col)//计数棋盘已下格数
{
	char *p = &board[0][0];
	int count=0;
	for (int i = 0; i < row * col; i++)
	{
		if (*(p + i) != ' ')
			{
				count++;
			}
		
	}
	return count;
	
}
void init_board(char board[ROWS][COLS], int row, int col)//初始化棋盘
{
	char *p = &board[0][0];
	for (int i = 0; i < (row * col); i++)
	{
		*(p + i) = ' ';

	}
}
void display_board(char board[ROWS][COLS], int row, int col)//打印
{
	printf("       \n");
	for (int i = 0; i < row; i++)
	{
		
		for (int j = 0; j < col; j++)
		{
			printf("%c", board[i][j]);
			printf("__|");
		}
		printf("\n");
	}
}
void player_move(char board[ROWS][COLS], int row, int col)//玩家下棋
{
	int i, j;
	while (1)
	{
		printf("请输入您想要落子的位置坐标");
		scanf("%d %d", &i, &j);
		if (board[i - 1][j - 1] == ' ')
		{
			board[i - 1][j - 1] = 'x';
			break;
		}
		else{
			printf("输入坐标有误,重新输入\n");
		}
	}


}
void computer_move(char board[ROWS][COLS], int row, int col)//电脑下棋
{
	while (count_board(board,3,3)!=9)
	{
		srand((unsigned)time(NULL));
		int i = rand() % 3;
		int j = rand() % 3;
		if (board[i][j] == ' ')
		{
			board[i][j] = 'o';
			break;
		}
	}

}
int check_win(char board[ROWS][COLS], 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][0] == 'x')) ||
			((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == 'x')))
			return 1;  //玩家胜利  横竖判断
	}
	if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == 'x')) ||
		((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[0][2] == 'x')))
	{
		return 1;  //玩家胜利  对角判断
	}
	for (int i = 0; i < row; i++)
	{
		if (((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] == 'o')) ||
			((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == 'o')))
			return 2;  //电脑胜利  横竖判断
	}
	if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == 'o')) ||
		((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[0][2] == 'o')))
	{
		return 2;  //电脑胜利  对角判断
	}
	if (count_board(board,3,3) == 9)
	{
		return 0;//平局
	}
	else{
		return 4;//继续判断
	}
}

void meau()
{
	printf("********************\n");
	printf("*****1.开始游戏*****\n");
	printf("*****0.退出游戏*****\n");
	printf("********************\n");
}
int main()
{
	char board[3][3];
	int num;
	do
	{
		meau();
		printf("请选择>:");
		scanf("%d", &num);
		switch (num)
		{
		case 1:
			init_board(board,3,3);
			display_board(board, 3, 3);
			while (1){


				player_move(board, 3, 3);
				computer_move(board, 3, 3);
				display_board(board, 3, 3);
				int a = check_win(board, 3, 3);

				if (a == 1)
				{
					printf("玩家赢\n");
					break;
				}
				else if (a == 2)
				{
					printf("电脑赢\n");
					break;


				}
				else if (a == 0)
				{
					printf("平局\n");
					break;

				}
			}
			break;
		case 0:
			break;
		default:
			printf("输入错误重新选择>\n:");
			break;
		}
	} while (num);


	system("pause");
	return 0;
}


你可能感兴趣的