coord.cpp

Go to the documentation of this file.
00001 /* Eliot                                                                     */
00002 /* Copyright (C) 1999  Antoine Fraboulet                                     */
00003 /*                                                                           */
00004 /* This file is part of Eliot.                                               */
00005 /*                                                                           */
00006 /* Eliot is free software; you can redistribute it and/or modify             */
00007 /* it under the terms of the GNU General Public License as published by      */
00008 /* the Free Software Foundation; either version 2 of the License, or         */
00009 /* (at your option) any later version.                                       */
00010 /*                                                                           */
00011 /* Eliot is distributed in the hope that it will be useful,                  */
00012 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
00013 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
00014 /* GNU General Public License for more details.                              */
00015 /*                                                                           */
00016 /* You should have received a copy of the GNU General Public License         */
00017 /* along with this program; if not, write to the Free Software               */
00018 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00019 
00020 /**
00021  *  \file   coord.cpp
00022  *  \brief  Board coordinate system
00023  *  \author Antoine Fraboulet
00024  *  \date   2005
00025  */
00026 
00027 #include <string>
00028 #include "coord.h"
00029 #include "board.h" // for BOARD_MIN and BOARD_MAX (TODO: remove this include)
00030 #include "debug.h"
00031 
00032 
00033 Coord::Coord(int iRow, int iCol, Direction iDir)
00034 {
00035     m_row = iRow;
00036     m_col = iCol;
00037     m_dir = iDir;
00038 }
00039 
00040 Coord::Coord(const string &iStr)
00041 {
00042     setFromString(iStr);
00043 }
00044 
00045 bool Coord::isValid() const
00046 {
00047     return (m_row >= BOARD_MIN && m_row <= BOARD_MAX &&
00048             m_col >= BOARD_MIN && m_col <= BOARD_MAX);
00049 }
00050 
00051 void Coord::operator=(const Coord &iOther)
00052 {
00053     m_dir = iOther.m_dir;
00054     m_row = iOther.m_row;
00055     m_col = iOther.m_col;
00056 }
00057 
00058 void Coord::swap()
00059 {
00060     int tmp = m_col;
00061     m_col = m_row;
00062     m_row = tmp;
00063 }
00064 
00065 void Coord::setFromString(const string &iStr)
00066 {
00067     char l[4];
00068     int col;
00069 
00070     if (sscanf(iStr.c_str(), "%1[a-oA-O]%2d", l, &col) == 2)
00071     {
00072         setDir(HORIZONTAL);
00073     }
00074     else if (sscanf(iStr.c_str(), "%2d%1[a-oA-O]", &col, l) == 2)
00075     {
00076         setDir(VERTICAL);
00077     }
00078     else
00079     {
00080         col = -1;
00081         l[0] = 'A' - 1;
00082     }
00083     int row = toupper(*l) - 'A' + 1;
00084     setCol(col);
00085     setRow(row);
00086 }
00087 
00088 string Coord::toString(coord_mode_t mode) const
00089 {
00090     ASSERT(isValid(), "Invalid coordinates");
00091 
00092     char res[7];
00093     char srow[3];
00094     char scol[3];
00095 
00096     sprintf(scol, "%d", m_col);
00097     sprintf(srow, "%c", m_row + 'A' - 1);
00098 
00099     switch (mode)
00100     {
00101     case COORD_MODE_COMPACT:
00102         if (getDir() == HORIZONTAL)
00103             sprintf(res,"%s%s",srow,scol);
00104         else
00105             sprintf(res,"%s%s",scol,srow);
00106         break;
00107     case COORD_MODE_LONG:
00108         if (getDir() == HORIZONTAL)
00109             sprintf(res,"%2s %2s",srow,scol);
00110         else
00111             sprintf(res,"%2s %2s",scol,srow);
00112         break;
00113     }
00114 
00115     return string(res);
00116 }
00117 
00118 /// Local Variables:
00119 /// mode: c++
00120 /// mode: hs-minor
00121 /// c-basic-offset: 4
00122 /// indent-tabs-mode: nil
00123 /// End:

Generated on Thu Dec 29 02:01:14 2005 for Eliot by  doxygen 1.4.5