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

granatier

  • sources
  • kde-4.14
  • kdegames
  • granatier
  • src
arena.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Mathias Kraus <k.hias@gmx.de>
3  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
4  * Copyright 2007-2008 Pierre-BenoĆ®t Besse <besse.pb@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "arena.h"
21 
22 #include <QPoint>
23 #include <QDateTime>
24 
25 #include <cstdlib>
26 
27 Arena::Arena()
28 {
29 }
30 
31 Arena::~Arena()
32 {
33  for(int i = 0 ; i < m_nbRows; ++i)
34  {
35  delete[] m_cells[i];
36  }
37  delete[] m_cells;
38 }
39 
40 void Arena::init(const int p_nbRows, const int p_nbColumns)
41 {
42  m_nbRows = p_nbRows;
43  m_nbColumns = p_nbColumns;
44  m_cells = new Cell*[m_nbRows];
45  for (int i = 0; i < m_nbRows; ++i)
46  {
47  m_cells[i] = new Cell[m_nbColumns];
48  }
49 
50  m_emptyCell.setType(Granatier::Cell::HOLE);
51 }
52 
53 QString Arena::getName () const
54 {
55  return m_strArenaName;
56 }
57 
58 void Arena::setName (const QString &p_strArenaName)
59 {
60  m_strArenaName = p_strArenaName;
61 }
62 
63 void Arena::setCellType(const int p_row, const int p_column, const Granatier::Cell::Type p_type)
64 {
65  if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
66  {
67  return;
68  }
69  m_cells[p_row][p_column].setType(p_type);
70 }
71 
72 void Arena::setCellElement(const int p_row, const int p_column, Element * p_element)
73 {
74  if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
75  {
76  return;
77  }
78  m_cells[p_row][p_column].setElement(p_element);
79 }
80 
81 void Arena::removeCellElement(const int p_row, const int p_column, Element * p_element)
82 {
83  if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
84  {
85  return;
86  }
87  m_cells[p_row][p_column].removeElement(p_element);
88 }
89 
90 void Arena::addPlayerPosition(const QPointF &p_position)
91 {
92  m_playerPosition.append(p_position);
93 
94  //TODO: maybe in own function
95  int nShuffle;
96  for (int i = 0; i < m_playerPosition.size(); i++)
97  {
98  nShuffle = m_playerPosition.size() * (qrand()/1.0)/RAND_MAX;
99  if(nShuffle >= m_playerPosition.size())
100  {
101  nShuffle = m_playerPosition.size() - 1;
102  }
103  else if(nShuffle < 0)
104  {
105  nShuffle = 0;
106  }
107  m_playerPosition.swap(i, nShuffle);
108  }
109 }
110 
111 QPointF Arena::getPlayerPosition(int p_player) const
112 {
113  if(m_playerPosition.count() > p_player)
114  {
115  return m_playerPosition.at(p_player);
116  }
117  return m_playerPosition.at(0); //to have a valid position
118 }
119 
120 Cell Arena::getCell(const int p_row, const int p_column) const
121 {
122  if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
123  {
124  return m_emptyCell;
125  }
126  return m_cells[p_row][p_column];
127 }
128 
129 QPoint Arena::getCoords(Cell* p_cell) const
130 {
131  for (int i = 0; i < m_nbRows; ++i)
132  {
133  for (int j = 0; j < m_nbColumns; ++j)
134  {
135  if (&m_cells[i][j] == p_cell)
136  {
137  return QPoint(j, i);
138  }
139  }
140  }
141  return QPoint();
142 }
143 
144 int Arena::getRowFromY(const qreal p_y) const
145 {
146  int nRow = (p_y / Granatier::CellSize);
147  if (p_y < 0)
148  {
149  nRow -= 1;
150  }
151  return nRow;
152 }
153 
154 int Arena::getColFromX(const qreal p_x) const
155 {
156  int nCol = (p_x / Granatier::CellSize);
157  if (p_x < 0)
158  {
159  nCol -= 1;
160  }
161  return nCol;
162 }
163 
164 int Arena::getNbColumns() const
165 {
166  return m_nbColumns;
167 }
168 
169 int Arena::getNbRows() const
170 {
171  return m_nbRows;
172 }
Cell::setElement
void setElement(Element *p_element)
Sets the Element that is on the Cell.
Definition: cell.cpp:80
Cell
This class represents a Cell of the Arena.
Definition: cell.h:29
Arena::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: arena.cpp:72
Arena::getNbColumns
int getNbColumns() const
Gets the number of columns of the Arena.
Definition: arena.cpp:164
QList::at
const T & at(int i) const
QPoint
Arena::removeCellElement
void removeCellElement(const int p_row, const int p_column, Element *p_element)
Removes the Element that is on the Cell whose coordinates are given in parameters.
Definition: arena.cpp:81
QList::size
int size() const
QPointF
QList::count
int count(const T &value) const
Granatier::CellSize
const qreal CellSize
The Cell size.
Definition: granatierglobals.h:31
QList::append
void append(const T &value)
Arena::init
void init(const int p_nbRows, const int p_nbColumns)
Creates the Arena matrix.
Definition: arena.cpp:40
Arena::Arena
Arena()
Creates a new Arena instance.
Definition: arena.cpp:27
Arena::setName
void setName(const QString &p_strArenaName)
Sets the Arena name.
Definition: arena.cpp:58
Arena::~Arena
~Arena()
Deletes the Arena instance.
Definition: arena.cpp:31
QString
Arena::getCoords
QPoint getCoords(Cell *p_cell) const
Gets the coordinates of the given Cell as a QPoint.
Definition: arena.cpp:129
Element
This class describes the common characteristics and behaviour of any game Element (character or item)...
Definition: element.h:32
Arena::getName
QString getName() const
Returns the Arean name.
Definition: arena.cpp:53
Arena::addPlayerPosition
void addPlayerPosition(const QPointF &p_position)
Sets a player position on the arena.
Definition: arena.cpp:90
Cell::setType
void setType(Granatier::Cell::Type p_type)
Sets the Cell type.
Definition: cell.cpp:57
Arena::getPlayerPosition
QPointF getPlayerPosition(int p_player) const
Gets the player position on the arena.
Definition: arena.cpp:111
QList::swap
void swap(QList< T > &other)
Granatier::Cell::Type
Type
Definition: granatierglobals.h:71
Arena::getColFromX
int getColFromX(const qreal p_x) const
Gets the column index corresponding to the given x-coordinate.
Definition: arena.cpp:154
arena.h
Granatier::Cell::HOLE
Definition: granatierglobals.h:73
Arena::setCellType
void setCellType(const int p_row, const int p_column, const Granatier::Cell::Type p_type)
Sets the CellType of the Cell whose coordinates are given in parameters.
Definition: arena.cpp:63
Arena::getNbRows
int getNbRows() const
Gets the number of rows of the Arena.
Definition: arena.cpp:169
Cell::removeElement
void removeElement(Element *p_element)
Removes the Element that is on the Cell.
Definition: cell.cpp:88
Arena::getCell
Cell getCell(const int p_row, const int p_column) const
Gets the Cell at the given coordinates.
Definition: arena.cpp:120
Arena::getRowFromY
int getRowFromY(const qreal p_y) const
Gets the row index corresponding to the given y-coordinate.
Definition: arena.cpp:144
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

granatier

Skip menu "granatier"
  • Main Page
  • Namespace List
  • Namespace Members
  • 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