File indexing completed on 2022-09-27 13:19:28

0001 /*
0002     SPDX-FileCopyrightText: 2006 Matthew Williams <matt@milliams.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 //header
0008 #include "ksquaresdemowindow.h"
0009 
0010 //qt
0011 #include <QTimer>
0012 
0013 //kde
0014 #include <QApplication>
0015 #include <QStatusBar>
0016 #include <KActionCollection>
0017 #include <QDebug>
0018 #include <KLocalizedString>
0019 #include <kstandardgameaction.h>
0020 
0021 //classes
0022 #include "aicontroller.h"
0023 #include "gameboardview.h"
0024 
0025 KSquaresDemoWindow::KSquaresDemoWindow() : KXmlGuiWindow(), m_view(new GameBoardView(this)), m_scene(nullptr)
0026 {
0027     sGame = new KSquaresGame();
0028     connect(sGame, &KSquaresGame::takeTurnSig, this, &KSquaresDemoWindow::playerTakeTurn);
0029     connect(sGame, &KSquaresGame::gameOver, this, &KSquaresDemoWindow::gameOver);
0030 
0031     m_view->setRenderHints(QPainter::Antialiasing);
0032     m_view->setFrameStyle(QFrame::NoFrame);
0033     m_view->setDisabled(true);
0034     setCentralWidget(m_view);
0035 
0036     KStandardGameAction::quit(qApp, &QApplication::quit, actionCollection());
0037     setupGUI();
0038 
0039     //QT5 statusBar()->insertPermanentItem(i18n("Current Player"), 0);
0040     statusBar()->show();
0041 }
0042 
0043 void KSquaresDemoWindow::gameNew()
0044 {
0045     //create players
0046     QVector<KSquaresPlayer> playerList;
0047     for (int i = 0; i < 4; i++) {
0048         QColor color;
0049         switch (i) {
0050         case 0:
0051             color = Qt::red;
0052             break;
0053         case 1:
0054             color = Qt::blue;
0055             break;
0056         case 2:
0057             color = Qt::green;
0058             break;
0059         case 3:
0060             color = Qt::yellow;
0061             break;
0062         default:
0063             qCritical() << "KSquaresGame::playerSquareComplete(); currentPlayerId() != 0|1|2|3";
0064         }
0065         playerList.append(KSquaresPlayer(i18n("Player %1", i + 1), color, false));
0066     }
0067 
0068     //create physical board
0069     GameBoardScene *temp = m_scene;
0070     m_scene = new GameBoardScene(15, 10);
0071 
0072     m_view->setScene(m_scene);
0073     delete temp;
0074 
0075     m_view->setBoardSize(); //refresh board zooming
0076 
0077     //start game etc.
0078     sGame->createGame(playerList, 15, 10);
0079     connect(m_scene, &GameBoardScene::lineDrawn, sGame, &KSquaresGame::addLineToIndex);
0080     connect(sGame, &KSquaresGame::drawLine, m_scene, &GameBoardScene::drawLine);
0081     connect(sGame, &KSquaresGame::highlightMove, m_scene, &GameBoardScene::highlightLine);
0082     connect(sGame, &KSquaresGame::drawSquare, m_scene, &GameBoardScene::drawSquare);
0083 
0084     sGame->start();
0085 }
0086 
0087 void KSquaresDemoWindow::playerTakeTurn(KSquaresPlayer *currentPlayer)
0088 {
0089     //QT5 statusBar()->changeItem(currentPlayer->name(), 0);
0090     QTimer::singleShot(200, this, &KSquaresDemoWindow::aiChooseLine);
0091 }
0092 
0093 void KSquaresDemoWindow::aiChooseLine()
0094 {
0095     aiController ai(sGame->currentPlayerId(), sGame->lines(), sGame->squares(), sGame->boardWidth(), sGame->boardHeight());
0096     sGame->addLineToIndex(ai.chooseLine());
0097 }
0098 
0099 void KSquaresDemoWindow::gameOver(const QVector<KSquaresPlayer> & /*playerList*/)
0100 {
0101     //qDebug() << "Game Over";
0102     QTimer::singleShot(1000, this, &KSquaresDemoWindow::gameNew);
0103 }
0104