C語言 二維陣列實現三子棋的思路及程式碼

這裡有一個暱稱發表於2020-11-17

首先需要列印棋盤,用橫線和豎線去拼湊一個三行三列的棋盤

在這一步中需要用到for迴圈對二維陣列進行初始化,及將棋盤列印成型

棋盤列印出來後需要開始遊戲的進行,分為玩家的操作和電腦隨機生成的座標

在每次有人進行操作後都需要一個方法去判斷是否有人贏,或者是否棋盤滿了達到平局

完整程式碼如下

分為三個檔案(game.h,game.c,Tab.c)

<game.h>

#define _CRT_SECURE_NO_WARNINGS 1
#ifndef _GAME_H_
#define _GAME_H_

#include<stdio.h>
#include<Windows.h>

#define ROW 3
#define COL 3

void InitBoard(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
void PlayerMove(char board[][COL], int row, int col);
void CmptMove(char board[][COL], int row, int col);
char IsWin(char board[][COL], int row, int col);
int IsFull(char board[][COL], int row, int col);


#endif

<game.c>

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

//初始化為空格
void InitBoard(char board[][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

//搭建棋盤
void ShowBoard(char board[][COL], int row, int col)
{
	for (int i = 0; i < row; i++)   //行數
	{
		for (int j = 0; j < col; j++)     //列數
		{
			printf(" %c ",board[i][j]);
			if (j < col - 1 )
			{
				printf("|");
			}
		}
		printf("\n");

		if (i < row - 1)
		{
			for (int j = 0; j < col; j++)     //列數
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

//玩家走
void PlayerMove(char board[][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("請輸入你的座標x,y(1-3):");
		scanf("%d%d", &x, &y);
		if ((x >= 1) && (x <= 3) && (y >= 1) && (y <= 3))
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = 'X';
				break;
			}
		}
		else
			printf("你輸入的座標不合法,請重新輸入:\n");
	}
}


//電腦走
void CmptMove(char board[][COL], int row, int col)
{
	printf("電腦走:\n");
	while (1)
	{
		int x = rand() % ROW;
		int y = rand() % COL;
		if (board[x ][y ] == ' ')
		{
			board[x][y] = 'O';
			break;
		}
	}
}

//判斷是否有人贏
char IsWin(char board[][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
			return board[i][1];//行
		if (board[0][i] == board[1][i] && board[2][i] == board[2][i] && board[0][i] != ' ')
			return board[0][i];//列
		if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
			return board[0][0];//對角線
		if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
			return board[0][2];//對角線
		if (IsFull(board, ROW, COL) == 1)
			return 'Q';//平局
	}
	return ' ';
}

//判斷是否平局
int IsFull(char board[][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] = ' ')
			{
				return 0;
			}
		}
	}
	return 1;
}

<Tab.c>

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void Menu()
{
	printf("***********************\n");
	printf("*********0.exit********\n");
	printf("*********1.play********\n");
	printf("***********************\n");
}

void Game()
{
	char board[ROW][COL] = { 0 };

	InitBoard(board, ROW, COL);
	ShowBoard(board, ROW, COL);

	int res = 0;
	while (1)
	{
		
		PlayerMove(board, ROW, COL);
		ShowBoard(board, ROW, COL);
		res = IsWin(board, ROW, COL);
		if (res != ' ')  //  有人贏了
		{
			break;
		}


		CmptMove(board, ROW, COL);
		ShowBoard(board, ROW, COL);
		res = IsWin(board, ROW, COL);
		if (res != ' ')  //  有人贏了
		{
			break;
		}
	}
	if (res == 'Q')
		printf("平局!\n");
	if (res == 'X')
		printf("恭喜你贏了!\n");
	if (res == 'O')
		printf("電腦贏!\n");
}

int main()
{
	srand((unsigned)time(NULL));
	int input= 0;
	do
	{
		Menu();
		printf("請輸入你的操作選項:");
		scanf("%d", &input);
		
		switch (input)
		{
			case 1:
				Game();
				break;
			case 0:
				printf("退出遊戲!");
				break;
			default:
				break;
		}
	} while (input);


	system("pause");
	return 0;
}

 

 

相關文章