C語言數組實現三子棋


C語言實現三子棋(通過數組)

  • 需要包含的頭文件
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  • 創建一個全局數組
    因為如果數組大小變化,游戲規則和實現思路都會有很大的變化,所以不進行宏定義常量來定義數組
char board[3][3];
  • 設計主程序框架
    game()函數為游戲過程框架
int main()
{

	do
	{
		int i = 0;
		printf("1.Start the game\n");
		printf("2.Quit the game\n");
		printf("Please enter your options->");
		scanf("%d", &i);
		switch (i)
		{
			case 1:
				game();    //game()函數為游戲過程框架
				break;
			case 2:
				return 0;
			default:
				printf("Input error,please enter again!\n");
				break;
		}
	} while(1);
}
  • 設計游戲過程框架
void game()
{
	initBoard();
	srand((unsigned)time(NULL));    //生成隨機種子,后面需要生成隨機坐標
	char temp = ' ';                //定義一個字符變量,用於接收后面判斷函數返回值,用於決定游戲勝負
	do
	{
		printBoard();           //打印棋盤的函數
		userPlay();             //玩家下棋的函數
		temp = judge();         //判斷游戲是否分出勝負
		if (temp != ' ')
			break;
		robotPlay();            //電腦下棋的函數
		temp = judge();
	} while (temp == ' ');
	printBoard();
	switch (temp)
	{
		case '@':
			printf("User WIN!\n");
			break;
		case '$':
			printf("Bobot WIN!\n");
			break;
		case '*':
			printf("Dogfall !\n");
			break;
		dafault:
			break;
	}
}
  • 設計棋盤樣式
    有興趣的可以搞的更加花里胡哨,這里草草了事,呸,簡單設計一下 哈
//##########
// | | 
//_|_|_
// | | 
//_|_|_
// | |
//##########
  • 打印棋盤
    重點就在這了,棋盤設計的再牛逼,也得能打印出來才行
    這里棋盤設計的比較簡易,打印起來也比較簡單,關鍵是思路要清晰
void printBoard()
{
	printf("##########\n");
	int i = 0,j = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			if (j != 2)
			{
				printf("%c|",board[i][j]);
			}
			else
				printf("%c\n",board[i][j]);

		}
		if (i != 2)
			printf("_|_|_\n");
	}
	printf("##########\n");
}
  • 玩家下棋
    本人習慣用界面編程的坐標系表示坐標,因此下面賦值時橫坐標和縱坐標倒過來了
void userPlay()
{
	printf("Please enter coordinates->");
	int x = 0,y = 0;
	while(1)
	{
		scanf("%d %d", &x, &y);
		if (x > 0 && x < 4 && y > 0 && y < 4)
		{
			
			if (board[y - 1][x - 1] == ' ')
			{
				board[y - 1][x - 1] = '@';
				break;
			}
			else
				printf("There are chess pieces here,please enter again!->");
		}
		else
			printf("Input error,please enter again!->");
	}
}

  • 電腦下棋
    這里關鍵是 要生成可行的坐標,以及隨機數的生成
void robotPlay()
{
	int i = 0, j = 0;
	while (1)
	{
		i = rand() % 3;
		j = rand() % 3;
		if (board[i][j] == ' ')
		{
			board[i][j] = '$';
			break;
		}
	}

}
  • 判斷游戲勝負
    這里就到了最難分析的一部分了
    廢話不多說,直接看代碼
char judge()
{
	int i = 0, j = 0;
        //判斷兩條對角線    對i j 的值不做帶動,所以放在前面
	if (board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] != ' ')
		return board[i][j];
	if (board[i][j + 2] == board[i + 1][j + 1] && board[i][j + 2] == board[i + 2][j] && board[i][j] != ' ')
		return board[i][j + 2];
        //依次判斷三行、三列
	for (i = 0,j = 0; j < 3; j++)
	{
		if (board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] != ' ')
			return board[i][j];
	}
	for (i = 0,j = 0; i < 3; i++)
	{
		if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] != ' ')
			return board[i][j];
	}
        //判斷棋盤是否滿了(平局)
	for (i = 0; i < 3; i++)
	{
		for (j = 0;j < 3; j++)
		{
			if (board[i][j] == ' ')
				return board[i][j];
		}
	}
        //如果沒出現以上情況,隨便返回一個不同符號,使游戲繼續進行
	return '*';
	
}

源代碼鏈接(Github)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM