教你怎麼用c++基本語法實現一個簡單的五子棋小遊戲

2020-09-28 09:01:47

這個小遊戲是在2020年5月份無聊寫的,程式碼量不大,權當娛樂哈

基本思路:

1.建立一個15*15棋盤類,並設計相關函數(輸出棋盤,下黑棋,下白棋等)
2.編寫judge()函數,判斷勝負條件
3.主函數思路:黑方先下,每次不斷更新棋盤的元素:如果該處已經有棋子了,則重新下一次,以此不斷迴圈直到遊戲結束。

程式碼如下:

1.標頭檔案

//wuziqi.h
#ifndef _WUZIQI_H_
#define _WUZIQI_H_
#include <iostream>
using namespace std;

class chessboard;
class chessboard
{
public:
	int judge();
	void build();//建立棋盤
	void changeblack(int goalx, int goaly, int& goal);//改變棋盤陣列元素即更新棋子
	void changewhite(int goalx, int goaly, int& goal);
	void out();//輸出棋盤
private:
	string arr[15][15];
};

#endif // !_WUZIQI_H_

2.cpp檔案

//wuziqi.cpp
#include <iostream>
#include "wuziqi.h"

using namespace std;

int chessboard::judge()
{
	for (int i = 0; i < 11; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			if (//橫一樣
				arr[i][j] == "●" &&
				arr[i][j + 1] == "●" &&
				arr[i][j + 2] == "●" &&
				arr[i][j + 3] == "●" &&
				arr[i][j + 4] == "●"

				) return 0;//不能使用連等
		}
		for (int j = 0; j < 11; j++)
		{
			if (//豎一樣
				arr[j][i] == "●" &&
				arr[j + 1][i] == "●" &&
				arr[j + 2][i] == "●" &&
				arr[j + 3][i] == "●" &&
				arr[j + 4][i] == "●"

				) return 0;
		}
		for (int j = 0; j < 11; j++)
		{
			if (//順斜一樣
				arr[i][j] == "●" &&
				arr[i + 1][j + 1] == "●" &&
				arr[i + 2][j + 2] == "●" &&
				arr[i + 3][j + 3] == "●" &&
				arr[i + 4][j + 4] == "●"

				) return 0;
		}
		for (int j = 14; j > 3; j--)
		{
			if (//逆斜一樣
				arr[i][j] == "●" &&
				arr[i + 1][j - 1] == "●" &&
				arr[i + 2][j - 2] == "●" &&
				arr[i + 3][j - 3] == "●" &&
				arr[i + 4][j - 4] == "●"

				) return 0;
		}
		for (int j = 0; j < 11; j++)
		{
			if (//橫一樣
				arr[i][j] == "○" &&
				arr[i][j + 1] == "○" &&
				arr[i][j + 2] == "○" &&
				arr[i][j + 3] == "○" &&
				arr[i][j + 4] == "○"

				) return 0;//不能使用連等
		}
		for (int j = 0; j < 11; j++)
		{
			if (//豎一樣
				arr[j][i] == "○" &&
				arr[j + 1][i] == "○" &&
				arr[j + 2][i] == "○" &&
				arr[j + 3][i] == "○" &&
				arr[j + 4][i] == "○"

				) return 0;
		}
		for (int j = 0; j < 11; j++)
		{
			if (//順斜一樣//列從0開始
				arr[i][j] == "○" &&
				arr[i + 1][j + 1] == "○" &&
				arr[i + 2][j + 2] == "○" &&
				arr[i + 3][j + 3] == "○" &&
				arr[i + 4][j + 4] == "○"

				) return 0;
		}
		for (int j = 14; j > 3; j--)
		{
			if (//逆斜一樣//列從14開始
				arr[i][j] == "○" &&
				arr[i + 1][j - 1] == "○" &&
				arr[i + 2][j - 2] == "○" &&
				arr[i + 3][j - 3] == "○" &&
				arr[i + 4][j - 4] == "○"

				) return 0;
		}
	}
	return 1;
}

//棋盤--------------------------------------------------------------------------------------

void chessboard::build()
{
	for (int i = 0; i < 15; i++)
	{
		for (int j = 0; j < 15; j++)
		{
			arr[i][j] = "  ";
		}
	}//初始化棋盤
}
void chessboard::changeblack(int goalx, int goaly, int& goal)//更新點位
{
	if (arr[goalx][goaly] != "  ")
	{
		cout << "這一點已經有子了,請重新下一次黑棋" << endl;
		goal--;
	}
	else
		arr[goalx][goaly] = "○";
}
void chessboard::changewhite(int goalx, int goaly, int& goal)//更新點位
{
	if (arr[goalx][goaly] != "  ")
	{
		cout << "這一點已經有子了,請重新下一次白旗" << endl;
		goal--;
	}
	else
		arr[goalx][goaly] = "●";
}
void chessboard::out()
{
	cout << "    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  " << endl;
	for (int i = 0; i < 15; i++)
	{
		cout << "---------------------------------------------------------------" << endl;
		if (i < 10)
			cout << i << " | ";
		else
			cout << i << "| ";
		for (int j = 0; j < 15; j++)
		{
			cout << arr[i][j] << "| ";
		}
		cout << endl;
	}//輸出棋盤,每次輸入一個點就輸出一次棋盤
	cout << "---------------------------------------------------------------" << endl;
}
//main.cpp
#include <iostream>
#include "wuziqi.h"

using namespace std;

int main(void)
{
	chessboard goalboard;//目標棋盤
	goalboard.build();

	goalboard.out();

	for (int i = 1; i < 10000; i++)
	{
		if (i % 2 != 0)//奇數黑方走,偶數白方走
		{
			int x = 0, y = 0;
			cout << "請輸入黑棋x座標和y座標" << endl;
			cin >> y >> x;
			goalboard.changeblack(x, y, i);//持續更新黑棋盤
			goalboard.out();

			if (goalboard.judge() == 0)
			{
				cout << "遊戲結束 黑棋勝利!" << endl;
				cout << "輸入任意座標重新開始" << endl;
				goalboard.build();
			}
		}
		else
		{
			int x = 0, y = 0;
			cout << "請輸入白棋x座標和y座標" << endl;
			cin >> y >> x;
			goalboard.changewhite(x, y, i);//持續更新白棋盤
			goalboard.out();

			if (goalboard.judge() == 0)
			{
				cout << "遊戲結束 白旗勝利!" << endl;
				cout << "輸入任意座標重新開始" << endl;
				goalboard.build();
			}
		}
	}

	return 0;
}

執行效果圖:

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

PS:

這個小遊戲還有不少不足之處,如輸入棋子座標超過棋盤大小怎麼辦?勝負的判定是否還有更簡單的方法?都值得再去思考完善。
菜雞一枚難免有很多不足之處,歡迎指教!感激不盡!