QT实现棋盘覆盖

QT棋盘覆盖

先是算法文件(棋盘覆盖的代码)没有用到类,但是报了好多错,改了半天也改不好。。。好晕,用了类就方便简单很多。

QT绘图一般就是重写paintEvent函数,然后让qpainter画图。
在这里插入图片描述

棋盘覆盖算法

//.cpp
#include "chessalo.h"
#include<cstdlib>
#include<ctime>
#include<iostream>
chessalo::chessalo(int size)
{
    std::pair<int,int> pos=initBoard(size);
    chessBoard(0,0,pos.first,pos.second,size);
}
void chessalo::chessBoard(int top_r,int top_c,int r,int c,int size)
{
    if(size<=1) return ;
    int t=tile++;
    //把他们分成四份
    size/=2;//bug 1
    /*
    0 1 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    */
    if(top_r<=r&&r<top_r+size&&top_c<=c&&c<top_c+size)
        chessBoard(top_r,top_c,r,c,size);
    else
    {
        board[top_r+size-1][top_c+size-1]=t;//bug2
        chessBoard(top_r,top_c,top_r+size-1,top_c+size-1,size);
    }
    if(top_r<=r&&r<top_r+size&&top_c+size<=c&&c<top_c+2*size)
        chessBoard(top_r,top_c+size,r,c,size);
    else
    {
        board[top_r+size-1][top_c+size]=t;//bug3
        chessBoard(top_r,top_c+size,top_r+size-1,top_c+size,size);
    }
    if(top_r+size<=r&&r<top_r+2*size&&top_c<=c&&c<top_c+size)
        chessBoard(top_r+size,top_c,r,c,size);
    else
    {
        board[top_r+size][top_c+size-1]=t;
        chessBoard(top_r+size,top_c,top_r+size,top_c+size-1,size);
    }
    if(top_r+size<=r&&r<top_r+2*size&&top_c+size<=c&&c<top_c+2*size)
        chessBoard(top_r+size,top_c+size,r,c,size);
    else
    {
        board[top_r+size][top_c+size]=t;
        chessBoard(top_r+size,top_c+size,top_r+size,top_c+size,size);
    }
}

std::pair<int,int> chessalo::initBoard(int size)
{
    srand((int )time(NULL));
    int r=rand()%size,c=rand()%size;
    board[r][c]=0;//bug 4
    std::pair<int,int> pa;
    pa.first=r,pa.second=c;
    return pa;
}

//.h
#ifndef CHESSALO_H
#define CHESSALO_H
#include<iostream>

class chessalo
{

   const static int maxn=512;
   int tile=1;
public :
    int board[maxn][maxn];
    chessalo(int size);
    std::pair<int ,int> initBoard(int size);
    void chessBoard(int top_r,int top_c,int r,int c,int size);
};

#endif // CHESSALO_H

绘图

//mainwindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<vector>
#include<QColor>
#include"chessalo.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    int boardSize;
    std::vector<QColor> colors;
    chessalo *myalg;
    int W_Size=512;

protected:
    void paintEvent(QPaintEvent *e);
};

#endif // MAINWINDOW_H

//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QDebug>
#include<QColor>
#include<QBrush>
#include<cstdlib>
#include<ctime>
#include"chessalo.h"
#include<QInputDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    boardSize= QInputDialog::getInt(this,"输入生成棋盘的大小","boardSize:");
    this->resize(W_Size+10,W_Size+10);
    myalg=new chessalo(boardSize);
    initColor();
}

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

void MainWindow::initColor()
{
    //随机颜色
    qDebug()<<"initcolor";
    int colorNum=(boardSize*boardSize-1)/3+1;
    srand((int )time(NULL));
    colors.clear();
    colors.push_back(QColor(0,0,0));
    for(int i=1;i<colorNum;++i)
    {
        int r=rand()%255,g=rand()%255,b=rand()%255;
        colors.push_back(QColor(r,g,b,rand()%255));
    }
}

void MainWindow::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    int dita=W_Size/boardSize;
    for(int y=0;y<=W_Size;y+=dita) painter.drawLine(0,y,W_Size,y);
    for(int x=0;x<=W_Size;x+=dita) painter.drawLine(x,0,x,W_Size);
    for(int x=0,i=0;x<W_Size;x+=dita,i++)
        for(int y=0,j=0;y<W_Size;y+=dita,j++)
        {
            painter.setBrush(QBrush(colors[myalg->board[i][j]]));
            painter.drawRect(x,y,dita,dita);
        }
}

  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值