VS+QT五子棋游戏开发

1、首先安装好VS软件和QT库,将其配置好,具体不在此展开说明。

2、文件结构如下图:

3、绘制棋盘代码,如下:

void Qwzq::paintEvent(QPaintEvent* event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);//防锯齿QBrush brush;brush.setStyle(Qt::SolidPattern);for (int i = 0; i < kGridCount+1; i++){painter.drawLine(kBoardMargin,kBoardMargin+i*kBlockSize,size().width()-kBoardMargin,kBoardMargin+i*kBlockSize);painter.drawLine(kBoardMargin+i*kBlockSize, kBoardMargin,kBoardMargin+i*kBlockSize,size().height()-kBoardMargin);}for (int i = 0; i < kGridCount; i++){for (int j = 0; j < kGridCount; j++){if (game->gameMap[i][j] == 1){brush.setColor(Qt::white);painter.setBrush(brush);painter.drawEllipse(kBoardMargin+kBlockSize*j-kRadius,kBoardMargin+kBlockSize*i-kRadius,kRadius*2,kRadius*2);}else if (game->gameMap[i][j]==-1){brush.setColor(Qt::black);painter.setBrush(brush);painter.drawEllipse(kBoardMargin + kBlockSize * j - kRadius, kBoardMargin + kBlockSize * i - kRadius, kRadius * 2, kRadius * 2);}}}QString str;if (game->isWin(clickPosRow, clickPosCol)){str = "Win!";QMessageBox::StandardButton btnValue = QMessageBox::information(this, tr("result"), str);close();}if (game->isDead()){str = "Over!";QMessageBox::StandardButton btnValue = QMessageBox::information(this, tr("result"), str);close();}
}

 4、绘制旗子代码,如下:

void Qwzq::mousePressEvent(QMouseEvent* event)
{int x = event->x();int y = event->y();int kr = kBoardMargin + kBlockSize * kGridCount;int kd = kBoardMargin + kBlockSize*kGridCount;if (x>=kBoardMargin&&x<=kr&&y>=kBoardMargin&&y<=kd){int x1 = (x - kBoardMargin) / kBlockSize;int y1 = (y - kBoardMargin) / kBlockSize;x1 = x - (kBoardMargin + kBlockSize * x1);y1 = y - (kBoardMargin + kBlockSize * y1);if (x1 > kBlockSize / 2)x = (x-kBoardMargin) / kBlockSize + 1;elsex = (x - kBoardMargin) / kBlockSize;if (y1 > kBlockSize / 2)y = (y-kBoardMargin) / kBlockSize + 1;elsey = (y - kBoardMargin) / kBlockSize;game->updateMap(y, x);clickPosRow = y;clickPosCol = x;}update();
}

5、gameModel.cpp代码如下:

