欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简易五子棋

程序员文章站 2024-03-16 22:43:58
...

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

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

相关标签: chessboard