1. 程式人生 > >C語言實現簡單的三子棋小遊戲

C語言實現簡單的三子棋小遊戲

函式實現的基本功能:

1.初始化棋盤:用空字元初始化

2.列印棋盤:控制列印九宮格來表示棋盤

3.玩家下棋:用P表示玩家的棋子

4.電腦隨機下棋:隨機生成座標,用0表示玩家的棋子

5.判斷輸贏:玩家贏,電腦贏,平局,繼續遊戲四種情況


多檔案實現:

測試部分test.c程式碼

#include<windows.h>
#include"game.h"
menu()
{
	printf("###############################\n");
	printf("#######歡迎來到三子棋遊戲######\n");
	printf("####### 1.play    0.exit ######\n");
	printf("###############################\n");
}
void game()
{
	char board[ROW][COL] = { 0 };
	char win = 0;
	InitBoard(board, ROW, COL);//初始化空格棋盤
	//列印棋盤
	PrintBoard(board, ROW, COL);
	do
	{
		PlayerMove(board, ROW, COL);//玩家走
	    PrintBoard(board, ROW, COL);
		win = CheckWin(board, ROW, COL);//檢測輸贏;玩家贏P 電腦贏0  繼續遊戲 平局E 
		if (win != ' ')
			break;
		ComputerMove(board, ROW, COL);//電腦走
		PrintBoard(board, ROW, COL);
		win = CheckWin(board, ROW, COL);
		if (win != ' ')
	       {
                       break;
		PrintBoard(board, ROW, COL);
               }
	}
	while (1);
	if (win == 'P')
		printf("恭喜你你贏了\n");
	else if (win == '0')

		printf("不好意思電腦贏了\n");
	else if (win == 'E')
		printf("平局\n");

}

void test()
{
	int input = 0;
	srand((unsigned)time(NULL));
	do{
      	menu();//列印選單
		printf("請選擇;>");
		scanf_s("%d",&input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出遊戲\n");
			break;
		default:
			printf("選擇錯誤\n");
			break;
		}
	} while (input);
}


int main()
{
	test();
	
	system("pause");
	return 0;
}
遊戲標頭檔案game.h程式碼

#ifndef  _GAME_H_
#define  _GAME_H_


#include<stdio.h>
#include<stdlib.h>
#include<time.h>


#define ROW 3
#define COL 3


void InitBoard(char board[ROW][COL], int row, int col);
void PrintBoard(char board[ROW][COL],int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char CheckWin(char board[ROW][COL], int row, int col);
#endif
遊戲實現部分game.c

#include<stdio.h>
#include "game.h"

void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < row; j++)
		{
			board[i][j] = ' ';
		}
	}


}
void PrintBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
		if (i<row-1)
			printf("---|---|---\n");
	}

}
void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("玩家下棋>:");
		scanf_s("%d%d", &x, &y);
		if (x >= 1 && x <= ROW && y>= 1 && y<= COL)//判斷座標合法性
		{
			//判斷座標是否被走過
			if (board[x-1][y-1] == ' ')
			{
				board[x-1][y-1] = 'P';
				break;
			}
			else
				printf("該步已經被走過\n");
		}
		else
			printf("座標輸入有誤請重新輸入\n");

	}

}
int  is_full(char board[ROW][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;//不滿返回0
		}
	}return 1;//滿了返回1
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("電腦下棋>:\n");
	while (1)
	{
		x = rand() % ROW;//隨機值0-2
		y = rand() % COL;//隨機值0-2
		if (board[x][y] == ' ')
		{
			board[x][y] = '0';
			break;
		}

	}
}
char CheckWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
		{
			return board[i][0];
		}
	}//處理行相等
	for (i = 0; i < row; i++)
	{
		if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][i];
		}
	}//處理列相等

		if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
		{
			return board[1][1];
		}
		if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
		{
			return board[1][1];
		}//處理對角線相等

		if (is_full(board, row, col) == 1)
		{
			return'E';

		}return' ';

}


效果展示:



以上只是簡單的實現部分,還望各位大佬多多指點!