File indexing completed on 2024-04-28 04:04:39

0001 /*
0002     This file is part of the game 'KTron'
0003 
0004     SPDX-FileCopyrightText: 1998-2000 Matthias Kiefer <matthias.kiefer@gmx.de>
0005     SPDX-FileCopyrightText: 2005 Benjamin C. Meyer <ben at meyerhome dot net>
0006     SPDX-FileCopyrightText: 2008-2009 Stas Verberkt <legolas at legolasweb dot nl>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 
0010 */
0011   
0012 #include "playfield.h"
0013 
0014 #include "ksnakeduel_debug.h"
0015 
0016 PlayField::PlayField()
0017 {
0018     m_width = TRON_PLAYFIELD_WIDTH;
0019     m_height = TRON_PLAYFIELD_HEIGHT;
0020     
0021     m_playfield.resize(m_width * m_height);
0022     initialize();
0023 }
0024 
0025 void PlayField::initialize()
0026 {   
0027     int i, j;
0028     for (i = 0; i < m_width; ++i) {
0029         for (j = 0; j < m_height; ++j) {
0030             Object newObj = Object();
0031             this->setObjectAt(i, j, newObj);
0032         }
0033     }
0034 }
0035 
0036 //
0037 // Methods for retrieval
0038 //
0039 
0040 Object *PlayField::getObjectAt(int x, int y)
0041 {
0042     if (x < 0 || x >= m_width || y < 0 || y >= m_height) {
0043         qCDebug(KSNAKEDUEL_LOG) << "Inexistent place accessed: (" << x << ", " << y << ")";
0044 
0045         return nullptr;
0046     }
0047 
0048     return &m_playfield[x * m_height + y];
0049 }
0050 
0051 int PlayField::getWidth()
0052 {
0053     return m_width;
0054 }
0055 
0056 int PlayField::getHeight()
0057 {
0058     return m_height;
0059 }
0060 
0061 //
0062 // Methods for setting
0063 //
0064 void PlayField::setObjectAt(int x, int y, Object &o)
0065 {
0066     if (x < 0 || x >= m_width || y < 0 || y >= m_height) {
0067         qCDebug(KSNAKEDUEL_LOG) << "Inexistent place accessed: (" << x << ", " << y << ")";
0068 
0069         return;
0070     }
0071     
0072     m_playfield[x * m_height + y] = o;
0073     o.setCoordinates(x, y);
0074 }