File indexing completed on 2024-04-14 04:01:09

0001 /*
0002     KShisen - A japanese game similar to Mahjongg
0003     SPDX-FileCopyrightText: 2016 Frederik Schwarzer <schwarzer@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // own
0009 #include "possiblemove.h"
0010 
0011 // KShisen
0012 #include "debug.h"
0013 
0014 namespace KShisen
0015 {
0016 PossibleMove::PossibleMove(Path & path)
0017     : m_path(path)
0018     , m_hasSlide(false)
0019     , m_slide()
0020 {
0021 }
0022 
0023 PossibleMove::PossibleMove(Path & path, Slide & slide)
0024     : m_path(path)
0025     , m_hasSlide(true)
0026     , m_slide(slide)
0027 {
0028 }
0029 
0030 bool PossibleMove::isInPath(TilePos tilePos) const
0031 {
0032     if (tilePos.x() == m_path.back().x() && tilePos.y() == m_path.back().y()) {
0033         return false;
0034     }
0035     qCDebug(KSHISEN_General) << "isInPath:" << tilePos.x() << "," << tilePos.y();
0036     Debug();
0037 
0038     // a path has at least 2 positions
0039     auto iter = m_path.cbegin();
0040     auto pathX = iter->x();
0041     auto pathY = iter->y();
0042     ++iter;
0043     for (; iter != m_path.cend(); ++iter) {
0044         // to fix
0045         if ((tilePos.x() == iter->x() && ((tilePos.y() > pathY && tilePos.y() <= iter->y())
0046                                           || (tilePos.y() < pathY && tilePos.y() >= iter->y())))
0047             || (tilePos.y() == iter->y() && ((tilePos.x() > pathX && tilePos.x() <= iter->x())
0048                                              || (tilePos.x() < pathX && tilePos.x() >= iter->x())))) {
0049             qCDebug(KSHISEN_General) << "isInPath:" << tilePos.x() << "," << tilePos.y() << "found in path" << pathX << "," << pathY << " => " << iter->x() << "," << iter->y();
0050             return true;
0051         }
0052         pathX = iter->x();
0053         pathY = iter->y();
0054     }
0055     return false;
0056 }
0057 
0058 void PossibleMove::Debug() const
0059 {
0060     qCDebug(KSHISEN_General) << "PossibleMove";
0061 
0062     for (auto const &iter : std::as_const(m_path)) {
0063         qCDebug(KSHISEN_General) << "    Path:" << iter.x() << "," << iter.y();
0064     }
0065 
0066     if (m_hasSlide) {
0067         qCDebug(KSHISEN_General) << "   hasSlide";
0068         for (auto const &iter : std::as_const(m_slide)) {
0069             qCDebug(KSHISEN_General) << "    Slide:" << iter.x() << "," << iter.y();
0070         }
0071     }
0072 }
0073 
0074 Path PossibleMove::path() const
0075 {
0076     return m_path;
0077 }
0078 
0079 bool PossibleMove::hasSlide() const
0080 {
0081     return m_hasSlide;
0082 }
0083 
0084 Slide PossibleMove::slide() const
0085 {
0086     return m_slide;
0087 }
0088 void PossibleMove::prependTile(TilePos const tilePos)
0089 {
0090     m_path.prepend(tilePos);
0091 }
0092 } // namespace KShisen
0093 
0094 // vim: expandtab:tabstop=4:shiftwidth=4
0095 // kate: space-indent on; indent-width 4