资源简介
此五子棋代码,实现了人机对战,开始界面可以选择棋子颜色和自动搜索的深度(即难度),搜索的游戏状态数目随着博弈的进行呈指数级增长,指数增长无法消除,不过我们使用了alpha-beta剪枝有效地将其减半,加快了搜索的速度!
代码片段和文件信息
#include “Board.h“
#include
#include
#include
Board::Board(QWidget *parent) :
QWidget(parent)
{
this->setWindowtitle(“人机大战“);
this->_r = 20;
setFixedSize(_r*2*19+1 _r*2*16);
newGame.setParent(this);
newGame.move(_r*2*16 _r*2*8);
newGame.setFixedSize(_r*4 _r*1.5);
newGame.setText(“New Game“);
connect(&newGame &QPushButton::clicked this &Board::InvertRenjiBoard);
connect(&newGame &QPushButton::clicked this &Board::sendSlot);
for(int row=0; row {
for(int col=0; col {
m_RenjiBoard[row][col] = 0; //初始化棋盘
}
}
//memset(m_RenjiBoard 0 GRID_COUNT * sizeof(int)); //初始化棋盘
}
void Board::InvertRenjiBoard()
{//重绘棋盘
for(int row=0; row {
for(int col=0; col {
m_RenjiBoard[row][col] = 0;
}
}
//memset(m_RenjiBoard 0 GRID_COUNT * sizeof(int));
update();
}
void Board::sendSlot()
{
emit from_board_to_mywidgetSignal(); //将棋盘收到的信号发送给选择窗口处理
}
void Board::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing true);
int d = 2 * _r;
//_r = d/2;
for(int i=1; i<16; i++)
{
painter.drawLine(QPoint(d i*d) QPoint(15*d i*d));
painter.drawLine(QPoint(i*d d) QPoint(i*d 15*d));
}
painter.save();
QBrush brush;
brush.setstyle(Qt::SolidPattern);
for(int row=0; row {
for(int col=0; col {
if(m_RenjiBoard[row][col] == 1)
{
brush.setColor(Qt::black);
painter.setBrush(brush);
painter.drawEllipse(center(row col) _r-2 _r-2);
}
else if(m_RenjiBoard[row][col] == 2)
{
brush.setColor(Qt::white);
painter.setBrush(brush);
painter.drawEllipse(center(row col) _r-2 _r-2);
}
}
}
painter.restore();
update();
}
QPoint Board::center(int row int col)
{//返回棋盘行列对应的像素坐标
QPoint ret;
ret.rx() = (col+1)* _r*2;
ret.ry() = (row+1)* _r*2;
return ret;
}
bool Board::getRowCol(QPoint pt int &row int &col)
{//得到此时对应棋盘的行和列
for(row=0; row<16; row++)
{
for(col=0; col<16; col++)
{
QPoint distance = center(row col) - pt;
if(distance.x() * distance.x() + distance.y() * distance.y() < _r * _r)
return true;
}
}
return false;
}
void Board::click(int row int col)
{
qDebug() << “父类“;
if(m_RenjiBoard[row][col] == 0)
{//防止重复落子
m_RenjiBoard[row][col] = BLACK;
_bBlackTurn = !_bBlackTurn;
}
}
void Board::click(QPoint pt)
{
int row = 0;
int col = 0;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-02 11:09 wuziqi\
文件 3423 2017-03-08 17:08 wuziqi\Board.cpp
文件 1192 2017-03-08 17:08 wuziqi\Board.h
文件 22069 2017-03-08 20:27 wuziqi\Eveluation.cpp
文件 1738 2017-03-07 23:08 wuziqi\Eveluation.h
文件 1107 2017-03-08 20:56 wuziqi\Mywidget.cpp
文件 753 2017-03-08 20:56 wuziqi\Mywidget.h
文件 2171 2017-03-04 19:07 wuziqi\Mywidget.ui
文件 8469 2017-03-08 20:56 wuziqi\Singlegame.cpp
文件 1073 2017-03-08 20:56 wuziqi\Singlegame.h
文件 101 2017-03-05 10:50 wuziqi\Step.cpp
文件 201 2017-03-05 10:50 wuziqi\Step.h
文件 44 2017-03-04 19:08 wuziqi\Stone.cpp
文件 301 2017-03-08 20:56 wuziqi\Stone.h
文件 214 2017-03-04 19:07 wuziqi\main.cpp
文件 567 2017-03-07 22:41 wuziqi\text1.pro
文件 18198 2017-03-07 22:39 wuziqi\text1.pro.user
评论
共有 条评论