00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <string>
00028 #include "coord.h"
00029 #include "board.h"
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
00119
00120
00121
00122
00123