• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

ksquares

  • sources
  • kde-4.14
  • kdegames
  • ksquares
  • src
ksquaresdemowindow.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Matthew Williams <matt@milliams.com> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  ***************************************************************************/
9 
10 //header
11 #include "ksquaresdemowindow.h"
12 
13 //qt
14 #include <QTimer>
15 
16 //kde
17 #include <KApplication>
18 #include <KStatusBar>
19 #include <KActionCollection>
20 #include <kdebug.h>
21 #include <KLocale>
22 #include <kstandardgameaction.h>
23 
24 //classes
25 #include "aicontroller.h"
26 #include "gameboardview.h"
27 
28 KSquaresDemoWindow::KSquaresDemoWindow() : KXmlGuiWindow(), m_view(new GameBoardView(this)), m_scene(0)
29 {
30  sGame = new KSquaresGame();
31  connect(sGame, SIGNAL(takeTurnSig(KSquaresPlayer*)), this, SLOT(playerTakeTurn(KSquaresPlayer*)));
32  connect(sGame, SIGNAL(gameOver(QVector<KSquaresPlayer>)), this, SLOT(gameOver(QVector<KSquaresPlayer>)));
33 
34  m_view->setRenderHints(QPainter::Antialiasing);
35  m_view->setFrameStyle(QFrame::NoFrame);
36  m_view->setDisabled(true);
37  setCentralWidget(m_view);
38 
39  KStandardGameAction::quit(kapp, SLOT(quit()), actionCollection());
40  setupGUI();
41 
42  statusBar()->insertPermanentItem(i18n("Current Player"), 0);
43  statusBar()->show();
44 }
45 
46 void KSquaresDemoWindow::gameNew()
47 {
48  //create players
49  QVector<KSquaresPlayer> playerList;
50  for(int i=0; i<4; i++)
51  {
52  QColor color;
53  switch(i)
54  {
55  case 0:
56  color = Qt::red;
57  break;
58  case 1:
59  color = Qt::blue;
60  break;
61  case 2:
62  color = Qt::green;
63  break;
64  case 3:
65  color = Qt::yellow;
66  break;
67  default:
68  kError() << "KSquaresGame::playerSquareComplete(); currentPlayerId() != 0|1|2|3";
69  }
70  playerList.append(KSquaresPlayer(i18n("Player %1", i+1), color, false));
71  }
72 
73  //create physical board
74  GameBoardScene* temp = m_scene;
75  m_scene = new GameBoardScene(15, 10);
76 
77  m_view->setScene(m_scene);
78  delete temp;
79 
80  m_view->setBoardSize(); //refresh board zooming
81 
82  //start game etc.
83  sGame->createGame(playerList, 15, 10);
84  connect(m_scene, SIGNAL(lineDrawn(int)), sGame, SLOT(addLineToIndex(int)));
85  connect(sGame, SIGNAL(drawLine(int,QColor)), m_scene, SLOT(drawLine(int,QColor)));
86  connect(sGame, SIGNAL(highlightMove(int)), m_scene, SLOT(highlightLine(int)));
87  connect(sGame, SIGNAL(drawSquare(int,QColor)), m_scene, SLOT(drawSquare(int,QColor)));
88 
89  sGame->start();
90 }
91 
92 void KSquaresDemoWindow::playerTakeTurn(KSquaresPlayer* currentPlayer)
93 {
94  statusBar()->changeItem(currentPlayer->name(), 0);
95  QTimer::singleShot(200, this, SLOT(aiChooseLine()));
96 }
97 
98 void KSquaresDemoWindow::aiChooseLine()
99 {
100  aiController ai(sGame->currentPlayerId(), sGame->lines(), sGame->squares(), sGame->boardWidth(), sGame->boardHeight());
101  sGame->addLineToIndex(ai.chooseLine());
102 }
103 
104 void KSquaresDemoWindow::gameOver(const QVector<KSquaresPlayer> & /*playerList*/)
105 {
106  kDebug() << "Game Over";
107  QTimer::singleShot(1000, this, SLOT(gameNew()));
108 }
109 
110 #include "ksquaresdemowindow.moc"
KSquaresPlayer
Player class for KSquares.
Definition: ksquaresplayer.h:23
KSquaresGame
The game controller.
Definition: ksquaresgame.h:44
KSquaresGame::createGame
void createGame(const QVector< KSquaresPlayer > &startPlayers, int startWidth, int startHeight)
Create a new game.
Definition: ksquaresgame.cpp:29
GameBoardScene
Scene for displaying the game board.
Definition: gameboardscene.h:27
QVector::append
void append(const T &value)
KSquaresGame::lines
QList< bool > lines() const
Definition: ksquaresgame.h:90
KSquaresDemoWindow::KSquaresDemoWindow
KSquaresDemoWindow()
Constructor.
Definition: ksquaresdemowindow.cpp:28
KSquaresGame::start
void start()
Starts the game.
Definition: ksquaresgame.h:64
QFrame::setFrameStyle
void setFrameStyle(int style)
KSquaresDemoWindow::gameNew
void gameNew()
Definition: ksquaresdemowindow.cpp:46
QGraphicsView::setRenderHints
void setRenderHints(QFlags< QPainter::RenderHint > hints)
ksquaresdemowindow.h
KSquaresGame::addLineToIndex
void addLineToIndex(int index)
Definition: ksquaresgame.cpp:134
KXmlGuiWindow
KSquaresGame::boardHeight
int boardHeight() const
Definition: ksquaresgame.h:98
QGraphicsView::setScene
void setScene(QGraphicsScene *scene)
KSquaresGame::squares
QList< int > squares() const
Definition: ksquaresgame.h:86
QColor
gameboardview.h
aiController
AI Controller for KSquares.
Definition: aicontroller.h:31
aicontroller.h
QVector< KSquaresPlayer >
GameBoardView::setBoardSize
void setBoardSize()
Automatically resizes the board according to the users preferences. Deprecated for a bit...
Definition: gameboardview.cpp:16
KSquaresGame::currentPlayerId
int currentPlayerId() const
Definition: ksquaresgame.h:76
QWidget::setDisabled
void setDisabled(bool disable)
KSquaresGame::boardWidth
int boardWidth() const
Definition: ksquaresgame.h:94
GameBoardView
Actual game board widget.
Definition: gameboardview.h:25
KSquaresPlayer::name
QString name() const
Sets the players name.
Definition: ksquaresplayer.h:50
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ksquares

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

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal