简易五子棋

本文分享了使用C语言开发五子棋游戏的过程,包括棋盘设置、游戏规则实现、光标定位显示等功能,并探讨了进一步改进的想法。

第一次写博客,这次写的主要内容就是我打五子棋的过程分享,代码打的不太好,不够精简。

1.先从第一个开始,设置棋盘,我想的是让玩家设置棋盘大小,代码如下:

char** intalChessboard(int Row, int Col);
char** intalChessboard(int Row, int Col)
{
	char **chessboard = NULL;
	int i;
	int j;
	if(chessboard != NULL || Row != Col || Row < 0 || Col < 0 || Row > MaxRow || Col > MaxCol)
	{
		return;
	}
	chessboard = (char**)malloc(sizeof(char*)*Row);
	for(i = 0;i< Col; i++){
		chessboard[i] = (char*)malloc(sizeof(char)*Col);
	}
	for(i = 0; i < Row; i++)
	{
		for(j = 0; j < Col; j++)
		{
			chessboard[i][j] = '0';
		}
	}
	return chessboard;
}
void showChessboard(char**chessboard, int Row, int Col)
{
	int i;
	int j;
	for(i = 0; i < Row; i++)
	{
    	for(j = 0; j < Col; j++)
	 	{
	 	printf("%4c", chessboard[i][j]);
		}
		 printf("\n\n");
	}
}

其实没有必要给chessboard数组空间赋值字符零,纯属个人操作。在写这块代码时主要难度就是二重指针和二维数组的关系·,这块不做讲解,因为本人还有几个点没有彻底理解。

2.规则编写,五子棋的规则比较简单,就是五个相同的棋子连在一起则胜利。当玩家落棋子时,需要判断此处能否落子,和落子之后是否有五个棋子连在一起即可。代码如下:

boolean chessRule(char**chessboard, InUser chess)
{
	int i;
	int j;
	int a = chess.row - 1;
	int b = chess.col - 1;
	int index = 0;
	for(i = a, j = b; i >= 0 && j >= 0; i--, j--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}
	for(i = a + 1, j = b + 1; i <= MinRow && j <= MinCol; i++,j++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
			if(index == 5)
			{
			return TURE;
            }
	index = 0;
	for(i = a , j = b ; i >= 0 && j <= MinCol; i--,j++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}		
	for(i = a + 1, j = b - 1; i <= MinRow && j >= 0; i++,j--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}
	index = 0;
		for(i = a, j = b; i >= 0; i--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
    	}else{
			 	break;
			 }
	}
		if(index == 5)
			{
				return TURE;
			}
	for(i = a + 1, j = b; i < MinRow; i++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
	    }else{
	    	break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}
	index = 0;
		for(i = a, j = b;j >= 0; j--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
	if(index == 5)
			{
				return TURE;
			}
	for(i = a, j = b + 1; j <= MinCol; j++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
		break;
		}
	}
	if(index == 5)
		{
			return TURE;
		}
	return FALSE;
}

3.我一直再想怎样不清屏显示棋盘,突然想到了光标定位函数,输入相应的坐标,即可将坐标定位在此处,(可能还会有别的方法处理,对这类函数还是不太熟悉),这里有个问题要注意,屏幕坐标横行是x轴,竖列是y轴,而二维数组是横行是行,竖列是列,所以y是行下标,x是列下标。代码如下:

