本节完成 棋盘初始化
#ifndef BOARD_H
#define BOARD_H
#include <QMainWindow>
#include <QPainter>
#include "chesshandler.h"
#include "chess.h"
#include "judge.h"
#include "player.h"
#include<QMouseEvent>
class Board : public QMainWindow
{
Q_OBJECT
public:
//chess m_chess;
enum MODE{IMAGE,DRAW};
public:
Board(QWidget *parent = 0);
~Board();
MODE m_mode; //设置显示模式
int m_chesssize; //棋子大小
int m_firstpoint;//第一点坐标
chesshandler *m_chesshander;
judge m_judge;
public:
void paintEvent(QPaintEvent *);
void InitBoard(QPainter &painter);//初始化绘制棋盘
void SetBackGroud();//设置背景
void DrawAllChess(QPainter &painter);//初始化棋子
void DrawChess(QPainter &painter,chess &chess,int &i,int &j);
void ImageChess(QPainter &painter,chess &chess,int &i,int &j);
QPoint Center(int row,int col); //计算坐标
bool IsVaildPoint(QPoint point,int &i,int &j);//是否是合法位置,并返回索引
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent();
};
#endif // BOARD_H
#include "board.h"
#include <QDebug>
Board::Board(QWidget *parent)
: QMainWindow(parent)
{
this->m_mode=Board::DRAW; //切换显示方式
this->m_chesssize=120;
this->m_firstpoint=40;
this->m_chesshander=new chesshandler();
this->m_judge.SetChessHandler(this->m_chesshander);
this->m_chesshander->InitChess();
}
Board::~Board()
{
delete this->m_chesshander;
}
void Board::paintEvent(QPaintEvent *)
{
画棋盘 开始///
QPainter painter(this);
this->SetBackGroud();
this->InitBoard(painter);
this->DrawAllChess(painter);
}
//背景
void Board::SetBackGroud(){
int &firstpoint=this->m_firstpoint; //设置第一点边距
int &chesssize=this->m_chesssize; //设置棋子大小
//固定窗口大小
this->setMaximumSize(2*firstpoint+8*chesssize,2*firstpoint+4*chesssize); //设置窗口大小
this->setMinimumSize(2*firstpoint+8*chesssize,2*firstpoint+4*chesssize); //设置窗口大小
QPixmap bg(":/image/bg.jpg");
QPalette pal;
switch (this->m_mode) {
case IMAGE:
//设置背景图片
pal.setBrush(QPalette::Window,QBrush(bg));
this->setPalette(pal);
break;
case DRAW:
/
//设置背景颜色
pal.setColor(QPalette::Background,QColor(255,255,255));
this->setPalette(pal);
break;
default:
break;
}
}
//棋盘
void Board::InitBoard(QPainter &painter){
画棋盘 开始///
QPen pen;
pen.setWidth(3);
painter.setPen(pen);
//init
int &firstpoint=this->m_firstpoint; //设置第一点边距
int &chesssize=this->m_chesssize; //设置棋子大小
//绘制棋盘
for(int i=0;i<5;i++) //画5条横线
{
painter.drawLine(QPoint(firstpoint,firstpoint+i*chesssize)
,QPoint(firstpoint+chesssize*8,firstpoint+chesssize*i));
}
for(int i=0;i<9;i++) //画9条竖线
{
painter.drawLine(QPoint(firstpoint+chesssize*i,firstpoint)
,QPoint(firstpoint+i*chesssize,firstpoint+chesssize*4));
}
画棋盘 结束///
}
//棋子
void Board::DrawAllChess(QPainter &painter){
画棋子 开始///
for(int i=0;i<4;i++){
for(int j=0;j<8;j++)
{
chess chess=this->m_chesshander->GetChess(i,j);
switch (this->m_mode){
case DRAW:
this->DrawChess(painter,chess,i,j);
break;
case IMAGE:
this->ImageChess(painter,chess,i,j);
break;
default:
break;
}
}
}
}
//图片棋子
void Board::ImageChess(QPainter &painter,chess &chess,int &i,int &j){
/
switch (chess.GetState()) {
case chess::DOWN:
{
QImage image(":/image/chessbg.png");
QPoint point=this->Center(i,j);
QRect rect(point,QSize(120,120));
painter.drawImage(rect,image);
break;
}
case chess::LIVE:
{
QImage image(chess.GetImageName());
QPoint point=this->Center(i,j);
QRect rect(point,QSize(120,120));
painter.drawImage(rect,image);
break;
}
default:
break;
}
}
//绘图棋子
void Board::DrawChess(QPainter &painter,chess &chess,int &i,int &j)
{
switch (chess.GetState()){
case chess::DOWN:
{
QPoint point=this->Center(i,j);
QRect rect(point,QSize(120,120));
QBrush brush;
brush.setColor(QColor(244,244,181));
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.drawEllipse(rect);
break;
}
case chess::LIVE:
{
if(chess.GetColor()==chess::RED){
painter.setPen(Qt::red);
}else{
painter.setPen(Qt::black);
}
QPoint point=this->Center(i,j);
QRect rect(point,QSize(120,120));
QBrush brush;
brush.setColor(QColor(244,244,181));
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.drawEllipse(rect);
int size=this->m_chesssize;
int x=point.x();
int y=point.y();
painter.setFont(QFont("楷体", size/2, size)); //字体
QRect textrect(x,y,size,size);
painter.drawText(textrect,chess.GetTextName(),QTextOption(Qt::AlignCenter));
break;
}
default:
break;
}
}
QPoint Board::Center(int row,int col)
{
int &firstpoint=this->m_firstpoint;
int &chesssize=this->m_chesssize;
QPoint point(0,0);
point.setX(firstpoint+chesssize*col);
point.setY(firstpoint+chesssize*row);
return point;
}
bool Board::IsVaildPoint(QPoint point,int &row,int &col)
{
//使用圆的方程 (x1-x0)^2+(y1-y0)^2=r^2 (x1-x0)^2+(y1-y0)^2<r^2则在范围内
int x1,y1;
int x0,y0;
int r;
int sum;
r=this->m_chesssize/2;
r=r*r;
x1=point.x();
y1=point.y();
for(int i=0;i<4;i++)
for(int j=0;j<8;j++){
QPoint index=Center(i,j);
x0=index.x()+this->m_chesssize/2;
y0=index.y()+this->m_chesssize/2;
sum=(x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
if(sum<r){
row=i;
col=j;
return true;
}
}
return false;
}
void Board::mousePressEvent(QMouseEvent *event){
QPoint point=event->pos();
int row,col;
//判断是否可选中
if(this->IsVaildPoint(point,row,col))
{
chess& chess=this->m_chesshander->GetChess(row,col);
chess.SetState(chess::LIVE);
qDebug()<<row;
qDebug()<<col;
update();//刷新界面
}
}
void Board::mouseReleaseEvent(){
}
#ifndef CHESSHANDLER_H
#define CHESSHANDLER_H
#include "chess.h"
#include <QDebug>
class chesshandler
{
private:
chess m_chess[4][8];
public:
chesshandler();
chess& GetChess(int row,int col){
return (this->m_chess[row][col]);
}
void InitChess();
};
#endif // CHESSHANDLER_H
#include "chesshandler.h"
#include<ctime>
#include<cstdlib>
#include <QString>
chesshandler::chesshandler()
{
}
void chesshandler::InitChess(){
int i=0;
int row,col,r_row,r_col,n=0;
设置棋子
for(i=0;i<32;i++){
row=i/8;
col=i-8*row;
n=i+16;
r_row=n/8;
r_col=n-8*r_row;
if(i<5){
this->m_chess[row][col].SetName(chess::BING);
this->m_chess[row][col].SetColor(chess::RED);
this->m_chess[ r_row][r_col].SetName(chess::BING);
this->m_chess[ r_row][r_col].SetColor(chess::BLACK);
}else if(i>=5&&i<7){
this->m_chess[row][col].SetName(chess::PAO);
this->m_chess[row][col].SetColor(chess::RED);
this->m_chess[ r_row][r_col].SetName(chess::PAO);
this->m_chess[ r_row][r_col].SetColor(chess::BLACK);
}else if(i>=7&&i<9){
this->m_chess[row][col].SetName(chess::CHE);
this->m_chess[row][col].SetColor(chess::RED);
this->m_chess[ r_row][r_col].SetName(chess::CHE);
this->m_chess[ r_row][r_col].SetColor(chess::BLACK);
}else if(i>=9&&i<11){
this->m_chess[row][col].SetName(chess::MA);
this->m_chess[row][col].SetColor(chess::RED);
this->m_chess[ r_row][r_col].SetName(chess::MA);
this->m_chess[ r_row][r_col].SetColor(chess::BLACK);
}else if(i>=11&&i<13){
this->m_chess[row][col].SetName(chess::SHI);
this->m_chess[row][col].SetColor(chess::RED);
this->m_chess[ r_row][r_col].SetName(chess::SHI);
this->m_chess[ r_row][r_col].SetColor(chess::BLACK);
}else if(i>=13&&i<15){
this->m_chess[row][col].SetName(chess::XIANG);
this->m_chess[row][col].SetColor(chess::RED);
this->m_chess[ r_row][r_col].SetName(chess::XIANG);
this->m_chess[ r_row][r_col].SetColor(chess::BLACK);
}else if(i>=15&&i<16){
this->m_chess[row][col].SetName(chess::SHUAI);
this->m_chess[row][col].SetColor(chess::RED);
this->m_chess[ r_row][r_col].SetName(chess::SHUAI);
this->m_chess[ r_row][r_col].SetColor(chess::BLACK);
}
}
srand((int)time(0));
int x;
chess temp;
for(int i=31; i>=1; i--)
{
row=i/8;
col=i-8*row;
x=rand()%(i+1);
r_row=x/8;
r_col=x-8*r_row;
temp = this->m_chess[row][col];
this->m_chess[row][col] = this->m_chess[r_row][r_col];
this->m_chess[r_row][r_col]=temp;
}
for(int i=31; i>=1; i--)
{
row=i/8;
col=i-8*row;
x=rand()%(i+1);
r_row=x/8;
r_col=x-8*r_row;
temp = this->m_chess[row][col];
this->m_chess[row][col] = this->m_chess[r_row][r_col];
this->m_chess[r_row][r_col]=temp;
}
///设置棋子颜色/
}
void chesshandler::Count(){
int row,col;
int red=0,black=0;
int bing=0,pao=0,ma=0,shuai=0,shi=0,che=0,xiang=0;
for(int i=0;i<32;i++){
row=i/8;
col=i-8*row;
if(this->m_chess[row][col].GetColor()==chess::RED){
red++;
switch (this->m_chess[row][col].GetName()) {
case chess::CHE:
che++;
break;
case chess::MA:
ma++;
break;
case chess::PAO:
pao++;
break;
case chess::BING:
bing++;
break;
case chess::SHUAI:
shuai++;
break;
case chess::SHI:
shi++;
break;
case chess::XIANG:
xiang++;
break;
default:
break;
}
}
else
black++;
}
qDebug()<<"daadadad";
qDebug()<<red;
qDebug()<<black;
qDebug()<<"兵"<<QString::number(bing);
qDebug()<<"马"<<QString::number(ma);
qDebug()<<"帅"<<QString::number(shuai);
qDebug()<<"士"<<QString::number(shi);
qDebug()<<"相"<<QString::number(xiang);
qDebug()<<"车"<<QString::number(che);
qDebug()<<"泡"<<QString::number(pao);
}