1 | /********************************************************************************** |
2 | This file is part of the game 'KTron' |
3 | |
4 | Copyright (C) 1998-2000 by Matthias Kiefer <matthias.kiefer@gmx.de> |
5 | Copyright (C) 2005 Benjamin C. Meyer <ben at meyerhome dot net> |
6 | Copyright (C) 2008-2009 Stas Verberkt <legolas at legolasweb dot nl> |
7 | |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | |
22 | *******************************************************************************/ |
23 | |
24 | #include "playfield.h" |
25 | |
26 | #include <KDebug> |
27 | |
28 | PlayField::PlayField() |
29 | { |
30 | m_width = TRON_PLAYFIELD_WIDTH; |
31 | m_height = TRON_PLAYFIELD_HEIGHT; |
32 | |
33 | m_playfield.resize(m_width * m_height); |
34 | initialize(); |
35 | } |
36 | |
37 | void PlayField::initialize() |
38 | { |
39 | int i, j; |
40 | for (i = 0; i < m_width; ++i) { |
41 | for (j = 0; j < m_height; ++j) { |
42 | Object newObj = Object(); |
43 | this->setObjectAt(i, j, newObj); |
44 | } |
45 | } |
46 | } |
47 | |
48 | // |
49 | // Methods for retrieval |
50 | // |
51 | |
52 | Object *PlayField::getObjectAt(int x, int y) |
53 | { |
54 | if (x < 0 || x >= m_width || y < 0 || y >= m_height) { |
55 | kDebug() << "Inexistent place accessed: (" << x << ", " << y << ")" ; |
56 | |
57 | return 0; |
58 | } |
59 | |
60 | return &m_playfield[x * m_height + y]; |
61 | } |
62 | |
63 | int PlayField::getWidth() |
64 | { |
65 | return m_width; |
66 | } |
67 | |
68 | int PlayField::getHeight() |
69 | { |
70 | return m_height; |
71 | } |
72 | |
73 | // |
74 | // Methods for setting |
75 | // |
76 | void PlayField::setObjectAt(int x, int y, Object &o) |
77 | { |
78 | if (x < 0 || x >= m_width || y < 0 || y >= m_height) { |
79 | kDebug() << "Inexistent place accessed: (" << x << ", " << y << ")" ; |
80 | |
81 | return; |
82 | } |
83 | |
84 | m_playfield[x * m_height + y] = o; |
85 | o.setCoordinates(x, y); |
86 | } |
87 | |