简单五子棋(单机版-C)

邹杰
2023-12-01

简单五子棋(单机版-C)

一个简单的五子棋过程。

之实现了开局和判断胜负。缺少载入棋谱,保存棋谱。

#include <stdio.h>
//图形输出棋盘
printGraphicBoard(int b[9][9]){
	int i,j;
	for(i=0;i<9;i++)
		{
			if(i==0)
				printf("  ①②③④⑤⑥⑦⑧⑨\n");

			switch (i)
			{
			case 0:
				printf("⒈");break;
			case 1:
				printf("⒉");break;
			case 2:
				printf("⒊");break;
			case 3:
				printf("⒋");break;
			case 4:
				printf("⒌");break;
			case 5:
				printf("⒍");break;
			case 6:
				printf("⒎");break;
			case 7:
				printf("⒏");break;
			case 8:
				printf("⒐");break;
			}

			for(j=0;j<9;j++)
			{
				if(i==0 && j==0 )
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("┌");
				if(i==0 && (j>=1 && j<=7))
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("┬");
				if(i==0 && j==8 )
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("┐");	
				if(j==0 && (i>=1 && i<=7))
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("├");
				if(j==8 && (i>=1 && i<=7))
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("┤");
				if(i!=0 && j!=0 && i!=8 && j!=8)
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("┼");

				if(i==8 && j==0 )
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("└");
				if(i==8 && (j>=1 && j<=7))
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("┴");
				if(i==8 && j==8 )
					if(b[i][j] == -1)
						printf("●");					
					else if (b[i][j]==1)
							printf("○");
						else
							printf("┘");	
			}
			printf("\n");
		}
}

//根据b数组判断该棋局是否已胜,如果已经胜了,返回1;否则,返回0
int isWin(int b[9][9]){
	int i,j,k;
	for(i=0;i<9;i++)
		for(j=0;j<9;j++)
		{
			for(k=1;k<=8;k++)
				if (b[i][j]!=0)
				switch (k)
				{
				case 1://左边
					if( j-4>=0 && b[i][j]==b[i][j-1] &&
						b[i][j]==b[i][j-2] &&
						b[i][j]==b[i][j-3] &&
						b[i][j]==b[i][j-4] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				case 2://右边
					if( j+4<=8 &&
						b[i][j]==b[i][j+1] &&
						b[i][j]==b[i][j+2] &&
						b[i][j]==b[i][j+3] &&
						b[i][j]==b[i][j+4] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				case 3://上面
					if( i-4>=0 &&
						b[i][j]==b[i-1][j] &&
						b[i][j]==b[i-2][j] &&
						b[i][j]==b[i-3][j] &&
						b[i][j]==b[i-4][j] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				case 4://down
					if( i+4 <= 8 &&
						b[i][j]==b[i+1][j] &&
						b[i][j]==b[i+2][j] &&
						b[i][j]==b[i+3][j] &&
						b[i][j]==b[i+4][j] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				case 5://左上
					if( i-4>=0 && j-4 >= 0 &&
						b[i][j]==b[i-1][j-1] &&
						b[i][j]==b[i-2][j-2] &&
						b[i][j]==b[i-3][j-3] &&
						b[i][j]==b[i-4][j-4] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				case 6://左下i+,j-
					if( i+4<=8 && j-4>=0 &&
						b[i][j]==b[i+1][j-1] &&
						b[i][j]==b[i+2][j-2] &&
						b[i][j]==b[i+3][j-3] &&
						b[i][j]==b[i+4][j-4] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				case 7://右上i-,j+
					if( i-4>=0 && j+4<=8 &&
						b[i][j]==b[i-1][j+1] &&
						b[i][j]==b[i-2][j+2] &&
						b[i][j]==b[i-3][j+3] &&
						b[i][j]==b[i-4][j+4] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				case 8://右下
					if( i+4<=8 && j+4 <=8 &&
						b[i][j]==b[i+1][j+1] &&
						b[i][j]==b[i+2][j+2] &&
						b[i][j]==b[i+3][j+3] &&
						b[i][j]==b[i+4][j+4] )
					{
						printf("%s\n",b[i][j]==1?"白方获胜。":"黑方获胜。");
						return 1;//一方获胜返回1
					}
					break;
				}

		}
	return 0;
}

void main()
{

	int board[9][9]={0};
	int key;
	int i,j,x,y;
	int turn = -1;

	//1 游戏屏幕
	printf("=================================================\n");
	printf("*            1.  开局                           *\n");
	printf("*            2.  载入                           *\n");
	printf("*            3.  保存                           *\n");
	printf("*            4.  退出                           *\n");
	printf("=================================================\n");

	//输入选择
	scanf("%d",&key);

	//根据选择进入不同过程
	switch (key)
	{
	case 1:
		
		//图形化输出初始的棋盘
		//print board
		printGraphicBoard(board);

		//game start

		while(!isWin(board)){
			if (turn == -1)
			{
				printf("请输入黑方落子点行(i,j)");
				scanf("%d",&x);
				scanf("%d",&y);
				
				if(board[x-1][y-1]==0)
					board[x-1][y-1] = -1;//记住一个黑子
				else
				{
					printf("此处不许落子!\n");
					turn = turn * -1;
				}

				//显示黑子
				printGraphicBoard(board);
			}
			else
			{
				//输入白字
				printf("请输入白方落子点行(i,j)");
				scanf("%d",&x);
				scanf("%d",&y);
	
				if(board[x-1][y-1]==0)
					board[x-1][y-1] = 1;//记住一个白子
				else
				{
					printf("此处不许落子!\n");
					turn = turn * -1;
				}
				

				//显示
				printGraphicBoard(board);
			}

			turn = turn * -1;//轮流下棋
		}
		//
		break;
	case 2:
	case 3:
	case 4:
	default:
		break;
	}
}


 类似资料: