• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

ksquares

ksquaresdemowindow.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Matthew Williams    <matt@milliams.com>         *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  ***************************************************************************/
00009 
00010 //header
00011 #include "ksquaresdemowindow.h"
00012 
00013 //qt
00014 #include <QTimer>
00015 
00016 //kde
00017 #include <KApplication>
00018 #include <KStatusBar>
00019 #include <KActionCollection>
00020 #include <kdebug.h>
00021 #include <KLocale>
00022 #include <kstandardgameaction.h>
00023 
00024 //classes
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     //create players
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     //create physical board
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(); //refresh board zooming
00081 
00082     //start game etc.
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> & /*playerList*/)
00104 {
00105     kDebug() << "Game Over";
00106     QTimer::singleShot(1000, this, SLOT(gameNew()));
00107 }
00108 
00109 #include "ksquaresdemowindow.moc"

ksquares

Skip menu "ksquares"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal