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

kapman

  • sources
  • kde-4.14
  • kdegames
  • kapman
maze.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3  * Copyright 2007-2008 Pierre-BenoĆ®t Besse <besse.pb@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "maze.h"
20 
21 #include <KDebug>
22 
23 #include <math.h>
24 
25 Maze::Maze() : m_totalNbElem(0), m_nbElem(0) {
26 
27 }
28 
29 Maze::~Maze() {
30  for(int i = 0 ; i < m_nbRows; ++i) {
31  delete[] m_cells[i];
32  }
33  delete[] m_cells;
34 }
35 
36 void Maze::init(const int p_nbRows, const int p_nbColumns) {
37  m_nbRows = p_nbRows;
38  m_nbColumns = p_nbColumns;
39  m_cells = new Cell*[m_nbRows];
40  for (int i = 0; i < m_nbRows; ++i) {
41  m_cells[i] = new Cell[m_nbColumns];
42  }
43 }
44 
45 void Maze::setCellType(const int p_row, const int p_column, const Cell::Type p_type) {
46  if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns) {
47  kError() << "Bad maze coordinates";
48  }
49  m_cells[p_row][p_column].setType(p_type);
50 }
51 
52 void Maze::setCellElement(const int p_row, const int p_column, Element * p_element) {
53  if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns) {
54  kError() << "Bad maze coordinates";
55  }
56  m_cells[p_row][p_column].setElement(p_element);
57  if (p_element != NULL) {
58  m_totalNbElem++;
59  m_nbElem++;
60  }
61 }
62 
63 void Maze::setResurrectionCell(QPoint p_resurrectionCell) {
64  // TODO : COORDINATES INVERTED, NEED TO CORRECT IT in the findPAth algorithm
65  m_resurrectionCell.setX(p_resurrectionCell.y());
66  m_resurrectionCell.setY(p_resurrectionCell.x());
67 }
68 
69 void Maze::decrementNbElem() {
70  m_nbElem--;
71  if (m_nbElem == 0) {
72  emit(allElementsEaten());
73  }
74 }
75 
76 void Maze::resetNbElem() {
77  m_nbElem = m_totalNbElem;
78 }
79 
80 QList<QPoint> Maze::getPathToGhostCamp(const int p_row, const int p_column) const {
81  QList<QPoint> path;
82  QList<QPoint> openList;
83  QList<QPoint> closedList;
84  QPoint currentCell;
85  QPoint tmpCell;
86  int lowestCost;
87  int icurrent = 0;
88  int oldSize = 0;
89 
90  // Initialize the starting cell
91  m_cells[p_row][p_column].setCost(abs(m_resurrectionCell.y() - p_row) + abs(m_resurrectionCell.x() - p_column));
92  // Add the starting cell to the openList
93  openList.append(QPoint(p_column, p_row));
94  // While the closed list does not contain the target cell
95  while (!closedList.contains(QPoint(m_resurrectionCell.x(), m_resurrectionCell.y())) && openList.size() != oldSize) {
96  // Look for the lowest cost cell on the open list
97  lowestCost = 1000;
98  for (int i = 0; i < openList.size(); ++i) {
99  if (m_cells[openList[i].y()][openList[i].x()].getCost() < lowestCost) {
100  lowestCost = m_cells[openList[i].y()][openList[i].x()].getCost();
101  currentCell = openList[i];
102  icurrent = i;
103  }
104  }
105  // Switch this cell to the closed list
106  closedList.append(currentCell);
107  openList.removeAt(icurrent);
108  oldSize = openList.size();
109  // For each of the 4 cells adjacent to the current node
110  // Left
111  tmpCell.setX(currentCell.x() - 1);
112  tmpCell.setY(currentCell.y());
113  if (m_cells[tmpCell.y()][tmpCell.x()].getType() != Cell::WALL) {
114  // If the cell is not in the closed list or the open list
115  if (!closedList.contains(tmpCell) && !openList.contains(tmpCell)) {
116  // Initialize the cell
117  m_cells[tmpCell.y()][tmpCell.x()].setCost(
118  abs(m_resurrectionCell.y() - tmpCell.y()) + abs(m_resurrectionCell.x() - (tmpCell.x())));
119  m_cells[tmpCell.y()][tmpCell.x()].setParent(&m_cells[currentCell.y()][currentCell.x()]);
120  // Add it to the open list
121  openList.append(tmpCell);
122  }
123  }
124  // Right
125  tmpCell.setX(currentCell.x() + 1);
126  tmpCell.setY(currentCell.y());
127  if (m_cells[tmpCell.y()][tmpCell.x()].getType() != Cell::WALL) {
128  // If the cell is not in the closed list or the open list
129  if (!closedList.contains(tmpCell) && !openList.contains(tmpCell)) {
130  // Initialize the cell
131  m_cells[tmpCell.y()][tmpCell.x()].setCost(
132  abs(m_resurrectionCell.y() - tmpCell.y()) + abs(m_resurrectionCell.x() - (tmpCell.x())));
133  m_cells[tmpCell.y()][tmpCell.x()].setParent(&m_cells[currentCell.y()][currentCell.x()]);
134  // Add it to the open list
135  openList.append(tmpCell);
136  }
137  }
138  // Top
139  tmpCell.setX(currentCell.x());
140  tmpCell.setY(currentCell.y() - 1);
141  if (m_cells[tmpCell.y()][tmpCell.x()].getType() != Cell::WALL) {
142  // If the cell is not in the closed list or the open list
143  if (!closedList.contains(tmpCell) && !openList.contains(tmpCell)) {
144  // Initialize the cell
145  m_cells[tmpCell.y()][tmpCell.x()].setCost(
146  abs(m_resurrectionCell.y() - tmpCell.y()) + abs(m_resurrectionCell.x() - (tmpCell.x())));
147  m_cells[tmpCell.y()][tmpCell.x()].setParent(&m_cells[currentCell.y()][currentCell.x()]);
148  // Add it to the open list
149  openList.append(tmpCell);
150  }
151  }
152  // Bottom
153  tmpCell.setX(currentCell.x());
154  tmpCell.setY(currentCell.y() + 1);
155  if (m_cells[tmpCell.y()][tmpCell.x()].getType() != Cell::WALL) {
156  // If the cell is not in the closed list or the open list
157  if (!closedList.contains(tmpCell) && !openList.contains(tmpCell)) {
158  // Initialize the cell
159  m_cells[tmpCell.y()][tmpCell.x()].setCost(
160  abs(m_resurrectionCell.y() - tmpCell.y()) + abs(m_resurrectionCell.x() - (tmpCell.x())));
161  m_cells[tmpCell.y()][tmpCell.x()].setParent(&m_cells[currentCell.y()][currentCell.x()]);
162  // Add it to the open list
163  openList.append(tmpCell);
164  }
165  }
166  }
167  if (oldSize == openList.size()) {
168  kError() << "Path to ghost home not found";
169  return QList<QPoint>();
170  }
171  // Save the path : from the target cell, go from each cell to its parent cell until reaching the starting cell
172  for (Cell* cell = &m_cells[m_resurrectionCell.y()][m_resurrectionCell.x()];
173  cell->getParent() != &m_cells[p_row][p_column]; cell = cell->getParent()) {
174  path.prepend(getCoords(cell));
175  }
176 
177  return path;
178 }
179 
180 Cell Maze::getCell(const int p_row, const int p_column) const {
181  if (p_row < 0 || p_row >= m_nbRows ||
182  p_column < 0 || p_column >= m_nbColumns) {
183  kError() << "Bad maze coordinates";
184  }
185  return m_cells[p_row][p_column];
186 }
187 
188 QPoint Maze::getCoords(Cell* p_cell) const {
189  for (int i = 0; i < m_nbRows; ++i) {
190  for (int j = 0; j < m_nbColumns; ++j) {
191  if (&m_cells[i][j] == p_cell) {
192  return QPoint(j, i);
193  }
194  }
195  }
196  return QPoint();
197 }
198 
199 int Maze::getRowFromY(const qreal p_y) const {
200  return (int)(p_y / Cell::SIZE);
201 }
202 
203 int Maze::getColFromX(const qreal p_x) const {
204  return (int)(p_x / Cell::SIZE);
205 }
206 
207 int Maze::getNbColumns() const {
208  return m_nbColumns;
209 }
210 
211 int Maze::getNbRows() const {
212  return m_nbRows;
213 }
214 
215 int Maze::getNbElem() const {
216  return m_nbElem;
217 }
218 
219 int Maze::getTotalNbElem() const {
220  return m_totalNbElem;
221 }
222 
223 QPoint Maze::getResurrectionCell() const {
224  return m_resurrectionCell;
225 }
Maze::init
void init(const int p_nbRows, const int p_nbColumns)
Creates the Maze matrix.
Definition: maze.cpp:36
Cell::setElement
void setElement(Element *p_element)
Sets the Element that is on the Cell.
Definition: cell.cpp:43
Cell::WALL
Definition: cell.h:37
Maze::getPathToGhostCamp
QList< QPoint > getPathToGhostCamp(const int p_row, const int p_column) const
Gets the path, as a list of Cell coordinates, to go to the Ghost camp from the Cell whose coordinates...
Definition: maze.cpp:80
Cell
This class represents a Cell of the Maze.
Definition: cell.h:28
Maze::getCell
Cell getCell(const int p_row, const int p_column) const
Gets the Cell at the given coordinates.
Definition: maze.cpp:180
Cell::getType
Type getType() const
Gets the Cell type.
Definition: cell.cpp:31
Maze::getNbRows
int getNbRows() const
Gets the number of rows of the Maze.
Definition: maze.cpp:211
QList::removeAt
void removeAt(int i)
Cell::getParent
Cell * getParent() const
Gets the parent Cell of this Cell for A* pathfinding algorithm.
Definition: cell.cpp:55
Cell::getCost
int getCost() const
Gets the Cell cost for A* pathfinding algorithm.
Definition: cell.cpp:47
QPoint
Maze::Maze
Maze()
Creates a new Maze instance.
Definition: maze.cpp:25
QPoint::x
int x() const
QPoint::y
int y() const
QList::size
int size() const
Maze::decrementNbElem
void decrementNbElem()
Decrements the number of remaining Elements.
Definition: maze.cpp:69
Maze::setCellElement
void setCellElement(const int p_row, const int p_column, Element *p_element)
Sets the Element that is on the Cell whose coordinates are given in parameters.
Definition: maze.cpp:52
Cell::setCost
void setCost(const int p_cost)
Sets a cost for the Cell, for A* pathfinding algorithm.
Definition: cell.cpp:51
Maze::resetNbElem
void resetNbElem()
Resets the number of remaining Elements to the initial number.
Definition: maze.cpp:76
Maze::getCoords
QPoint getCoords(Cell *p_cell) const
Gets the coordinates of the given Cell as a QPoint.
Definition: maze.cpp:188
Cell::SIZE
static const qreal SIZE
The Cell side size.
Definition: cell.h:33
Maze::setResurrectionCell
void setResurrectionCell(QPoint p_resurrectionCell)
Sets the cell on witch the ghosts resurrect from prey state.
Definition: maze.cpp:63
QList::append
void append(const T &value)
maze.h
QList< QPoint >
Maze::getNbColumns
int getNbColumns() const
Gets the number of columns of the Maze.
Definition: maze.cpp:207
Element
This class describes the common characteristics and behaviour of any game Element (character or item)...
Definition: element.h:30
Maze::setCellType
void setCellType(const int p_row, const int p_column, const Cell::Type p_type)
Sets the CellType of the Cell whose coordinates are given in parameters.
Definition: maze.cpp:45
QList::contains
bool contains(const T &value) const
Maze::~Maze
~Maze()
Deletes the Maze instance.
Definition: maze.cpp:29
Cell::setParent
void setParent(Cell *p_parent)
Sets the parent Cell of this Cell for A* pathfinding algorithm.
Definition: cell.cpp:59
Cell::setType
void setType(Type p_type)
Sets the Cell type.
Definition: cell.cpp:35
QPoint::setX
void setX(int x)
QPoint::setY
void setY(int y)
QList::prepend
void prepend(const T &value)
Maze::getTotalNbElem
int getTotalNbElem() const
Gets the number of Elements initially on the Maze.
Definition: maze.cpp:219
Cell::Type
Type
The Cell possible types.
Definition: cell.h:36
Maze::getNbElem
int getNbElem() const
Gets the number of remaining Elements still on the Maze.
Definition: maze.cpp:215
Maze::allElementsEaten
void allElementsEaten()
Emitted when all the elements on the Maze have been eaten.
Maze::getRowFromY
int getRowFromY(const qreal p_y) const
Gets the row index corresponding to the given y-coordinate.
Definition: maze.cpp:199
Maze::getColFromX
int getColFromX(const qreal p_x) const
Gets the column index corresponding to the given x-coordinate.
Definition: maze.cpp:203
Maze::getResurrectionCell
QPoint getResurrectionCell() const
Gets the cell on witch the ghosts resurects.
Definition: maze.cpp:223
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kapman

Skip menu "kapman"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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