#include"GameModel.h"
#include<utility>
#include<qDebug>void GameModel::startGame()
{gameMap.clear();for (int i = 0; i < kBoardSize; i++){std::vector<int> lineBoard;for (int j = 0; j < kBoardSize; j++){lineBoard.push_back(0);}gameMap.push_back(lineBoard);}playerFlag = true;
}void GameModel::updateMap(int row, int col)
{if (playerFlag)gameMap[row][col] = 1;elsegameMap[row][col] = -1;playerFlag = !playerFlag;
}void GameModel::actionByPerson(int row, int col)
{updateMap(row,col);
}bool GameModel::isWin(int row, int col)
{//水平方向检查for (int i = 0; i <= kBoardSize - 5; i++){if (gameMap[row][i] == 1||gameMap[row][i]==-1){if (gameMap[row][i] == gameMap[row][i + 1]){if (gameMap[row][i + 1] == gameMap[row][i + 2]){if (gameMap[row][i + 2] == gameMap[row][i + 3]){if (gameMap[row][i + 3] == gameMap[row][i + 4]){return true;}}}}}}//竖直方向检查for (int i = 0; i <= kBoardSize - 5; i++){if (gameMap[i][col] == 1 || gameMap[i][col] == -1){if (gameMap[i][col] == gameMap[i+1][col]){if (gameMap[i+1][col] == gameMap[i+2][col]){if (gameMap[i+2][col] == gameMap[i+3][col]){if (gameMap[i+3][col] == gameMap[i+4][col]){return true;}}}}}}//135度方向检查int krow = kBoardSize -1- row;if (krow <= col){int i_135 = row + krow;int j_135 = col - krow;for (j_135; j_135 <= kBoardSize - 5; j_135++){if (gameMap[i_135][j_135] == 1){if (gameMap[i_135][j_135] == gameMap[i_135 - 1][j_135 + 1]){if (gameMap[i_135 - 1][j_135 + 1] == gameMap[i_135 - 2][j_135 + 2]){if (gameMap[i_135 - 2][j_135 + 2] == gameMap[i_135 - 3][j_135 + 3]){if (gameMap[i_135 - 3][j_135 + 3] == gameMap[i_135 - 4][j_135 + 4]){return true;}}}}}i_135--;}}if (krow > col){int i_135 = row + col;int j_135 = col - col;for (i_135; i_135 >= 4; i_135--){if (gameMap[i_135][j_135] == 1){if (gameMap[i_135][j_135] == gameMap[i_135 - 1][j_135 + 1]){if (gameMap[i_135 - 1][j_135 + 1] == gameMap[i_135 - 2][j_135 + 2]){if (gameMap[i_135 - 2][j_135 + 2] == gameMap[i_135 - 3][j_135 + 3]){if (gameMap[i_135 - 3][j_135 + 3] == gameMap[i_135 - 4][j_135 + 4]){return true;}}}}}j_135++;}}//45度方向检查if (row >= col){int i_45 = row - col;int j_45 = col - col;for (i_45; i_45 < kBoardSize; i_45++){if (gameMap[i_45][j_45] == 1){if (gameMap[i_45][j_45] == gameMap[i_45 + 1][j_45 + 1]){if (gameMap[i_45 + 1][j_45 + 1] == gameMap[i_45 + 2][j_45 + 2]){if (gameMap[i_45 + 2][j_45 + 2] == gameMap[i_45 + 3][j_45 + 3]){if (gameMap[i_45 + 3][j_45 + 3] == gameMap[i_45 + 4][j_45 + 4]){return true;}}}}}j_45++;}}if (row < col){int i_45 = col - row;int j_45 = col - row;for (j_45; j_45 < kBoardSize; j_45++){if (gameMap[i_45][j_45] == 1){if (gameMap[i_45][j_45] == gameMap[i_45 + 1][j_45 + 1]){if (gameMap[i_45 + 1][j_45 + 1] == gameMap[i_45 + 2][j_45 + 2]){if (gameMap[i_45 + 2][j_45 + 2] == gameMap[i_45 + 3][j_45 + 3]){if (gameMap[i_45 + 3][j_45 + 3] == gameMap[i_45 + 4][j_45 + 4]){return true;}}}}}i_45++;}}return false;
}bool GameModel::isDead()//死局,即棋盘被下满了
{for(int i=0;i<kBoardSize;i++)for (int j = 0; j < kBoardSize; j++){if (gameMap[i][j] == 0)return false;}return true;
}

6、gameModel.h代码如下:

#ifndef GAMEMODEL_H
#define GAMEMODEL_H#include<QObject>
#include<vector>enum GameStatus
{playing,win,dead
};const int kBoardSize = 15;class GameModel
{
public:public:std::vector<std::vector<int>>gameMap;bool playerFlag;GameStatus gameStatus;void startGame();void actionByPerson(int row, int col);void updateMap(int row, int col);bool isWin(int row, int col);bool isDead();};
#endif // !GAMEMODEL_H

7、Qwzq.cpp代码如下:

