1. 程式人生 > >三子棋遊戲(c語言實現)

三子棋遊戲(c語言實現)

</pre>一說到寫個三子棋遊戲,首先我們得想到有個棋盤初始化和棋盤列印函式。<p></p><p>接下來,玩遊戲階段,人玩遊戲,電腦玩遊戲,各寫一個函式,每次下完棋都得判斷有沒有人贏了遊戲,若有人贏了,遊戲退出,否則繼續遊戲。在這過程中,還有可能棋盤滿</p><p>了,所以寫個判斷棋盤是否滿的的函式,棋盤滿了,程式結束,沒人贏,此時就平局。</p><p>當然了,還有一些輔助函式,比如game函式,print_manu函式,只是起到封裝的作用,也防止main函式過長。</p><p>程式亮點:巧妙地運用了逗號表示式(在電腦下棋遊戲中),但是,得用個計數器,while語句執行一次就好,不然電腦就耍賴了。</p><p>                   程式有一定的容錯性。</p><p>程式缺陷:遊戲只能一次性玩一局。</p><p>                   只寫了人機遊戲,沒有人人遊戲。這個需要之後繼續實現。</p><p> <span style="background-color:rgb(255,102,102)"> game.c檔案</span></p><pre name="code" class="objc">#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

int chess_full(char chessBoard[3][3])//棋盤判滿函式
{
	int i = 0;
	int j = 0;
	for (i = 0;i < 3;i++)
	{
		for (j = 0;j < 3;j++)
		{
			if (chessBoard[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
void init(char chessBoard[3][3])//棋盤初試化函式
{
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			chessBoard[i][j] = ' ';
		}
	}

	distchessBoard(chessBoard);
}
void distchessBoard(char chessBoard[3][3])//列印棋盤函式
{
	int i = 0;
	for (i = 0;i < 3;i++)//列印棋盤
	{
		printf(" %c | %c | %c \n", chessBoard[i][0], chessBoard[i][1], chessBoard[i][2]);
		if (i != 2)
			printf("---|---|---\n");//pchessBoard
	}
}
void manPlay(char chessBoard[3][3])//人下棋函式
{
	if (chess_full(chessBoard) == 1)
	{
		exit(0);
	}
	int line = 0;
	int column = 0;
	do
	{
		printf("請輸入你的棋子的位置(注意最小下標是0哦):");
		scanf("%d%d", &line, &column);
		while (chessBoard[line][column] != ' ')
			if ((line < 0) || (line > 2) || (column < 0) || (column > 2))
			{
				printf("對不起,您輸入的是無效的位置!");
			}
		chessBoard[line][column] = 'X';
	} while (chessBoard[line][column] == ' ');
	distchessBoard(chessBoard);
}
void pcplay(char chessBoard[3][3])//電腦下棋函式
{
	if (chess_full(chessBoard) == 1)
	{
		exit(0);
	}
	printf("電腦下棋中...\n");
	int line = 0;
	int column = 0;
	int count = 0;
	while (line = rand() % 3, column = rand() % 3, chessBoard[line][column] == ' ')
	{
		count++;
		chessBoard[line][column] = 'Y';
		if (count == 1)
		{
			break;
		}
	}
	distchessBoard(chessBoard);
}
int judge(char chessBoard[3][3])
{
	int i = 0;
	if ((chessBoard[0][0] == chessBoard[1][1]) && (chessBoard[1][1] == chessBoard[2][2]))
	{
		if (chessBoard[1][1] == 'X')
		{
			printf("玩家贏了\n");
			return 1;
		}
		if (chessBoard[1][1] == 'Y')
		{
			printf("對方贏了\n");
			return 1;
		}
	}
	if ((chessBoard[0][2] == chessBoard[1][1]) && (chessBoard[1][1] == chessBoard[2][0]))
	{
		if (chessBoard[1][1] == 'X')
		{
			printf("玩家贏了\n");
			return 1;
		}
		if (chessBoard[1][1] == 'Y')
		{
			printf("對方贏了\n");
			return 1;
		}
	}
	for (i = 0;i < 3;i++)
	{
		if ((chessBoard[i][1] == chessBoard[i][0]) && (chessBoard[i][1] == chessBoard[i][2]))
		{
			if (chessBoard[i][1] == 'X')
			{
				printf("玩家贏了\n");
				return 1;
			}
			if (chessBoard[i][1] == 'Y')
			{
				printf("對方贏了\n");
				return 1;
			}
		}

	}
	for (i = 0;i < 3;i++)
	{
		if ((chessBoard[0][i] == chessBoard[1][i]) && (chessBoard[1][i] == chessBoard[2][i]))
		{
			if (chessBoard[0][i] == 'X')
			{
				printf("玩家贏了\n");
				return 1;
			}
			if (chessBoard[0][i] == 'Y')
			{
				printf("對方贏了\n");
				return 1;
			}
		}
	}
	return 0;
}
void game(char chessBoard[3][3])
{
	//char chessBoard[3][3];
	int i = 0;
	int ret = 0;
	init(chessBoard);//呼叫初始化函式
	while ((chess_full(chessBoard) == 0))
	{
		manPlay(chessBoard);
		pcplay(chessBoard);
		ret = judge(chessBoard);
		if (ret == 1)
			break;
	}
	if (ret == 0)
	{
		printf("平局\n");
	}
}

void print_manu()
{
	printf("----------------歡迎進入三子棋系統-------------------\n");
	printf("*****************************************************\n");
	printf("**********************1.play*************************\n");
	printf("**********************2.exit*************************\n");
	printf("*****************************************************\n");
}
<pre name="code" class="objc"><span style="background-color: rgb(255, 102, 102);">game.h檔案</span>
</pre><pre name="code" class="objc">#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>

int chess_full(char chessBoard[3][3]);//棋盤判滿函式
void init(char chessBoard[3][3]);//棋盤初試化函式
void distchessBoard(char chessBoard[3][3]);//列印棋盤函式
void manPlay(char chessBoard[3][3]);//人下棋函式
void pcplay(char chessBoard[3][3]);//電腦下棋函式
int judge(char chessBoard[3][3]);//評判勝負函式
void game(char chessBoard[3][3]);//遊戲函式
void print_manu();//列印選單函式

test.c檔案
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

int main()
{
	print_manu();
	char chessBoard[3][3];
	printf("請輸入你的選擇>");
	int choose = 0;
	scanf("%d", &choose);
	switch (choose)
	{
	case 1:
		game(chessBoard);
		break;
	case 0:
		exit(0);
		break;
	default:
		printf("input error");
	}
	system("pause");
	return 0;
}