• 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
ksquaresgame.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 #include "ksquaresgame.h"
11 
12 #include <kdebug.h>
13 
14 //generated
15 #include "settings.h"
16 
17 KSquaresGame::KSquaresGame()
18 {
19  kDebug() << "Constructing Game";
20  gameInProgress = false;
21 }
22 
23 KSquaresGame::~KSquaresGame()
24 {
25  kDebug() << "Destroying game";
26  gameInProgress = false;
27 }
28 
29 void KSquaresGame::createGame(const QVector<KSquaresPlayer> &startPlayers, int startWidth, int startHeight)
30 {
31  resetEverything(); //reset everything
32  kDebug() << "Creating Game with" << startPlayers.size() << "player(s)";
33 
34  //BEGIN Initialisation
35  width = startWidth;
36  height = startHeight;
37  for(int i=0; i < startPlayers.size(); i++)
38  {
39  players.append(startPlayers[i]);
40  }
41  for(int i = 0; i < (2*width*height + width + height); i++)
42  {
43  lineList.append(false);
44  }
45  for(int i = 0; i < (width*height); i++)
46  {
47  squareOwnerTable.append(-1);
48  }
49  //END Initialisation
50 
51  kDebug() << "Game Starting";
52  nextPlayer();
53 }
54 
55 void KSquaresGame::switchPlayer()
56 {
57  anotherGo = false;
58  currentPlayerId() >= (players.size()-1) ? i_currentPlayerId = 0 : i_currentPlayerId++;
59 }
60 
61 int KSquaresGame::nextPlayer()
62 {
63  anotherGo = false; //just to reset the variable
64  currentPlayerId() >= (players.size()-1) ? i_currentPlayerId = 0 : i_currentPlayerId++;
65  kDebug()<< "- Moving to next player:" << currentPlayer()->name() << "(" << currentPlayerId() << ")";
66  kDebug() << "-";
67  emit takeTurnSig(currentPlayer());
68 
69  return currentPlayerId();
70 }
71 
72 void KSquaresGame::playerSquareComplete(int index)
73 {
74  kDebug() << "- - " << currentPlayer()->name() << "(" << currentPlayerId() << ") has completed a square";
75  anotherGo = true;
76 
77  squareOwnerTable[index] = currentPlayerId(); //add square to index
78  emit drawSquare(index, currentPlayer()->colour());
79  currentPlayer()->incScore();
80 
81  int totalPoints=0;
82  for (int i=0; i < players.size(); i++)
83  {
84  totalPoints += players.at(i).score();
85  }
86  kDebug() << "- - Square Completed";
87  if (totalPoints >= width*height) //if the board is full
88  {
89  kDebug() << "Game Over";
90  gameInProgress = false;
91  emit gameOver(players);
92  }
93 }
94 
95 void KSquaresGame::tryEndGo()
96 {
97  kDebug() << "- - Trying to end go";
98  if (anotherGo)
99  {
100  if(gameInProgress)
101  {
102  kDebug() << "- - - Having another go";
103  kDebug() << "-";
104  anotherGo = false;
105  emit takeTurnSig(currentPlayer());
106  }
107  }
108  else
109  {
110  kDebug() << "- - - Go ending";
111  if (!currentPlayer()->isHuman())
112  {
113  emit highlightMove(lastLine);
114  }
115  nextPlayer();
116  }
117 }
118 
119 void KSquaresGame::resetEverything()
120 {
121  kDebug() << "Game Values Resetting";
122  numOfPlayers = 0;
123  players.resize(0);
124  lineList.clear();
125  squareOwnerTable.clear();
126  width = 0;
127  height = 0;
128  i_currentPlayerId = -1;
129  anotherGo = false;
130  gameInProgress = false;
131  lastLine = -1;
132 }
133 
134 void KSquaresGame::addLineToIndex(int index)
135 {
136  if (lineList[index] == true) //if there is already a line
137  {
138  kWarning() << "KSquaresGame::addLineToIndex():"
139  << "trying to add line already there!";
140  return;
141  }
142  lineList[index] = true;
143  lastLine = index;
144 
145  emit drawLine(index, Settings::lineColor());
146 
147  if (gameInProgress)
148  checkForNewSquares();
149 }
150 
151 void KSquaresGame::checkForNewSquares()
152 {
153  for(int i=0; i < (width*height); i++) //cycle through every box..
154  {
155  if (squareOwnerTable.at(i) == -1) //..checking it if there is no current owner
156  {
157  //indices of the lines surrounding the box; Correlates to "QVector<bool> lineList"
158  int index1 = (i/width) * ((2*width) + 1) + (i%width);
159  int index2 = index1 + width;
160  int index3 = index2 + 1;
161  int index4 = index3 + width;
162  //cout << index1 << "," << index2 << "," << index3 << "," << index4 << " - " << lineList.size() << endl;
163  if (lineList.at(index1) && lineList.at(index2) && lineList.at(index3) && lineList.at(index4))
164  {
165  kDebug() << "- - Square" << i << "completed.";
166  playerSquareComplete(i);
167  }
168  }
169  }
170  //emit lineDrawnSig();
171  tryEndGo();
172 }
173 
174 #include "ksquaresgame.moc"
QList::clear
void clear()
KSquaresGame::height
int height
Height of the game board.
Definition: ksquaresgame.h:133
KSquaresGame::numOfPlayers
int numOfPlayers
Number of players in this game.
Definition: ksquaresgame.h:129
KSquaresGame::createGame
void createGame(const QVector< KSquaresPlayer > &startPlayers, int startWidth, int startHeight)
Create a new game.
Definition: ksquaresgame.cpp:29
KSquaresGame::tryEndGo
void tryEndGo()
A line was drawn, see if the player gets another go.
Definition: ksquaresgame.cpp:95
KSquaresGame::KSquaresGame
KSquaresGame()
Constructor.
Definition: ksquaresgame.cpp:17
KSquaresGame::players
QVector< KSquaresPlayer > players
List of all the players in the game.
Definition: ksquaresgame.h:141
QVector::append
void append(const T &value)
KSquaresGame::lastLine
int lastLine
last line added
Definition: ksquaresgame.h:151
QList::at
const T & at(int i) const
KSquaresGame::gameOver
void gameOver(const QVector< KSquaresPlayer > &)
emitted when the game board is completed. Allows you to construct a scoreboard
KSquaresGame::resetEverything
void resetEverything()
Sets lots of things to zero, clears lists etc.
Definition: ksquaresgame.cpp:119
KSquaresGame::gameInProgress
bool gameInProgress
is there currently a game in progress
Definition: ksquaresgame.h:149
Settings::lineColor
static QColor lineColor()
Get Line Color.
Definition: settings.h:163
KSquaresGame::addLineToIndex
void addLineToIndex(int index)
Definition: ksquaresgame.cpp:134
QList::append
void append(const T &value)
QVector::resize
void resize(int size)
KSquaresGame::takeTurnSig
void takeTurnSig(KSquaresPlayer *)
A player's turn has started. This allows you to use AI/networking etc.
KSquaresPlayer::incScore
void incScore()
Increase the players score by 1.
Definition: ksquaresplayer.h:63
KSquaresGame::squareOwnerTable
QList< int > squareOwnerTable
List of the squares and their owners.
Definition: ksquaresgame.h:135
KSquaresGame::i_currentPlayerId
int i_currentPlayerId
Id of the current player.
Definition: ksquaresgame.h:145
ksquaresgame.h
KSquaresGame::highlightMove
void highlightMove(int)
Emitted when the last move in a series is played by the AI.
KSquaresGame::nextPlayer
int nextPlayer()
Moves play control to the next player, looping round when necessary.
Definition: ksquaresgame.cpp:61
KSquaresGame::~KSquaresGame
~KSquaresGame()
Definition: ksquaresgame.cpp:23
QVector::at
const T & at(int i) const
QVector< KSquaresPlayer >
settings.h
KSquaresGame::currentPlayer
KSquaresPlayer * currentPlayer()
Definition: ksquaresgame.h:80
KSquaresGame::playerSquareComplete
void playerSquareComplete(int index)
A player completed a square.
Definition: ksquaresgame.cpp:72
KSquaresGame::switchPlayer
void switchPlayer()
Externally determined player switch, for network game.
Definition: ksquaresgame.cpp:55
KSquaresGame::currentPlayerId
int currentPlayerId() const
Definition: ksquaresgame.h:76
KSquaresGame::drawSquare
void drawSquare(int, QColor)
Emits the index and colour of the square.
KSquaresGame::drawLine
void drawLine(int, QColor)
Emits the index and colour of the line.
KSquaresGame::anotherGo
bool anotherGo
Should the current player have another go.
Definition: ksquaresgame.h:147
KSquaresGame::checkForNewSquares
void checkForNewSquares()
Scans the board to see if any new squares were completed.
Definition: ksquaresgame.cpp:151
KSquaresGame::lineList
QList< bool > lineList
List of the lines and whether they're drawn.
Definition: ksquaresgame.h:137
KSquaresGame::width
int width
Width of the game board.
Definition: ksquaresgame.h:131
QVector::size
int size() const
KSquaresPlayer::name
QString name() const
Sets the players name.
Definition: ksquaresplayer.h:50
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