C语言实现三子棋

在朋友的帮助下,终于艰难的完成了这一篇三子棋,到现在都觉得555

话不多说,上代码

tese.c的代码比较简单,但是他确定了基本的方向

#include"game.h"
void menu()
{
	printf("*************************\n");
	printf("******  1.play    *******\n");
	printf("******  0.exit    *******\n");
	printf("*************************\n");

}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请输入你的选择\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}

	} while (input);
	return 0;
}

game.h 是一些函数的声明,一些常用的头文件也可以放在里面,test.c 和game.c里面没有必要在包含一些头文件,不小心重复引用的话还会产生bug

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

#include
#include
#include
#include
#define ROW 3
#define COL 3
//游戏函数
void game();
//初始化棋盘
void Init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void Print_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断输赢
char Is_win(char board[ROW][COL], int row, int col);
//判断赢家 并输出
void Is_who(char ret);

最重要的是game.c文件,它包含游戏具体的实现,主函数中声明的函数的定义大多都在这

#include"game.h"
#define _CRT_SECURE_NO_WARNINGS 1

void game()
{
	char board[ROW][COL] = { 0 };
	//初始化棋盘
	Init_board(board, ROW, COL);
	//打印棋盘
	Print_board(board, ROW, COL);
	//下棋
	
	while (1)
	{
		//玩家下棋并打印棋盘
		player_move(board, ROW, COL);
		Print_board(board, ROW, COL);
		//判断是否还能下棋
		if (Is_win(board, ROW, COL) != 'C')
		{
		 Is_who(Is_win(board, ROW, COL));
		 break;
		}
		computer_move(board, ROW, COL);
		Print_board(board, ROW, COL);
		if (Is_win(board, ROW, COL) != 'C')
		{
			Is_who(Is_win(board, ROW, COL));
			break;
		}

	}
	
}



//初始化棋盘
void Init_board(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 Print_board(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++)
		{//数据
			printf(" %c ",board[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		//分割行
			
			if (i < row - 1)
			{
				for (j = 0;j < col;j++)
				{
					printf("---");
					if (j < col - 1)
						printf("|");
				}
				
			}
			printf("\n");
		
	}
}
void player_move(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	while (1)
	{
		int i = 0;
		int j = 0;
		printf("请输入下棋位置->");
		scanf("%d %d", &i, &j);
		if (i <= row && i >= 1 && j <= col && j >= 1)
		{
			if (board[i - 1][j - 1] == ' ')
			{
				board[i - 1][j - 1] = '*';
				break;
			}
			else
			{
				printf("坐标已有棋,清重新输入!\n");
			}
		}
		else
		{
			printf("坐标非法,清重新输入!\n");
		}
	}
}

void computer_move(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	while (1)
	{
		i = rand() % 3;//0-2
		j = rand() % 3;//0-2
		if (board[i][j] == ' ')
		{
			board[i][j] = '#';
			break;
		}
		else
			continue;//重新产生一个随机位置
	}

}

//判断棋盘是否还能下棋
int is_full(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 Is_win(char board[ROW][COL], int row, int col)
{
	int i = 0;int j = 0;
	//判断行
	for (i = 0;i < row;i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
			return board[i][1];
	}
	//判断列
	for (j = 0;j < col;j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ')
			return board[0][j];
	}
	//判断对角线
	if (board[1][1] == board[0][0] && board[1][1] == board[2][2] && board[0][0] != ' ')
		return board[1][1];
	if (board[1][1] == board[0][2] && board[1][1] == board[2][0] && board[1][1] != ' ')
		return board[1][1];
	if (1 == is_full(board, row, col))
	{
		return 'Q';
	}
	else
		return 'C';

}
void Is_who(char ret)
{
	if (ret == '*')
	{
		printf("玩家获胜!\n");
		system("pause");
		system("cls");
	}
	else if (ret == '#')
	{
		printf("电脑获胜!\n");
		system("pause");
		system("cls");
	}
	else if (ret == 'Q')
	{
		printf("平局!\n");
		system("pause");
		system("cls");
	}
}














不会说什么花里胡哨的东西,但是这个游戏确实搞了好久。最后还是要谢谢那个朋友。

你可能感兴趣的:(笔记,c语言,开发语言,后端)