简易五子棋

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

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对多模式、界面设计、通过键盘来控制棋子位置等等,奈何学艺不精。。望大佬指教。。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值