#include "Qwzq.h"
#include <QMouseEvent>
#include <QMenuBar>
#include <QPainter>
#include <QDebug>
#include<QMessageBox>const int kBoardMargin = 30;
const int kRadius = 15;
const int kBlockSize = 40;
const int kGridCount = 15;Qwzq::Qwzq(QWidget *parent): QMainWindow(parent)
{//ui.setupUi(this);setFixedSize(kBoardMargin * 2 + kGridCount * kBlockSize, kBoardMargin * 2 + kGridCount * kBlockSize);setMouseTracking(true);QMenu* gameMenu = menuBar()->addMenu(tr("Game Model:"));QAction* actionPVP = new QAction("PVP", this);menuBar()->addAction(actionPVP);connect(actionPVP, SIGNAL(triggered()), this, SLOT(initPVPGame()));initGame();
}void Qwzq::initGame()
{game = new GameModel;initPVPGame();clickPosRow = clickPosCol = 0;
}void Qwzq::initPVPGame()
{game->gameStatus = playing;game->startGame();
}Qwzq::~Qwzq()
{if (game){delete game;game = nullptr;}}void Qwzq::paintEvent(QPaintEvent* event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);//防锯齿QBrush brush;brush.setStyle(Qt::SolidPattern);for (int i = 0; i < kGridCount+1; i++){painter.drawLine(kBoardMargin,kBoardMargin+i*kBlockSize,size().width()-kBoardMargin,kBoardMargin+i*kBlockSize);painter.drawLine(kBoardMargin+i*kBlockSize, kBoardMargin,kBoardMargin+i*kBlockSize,size().height()-kBoardMargin);}for (int i = 0; i < kGridCount; i++){for (int j = 0; j < kGridCount; j++){if (game->gameMap[i][j] == 1){brush.setColor(Qt::white);painter.setBrush(brush);painter.drawEllipse(kBoardMargin+kBlockSize*j-kRadius,kBoardMargin+kBlockSize*i-kRadius,kRadius*2,kRadius*2);}else if (game->gameMap[i][j]==-1){brush.setColor(Qt::black);painter.setBrush(brush);painter.drawEllipse(kBoardMargin + kBlockSize * j - kRadius, kBoardMargin + kBlockSize * i - kRadius, kRadius * 2, kRadius * 2);}}}QString str;if (game->isWin(clickPosRow, clickPosCol)){str = "Win!";QMessageBox::StandardButton btnValue = QMessageBox::information(this, tr("result"), str);close();}if (game->isDead()){str = "Over!";QMessageBox::StandardButton btnValue = QMessageBox::information(this, tr("result"), str);close();}
}void Qwzq::mousePressEvent(QMouseEvent* event)
{int x = event->x();int y = event->y();int kr = kBoardMargin + kBlockSize * kGridCount;int kd = kBoardMargin + kBlockSize*kGridCount;if (x>=kBoardMargin&&x<=kr&&y>=kBoardMargin&&y<=kd){int x1 = (x - kBoardMargin) / kBlockSize;int y1 = (y - kBoardMargin) / kBlockSize;x1 = x - (kBoardMargin + kBlockSize * x1);y1 = y - (kBoardMargin + kBlockSize * y1);if (x1 > kBlockSize / 2)x = (x-kBoardMargin) / kBlockSize + 1;elsex = (x - kBoardMargin) / kBlockSize;if (y1 > kBlockSize / 2)y = (y-kBoardMargin) / kBlockSize + 1;elsey = (y - kBoardMargin) / kBlockSize;game->updateMap(y, x);clickPosRow = y;clickPosCol = x;}update();
}

8、Qwzq.h代码如下:

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_Qwzq.h"
#include <GameModel.h>class Qwzq : public QMainWindow
{Q_OBJECTpublic:Qwzq(QWidget *parent = nullptr);~Qwzq();GameModel* game;int clickPosRow, clickPosCol;void initGame();private slots:void initPVPGame();private:private:Ui::QwzqClass ui;void paintEvent(QPaintEvent* event);void mousePressEvent(QMouseEvent* event);
};

9、main.cpp代码如下:

#include "Qwzq.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Qwzq w;w.show();return a.exec();
}

10、运行界面如下:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/316973.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

上门洗衣洗鞋小程序足不出户上门预约即可清洗,相较于传统线下洗鞋有哪些优势?

校园洗护小程序&#xff0c;相较于传统的线下洗鞋方式&#xff0c;有着以下显著优势&#xff1a; 1. 便捷性&#xff1a;通过手机&#xff0c;随时随地轻松下单与查询&#xff0c;免去了用户来回奔波之苦。学生们在校园内或宿舍&#xff0c;即可轻松预约洗鞋并完成取件。 2. 智…

RabbitMQ 发送消息 绕过系统配置的 消息转换器方法

1. 背景 我负责的系统需要与其他系统交互&#xff0c;需要我将消息发送到 MQ 上&#xff0c;其他系统监听消息。 遇到问题&#xff0c;两个系统的发送消息格式都是不一致的&#xff0c;所以这个地方是需要协商打通的。 目前这个系统经我查阅发现用的 fastjson 的 jsonb 序列…

