C++开发控制台小游戏之旅(2)——《五子棋》

歡迎光臨

相信每一位學編程的朋友,都曾有一顆游戲夢想,希望能親手打造出屬於自己的游戲!

如果我可以創作一款結合AR技術的、如**動漫《游戲王》**中那樣進行決鬥的游戲,該有多好!

游戲背景

《五子棋》這款游戲,從我讀小學起就經常在玩,與小夥伴們一起在課餘時間一決高下,不亦樂乎!曾經的美好,只留下回憶,但我相信我的人生始終會與快樂相伴……

如今我萌發心思,欲抽空發展一下愛好,自己動手逐步打造出一款可玩的小游戲,同時也為廣大游戲編程愛好者們提供略微的參考,一起爲了夢想而前行!

本游戲采用純C++語言進行編寫,可在Windows控制臺Linux終端運行。
使用Visual StudioGCC進行編譯即可。
游戲内操作均通過鍵盤輸入達成,簡單易上手。
全部源碼文件僅有1個,直接複製粘貼到本地即可輕鬆完成編譯構建,生產可執行程序並開啓游戲。

注:游戲源碼在頁面底部。


若諸位喜歡《五子棋》,並希望改善游戲體驗(如期待後續增加的游戲功能、玩法等),可在留言區添加評論,進行吐槽或提供寶貴建議(如發現游戲bug),亦可添加作者微信、QQ,
相互交流,共同進步!
我將認真回復並虛心學習,感謝諸位!

我的聯係方式
QQ:1575335819
微信:CS-huo

游戲截圖

開始游戲

錯誤提示

游戲結束

游戲特色

  • 背景故事
  • 自定義地圖大小
  • 花式字體顔色

後續待完善

  • Bug修復
  • AI敵人

源代碼

#include <iostream>
#include <string>
#ifdef _WIN32
#include <Windows.h>
#endif // _WIN32
using namespace std;


typedef struct Point
{
	int row;
	int col;

	Point() { row = 0; col = 0; }

	Point(int r, int c) 
	{
		row = r;
		col = c;
	}
} Map;


static Map map;
static Point point;
static char **arr;


void setColor(int n);
void restoreColor();
void startGame();
void endGame();
void tellStory();
void initMap();
void playerRound(int n);
bool isValidRowCol(const Map &m);
bool isValid(const Point &p);
bool win(int n);
void showBoard();


int main()
{
	startGame();
}

#ifdef _WIN32
void setColor(int n)
{
	WORD attr;
	if (n == 31) attr = FOREGROUND_RED;
	else if (n == 34) attr = FOREGROUND_BLUE;
	else if (n > 30 && n < 39) attr = FOREGROUND_GREEN;
	else if (n == 41) attr = BACKGROUND_RED;
	else if (n == 42) attr = BACKGROUND_GREEN;
	else if (n > 40 && n < 49) attr = BACKGROUND_BLUE;

	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr);
}

void restoreColor()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
}
#endif	// WIN32
#ifdef __linux
void setColor(int n)
{
	cout << "\033[" << to_string(n) + "m";
}
	
void restoreColor()
{
	cout << "\033[39m";
	cout << "\033[49m";
}
#endif	// __linux

void startGame()
{
	string info = "五子棋游戲開始!";
	setColor(31);
	cout << info << endl;
	restoreColor();
	
	// Tell story
	tellStory();

	// Initialize the map by player
	initMap();

	// Play round, use a loop to control the game
	for (int i = 0; i < 10000; ++i)
	{
		playerRound(i%2+1);
	}
}

void endGame()
{
	string info = "勝負已分,游戲結束!";
	setColor(31);
	cout << endl << info << endl;
	restoreColor();

	// Exit the process
	exit(0);
}

void tellStory()
{
	string story = "滾滾長江東逝水,浪花淘儘英雄。是非成敗轉頭空,青山依舊在,幾度夕陽紅。\n"
		"一代五子棋天才少年——江流兒,欲與棋聖林心誠一決高下!今天就是他倆的決鬥之日……";
	setColor(32);
	cout << story << endl << endl;
	restoreColor();
}

void initMap()
{
	// Initialize the map by player
	setColor(34);
	cout << "初始化地圖,請輸入網格地圖行列數……" << endl;

	setColor(31);
	do 
	{
		cout << "網格地圖行數:";
		cin >> map.row;
		cout << "網格地圖列數:";
		cin >> map.col;
	} while (!isValidRowCol(map));

	// Create board, assign to array
	arr = new char*[map.row];
	for (int i = 0; i < map.row; ++i)
	{
		arr[i] = new char[map.col];
		for (int j = 0; j < map.col; ++j)
		{
			arr[i][j] = '-';
		}
	}

	setColor(31);
	cout << "各位棋手,地圖初始化已完成,游戲正式啓動!" << endl;
	restoreColor();

	// Show the chess board
	showBoard();
}

void playerRound(int n)
{
	string info = "輪到棋手" + to_string(n) + "的回合!";
	setColor(31);
	cout << info << endl;

	// Input the position of chess
	setColor(32);
	do 
	{
		cout << "請決定棋子放置位置……" << endl;
		cout << "行:";
		cin >> point.row;
		cout << "列:";
		cin >> point.col;
	} while (!isValid(point));
	restoreColor();

	// Set chess
	arr[point.row-1][point.col-1] = (n == 1 ? 'X' : 'O');

	// Judge win
	bool res = win(n);
	if (res)
	{
		setColor(31);
		cout << "恭喜棋手" << to_string(n) << "取得了本局勝利!" << endl;
		restoreColor();

		showBoard();
		endGame();
	}

	// Show the board
	showBoard();
}

bool isValidRowCol(const Map &m)
{
	if (m.row > 0 && m.col > 0 && m.row < 100 && m.col < 100) return true;
	return false;
}

bool isValid(const Point &p)
{
	int r = p.row-1, c = p.col-1;

	if (r < 0 || c < 0 || r >= map.row || c >= map.col) 
	{
		setColor(31);
		cout << "不可在此位置擺放棋子,請再三考慮!" << endl;
		restoreColor();
		return false;
	}

	if (arr[r][c] == '-') return true;
	else 
	{
		setColor(31);
		cout << "不可在此位置擺放棋子,請再三考慮!" << endl;
		restoreColor();
	}

	return false;
}

bool win(int n)
{
#define CLEAR	\
	count = 0;	\
	last = tmp = '-';

#define JUDGE	\
	{	\
	tmp = arr[i][j];	\
	if (tmp == '-' || last != tmp) count = 0;	\
	last = tmp;	\
	++count;	\
	if (count >= 5) return true;	\
	}

	int count = 0;
	char last = '-';
	char tmp = '-';

	for (int i = 0; i < map.row; ++i)
	{
		CLEAR
		for (int j = 0; j < map.col; ++j) JUDGE
	}

	for (int i = -1; i < map.row; ++i)
	{
		CLEAR
		int j = -1;
		while (++j < map.col && ++i < map.row) JUDGE
	}

	for (int j = 0; j < map.col; ++j)
	{
		CLEAR
		for (int i = 0; i < map.row; ++i) JUDGE
	}

	for (int j = -1; j < map.col; ++j)
	{
		CLEAR
		int i = -1;
		while (++i < map.row && ++j < map.col) JUDGE
	}

	return false;
#undef CLEAR
#undef JUDGE
}

void showBoard()
{
	string info = "當前棋盤格局為:";
	setColor(34);
	cout << endl << info << endl;
	
	// Show chess board
	setColor(32);
	for (int i = 0; i < map.row; ++i)
	{
		for (int j = 0; j < map.col; ++j)
		{
			cout << " " << arr[i][j];
		}
		cout << endl;
	}
	restoreColor();
}

关于我

我是一位喜欢创新、乐观向上的少年
爱好是看书、踢足球、玩LOL等
喜欢我的文章的朋友,可以添加个人微信:CS-huo
有问题可以相互探讨,共同学习!

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值