File indexing completed on 2024-05-12 04:04:14

0001 /*
0002     This file is part of Knights, a chess board for KDE SC 4.
0003     SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "pos.h"
0009 #include "board.h"
0010 
0011 #include <QDebug>
0012 
0013 namespace Knights {
0014 const QString Pos::rowNames = QStringLiteral ( "abcdefgh" );
0015 
0016 
0017 QChar Pos::row ( int num ) {
0018     if ( num > 0 && num < 9 )
0019         return rowNames[num - 1];
0020     else
0021         return QLatin1Char ( 'r' );
0022 }
0023 
0024 int Pos::numFromRow ( const QChar& row ) {
0025     return rowNames.indexOf ( row ) + 1;
0026 }
0027 
0028 Pos::Pos() {
0029 
0030 }
0031 
0032 Pos::Pos ( const int& t1, const int& t2 ) : QPair< int, int > ( t1, t2 ) {
0033 
0034 }
0035 
0036 Pos::Pos ( const QString &string ) {
0037     if ( string.size() == 2 ) {
0038         first = numFromRow ( string.at ( 0 ) );
0039         second = string.right ( 1 ).toInt();
0040     }
0041 }
0042 
0043 Pos::~Pos() = default;
0044 
0045 QString Pos::string() const {
0046     if ( isValid() )
0047         return row ( first ) + QString::number ( second );
0048     else
0049         return QString();
0050 }
0051 
0052 bool Pos::isValid() const {
0053     return first > 0 && first < 9 && second > 0 && second < 9;
0054 }
0055 
0056 const Pos& Pos::operator+= ( const Pos & other ) {
0057     first += other.first;
0058     second += other.second;
0059     return *this;
0060 }
0061 
0062 Pos operator+ ( const Pos& one, const Pos& other ) {
0063     return Pos ( one.first + other.first, one.second + other.second );
0064 }
0065 
0066 Pos operator- ( const Pos& one, const Pos& other ) {
0067     return Pos ( one.first - other.first, one.second - other.second );
0068 }
0069 
0070 Pos operator* ( int m, const Pos& other ) {
0071     return Pos ( m*other.first, m*other.second );
0072 }
0073 
0074 Pos operator/ ( const Pos& other, int m ) {
0075     return Pos ( other.first / m, other.second / m );
0076 }
0077 }
0078 
0079 QDebug operator<< ( QDebug debug, const Knights::Pos& pos ) {
0080     debug.nospace() << Knights::Pos::row ( pos.first ) << pos.second;
0081     return debug;
0082 }