QT writes a small game between human and computer rock-paper-scissors

The reason is that I saw a formula for judging the winning or losing of rock-paper-scissors and a picture of the interface of a small rock-paper-scissors game, so I thought of using QT and this formula to implement a simple rock-paper-scissors game. The specific formula and interface are as follows:

Formula:

result = (manNum - computerNum + 4) % 3 - 1;

Target interface:

The middle involves:

1. Use Qlabel to display pictures

2. Use the slot function to realize the button to trigger the Qlabel to display the picture

3. Use the vector container to store the address of the rock-paper-scissors image, so that the random number function can be used to realize the random appearance of the rock-paper-scissors

The content is relatively naive and simple, so I directly paste the source code, and the various optimization masters in the middle can explain it to me, I would be very grateful (there is no optimization head, it is very boring):

code:

1、UI

2、game.pro

#-------------------------------------------------
#
# Project created by QtCreator 2023-02-17T09:04:46
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = game
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

 3、mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

4、main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    srand((unsigned)time(NULL)); //seed
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

5、mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QMessageBox>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
#include <time.h>

using namespace std;
QString filename1 = "C:\\Users\\Administrator\\Desktop\\1.png";
QString filename2 = "C:\\Users\\Administrator\\Desktop\\2.png";
QString filename3 = "C:\\Users\\Administrator\\Desktop\\3.png";

//以下代码实现vector容器内三元素的随机选取(方法二)
//template<typename Iter, typename RandomGenerator>
//Iter select_randomly(Iter start, Iter end, RandomGenerator *g) {
//  std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
//  std::advance(start, dis(*g));
//  return start;
//}

//template<typename Iter>
//Iter select_randomly(Iter start, Iter end) {
//  static std::random_device rd;
//  static std::mt19937 gen(rd());
//  return select_randomly(start, end, &gen);
//}

//比较函数,得出猜拳结果
QString competition(QString man,QString computer)
{
    int manNum = 0;
    int computerNum = 0;
    QString qstrResult = "";
    if(man == filename1)
    {
        manNum = 0;
    }
    else if(man == filename2)
    {
        manNum = 1;
    }
    else
    {
        manNum = 2;
    }

    if(computer == filename1)
    {
        computerNum = 0;
    }
    else if(computer == filename2)
    {
        computerNum = 1;
    }
    else
    {
        computerNum = 2;
    }

    int result = (manNum - computerNum + 4) % 3 - 1;
    if(result > 0)
    {
        qstrResult = "你赢了!";
    }
    else if(result == 0)
    {
        qstrResult = "平局。";
    }
    else
    {
        qstrResult = "抱歉,你输了。";
    }
    return qstrResult;
}



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
        QImage* img1=new QImage,*img2=new QImage,*img3=new QImage;
        img1->load(filename1);
        img2->load(filename2);
        img3->load(filename3);
        ui->label_4->setPixmap(QPixmap::fromImage(*img1));
        ui->label_4->setScaledContents(true);
        ui->label_5->setPixmap(QPixmap::fromImage(*img2));
        ui->label_5->setScaledContents(true);
        ui->label_6->setPixmap(QPixmap::fromImage(*img3));
        ui->label_6->setScaledContents(true);
}



MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    vector<QString> vec{filename1, filename2,filename3};

    //方法一:按更改vector容器下标的形式实现随机vector容器内随机图片的选择
//    srand((unsigned)time(NULL)); //seed
    int n = rand()%3;
    QString filename4 = vec[n];

    //方法二:结合select_randomly函数实现随机图片地址的赋值
    //QString filename4 = *select_randomly(vec.begin(),vec.end());
    //人选择剪刀,并展示出剪刀图品
    QImage* img=new QImage;
            if(! ( img->load(filename1) ) ) //加载图像
            {
                QMessageBox::information(this,
                                         tr("打开图像失败"),
                                         tr("打开图像失败!"));
                delete img;
                return;
            }
            ui->label_showImage->setPixmap(QPixmap::fromImage(*img));
            ui->label_showImage->setScaledContents(true);
    //电脑随机选择,并展示出图片
            QImage* img1=new QImage;
                    if(! ( img1->load(filename4) ) ) //加载图像
                    {
                        QMessageBox::information(this,
                                                 tr("打开图像失败"),
                                                 tr("打开图像失败!"));
                        delete img1;
                        return;
                    }
                    ui->label_showImage2->setPixmap(QPixmap::fromImage(*img1));
                    ui->label_showImage2->setScaledContents(true);

    QString final = competition(filename1,filename4);
    ui->label_8->setText(final);
}

void MainWindow::on_pushButton_2_clicked()
{
    vector<QString> vec{filename1, filename2,filename3};

    //方法一:按更改vector容器下标的形式实现随机vector容器内随机图片的选择
//    srand((unsigned)time(NULL)); //seed
    QString filename4 = vec[rand()%3];

    //方法二:结合select_randomly函数实现随机图片地址的赋值
    //QString filename4 = *select_randomly(vec.begin(),vec.end());
    //人选择剪刀,并展示出剪刀图品
    QImage* img=new QImage;
            if(! ( img->load(filename2) ) ) //加载图像
            {
                QMessageBox::information(this,
                                         tr("打开图像失败"),
                                         tr("打开图像失败!"));
                delete img;
                return;
            }
            ui->label_showImage->setPixmap(QPixmap::fromImage(*img));
            ui->label_showImage->setScaledContents(true);
    //电脑随机选择,并展示出图片
    QImage* img1=new QImage;
    if(! ( img1->load(filename4) ) ) //加载图像
    {
        QMessageBox::information(this,
                                 tr("打开图像失败"),
                                 tr("打开图像失败!"));
        delete img1;
        return;
    }
    ui->label_showImage2->setPixmap(QPixmap::fromImage(*img1));
    ui->label_showImage2->setScaledContents(true);
    QString final = competition(filename1,filename4);
    ui->label_8->setText(final);
}

void MainWindow::on_pushButton_3_clicked()
{
    vector<QString> vec{filename1, filename2,filename3};
    //方法一:按更改vector容器下标的形式实现随机vector容器内随机图片的选择
//    srand((unsigned)time(NULL)); //seed
    QString filename4 = vec[rand()%3];

    //方法二:结合select_randomly函数实现随机图片地址的赋值
    //QString filename4 = *select_randomly(vec.begin(),vec.end());
    //人选择剪刀,并展示出剪刀图品
    QImage* img=new QImage;
    if(! ( img->load(filename3) ) ) //加载图像
    {
        QMessageBox::information(this,
                                 tr("打开图像失败"),
                                 tr("打开图像失败!"));
        delete img;
        return;
    }
    ui->label_showImage->setPixmap(QPixmap::fromImage(*img));
    ui->label_showImage->setScaledContents(true);
    //电脑随机选择,并展示出图片
    QImage* img1=new QImage;
    if(! ( img1->load(filename4) ) ) //加载图像
    {
        QMessageBox::information(this,
                                 tr("打开图像失败"),
                                 tr("打开图像失败!"));
        delete img1;
        return;
    }
    ui->label_showImage2->setPixmap(QPixmap::fromImage(*img1));
    ui->label_showImage2->setScaledContents(true);

    QString final = competition(filename1,filename4);
    ui->label_8->setText(final);
}

final effect:

 

Guess you like

Origin blog.csdn.net/jianlai_/article/details/129147410