vivo 数据库备份恢复系统演化

作者&#xff1a;vivo 互联网数据库团队 - Han Chaobing 介绍 vivo 数据库备份恢复功能的演化&#xff0c;以及对备份文件的功能扩展。 一、概述 vivo互联网领域拥有的数据库组件分别为 MySQL、MongoDB、TiDB 等&#xff0c;其中MySQL集群占比绝大部分&#xff0c; MongoDB …

一、医学影像云平台(AI赋能基层的最佳实践)

医学影像云平台&#xff0c;是一个目前影像业务中&#xff0c;各省市级领导比较重视的业务板块。因为&#xff0c;影像平台不但可以直接给原来没有PACS系统的1,2级医院提供PACS服务&#xff0c;还可以提供院间之间的业务&#xff0c;例如发送远程诊断&#xff0c;远程会诊业务&…

【华为机试】2023年真题B卷(python)-快速人名查找

一、题目 题目描述&#xff1a; 给一个字符串&#xff0c;表示用’, \’分开的人名。然后给定一个字符串&#xff0c;进行快速人名查找&#xff0c;符合要求的输出。 快速人名查找要求︰人名的每个单词的连续前几位能组成给定字符串&#xff0c;一定要用到每个单词。 二、输入输…

STM32MP157/linux驱动学习记录

1. uboot烧录 2.linux安装nfs服务 sudo apt-get install nfs-kernel-server rpcbind安装nfs服务 在用户根目录下创建一个名为“linux”的文件夹&#xff0c;以后所有的东西都放到这个“linux”文件夹里面&#xff0c;在“linux”文件夹里面新建一个名为“nfs”的文件夹&#…

企业使用人工智能情况调查

企业使用人工智能情况调查 人工智能在商业中的应用并不是什么新鲜事。多年来&#xff0c;公司一直在使用人工智能技术来削减成本并提高效率。 但最近生成式人工智能市场的激增帮助人工智能成为主流商业技术。具体来说&#xff0c;ChatGPT 和 Midjourney 等大型语言模型 (LLM)…

2020年认证杯SPSSPRO杯数学建模C题(第一阶段)抗击疫情,我们能做什么全过程文档及程序

2020年认证杯SPSSPRO杯数学建模 C题 抗击疫情&#xff0c;我们能做什么 原题再现&#xff1a; 2020 年 3 月 12 日&#xff0c;世界卫生组织&#xff08;WHO&#xff09;宣布&#xff0c;席卷全球的冠状病毒引发的病毒性肺炎&#xff08;COVID-19&#xff09;是一种大流行病。…

团结引擎正式发布,已开放下载

2024年1月1日&#xff0c;Unity 正式发布了针对中国开发者的引擎--团结引擎创世版&#xff0c;现在已经可以开放下载&#xff0c;想体验的小伙伴们可以到官网下载了。 团结引擎是专为中国开发者定制的实时3D引擎&#xff0c;目前是基于Unity 2022LTS版本开发的。团结引擎内置了…

一篇关于大模型在信息抽取(实体识别、关系抽取、事件抽取)的研究进展综述

信息提取&#xff08;IE&#xff09;旨在从普通自然语言文本中提取结构化知识&#xff08;如实体、关系和事件&#xff09;。最近&#xff0c;生成式大型语言模型&#xff08;LLMs&#xff09;展现了在文本理解和生成方面的卓越能力&#xff0c;使得它们能够广泛应用于各种领域…

【竞技宝】LOL:S14新赛季改动 将trueskill2隐藏分算法

北京时间2024年1月3日,随着英雄联盟德玛西亚杯的进行,英雄联盟赛事已经进入新赛季的征途。每个赛季的春季赛之前,都会进行一次大的版本更新。据爆料,今年的S14版本大更新中,除了游戏内的英雄、道具、地图的更新之外,排位的隐藏分算法也将进行重大改变。 昨日,英雄联盟设计总监…

【Java进阶篇】字符串常量、字符串常量池详解

字符串常量、字符串常量池详解 ✔️字符串常量池是如何实现的?✔️字符串常量从哪来的? ✔️字符串常量是什么时候进入到字符串常量池的? ✔️字符串常量池是如何实现的? 字符串常量池 (String Constant Pool) 是Java中一块特殊的内存区域&#xff0c;用于存储字符串常量。…