• 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
ksquareswindow.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 "ksquareswindow.h"
12 
13 //qt
14 #include <QtGui/QStandardItemModel>
15 #include <QtCore/QTimer>
16 
17 //kde
18 #include <KConfigDialog>
19 #include <KActionCollection>
20 #include <KDebug>
21 #include <KLocale>
22 #include <KScoreDialog>
23 #include <KHighscore>
24 #include <KStandardGameAction>
25 #include <KStatusBar>
26 #include <KAction>
27 
28 //generated
29 #include "settings.h"
30 
31 //classes
32 #include "aicontroller.h"
33 #include "gameboardview.h"
34 
35 //ui
36 #include "newgamedialog.h"
37 #include "scoresdialog.h"
38 
39 KSquaresWindow::KSquaresWindow() : KXmlGuiWindow(), m_view(new GameBoardView(this)), m_scene(0)
40 {
41  setCentralWidget(m_view);
42  QTimer::singleShot(0, this, SLOT(initObject()));
43 }
44 
45 KSquaresWindow::~KSquaresWindow()
46 {
47  delete m_view;
48  delete m_scene;
49  delete sGame;
50 }
51 
52 void KSquaresWindow::initObject()
53 {
54  sGame = new KSquaresGame();
55  connect(sGame, SIGNAL(takeTurnSig(KSquaresPlayer*)), this, SLOT(playerTakeTurn(KSquaresPlayer*)));
56  connect(sGame, SIGNAL(gameOver(QVector<KSquaresPlayer>)), this, SLOT(gameOver(QVector<KSquaresPlayer>)));
57  m_view->setRenderHints(QPainter::Antialiasing);
58  m_view->setFrameStyle(QFrame::NoFrame);
59  setupActions();
60  statusBar()->insertPermanentItem(i18n("Current Player"), statusplayer);
61  statusBar()->show();
62  setAutoSaveSettings();
63 
64  gameNew();
65 }
66 
67 //void KSquaresWindow::configureHighscores() {KExtHighscore::configure(this);}
68 void KSquaresWindow::showHighscores()
69 {
70  KScoreDialog ksdialog(KScoreDialog::Name, this);
71  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Easy"), i18n("Easy")));
72  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Medium"), i18n("Medium")));
73  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Hard"), i18n("Hard")));
74  ksdialog.exec();
75 }
76 
77 void KSquaresWindow::gameNew()
78 {
79  //load settings
80  NewGameDialog dialog(this);
81 
82  //create indexed arrays of the widgets
83  QList<QLineEdit*> nameLineEditList;
84  nameLineEditList.append(dialog.playerOneName);
85  nameLineEditList.append(dialog.playerTwoName);
86  nameLineEditList.append(dialog.playerThreeName);
87  nameLineEditList.append(dialog.playerFourName);
88  QList<QCheckBox*> humanCheckBoxList;
89  humanCheckBoxList.append(dialog.playerOneHuman);
90  humanCheckBoxList.append(dialog.playerTwoHuman);
91  humanCheckBoxList.append(dialog.playerThreeHuman);
92  humanCheckBoxList.append(dialog.playerFourHuman);
93 
94  //get settings from file
95  for(int i=0; i<Settings::playerNames().size(); i++)
96  {
97  nameLineEditList.at(i)->setText(Settings::playerNames().at(i));
98  }
99  for(int i=0; i<Settings::playerNames().size(); i++)
100  {
101  if (Settings::humanList().at(i) == 2)
102  humanCheckBoxList.at(i)->setCheckState(Qt::Checked);
103  else
104  humanCheckBoxList.at(i)->setCheckState(Qt::Unchecked);
105  }
106  dialog.spinNumOfPlayers->setValue(Settings::numOfPlayers());
107  dialog.spinHeight->setValue(Settings::boardHeight());
108  dialog.spinWidth->setValue(Settings::boardWidth());
109  if (Settings::quickStart() == 2)
110  dialog.quickStartCheck->setCheckState(Qt::Checked);
111  else
112  dialog.quickStartCheck->setCheckState(Qt::Unchecked);
113 
114  //run dialog
115  dialog.exec();
116  if (dialog.result() == QDialog::Rejected) return;
117 
118  //save settings
119  Settings::setNumOfPlayers(dialog.spinNumOfPlayers->value());
120 
121  QStringList tempNames;
122  for(int i=0; i<=3; i++)
123  {
124  tempNames.append(nameLineEditList.at(i)->text());
125  }
126  Settings::setPlayerNames(tempNames);
127 
128  QList<int> tempHuman;
129  for(int i=0; i<=3; i++)
130  {
131  tempHuman.append(humanCheckBoxList.at(i)->checkState());
132  }
133  Settings::setHumanList(tempHuman);
134 
135  Settings::setBoardHeight(dialog.spinHeight->value());
136  Settings::setBoardWidth(dialog.spinWidth->value());
137  Settings::setQuickStart(dialog.quickStartCheck->checkState());
138  Settings::self()->writeConfig();
139 
140  gameReset();
141 }
142 
143 void KSquaresWindow::gameReset()
144 {
145  //create players
146  QVector<KSquaresPlayer> playerList;
147  for(int i=0; i<Settings::numOfPlayers(); i++)
148  {
149  QColor color;
150  switch(i)
151  {
152  case 0: //Red
153  color = QColor(191,3,3); //or darker: (156,15,15);
154  break;
155  case 1: //Blue
156  color = QColor(0,67,138); //or darker: (0,49,110);
157  break;
158  case 2: //Green
159  color = QColor(0,137,44); //or darker: (0,110,41);
160  break;
161  case 3: //Yellow
162  color = QColor(243,195,0); //or darker: (227,173,0);
163  break;
164  default:
165  kError() << "KSquaresGame::playerSquareComplete(); currentPlayerId() != 0|1|2|3";
166  }
167  playerList.append(KSquaresPlayer(Settings::playerNames().at(i), color, Settings::humanList().at(i)));
168  }
169 
170  //create physical board
171  GameBoardScene* temp = m_scene;
172  m_scene = new GameBoardScene(Settings::boardWidth(), Settings::boardHeight());
173 
174  m_view->setScene(m_scene);
175  delete temp;
176 
177  m_view->setBoardSize(); //refresh board zooming
178 
179  //start game etc.
180  sGame->createGame(playerList, Settings::boardWidth(), Settings::boardHeight());
181  connect(m_scene, SIGNAL(lineDrawn(int)), sGame, SLOT(addLineToIndex(int)));
182  connect(m_scene, SIGNAL(signalMoveRequest(int,int,int,int)), SLOT(slotMoveRequest(int,int,int,int)));
183  connect(sGame, SIGNAL(drawLine(int,QColor)), m_scene, SLOT(drawLine(int,QColor)));
184  connect(sGame, SIGNAL(highlightMove(int)), m_scene, SLOT(highlightLine(int)));
185  connect(sGame, SIGNAL(drawSquare(int,QColor)), m_scene, SLOT(drawSquare(int,QColor)));
186 
187  if (Settings::quickStart() == 2)
188  {
189  //This is being done before sGame->start(); to avoid the players cycling
190  aiController ai(-1, sGame->lines(), QList<int>(), sGame->boardWidth(), sGame->boardHeight());
191  QList<int> lines = ai.autoFill(8); //There will be 8 possible safe move for the players
192  QListIterator<int> i(lines);
193  while (i.hasNext())
194  {
195  sGame->addLineToIndex(i.next());
196  }
197  }
198  sGame->start();
199 }
200 
201 void KSquaresWindow::gameOver(const QVector<KSquaresPlayer> &_playerList)
202 {
203  QVector<KSquaresPlayer> playerList = _playerList;
204  qSort(playerList.begin(), playerList.end(), qGreater<KSquaresPlayer>());
205  //m_scene->displayScoreTable(playerList);
206 
207  ScoresDialog scoresDialog(this);
208  scoresDialog.setButtons(KDialog::Close);
209  scoresDialog.setDefaultButton(KDialog::Close);
210  scoresDialog.setCaption(i18n("Scores"));
211 
212  QStandardItemModel* scoreTableModel = new QStandardItemModel(&scoresDialog);
213  scoreTableModel->setRowCount(playerList.size());
214  scoreTableModel->setColumnCount(2);
215  scoreTableModel->setHeaderData(0, Qt::Horizontal, i18n("Player Name"));
216  scoreTableModel->setHeaderData(1, Qt::Horizontal, i18n("Completed Squares"));
217 
218  for(int i = 0; i < playerList.size(); i++)
219  {
220  scoreTableModel->setItem(i, 0, new QStandardItem(playerList.at(i).name()));
221  scoreTableModel->item(i, 0)->setEditable(false);
222 
223  QString temp;
224  temp.setNum(playerList.at(i).score());
225  scoreTableModel->setItem(i, 1, new QStandardItem(temp));
226  scoreTableModel->item(i, 1)->setEditable(false);
227  }
228 
229  scoresDialog.scoreTable->setModel(scoreTableModel);
230  scoresDialog.scoreTable->resizeColumnsToContents();
231  scoresDialog.exec();
232 
233  if(playerList.at(0).isHuman())
234  {
235  int score = (int)(static_cast<double>(playerList.at(0).score()) - (static_cast<double>(Settings::boardWidth()*Settings::boardHeight()) / static_cast<double>(playerList.size())));
236 
237  KScoreDialog ksdialog(KScoreDialog::Name, this);
238  switch(Settings::difficulty())
239  {
240  case 0:
241  ksdialog.setConfigGroup(qMakePair(QByteArray("Easy"), i18n("Easy")));
242  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Medium"), i18n("Medium")));
243  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Hard"), i18n("Hard")));
244  break;
245  case 1:
246  ksdialog.setConfigGroup(qMakePair(QByteArray("Medium"), i18n("Medium")));
247  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Easy"), i18n("Easy")));
248  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Hard"), i18n("Hard")));
249  break;
250  case 2:
251  ksdialog.setConfigGroup(qMakePair(QByteArray("Hard"), i18n("Hard")));
252  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Easy"), i18n("Easy")));
253  ksdialog.addLocalizedConfigGroupName(qMakePair(QByteArray("Medium"), i18n("Medium")));
254  break;
255  }
256  KScoreDialog::FieldInfo scoreInfo;
257  scoreInfo[KScoreDialog::Name]=playerList.at(0).name();
258  scoreInfo[KScoreDialog::Score].setNum(score);
259 
260  if(ksdialog.addScore(scoreInfo, KScoreDialog::AskName))
261  ksdialog.exec();
262  }
263 }
264 
265 void KSquaresWindow::playerTakeTurn(KSquaresPlayer* currentPlayer)
266 {
267  //kDebug() << "void KSquares::playerTakeTurn(KSquaresPlayer* currentPlayer)";
268  statusBar()->changeItem(
269  QString::fromLatin1("<font color=\"%1\">%2</font>")
270  .arg(currentPlayer->colour().name())
271  .arg(currentPlayer->name()),
272  statusplayer
273  );
274  if(currentPlayer->isHuman())
275  {
276  //Let the human player interact with the board through the GameBoardView
277 
278  setCursor(Qt::ArrowCursor);
279  m_scene->enableEvents();
280  }
281  else //AI
282  {
283  //lock the view to let the AI do it's magic
284  setCursor(Qt::WaitCursor);
285  m_scene->disableEvents();
286 
287  QTimer::singleShot(200, this, SLOT(aiChooseLine()));
288  setCursor(Qt::ArrowCursor);
289  }
290 }
291 
292 // testing only
293 void KSquaresWindow::aiChooseLine()
294 {
295  aiController ai(sGame->currentPlayerId(), sGame->lines(), sGame->squares(), sGame->boardWidth(), sGame->boardHeight());
296  sGame->addLineToIndex(ai.chooseLine());
297 }
298 
299 void KSquaresWindow::setupActions()
300 {
301  KStandardGameAction::gameNew(this, SLOT(gameNew()), actionCollection());
302  KAction *resetGame = KStandardGameAction::restart(this, SLOT(gameReset()), actionCollection());
303  resetGame->setStatusTip(i18n("Start a new game with the current settings"));
304 
305  KStandardGameAction::highscores(this, SLOT(showHighscores()), actionCollection());
306  KStandardGameAction::quit(this, SLOT(close()), actionCollection());
307 
308  // Preferences
309  KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
310 
311  setupGUI();
312 }
313 
314 void KSquaresWindow::optionsPreferences()
315 {
316  KConfigDialog *dialog = new KConfigDialog(this, "settings", Settings::self());
317 
318  QWidget *displaySettingsDialog = new QWidget;
319  ui_prefs_display.setupUi(displaySettingsDialog);
320  dialog->addPage(displaySettingsDialog, i18n("Display"), "preferences-desktop-display");
321 
322  QWidget *aiSettingsDialog = new QWidget;
323  ui_prefs_ai.setupUi(aiSettingsDialog);
324  dialog->addPage(aiSettingsDialog, i18n("Computer Player"), "games-difficult");
325 
326  connect(dialog, SIGNAL(settingsChanged(QString)), m_view, SLOT(setBoardSize()));
327  dialog->show();
328 }
329 
330 #include "ksquareswindow.moc"
KSquaresPlayer
Player class for KSquares.
Definition: ksquaresplayer.h:23
KSquaresWindow::~KSquaresWindow
~KSquaresWindow()
Definition: ksquareswindow.cpp:45
QStandardItemModel
QWidget
GameBoardScene::disableEvents
void disableEvents()
enables mouse events
Definition: gameboardscene.h:70
KSquaresGame
The game controller.
Definition: ksquaresgame.h:44
Settings::quickStart
static int quickStart()
Get Quick start the game.
Definition: settings.h:125
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
QListIterator::next
const T & next()
Settings::setBoardWidth
static void setBoardWidth(int v)
Set Width of board.
Definition: settings.h:77
QVector::append
void append(const T &value)
QVector::begin
iterator begin()
QByteArray
QColor::name
QString name() const
Settings::setQuickStart
static void setQuickStart(int v)
Set Quick start the game.
Definition: settings.h:115
KSquaresGame::lines
QList< bool > lines() const
Definition: ksquaresgame.h:90
Settings::setNumOfPlayers
static void setNumOfPlayers(int v)
Set Number of Players.
Definition: settings.h:20
QList::at
const T & at(int i) const
Settings::setPlayerNames
static void setPlayerNames(const QStringList &v)
Set Player Names.
Definition: settings.h:39
KSquaresGame::start
void start()
Starts the game.
Definition: ksquaresgame.h:64
QFrame::setFrameStyle
void setFrameStyle(int style)
QGraphicsView::setRenderHints
void setRenderHints(QFlags< QPainter::RenderHint > hints)
KSquaresWindow::gameReset
void gameReset()
Start a new game with the same settings (read from KConfig Settings)
Definition: ksquareswindow.cpp:143
KSquaresWindow::showHighscores
void showHighscores()
Definition: ksquareswindow.cpp:68
ksquareswindow.h
QList::size
int size() const
Settings::self
static Settings * self()
Definition: settings.cpp:17
KSquaresGame::addLineToIndex
void addLineToIndex(int index)
Definition: ksquaresgame.cpp:134
QStandardItemModel::setColumnCount
void setColumnCount(int columns)
Settings::numOfPlayers
static int numOfPlayers()
Get Number of Players.
Definition: settings.h:30
QList::append
void append(const T &value)
KXmlGuiWindow
QStandardItemModel::setItem
void setItem(int row, int column, QStandardItem *item)
Settings::setBoardHeight
static void setBoardHeight(int v)
Set Height of board.
Definition: settings.h:96
KSquaresGame::boardHeight
int boardHeight() const
Definition: ksquaresgame.h:98
Settings::boardHeight
static int boardHeight()
Get Height of board.
Definition: settings.h:106
QGraphicsView::setScene
void setScene(QGraphicsScene *scene)
KSquaresGame::squares
QList< int > squares() const
Definition: ksquaresgame.h:86
QString
Settings::playerNames
static QStringList playerNames()
Get Player Names.
Definition: settings.h:49
QList
QColor
Settings::difficulty
static int difficulty()
Get Difficulty.
Definition: settings.h:144
QStringList
gameboardview.h
Settings::boardWidth
static int boardWidth()
Get Width of board.
Definition: settings.h:87
QStandardItemModel::setHeaderData
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
QStandardItemModel::item
QStandardItem * item(int row, int column) const
aiController
AI Controller for KSquares.
Definition: aicontroller.h:31
KSquaresPlayer::isHuman
bool isHuman() const
Definition: ksquaresplayer.h:68
QVector::at
const T & at(int i) const
KSquaresWindow::gameNew
void gameNew()
Launch the new game dialog and create a new game.
Definition: ksquareswindow.cpp:77
aicontroller.h
QVector< KSquaresPlayer >
settings.h
GameBoardView::setBoardSize
void setBoardSize()
Automatically resizes the board according to the users preferences. Deprecated for a bit...
Definition: gameboardview.cpp:16
QString::setNum
QString & setNum(short n, int base)
KSquaresGame::currentPlayerId
int currentPlayerId() const
Definition: ksquaresgame.h:76
KSquaresWindow::KSquaresWindow
KSquaresWindow()
Constructor.
Definition: ksquareswindow.cpp:39
ScoresDialog
Definition: scoresdialog.h:17
QString::fromLatin1
QString fromLatin1(const char *str, int size)
Settings::setHumanList
static void setHumanList(const QList< int > &v)
Set Human or AI.
Definition: settings.h:58
QListIterator
NewGameDialog
Definition: newgamedialog.h:17
QStandardItemModel::setRowCount
void setRowCount(int rows)
QStandardItem
GameBoardScene::enableEvents
void enableEvents()
disables mouse events
Definition: gameboardscene.h:68
newgamedialog.h
QVector::size
int size() const
QVector::end
iterator end()
Settings::humanList
static QList< int > humanList()
Get Human or AI.
Definition: settings.h:68
QStandardItem::setEditable
void setEditable(bool editable)
KSquaresGame::boardWidth
int boardWidth() const
Definition: ksquaresgame.h:94
GameBoardView
Actual game board widget.
Definition: gameboardview.h:25
KSquaresPlayer::colour
QColor colour() const
Definition: ksquaresplayer.h:73
KSquaresPlayer::name
QString name() const
Sets the players name.
Definition: ksquaresplayer.h:50
scoresdialog.h
QTimer::singleShot
singleShot
QListIterator::hasNext
bool hasNext() const
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