void gotoxy(int x, int y) //gotoxy 源代码
{
COORD pos;
pos.X= x - 1;
pos.Y= y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void chessLocation(InUser chess)
{
	gotoxy(4*chess.col,2*chess.row - 1);
	printf("%c", chess.value);
}

其中gotoxy()括号中的内容是根据你输入的棋盘大小计算出来的。

4.剩下的内容就是主函数中函数的调用和函数之间的调用顺序。代码如下:

#include <stdio.h>
#include <conio.h>
#include <windows.h> 
#define MinRow 5
#define MinCol 5
#define MaxRow 15
#define MaxCol 15
#define TURE 1
#define FALSE 0
typedef struct InUser{
	int row;
	int col;
	char value;
}InUser;
typedef unsigned char boolean;
void gotoxy(int x, int y);
char** intalChessboard(int Row, int Col);
void showChessboard(char**chessboard, int Row, int Col);
boolean judgeRowCol(int Row, int Col);
void inInformation(int Row,int Col);
boolean chessRule(char**chessboard, InUser chess);
void chessLocation(InUser chess);
void GoChess(char**chessboard, InUser chess);
boolean chessJudge(char**chessboard, InUser chessMan);
boolean chessJudge(char**chessboard, InUser chessMan)
{
	if(chessboard[chessMan.row - 1][chessMan.col - 1] == '+' || chessboard[chessMan.row - 1][chessMan.col - 1] == '-')
	{
		return FALSE;
	}
	return TURE;
}
void GoChess(char**chessboard, InUser chess)
{
	chessboard[chess.row - 1][chess.col - 1] = chess.value;
}
void chessLocation(InUser chess)
{
	gotoxy(4*chess.col,2*chess.row - 1);
	printf("%c", chess.value);
}
void inInformation(int Row, int Col)
{
	gotoxy(1,Col+2*(Col-1) + 2);
}
void gotoxy(int x, int y)
{
COORD pos;
pos.X= x - 1;
pos.Y= y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
boolean judgeRowCol(int Row, int Col)
{
	if(Row < 5 || Col < 5 || Row > 15 || Col > 15 || Col != Row)
	{
		return FALSE;
	}
	return TURE;
}
void showChessboard(char**chessboard, int Row, int Col);
void showChessboard(char**chessboard, int Row, int Col)
{
	int i;
	int j;
	for(i = 0; i < Row; i++)
	{
    	for(j = 0; j < Col; j++)
	 	{
	 	printf("%4c", chessboard[i][j]);
		}
		 printf("\n\n");
	}
}
char** intalChessboard(int Row, int Col);
char** intalChessboard(int Row, int Col)
{
	char **chessboard = NULL;
	int i;
	int j;
	if(chessboard != NULL || Row != Col || Row < 0 || Col < 0 || Row > MaxRow || Col > MaxCol)
	{
		return;
	}
	chessboard = (char**)malloc(sizeof(char*)*Row);
	for(i = 0;i< Col; i++){
		chessboard[i] = (char*)malloc(sizeof(char)*Col);
	}
	for(i = 0; i < Row; i++)
	{
		for(j = 0; j < Col; j++)
		{
			chessboard[i][j] = '0';
		}
	}
	return chessboard;
}
boolean chessRule(char**chessboard, InUser chess)
{
	int i;
	int j;
	int a = chess.row - 1;
	int b = chess.col - 1;
	int index = 0;
	for(i = a, j = b; i >= 0 && j >= 0; i--, j--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}
	for(i = a + 1, j = b + 1; i <= MinRow && j <= MinCol; i++,j++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
			if(index == 5)
			{
			return TURE;
            }
	index = 0;
	for(i = a , j = b ; i >= 0 && j <= MinCol; i--,j++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}		
	for(i = a + 1, j = b - 1; i <= MinRow && j >= 0; i++,j--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}
	index = 0;
		for(i = a, j = b; i >= 0; i--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
    	}else{
			 	break;
			 }
	}
		if(index == 5)
			{
				return TURE;
			}
	for(i = a + 1, j = b; i < MinRow; i++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
	    }else{
	    	break;
		}
	}
		if(index == 5)
			{
				return TURE;
			}
	index = 0;
		for(i = a, j = b;j >= 0; j--)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
			break;
		}
	}
	if(index == 5)
			{
				return TURE;
			}
	for(i = a, j = b + 1; j <= MinCol; j++)
	{
		if(chessboard[i][j] == chess.value)
		{
			index++;
		}else{
		break;
		}
	}
	if(index == 5)
		{
			return TURE;
		}
	return FALSE;
}
int main()
{
    int Row;
	int Col;
	char **chessboard;
	boolean ok = FALSE;
	boolean gamer = FALSE;
	InUser chess;
	char num;
	char Key = FALSE;
	printf("请输入棋盘大小(棋盘行和列值相同):");
    scanf("%d %d",&Row, &Col);
	while(!ok)
	{
    if(TURE == judgeRowCol(Row,Col))
    {
    	chessboard = intalChessboard(Row, Col);
    	showChessboard(chessboard, Row, Col);
    	ok = TURE;
	}else{
	gotoxy(1,1);
	printf("                                      ");
	gotoxy(1,1);
	printf("请输入棋盘大小(棋盘行和列值相同):");
        scanf("%d %d",&Row, &Col);
	     }
    }
    ok = FALSE; 
    printf("游戏开始...");
    getch();
	system("cls");
	intalChessboard(Row, Col);
    showChessboard(chessboard, Row, Col);
    inInformation(Row, Col);
    printf("请白家出棋:");
	scanf("%d %d", &chess.row, &chess.col);
	chess.value = '-';
	while(!ok)
	{
		if(chessJudge(chessboard,chess) == TURE)
		{
			GoChess(chessboard,chess);
			if(chessRule(chessboard,chess) == TURE)
			{
				ok = TURE;
				if(chess.value == '-')
				{
				chessLocation(chess); 
				inInformation(Row, Col);
					printf("白家获胜!");
				}else
				{
					chessLocation(chess);
				inInformation(Row,Col);
					printf("黑家获胜!");
				}
				//printf("%c获胜", chess.value);
			}else
			{
				if(gamer == FALSE)
				{
					chessLocation(chess);
					inInformation(Row,Col);
					printf("                    ");
					inInformation(Row,Col);
					printf("请黑家出棋:");
					getch();
					scanf("%d %d", &chess.row, &chess.col);					
					chess.value = '+';
					gamer = TURE;
				}else if(gamer == TURE)
				{
				chessLocation(chess);
				inInformation(Row,Col);
				printf("                    \n                                           \n              ");
				inInformation(Row,Col);
				printf("请白家出棋:");
				scanf("%d %d", &chess.row, &chess.col);
				chess.value == '-';
				gamer = FALSE;
				}
			}
		}else{
			printf("此处不能落子,请重新输入。退出请输(0,0,#)");
			scanf("%d %d %c", &chess.row, &chess.col, &chess.value);
			if(chess.value == '#')
			{
				break;
			}
		    }
      }

  free(chessboard); 
      return 0;
}

其中有许多地方处理的不好,代码不够简便,开始写的时候主要是想写成工具,以后写五子棋的时候直接调用即可,但发现写完后写成工具难度有些大,感觉很难想。其实还有几个功能想实现,人机对战(这里我想的是把用户和用户之间下棋的胜局保存下来,可以设置难度等级)、用户之间的1对多模式、界面设计、通过键盘来控制棋子位置等等,奈何学艺不精。。望大佬指教。。

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值