C++五子棋(附源码)

话不多说,源码奉上!

//玩家一与电脑用‘x’标识,玩家二用'o'标识
#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
using namespace::std;
#define N 20
char chessboardflag = ' ';  //棋盘标志
void color(int a)//改变颜色的函数
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
class coordinate            //坐标类
{
public:
	int x;
	int y;
};
class fivechess
{
public:
	void initchessboard()//初始化棋盘
	{
		for (int i = 0; i < N; ++i)
			for (int j = 0; j < N; ++j)
				chessboard[i][j] = chessboardflag;
		printchessboard();
	}
	void printchessboard()//打印棋盘
	{
		system("cls");
		system("color 70");
		cout << "  1 2 3 4 5 6 7 8 9 10111213141516171819"<<endl;
		for (int i = 1; i < N; ++i) {
			for (int j = 0; j < N; ++j)
			{
				if (j == 0) {
					if (i <= 9)cout << i << " ";
					else cout << i;
				}
				else
				{
					if (chessboard[i][j] == 'x')
					{
						color(0x70);
						cout << "●";
					}
					else if (chessboard[i][j] == 'o')
					{
						color(0x7f);
						cout << "●";
					}
					else 
					{
						color(0x74);
						cout << "■";
					}
				}
			}
			cout << endl;
			color(0x70);
		}
	}
	coordinate playchess1()  //玩家一下棋
	{
		cout << "请玩家一输入坐标:" << endl;
		int x1, y1;
		while (cin >> x1 >> y1)
		{
			if (x1 > N-1 || y1 > N-1 || x1 < 0 || y1 < 0)
			{
				cout << "输入超界,请从新输入" << endl;
				continue;
			}
			if (chessboard[x1][y1] == ' ') {
				chessboard[x1][y1] = 'x';
				break;
			}
			else
			{
				cout << "输入错误,请重新输入";
			}
		}
		coordinate temp;
		temp.x = x1;
		temp.y = y1;
		return temp;
	}
	coordinate playchess2()  //玩家二下棋
	{
		cout << "请玩家2输入坐标:" << endl;
		int x2, y2;
		while (cin >> x2 >> y2)
		{
			if (x2 > N-1 || y2 > N-1 || x2 < 0 || y2 < 0)
			{
				cout << "输入超界,请从新输入" << endl;
				continue;
			}
			if (chessboard[x2][y2] == ' ') {
				chessboard[x2][y2] = 'o';
				break;
			}
			else
			{
				cout << "输入错误,请重新输入";
			}
		}
		coordinate temp;
		temp.x = x2;
		temp.y = y2;
		return temp;
	}
	coordinate computerplayer()//电脑下棋
	{
		coordinate temp;
		srand((unsigned)time(NULL));
		int x1 = 0, y1 = 0;
		while ((x1 = (rand() % (N-1)) + 1) && (y1 = (rand() % (N-1)) + 1))
		{
			if (chessboard[x1][y1] == ' ') {
				chessboard[x1][y1] = 'x';
				break;
			}
			else continue;
		}
		temp.x = x1; temp.y = y1;
		return temp;
	}
	int judgewiner1(char a, coordinate temp)//判断横排
	{
		coordinate begin, end;
		begin.x = end.x = temp.x;
		if (temp.y <= 5)begin.y = 1;
		else begin.y = temp.y - 4;
		if (temp.y >= N-5)end.y = N-1;
		else end.y = temp.y + 4;
		for (int i = begin.x, j = begin.y; j <= end.y - 4; ++j)
		{
			if (chessboard[i][j] == a 
				&& chessboard[i][j] == chessboard[i][j + 1] 
				&& chessboard[i][j + 1] == chessboard[i][j + 2]
				&& chessboard[i][j + 2] == chessboard[i][j + 3] 
				&& chessboard[i][j + 3] == chessboard[i][j + 4])
				return 1;
		}
		return 0;
	}
	int judgewiner2(char a, coordinate temp)//判断竖排
	{
		coordinate begin, end;
		begin.y = end.y = temp.y;
		if (temp.x <= 5)begin.x = 1;
		else begin.x = temp.x - 4;
		if (temp.x >= N-5)end.x =N-1;
		else end.x = temp.x + 4;
		for (int i = begin.x, j = begin.y; i <= end.x - 4; ++i)
		{
			if (chessboard[i][j] == a
				&& chessboard[i][j] == chessboard[i + 1][j] 
				&& chessboard[i + 1][j] == chessboard[i + 2][j] 
				&& chessboard[i + 2][j] == chessboard[i + 3][j] 
				&& chessboard[i + 3][j] == chessboard[i + 4][j])
				return 1;
		}
		return 0;
	}
	int judgewiner3(char a, coordinate temp)//判断主对角线
	{
		coordinate begin, end;
		if (temp.x <= 5)begin.x = 1;
		else begin.x = temp.x - 4;
		if (temp.y <= 5)begin.y = 1;
		else begin.y = temp.y - 4;
		if (temp.x >= N-5)end.x = N-1;
		else end.x = temp.x + 4;
		if (temp.y >= N-5)end.y = N-1;
		else end.y = temp.y + 4;
		for (int i = begin.x, j = begin.y; i <= end.x - 4 && j <= end.y - 4; ++i, ++j)
		{
			if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j + 1] 
				&& chessboard[i + 1][j + 1] == chessboard[i + 2][j + 2] 
				&& chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3] 
				&& chessboard[i + 3][j + 3] == chessboard[i + 4][j + 4])
				return 1;
		}
		return 0;
	}
	int judgewiner4(char a, coordinate temp)//判断负对角线
	{
		coordinate begin, end;
		if (temp.x <= 5)begin.x = 1;
		else begin.x = temp.x - 4;
		if (temp.y >= N-5)begin.y = N-1;
		else begin.y = temp.y + 4;
		if (temp.x >= N-5)end.x = N-1;
		else end.x = temp.x + 4;
		if (temp.y <= 5)end.y = 1;
		else end.y = temp.y - 4;
		for (int i = begin.x, j = begin.y; i <= end.x - 4 && j >= end.y - 4; ++i, --j)
		{
			if (chessboard[i][j] == a 
				&& chessboard[i][j] == chessboard[i + 1][j - 1] 
				&& chessboard[i + 1][j - 1] == chessboard[i + 2][j - 2]
				&& chessboard[i + 2][j - 2] == chessboard[i + 3][j - 3] 
				&& chessboard[i + 3][j - 3] == chessboard[i + 4][j - 4])
				return 1;
		}
		return 0;
	}
	int judgeheqi()//判断和棋
	{
		for (int i = 1; i < N; ++i)
			for (int j = 1; j < N; ++j)
				if (chessboard[i][j] == ' ')return 1;
		return -1;
	}
	void play()
	{
		initchessboard();//初始化棋盘
		int t;
		cout << "请选择模式,人机模式输入1,人人模式输入2。" << endl;
		cin >> t;
		while (t == 1)
		{
			int m = judgeheqi();
			if (m == -1)
			{
				cout << "和棋!!!" << endl;
				break;
			}
			coordinate temp1 = computerplayer();//电脑下棋
			printchessboard();//打印棋盘
			if (judgewiner1('x', temp1) || judgewiner2('x', temp1) || judgewiner3('x', temp1) || judgewiner4('x', temp1))
			{
				cout << "电脑胜!!!";
				break;
			}
			coordinate temp2 = playchess2();//玩家2下棋
			printchessboard();//打印棋盘
			if (judgewiner1('o', temp2) || judgewiner2('o', temp2) || judgewiner3('o', temp2) || judgewiner4('o', temp2))
			{
				cout << "玩家2胜!!!";
				break;
			}
		}
		while (t == 2)
		{
			int m = judgeheqi();
			if (m == -1)
			{
				cout << "和棋!!!" << endl;
				break;
			}
			coordinate temp1 = playchess1();//电脑下棋
			printchessboard();//打印棋盘
			if (judgewiner1('x', temp1) || judgewiner2('x', temp1) || judgewiner3('x', temp1) || judgewiner4('x', temp1))
			{
				cout << "玩家1胜!!!";
				break;
			}
			coordinate temp2 = playchess2();//玩家2下棋
			printchessboard();//打印棋盘
			if (judgewiner1('o', temp2) || judgewiner2('o', temp2) || judgewiner3('o', temp2) || judgewiner4('o', temp2))
			{
				cout << "玩家2胜!!!";
				break;
			}
		}
	}
private:
	char chessboard[N][N];  //棋盘
};
int main()
{
	fivechess one;
	one.play();
	return 0;
}

可选择人与机模式和人与人模式。

输入坐标先行后列。

祝你玩得开心! 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然可以!以下是一个简单的五子棋游戏的C++源码,使用了Qt框架进行界面开发: ```cpp #include <QApplication> #include <QWidget> #include <QPushButton> #include <QGridLayout> #include <QMessageBox> enum class ChessType { Empty, Black, White }; class ChessBoard : public QWidget { Q_OBJECT public: explicit ChessBoard(QWidget *parent = nullptr) : QWidget(parent) { setFixedSize(500, 500); gridLayout = new QGridLayout(this); gridLayout->setSpacing(0); for (int row = 0; row < 15; ++row) { for (int col = 0; col < 15; ++col) { QPushButton *btn = new QPushButton("", this); btn->setFixedSize(30, 30); btn->setProperty("row", row); btn->setProperty("col", col); btn->setStyleSheet("background-color: white;"); connect(btn, &QPushButton::clicked, this, &ChessBoard::handleButtonClick); gridLayout->addWidget(btn, row, col); chessboard[row][col] = ChessType::Empty; } } } private slots: void handleButtonClick() { QPushButton *btn = qobject_cast<QPushButton *>(sender()); int row = btn->property("row").toInt(); int col = btn->property("col").toInt(); if (chessboard[row][col] != ChessType::Empty) { return; } if (currentPlayer == ChessType::Black) { btn->setStyleSheet("background-color: black;"); chessboard[row][col] = ChessType::Black; currentPlayer = ChessType::White; } else { btn->setStyleSheet("background-color: white;"); chessboard[row][col] = ChessType::White; currentPlayer = ChessType::Black; } if (checkWin(row, col)) { QString winner = (currentPlayer == ChessType::Black) ? "White" : "Black"; QMessageBox::information(this, "Game Over", winner + " wins!"); resetBoard(); } } private: QGridLayout *gridLayout; ChessType chessboard[15][15]; ChessType currentPlayer = ChessType::Black; bool checkWin(int row, int col) { int dx[] = {0, 1, 1, 1}; int dy[] = {1, 0, 1, -1}; for (int i = 0; i < 4; ++i) { int count = 1; int r = row + dx[i]; int c = col + dy[i]; while (r >= 0 && r < 15 && c >= 0 && c < 15 && chessboard[r][c] == chessboard[row][col]) { ++count; r += dx[i]; c += dy[i]; } r = row - dx[i]; c = col - dy[i]; while (r >= 0 && r < 15 && c >= 0 && c < 15 && chessboard[r][c] == chessboard[row][col]) { ++count; r -= dx[i]; c -= dy[i]; } if (count >= 5) { return true; } } return false; } void resetBoard() { for (int row = 0; row < 15; ++row) { for (int col = 0; col < 15; ++col) { chessboard[row][col] = ChessType::Empty; } } QList<QPushButton *> buttons = findChildren<QPushButton *>(); for (QPushButton *btn : buttons) { btn->setStyleSheet("background-color: white;"); } currentPlayer = ChessType::Black; } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); ChessBoard chessBoard; chessBoard.show(); return app.exec(); } #include "main.moc" ``` 这个源码实现了一个简单的五子棋游戏,使用了Qt的QPushButton来表示棋盘上的每一个格子。玩家可以通过点击按钮来下子,当有一方连成五子时,游戏结束。如果你需要更多的功能,可以在此基础上进行扩展。希望对你有帮助!如果有任何问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值