一、三子棋

1.演示效果

【C语言】三子棋小游戏-LMLPHP

2.完整代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3//行
#define COL 3//列
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;
	for (i = 0; i < row; i++)
	{
		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)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}
void Player_board(char board[ROW][COL], int row, int col)//玩家下棋
{
	int x = 0;
	int y = 0;
	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("坐标非法,请重新输入!\n");
		}
	}
}
void Computer_board(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 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;
	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][0];
		}
	}
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')//判断一列中是否相等
		{
			return board[0][i];
		}
	}
	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[0][2] && board[0][2] != ' ')//判断对角线是否相等
	{
		return board[1][1];
	}
	if (1 == Is_full(board, row, col))//Is_full的返回值等于1,说明棋盘已经满了,没有空格了
		return 'P';
	return 'C';
}
void menu()
{
	printf("*******************\n");
	printf("***  1.开始游戏  **\n");
	printf("***  0.结束游戏  **\n");
	printf("*******************\n");
}
void game()
{
	printf("****  游戏开始  ***\n");
	char ret = 0;
	char board[ROW][COL] = { 0 };
	Init_board(board, ROW, COL);
	Print_board(board, ROW, COL);
	while(1)
	{
		Player_board(board, ROW, COL);
		Print_board(board, ROW, COL);
		ret = Is_win(board, ROW, COL);
		if (ret != 'C')
			break;

		Computer_board(board, ROW, COL);
		Print_board(board, ROW, COL);
		ret = Is_win(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
		printf("玩家赢!\n");
	if (ret == '#')
		printf("电脑赢!\n");
	if (ret == 'P')
		printf("平局!\n");
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do {
		menu();
		printf("请选择>:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("游戏结束!\n");
			break;
		default:
			printf("选择项不存在,请重新选择!\n");
			break;
		}
	} while (input);
}

二、代码解析

1.初始化棋盘

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] = ' ';
		}
	}
}

2.打印棋盘

void Print_board(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++)
		{
			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");
		}
	}
}

3.玩家下棋

void Player_board(char board[ROW][COL], int row, int col)//玩家下棋
{
	int x = 0;
	int y = 0;
	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("坐标非法,请重新输入!\n");
		}
	}
}

4.电脑下棋

void Computer_board(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 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;
	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][0];
		}
	}
	for (i = 0; i < row; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][i];
		}
	}
	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[0][2] && board[0][2] != ' ')
	{
		return board[1][1];
	}
	if (1 == Is_full(board, row, col))//Is_full的返回值等于1,说明棋盘已经满了,没有空格了
		return 'P';
	return 'C';
}

6.游戏主体函数

void game()
{
	printf("****  游戏开始  ***\n");
	char ret = 0;
	char board[ROW][COL] = { 0 };
	Init_board(board, ROW, COL);
	Print_board(board, ROW, COL);
	while(1)
	{
		Player_board(board, ROW, COL);
		Print_board(board, ROW, COL);
		ret = Is_win(board, ROW, COL);
		if (ret != 'C')
			break;

		Computer_board(board, ROW, COL);
		Print_board(board, ROW, COL);
		ret = Is_win(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
		printf("玩家赢!\n");
	if (ret == '#')
		printf("电脑赢!\n");
	if (ret == 'P')
		printf("平局!\n");
}

7.菜单函数

void menu()
{
	printf("*******************\n");
	printf("***  1.开始游戏  **\n");
	printf("***  0.结束游戏  **\n");
	printf("*******************\n");
}

8.头文件及主函数

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3//行
#define COL 3//列
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do {
		menu();
		printf("请选择>:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("游戏结束!\n");
			break;
		default:
			printf("选择项不存在,请重新选择!\n");
			break;
		}
	} while (input);
}
10-24 16:20