添加自动运行功能;

修正招法菜单bug;
优化局面显示方法,为动画做准备。
This commit is contained in:
liuweilhy 2018-10-17 00:49:03 +08:00
parent c4c1bdf583
commit 4f67764a69
9 changed files with 163 additions and 60 deletions

4
.gitignore vendored
View File

@ -261,7 +261,9 @@ __pycache__/
*.pyc *.pyc
# exe and dll
*.exe
*.dll

View File

@ -72,8 +72,6 @@
<addaction name="actionSave_S"/> <addaction name="actionSave_S"/>
<addaction name="actionSaveAs_A"/> <addaction name="actionSaveAs_A"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionViewText_V"/>
<addaction name="separator"/>
<addaction name="actionExit_X"/> <addaction name="actionExit_X"/>
</widget> </widget>
<widget class="QMenu" name="menu_C"> <widget class="QMenu" name="menu_C">
@ -493,15 +491,6 @@
<string>另存为(&amp;A)...</string> <string>另存为(&amp;A)...</string>
</property> </property>
</action> </action>
<action name="actionViewText_V">
<property name="icon">
<iconset resource="ninechesswindow.qrc">
<normaloff>:/icon/Resources/icon/EditInformationHS.png</normaloff>:/icon/Resources/icon/EditInformationHS.png</iconset>
</property>
<property name="text">
<string>查看文本棋谱(&amp;V)</string>
</property>
</action>
<action name="actionExit_X"> <action name="actionExit_X">
<property name="text"> <property name="text">
<string>退出(&amp;X)</string> <string>退出(&amp;X)</string>

View File

@ -29,6 +29,7 @@ public:
static const int RING = 3; static const int RING = 3;
// 8位禁止修改 // 8位禁止修改
static const int SEAT = 8; static const int SEAT = 8;
private: private:
// 棋盘尺寸 // 棋盘尺寸
qreal size; qreal size;

View File

