ksquares
ksquaresdemowindow.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "ksquaresdemowindow.h"
00012
00013
00014 #include <QTimer>
00015
00016
00017 #include <KApplication>
00018 #include <KStatusBar>
00019 #include <KActionCollection>
00020 #include <kdebug.h>
00021 #include <KLocale>
00022 #include <kstandardgameaction.h>
00023
00024
00025 #include "aicontroller.h"
00026 #include "gameboardview.h"
00027
00028 KSquaresDemoWindow::KSquaresDemoWindow() : KXmlGuiWindow(), m_view(new GameBoardView(this)), m_scene(0)
00029 {
00030 sGame = new KSquaresGame();
00031 connect(sGame, SIGNAL(takeTurnSig(KSquaresPlayer*)), this, SLOT(playerTakeTurn(KSquaresPlayer*)));
00032 connect(sGame, SIGNAL(gameOver(QVector<KSquaresPlayer>)), this, SLOT(gameOver(QVector<KSquaresPlayer>)));
00033
00034 m_view->setRenderHints(QPainter::Antialiasing);
00035 m_view->setFrameStyle(QFrame::NoFrame);
00036 m_view->setDisabled(true);
00037 setCentralWidget(m_view);
00038
00039 KStandardGameAction::quit(kapp, SLOT(quit()), actionCollection());
00040 setupGUI();
00041
00042 statusBar()->insertPermanentItem(i18n("Current Player"), 0);
00043 statusBar()->show();
00044 }
00045
00046 void KSquaresDemoWindow::gameNew()
00047 {
00048
00049 QVector<KSquaresPlayer> playerList;
00050 for(int i=0; i<4; i++)
00051 {
00052 QColor color;
00053 switch(i)
00054 {
00055 case 0:
00056 color = Qt::red;
00057 break;
00058 case 1:
00059 color = Qt::blue;
00060 break;
00061 case 2:
00062 color = Qt::green;
00063 break;
00064 case 3:
00065 color = Qt::yellow;
00066 break;
00067 default:
00068 kError() << "KSquaresGame::playerSquareComplete(); currentPlayerId() != 0|1|2|3";
00069 }
00070 playerList.append(KSquaresPlayer(i18n("Player %1", i+1), color, false));
00071 }
00072
00073
00074 GameBoardScene* temp = m_scene;
00075 m_scene = new GameBoardScene(15, 10);
00076
00077 m_view->setScene(m_scene);
00078 delete temp;
00079
00080 m_view->setBoardSize();
00081
00082
00083 sGame->createGame(playerList, 15, 10);
00084 connect(m_scene, SIGNAL(lineDrawn(int)), sGame, SLOT(addLineToIndex(int)));
00085 connect(sGame, SIGNAL(drawLine(int,QColor)), m_scene, SLOT(drawLine(int,QColor)));
00086 connect(sGame, SIGNAL(drawSquare(int,QColor)), m_scene, SLOT(drawSquare(int,QColor)));
00087
00088 sGame->start();
00089 }
00090
00091 void KSquaresDemoWindow::playerTakeTurn(KSquaresPlayer* currentPlayer)
00092 {
00093 statusBar()->changeItem(currentPlayer->name(), 0);
00094 QTimer::singleShot(200, this, SLOT(aiChooseLine()));
00095 }
00096
00097 void KSquaresDemoWindow::aiChooseLine()
00098 {
00099 aiController ai(sGame->currentPlayerId(), sGame->lines(), sGame->squares(), sGame->boardWidth(), sGame->boardHeight());
00100 sGame->addLineToIndex(ai.chooseLine());
00101 }
00102
00103 void KSquaresDemoWindow::gameOver(const QVector<KSquaresPlayer> & )
00104 {
00105 kDebug() << "Game Over";
00106 QTimer::singleShot(1000, this, SLOT(gameNew()));
00107 }
00108
00109 #include "ksquaresdemowindow.moc"