簡單五子棋(單機版-C)

簡單五子棋(單機版-C)
數組

一個簡單的五子棋過程。code

之實現了開局和判斷勝負。缺乏載入棋譜,保存棋譜。
遊戲

#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;
	}
}
相關文章
相關標籤/搜索