C语言实现三子棋(井字棋)小游戏

 

 目录

 我写这个小游戏创建了三个文件就像这样:

test.c的全部内容如下:

初始化棋盘:

展示棋盘:

玩家下棋:

 电脑下棋:

判断输赢还是平局:

game.c的全部内容如下:

最终运行结果

 系统头文件用<>比如 : #include

自己写的头文件用""比如 : #include"game.h"

 我建议写的代码量大的话可以自己创建一个头文件里面包含着系统头文件以及自定义的函数。

每段代码的意思我将会在代码段里面解释

 我写这个小游戏创建了三个文件就像这样:

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

//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);

//判断输赢的代码
//玩家赢 - '*'
//电脑赢 - '#'
//平局 --- 'Q'
//继续 ----'C'

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

 本人建议之后写代码如果涉及到坐标的大小建议传ROW COL

因为这样如果想要改棋盘大小直接在宏定义那一部分改大小,这样比较方便。

比如:将3*3的棋盘改为5*5:

#define ROW 3->#define ROW 5
#define COL 3 ->#define COL 5

test.c的全部内容如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
	printf("\t\t\t\t***三子棋***\t\t\t\t\n");
	printf("\t\t\t\t************\t\t\t\t\n");
	printf("\t\t\t\t*  1.play  *\t\t\t\t\n");
	printf("\t\t\t\t*  0.exit  *\t\t\t\t\n");
	printf("\t\t\t\t************\t\t\t\t\n");
}
void game()
{
	//数据存储到一个字符的二维数组中,玩家下棋是'*',电脑下棋是'#'
	char board[ROW][COL] = { 0 };//数组的初始内容应该是全部空格
	InitBoard(board, ROW, COL);//初始化棋牌
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	//下棋
	char ret = 0;
	while (1)
	{
		player_move(board, ROW, COL);
		Sleep(1000);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		computer_move(board, ROW, COL);
		Sleep(1000);
		DisplayBoard(board,ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("\t\t\t\t恭喜你赢了!\t\t\t\t\n");
	}
	else if (ret == '#')
	{
		printf("\t\t\t\t很可惜你输了\t\t\t\t\n");
	}
	else 
	{
		printf("平局\n");
	}
}
void test()
{
	int input = 0;
	do
	{
		menu();
		printf("\t\t\t\t请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("\t\t\t\t退出游戏");
			break;
		default:
			printf("\t\t\t\t选择错误");
			break;
		}	
	} while (input);

}
int main(void)
{
	test();
	return 0;
}

C语言实现三子棋(井字棋)小游戏_第1张图片 C语言实现三子棋(井字棋)小游戏_第2张图片

初始化棋盘:

                因为刚开始还没有输入字符,所以将内容初始化为空格。

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;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		printf("\t\t\t\t");
		for (j = 0; j < col; j++)
		{
			
			printf(" %c ", board[i][j]);
			if (j < col - 1)
			{
				printf("|");//分割列
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			printf("\t\t\t\t");
			for (j = 0; j < col; j++)
			{
				printf("---");//分割行
				if (j < col - 1)
				{
					printf("|");//分割列
				}
			}
		}
		printf("\n");//换行
	}
}

​

初始化棋盘和展示棋盘:

        C语言实现三子棋(井字棋)小游戏_第3张图片

玩家下棋:

void player_move(char board[ROW][COL], int row, int col)
{
	printf("\t\t\t\t该你操作了->\n"); 
	printf("\t\t\t\t请输入坐标->\n");
	printf("\t\t\t\ttip:坐标1-3,用空格分开:");
	int x = 0;
	int y = 0;
	while (1)
	{
		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("\t\t\t\t该坐标已被占用请重新输入:\n");
				printf("\t\t\t\t");
			}
		}
		else
		{
			printf("\t\t\t\t该坐标非法啦\n");
			printf("\t\t\t\t请重新输入\n");
		}
	}
}

 电脑下棋:

