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);
}
}