【发布时间】:2017-02-02 17:18:28
【问题描述】:
我正在用 Qt quick 和 C++ 构建一个黑白棋游戏。
我使用 QML 对话框中的这段代码开始新游戏,当一个玩家是人类时,我等待输入。当两个玩家是PC时,我面临GUI将等待插槽完成的问题,在PC玩家中插槽调用flipTurn()(因为是PC,没有输入等待),flipTurn是递归的,所以GUI 会一直阻塞直到游戏结束。
我希望它在每次移动后更新电路板。项目链接:
reversi project
myBroker.createGame(blacktypebutton.checkedButton.playertype,
whitetypebutton.checkedButton.playertype,
getBlackAlgo(),
getWhiteAlgo(),
getBlackDep(),
getWhiteDep());
console.log("finished creating game !");
void Broker::flipTurn()
{
if(someoneWon()){
emit gameEnd();
return;
}
emit updateBoard();
if (currentGame->getTurn() == Constants::BLACK){
currentGame->setTurn(Constants::WHITE);
if (currentGame->getWhitePlayer().getType() == Constants::PC){
currentGame->pcMove(Constants::WHITE);
flipTurn();
}
else
currentGame->updatePossible();
}
else{
currentGame->setTurn(Constants::BLACK);
if (currentGame->getBlackPlayer().getType() == Constants::PC){
currentGame->pcMove(Constants::BLACK);
flipTurn();
}
else
currentGame->updatePossible();
return;
}
emit updateBoard();
}
bool Broker::createGame(int blacktype, int whitetype, int blackalg, int whitealg, int blackdep, int whitedep)
{
currentGame = new Game(blacktype,whitetype,blackalg,whitealg,blackdep,whitedep);
currentGame->setGameBoard(&gameBoard);
updatePossible();
emit gameStart();
}
void Broker::onGameStart()
{
if(currentGame->getTurnType() == Constants::PC){
currentGame->pcMove(currentGame->getTurn());
cout<<"in ongamestart slot"<<endl;
flipTurn();
}
}
【问题讨论】:
-
我觉得这个程序应该是多线程的,分成GUI部分和AI部分。
-
您不应该像在所有其他框架中一样,通过连续操作阻止 GUI 线程。将所有这些操作移至不同的线程,在 QML 中您可以使用 WorkerScript 或 C++ 中的 QThread
-
我不认为这是一个需要线程的问题
-
我考虑过多线程,但我没有找到任何好的教程,其中包含展示如何构建此类东西的示例,任何展示如何构建 GUI 并将其从逻辑中分离出来的教程都将被接受。