void computer_move(char board[ROW][COL], int row, int col)
{
	printf("\t\t\t\t该电脑了->\n");
	printf("\t\t\t\t你要完蛋了\n");
	while (1)
	{
		int i = 0;
		int flag = 0;
		for (i = 0; i < row; i++)//判断行
		{
			if (board[i][0] == '*' && board[i][1] == '*'&& board[i][2]==' ')
			{
				 board[i][2]='#';
				 flag = 1;
				 break;
			}
			if (board[i][0] == '*' && board[i][2] == '*' && board[i][1] == ' ')
			{
				board[i][1] = '#';
				flag = 1;
				break;
			}
		}
		if (1 == flag)
			break;
		for (i = 0; i < row; i++)//判断列
		{
			if (board[0][i] == '*' && board[1][i] == '*'&& board[2][i]  == ' ')
			{
				board[2][i]='#';
				flag = 2;
				break;
			}
			if (board[0][i] == '*' && board[2][i] == '*' && board[1][i] == ' ')
			{
				board[1][i] = '#';
				flag = 2;
				break;
			}
        }
		if(2==flag)
			break;
		if (board[0][0] == '* ' && board[1][1] == '* ' && board[2][2] == ' ')//判断对角线
		{
			 board[2][2]='#';
			 break;
		}
		if (board[2][0] == '* ' && board[1][1] == '* ' && board[0][2] == ' ')
		{
			 board[0][2]='#';
			 break;
		}

		if (' ' == board[2][2])
		{
			board[2][2] = '#';
			break;
		}
		if (' ' == board[0][0])
		{
			board[0][0] = '#';
			break;
		}
		if (' ' == board[0][2])
		{
			board[0][2] = '#';
			break;
		}
		if (' ' == board[2][1])
		{
			board[2][1] = '#';
			break;
		}
		if (' ' == board[2][0])
		{
			board[2][0] = '#';
			break;
		}
		if (' ' == board[1][1])
		{
			board[1][1] = '#';
			break;
		}
		if (' ' == board[0][1])
		{
			board[0][1] = '#';
			break;
		}
		if (' ' == board[1][0])
		{
			board[1][0] = '#';
			break;
		}
		if (' ' == board[1][2])
		{
			board[1][2] = '#';
			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][1]!=' ')
		{
			return board[i][1];
		}
			
	}
	for (i = 0; i < row; i++)//判断列
	{
		if (board[0][i]==board[1][i]&&board[1][i]==board[2][i]&&board[1][i]!=' ')
		{
			return board[1][i];
		}
			
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')//判断对角线
	{
		return board[1][1];
	}
	if (board[2][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	//判断平局
	if (1 == is_full(board, row, col))
	{
		return 'Q';
	}
	//继续
	return 'C';
}


C语言实现三子棋(井字棋)小游戏_第4张图片C语言实现三子棋(井字棋)小游戏_第5张图片 

game.c的全部内容如下:

#define _CRT_SECURE_NO_WARNINGS 1
#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;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		printf("\t\t\t\t");
		for (j = 0; j < col; j++)
		{
			
			printf(" %c ", board[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			printf("\t\t\t\t");
			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)
{
	printf("\t\t\t\t该你操作了->\n"); 
	printf("\t\t\t\t请输入坐标->\n");
	printf("\t\t\t\ttip:坐标1-3,用空格分开:");
	int x = 0;
	int y = 0;
	while (1)
	{
		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("\t\t\t\t该坐标已被占用请重新输入:\n");
			}
		}
		else
		{
			printf("\t\t\t\t该坐标非法啦\n");
			printf("\t\t\t\t请重新输入\n");
		}
	}
}
void computer_move(char board[ROW][COL], int row, int col)
{
	printf("\t\t\t\t该电脑了->\n");
	printf("\t\t\t\t你要完蛋了\n");
	while (1)
	{
		int i = 0;
		int flag = 0;
		for (i = 0; i < row; i++)//判断行
		{
			if (board[i][0] == '*' && board[i][1] == '*'&& board[i][2]==' ')
			{
				 board[i][2]='#';
				 flag = 1;
				 break;
			}
			if (board[i][0] == '*' && board[i][2] == '*' && board[i][1] == ' ')
			{
				board[i][1] = '#';
				flag = 1;
				break;
			}
		}
		if (1 == flag)
			break;
		for (i = 0; i < row; i++)//判断列
		{
			if (board[0][i] == '*' && board[1][i] == '*'&& board[2][i]  == ' ')
			{
				board[2][i]='#';
				flag = 2;
				break;
			}
			if (board[0][i] == '*' && board[2][i] == '*' && board[1][i] == ' ')
			{
				board[1][i] = '#';
				flag = 2;
				break;
			}
        }
		if(2==flag)
			break;
		if (board[0][0] == '* ' && board[1][1] == '* ' && board[2][2] == ' ')//判断对角线
		{
			 board[2][2]='#';
			 break;
		}
		if (board[2][0] == '* ' && board[1][1] == '* ' && board[0][2] == ' ')
		{
			 board[0][2]='#';
			 break;
		}

		if (' ' == board[2][2])
		{
			board[2][2] = '#';
			break;
		}
		if (' ' == board[0][0])
		{
			board[0][0] = '#';
			break;
		}
		if (' ' == board[0][2])
		{
			board[0][2] = '#';
			break;
		}
		if (' ' == board[2][1])
		{
			board[2][1] = '#';
			break;
		}
		if (' ' == board[2][0])
		{
			board[2][0] = '#';
			break;
		}
		if (' ' == board[1][1])
		{
			board[1][1] = '#';
			break;
		}
		if (' ' == board[0][1])
		{
			board[0][1] = '#';
			break;
		}
		if (' ' == board[1][0])
		{
			board[1][0] = '#';
			break;
		}
		if (' ' == board[1][2])
		{
			board[1][2] = '#';
			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][1]!=' ')
		{
			return board[i][1];
		}
			
	}
	for (i = 0; i < row; i++)//判断列
	{
		if (board[0][i]==board[1][i]&&board[1][i]==board[2][i]&&board[1][i]!=' ')
		{
			return board[1][i];
		}
			
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')//判断对角线
	{
		return board[1][1];
	}
	if (board[2][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	//判断平局
	if (1 == is_full(board, row, col))
	{
		return 'Q';
	}
	//继续
	return 'C';
}

最终运行结果

C语言实现三子棋(井字棋)小游戏_第6张图片

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