@ -10,6 +10,7 @@
#include <QDebug> #include <QDebug>
#include <QMessageBox> #include <QMessageBox>
#include <QAbstractButton> #include <QAbstractButton>
#include <QMap>
#include "gamecontroller.h" #include "gamecontroller.h"
#include "graphicsconst.h" #include "graphicsconst.h"
#include "boarditem.h" #include "boarditem.h"
@ -42,6 +43,10 @@ stepsLimit(0)
GameController::~GameController() GameController::~GameController()
{ {
// 清除棋子
qDeleteAll(pieceList);
pieceList.clear();
piece = NULL;
} }
const QMap<int, QStringList> GameController::getActions() const QMap<int, QStringList> GameController::getActions()
@ -89,6 +94,35 @@ void GameController::gameReset()
// 重新绘制棋盘 // 重新绘制棋盘
scene.setDiagonal(chess.getRule()->hasObliqueLine); scene.setDiagonal(chess.getRule()->hasObliqueLine);
// 绘制所有棋子放在起始位置分成2组写后面好区分
for (int i = 0; i < chess.getRule()->numOfChess; i++)
{
PieceItem::Models md = isInverted ? PieceItem::whitePiece : PieceItem::blackPiece;
PieceItem *newP = new PieceItem;
newP->setModel(md);
newP->setPos(scene.pos_p1);
newP->setNum(i + 1);
// 如果重复三连不可用,则显示棋子序号,九连棋专用玩法
if (!(chess.getRule()->canRepeated))
newP->setShowNum(true);
pieceList.append(newP);
scene.addItem(newP);
}
for (int i = 0; i < chess.getRule()->numOfChess; i++)
{
PieceItem::Models md = isInverted ? PieceItem::blackPiece : PieceItem::whitePiece;
PieceItem *newP = new PieceItem;
newP->setModel(md);
newP->setPos(scene.pos_p2);
newP->setNum(i + 1);
// 如果重复三连不可用,则显示棋子序号,九连棋专用玩法
if (!(chess.getRule()->canRepeated))
newP->setShowNum(true);
pieceList.append(newP);
scene.addItem(newP);
}
// 读取规则限时要求 // 读取规则限时要求
timeLimit = chess.getRule()->maxTime; timeLimit = chess.getRule()->maxTime;
// 如果规则不要求计时则time1和time2表示已用时间 // 如果规则不要求计时则time1和time2表示已用时间
@ -511,43 +545,67 @@ bool GameController::giveUp()
bool GameController::updateScence(NineChess &chess) bool GameController::updateScence(NineChess &chess)
{ {
// 清除棋子
qDeleteAll(pieceList);
pieceList.clear();
piece = NULL;
const char *board = chess.getBoard(); const char *board = chess.getBoard();
QPointF pos; QPointF pos;
int key;
int n = chess.getRule()->numOfChess * 2;
for (int i = NineChess::SEAT; i < (NineChess::SEAT)*(NineChess::RING + 1); i++) { // 棋子就位
pos = scene.cp2pos(i / NineChess::SEAT, i % NineChess::SEAT + 1); for (int i = 0; i < n; i++)
if (board[i] & 0x30) { {
PieceItem *newP = NULL; key = (i >= n/2) ? (i + 0x21 -n/2) : (i + 0x11);
PieceItem::Models md; int j;
if (isInverted) for (j = NineChess::SEAT; j < (NineChess::SEAT)*(NineChess::RING + 1); j++)
md = (board[i] & 0x10) ? PieceItem::whitePiece : PieceItem::blackPiece; {
else if (board[j] == key)
md = (board[i] & 0x10) ? PieceItem::blackPiece : PieceItem::whitePiece; {
newP = new PieceItem; pos = scene.cp2pos(j / NineChess::SEAT, j % NineChess::SEAT + 1);
newP->setModel(md); pieceList.at(i)->setPos(pos);
newP->setPos(pos); break;
newP->setNum(chess.getPieceNum(i / NineChess::SEAT, i % NineChess::SEAT + 1));
// 如果重复三连不可用,则显示棋子序号
if (!(chess.getRule()->canRepeated))
newP->setShowNum(true);
pieceList.append(newP);
scene.addItem(newP);
} }
else if (board[i] & 0x0F) { }
PieceItem *newP = NULL; if (j == (NineChess::SEAT)*(NineChess::RING + 1))
newP = new PieceItem; {
pieceList.at(i)->setPos((i % 2) ? scene.pos_p2 : scene.pos_p1);
}
}
// 添加开局禁子点
if (chess.getRule()->hasForbidden && chess.getPhase() == NineChess::GAME_OPENING)
{
for (int j = NineChess::SEAT; j < (NineChess::SEAT)*(NineChess::RING + 1); j++)
{
if (board[j] == 0x0F)
{
pos = scene.cp2pos(j / NineChess::SEAT, j % NineChess::SEAT + 1);
if (n < pieceList.size())
{
pieceList.at(n++)->setPos(pos);
}
else
{
PieceItem *newP = new PieceItem;
newP->setDeleted(); newP->setDeleted();
newP->setPos(pos); newP->setPos(pos);
pieceList.append(newP); pieceList.append(newP);
n++;
scene.addItem(newP); scene.addItem(newP);
} }
} }
}
}
// 中局清除禁子点
if (chess.getRule()->hasForbidden && chess.getPhase() != NineChess::GAME_OPENING)
{
while (n < pieceList.size())
{
delete pieceList.at(n);
pieceList.removeAt(n);
}
}
// 选中当前棋子
int ipos = chess.getCurrentPos(); int ipos = chess.getCurrentPos();
if (ipos) { if (ipos) {
pos = scene.cp2pos(ipos / NineChess::SEAT, ipos % NineChess::SEAT + 1); pos = scene.cp2pos(ipos / NineChess::SEAT, ipos % NineChess::SEAT + 1);

View File

@ -1,13 +1,19 @@
#include "gamescene.h" #include "gamescene.h"
#include "pieceitem.h" #include "pieceitem.h"
#include "boarditem.h" #include "boarditem.h"
#include "graphicsconst.h"
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QKeyEvent> #include <QKeyEvent>
#include <QDebug> #include <QDebug>
GameScene::GameScene(QObject *parent) : QGraphicsScene(parent), board(NULL) GameScene::GameScene(QObject *parent) : QGraphicsScene(parent),
board(NULL),
pos_p1(LINE_INTERVAL * 6, LINE_INTERVAL * 3),
pos_p1_g(LINE_INTERVAL* (-6), LINE_INTERVAL * 3),
pos_p2(LINE_INTERVAL * (-6), LINE_INTERVAL * (-3)),
pos_p2_g(LINE_INTERVAL * 6, LINE_INTERVAL * (-3))
{ {
// 添加棋盘 // 添加棋盘
board = new BoardItem; board = new BoardItem;

View File

@ -17,6 +17,10 @@ public:
bool pos2cp(QPointF pos, int &c, int &p); bool pos2cp(QPointF pos, int &c, int &p);
// 设置棋盘斜线 // 设置棋盘斜线
void setDiagonal(bool arg = true); void setDiagonal(bool arg = true);
// 玩家1的己方棋盒及对方棋盒位置
const QPointF pos_p1, pos_p1_g;
// 玩家2的己方棋盒及对方棋盒位置
const QPointF pos_p2, pos_p2_g;
protected: protected:
void keyPressEvent(QKeyEvent *keyEvent); void keyPressEvent(QKeyEvent *keyEvent);

View File

@ -51,14 +51,11 @@ NineChessWindow::NineChessWindow(QWidget *parent)
ui.gameView->setRenderHint(QPainter::Antialiasing); ui.gameView->setRenderHint(QPainter::Antialiasing);
// 因功能限制,使部分功能不可用 // 因功能限制,使部分功能不可用
ui.actionViewText_V->setDisabled(true);
ui.actionAutoRun_A->setDisabled(true);
ui.actionEngine_E->setDisabled(true); ui.actionEngine_E->setDisabled(true);
ui.actionInternet_I->setDisabled(true); ui.actionInternet_I->setDisabled(true);
ui.actionEngine1_T->setDisabled(true); ui.actionEngine1_T->setDisabled(true);
ui.actionEngine2_R->setDisabled(true); ui.actionEngine2_R->setDisabled(true);
ui.actionSetting_O->setDisabled(true); ui.actionSetting_O->setDisabled(true);
ui.actionAnimation_A->setDisabled(true);
// 关联既有动作信号和主窗口槽 // 关联既有动作信号和主窗口槽
// 视图上下翻转 // 视图上下翻转
@ -328,11 +325,6 @@ void NineChessWindow::on_actionSaveAs_A_triggered()
} }
void NineChessWindow::on_actionViewText_V_triggered()
{
}
void NineChessWindow::on_actionEdit_E_toggled(bool arg1) void NineChessWindow::on_actionEdit_E_toggled(bool arg1)
{ {
@ -395,6 +387,7 @@ void NineChessWindow::on_actionRowChange()
ui.actionPrevious_B->setEnabled(false); ui.actionPrevious_B->setEnabled(false);
ui.actionNext_F->setEnabled(false); ui.actionNext_F->setEnabled(false);
ui.actionEnd_E->setEnabled(false); ui.actionEnd_E->setEnabled(false);
ui.actionAutoRun_A->setEnabled(false);
} }
else { else {
if (currentRow <= 0) { if (currentRow <= 0) {
@ -402,13 +395,23 @@ void NineChessWindow::on_actionRowChange()
ui.actionPrevious_B->setEnabled(false); ui.actionPrevious_B->setEnabled(false);
ui.actionNext_F->setEnabled(true); ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true); ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
} }
if (currentRow >= rows - 1) else if (currentRow >= rows - 1)
{ {
ui.actionBegin_S->setEnabled(true); ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true); ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(false); ui.actionNext_F->setEnabled(false);
ui.actionEnd_E->setEnabled(false); ui.actionEnd_E->setEnabled(false);
ui.actionAutoRun_A->setEnabled(false);
}
else
{
ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
} }
} }
@ -416,9 +419,50 @@ void NineChessWindow::on_actionRowChange()
game->phaseChange(currentRow); game->phaseChange(currentRow);
} }
// 自动运行
void NineChessWindow::on_actionAutoRun_A_toggled(bool arg1) void NineChessWindow::on_actionAutoRun_A_toggled(bool arg1)
{ {
int rows = ui.listView->model()->rowCount();
int currentRow = ui.listView->currentIndex().row();
if (rows <= 1)
return;
// 反复执行“下一招”
while (currentRow < rows - 1)
{
if (currentRow < rows - 1)
{
ui.listView->setCurrentIndex(ui.listView->model()->index(currentRow + 1, 0));
}
currentRow = ui.listView->currentIndex().row();
// 更新动作状态
if (currentRow <= 0) {
ui.actionBegin_S->setEnabled(false);
ui.actionPrevious_B->setEnabled(false);
ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
}
else if (currentRow >= rows - 1)
{
ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(false);
ui.actionEnd_E->setEnabled(false);
ui.actionAutoRun_A->setEnabled(false);
}
else
{
ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
}
// 更新局面
game->phaseChange(currentRow);
}
} }

View File

@ -48,7 +48,6 @@ private slots:
void on_actionOpen_O_triggered(); void on_actionOpen_O_triggered();
void on_actionSave_S_triggered(); void on_actionSave_S_triggered();
void on_actionSaveAs_A_triggered(); void on_actionSaveAs_A_triggered();
void on_actionViewText_V_triggered();
//void on_actionExit_X_triggered(); //void on_actionExit_X_triggered();
void on_actionEdit_E_toggled(bool arg1); void on_actionEdit_E_toggled(bool arg1);
//void on_actionFlip_F_triggered(); //void on_actionFlip_F_triggered();