diff --git a/game/board.cpp b/game/board.cpp index ebe25d4..e96c19f 100644 --- a/game/board.cpp +++ b/game/board.cpp @@ -104,6 +104,27 @@ Board::Board(): } +char Board::getChar(int iRow, int iCol) const +{ + char letter = 0; + Tile tile = getTile(iRow, iCol); + if (!tile.isEmpty()) + { + letter = tile.toChar(); + if (isJoker(iRow, iCol)) + letter = tolower(letter); + } + return letter; +} + +int Board::getCharAttr(int iRow, int iCol) const +{ + int t = getTestChar(iRow, iCol); + int j = isJoker(iRow, iCol); + return (t << 1) | j; +} + + Tile Board::getTile(int iRow, int iCol) const { return m_tilesRow[iRow][iCol]; diff --git a/game/board.h b/game/board.h index 1bc1fe4..f66d5fd 100644 --- a/game/board.h +++ b/game/board.h @@ -63,10 +63,29 @@ public: Board(); virtual ~Board() {} + /************************* + * Coordinates have to be BOARD_MIN <= int <= BOARD_MAX + * + * getChar returns an upper case letter for normal tiles and a + * lower case letter for jokers. + * + * getCharAttr tells the attributes of the tile + * 0 : normal played tile + * 1 : joker tile + * 2 : test tile for preview purpose + * Attributes can be combined with the or (|) operator + *************************/ +#define ATTR_NORMAL 0 +#define ATTR_JOKER 1 +#define ATTR_TEST 2 + + char getChar (int iRow, int iCol) const; + int getCharAttr(int iRow, int iCol) const; + Tile getTile(int iRow, int iCol) const; bool isJoker(int iRow, int iCol) const; bool isVacant(int iRow, int iCol) const; - /*int score(Round);*/ + void addRound(const Dictionary &iDic, const Round &iRound); void removeRound(const Dictionary &iDic, const Round &iRound); int checkRound(Round &iRound, bool iFirstTurn); diff --git a/game/coord.cpp b/game/coord.cpp index 37c01ba..1415eb7 100644 --- a/game/coord.cpp +++ b/game/coord.cpp @@ -27,81 +27,104 @@ #include #include "coord.h" + Coord::Coord() { - m_row = 1; - m_col = 1; - m_dir = HORIZONTAL; + m_row = 1; + m_col = 1; + m_dir = HORIZONTAL; +} + +Coord::Coord(const string &iStr) +{ + m_row = 1; + m_col = 1; + setFromString(iStr); } Coord::~Coord() { } -Direction -Coord::getDir() const +Direction Coord::getDir() const { return m_dir; } -int -Coord::getRow() const +int Coord::getRow() const { return m_row; } -int -Coord::getCol() const +int Coord::getCol() const { return m_col; } -void -Coord::setRow(int iRow) +void Coord::setRow(int iRow) { m_row = iRow; } -void -Coord::setCol(int iCol) +void Coord::setCol(int iCol) { m_col = iCol; } -void -Coord::setDir(Direction iDir) +void Coord::setDir(Direction iDir) { m_dir = iDir; } -void -Coord::operator=(const Coord &iOther) +void Coord::operator=(const Coord &iOther) { m_dir = iOther.m_dir; m_row = iOther.m_row; m_col = iOther.m_col; } -std::string -Coord::toString() const +void Coord::setFromString(const string &iStr) { - std::string rs; + char l[4]; + int col; + + if (sscanf(iStr.c_str(), "%1[a-oA-O]%2d", l, &col) == 2) + { + setDir(HORIZONTAL); + } + else if (sscanf(iStr.c_str(), "%2d%1[a-oA-O]", &col, l) == 2) + { + setDir(VERTICAL); + } + else + { + col = 1; + l[0] = 'A'; + } + int row = toupper(*l) - 'A' + 1; + setCol(col); + setRow(row); +} + +string Coord::toString() const +{ + string rs; if (getDir() == HORIZONTAL) { char s[5]; sprintf(s, "%d", m_col); - rs = std::string(1, m_row + 'A' - 1) + s; + rs = string(1, m_row + 'A' - 1) + s; } else { char s[5]; sprintf(s, "%d", m_col); - rs = s + std::string(1, m_row + 'A' - 1); + rs = s + string(1, m_row + 'A' - 1); } return rs; } - + /// Local Variables: /// mode: hs-minor /// c-basic-offset: 4 diff --git a/game/coord.h b/game/coord.h index af00cb2..cce588b 100644 --- a/game/coord.h +++ b/game/coord.h @@ -27,6 +27,8 @@ #ifndef _COORD_H #define _COORD_H +using std::string; + class Coord { public: @@ -34,7 +36,7 @@ public: enum Direction {VERTICAL, HORIZONTAL}; Coord(); - Coord(const std::string& iCoord); + Coord(const string &iStr); virtual ~Coord(); void setRow(int iRow); @@ -46,9 +48,10 @@ public: int getCol() const; void operator=(const Coord &iOther); - std::string toString() const; + void setFromString(const string &iStr); + string toString() const; - private: +private: Direction m_dir; int m_row, m_col; diff --git a/game/game.cpp b/game/game.cpp index e44e0b9..084938f 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -454,51 +454,15 @@ int Game::back(int n) m_roundHistory.pop_back(); m_playerHistory.pop_back(); } - else - { + else + { return 1; - } + } } return 0; } -/********************************************************* - *********************************************************/ - -char Game::getBoardChar(int iRow, int iCol) const -{ - char letter = 0; - Tile tile = m_board.getTile(iRow, iCol); - if (!tile.isEmpty()) - { - letter = tile.toChar(); - if (m_board.isJoker(iRow, iCol)) - letter = tolower(letter); - } - return letter; -} - - -int Game::getBoardCharAttr(int iRow, int iCol) const -{ - int t = m_board.getTestChar(iRow, iCol); - int j = m_board.isJoker(iRow, iCol); - return (t << 1) | j; -} - - -int Game::getBoardWordMultiplier(int iRow, int iCol) const -{ - return m_board.getWordMultiplier(iRow, iCol); -} - - -int Game::getBoardLetterMultiplier(int iRow, int iCol) const -{ - return m_board.getLetterMultiplier(iRow, iCol); -} - /********************************************************* *********************************************************/ @@ -751,12 +715,6 @@ int Game::helperSetRackManual(int p, bool iCheck, const string &iLetters) /********************************************************* *********************************************************/ -int Game::getNCharInBag(const Tile &c) const -{ - return m_bag.in(c); -} - - string Game::formatCoords(const Round &iRound) const { if (iRound.getDir() == HORIZONTAL) diff --git a/game/game.h b/game/game.h index 3063fe3..bbf309f 100644 --- a/game/game.h +++ b/game/game.h @@ -41,14 +41,6 @@ using namespace std; *************************/ #define IDENT_STRING "Eliot" -/************************* - * Dimensions of the board, the tiles placed on - * the board can be accessed via getBoardChar() - *************************/ -#define BOARD_MIN 1 -#define BOARD_MAX 15 - - /** * Parent class of all the Game types. * It offers the common attributes (Board, Bag, etc...) as well as useful @@ -77,6 +69,9 @@ public: kJOKER // Joker game }; + const Board& getBoard() const { return m_board; } + const Bag& getBag() const { return m_bag; } + /** * Accessors for the variant of the game. * The variant can be changed during a game without any problem @@ -108,28 +103,6 @@ public: *************************/ int back(int); - /************************* - * int coordinates have to be BOARD_MIN <= int <= BOARD_MAX - * - * getBoardChar returns an upper case letter - * for normal tiles and a lower case letter for jokers. - * - * getBoardCharAttr tells the attributes of the tile - * 0 : normal played tile - * 1 : joker tile - * 2 : test tile for preview purpose - * attributes can be combined with the or (|) operator - *************************/ -#define ATTR_NORMAL 0 -#define ATTR_JOKER 1 -#define ATTR_TEST 2 - - char getBoardChar (int iRow, int iCol) const; - int getBoardCharAttr(int iRow, int iCol) const; - - int getBoardWordMultiplier (int iRow, int iCol) const; - int getBoardLetterMultiplier(int iRow, int iCol) const; - /************************* * Set the rack for searching * @@ -149,13 +122,6 @@ public: static const int RACK_SIZE; typedef enum {RACK_ALL, RACK_NEW} set_rack_mode; - /************************* - * Get the number of tiles available in the bag. - * The parameter has to be - * 'a' <= char <= 'z' or 'A' <= char <= 'Z' or '?' - *************************/ - int getNCharInBag(const Tile&) const; - /** * Methods to access already played words. * The int parameter should be 0 <= int < getNRounds() diff --git a/game/turn.cpp b/game/turn.cpp index 2c03dd0..f00471a 100644 --- a/game/turn.cpp +++ b/game/turn.cpp @@ -90,8 +90,7 @@ Round Turn::getRound() const } -void -Turn::operator=(const Turn &iOther) +void Turn::operator=(const Turn &iOther) { num = iOther.num; pldrack = iOther.pldrack; @@ -99,20 +98,18 @@ Turn::operator=(const Turn &iOther) } -string -Turn::toString(bool showExtraSigns) const +string Turn::toString(bool showExtraSigns) const { string rs = ""; if (showExtraSigns) - { - rs = ""; // TODO - } + { + rs = ""; // TODO + } rs = rs + pldrack.toString() + " " + round.toString(); return rs; } - /// Local Variables: /// mode: hs-minor /// c-basic-offset: 4 diff --git a/utils/game_io.cpp b/utils/game_io.cpp index 4b43c4a..2962f9f 100644 --- a/utils/game_io.cpp +++ b/utils/game_io.cpp @@ -41,7 +41,7 @@ void GameIO::printBoard(ostream &out, const Game &iGame) out << " " << (char)(row - BOARD_MIN + 'A') << " "; for (col = BOARD_MIN; col <= BOARD_MAX; col++) { - char l = iGame.getBoardChar(row, col); + char l = iGame.getBoard().getChar(row, col); out << setw(3) << (l ? l : '-'); } out << endl; @@ -63,8 +63,8 @@ void GameIO::printBoardJoker(ostream &out, const Game &iGame) out << " " << (char)(row - BOARD_MIN + 'A') << " "; for (col = BOARD_MIN; col <= BOARD_MAX; col++) { - char l = iGame.getBoardChar(row, col); - bool j = (iGame.getBoardCharAttr(row, col) & ATTR_JOKER); + char l = iGame.getBoard().getChar(row, col); + bool j = (iGame.getBoard().getCharAttr(row, col) & ATTR_JOKER); out << " " << (j ? '.' : (l ? ' ' : '-')); out << (l ? l : '-'); @@ -88,13 +88,13 @@ void GameIO::printBoardMultipliers(ostream &out, const Game &iGame) out << " " << (char)(row - BOARD_MIN + 'A') << " "; for (col = BOARD_MIN; col <= BOARD_MAX; col++) { - char l = iGame.getBoardChar(row, col); + char l = iGame.getBoard().getChar(row, col); if (l != 0) out << " " << l; else { - int wm = iGame.getBoardWordMultiplier(row, col); - int tm = iGame.getBoardLetterMultiplier(row, col); + int wm = iGame.getBoard().getWordMultiplier(row, col); + int tm = iGame.getBoard().getLetterMultiplier(row, col); if (wm > 1) out << " " << ((wm == 3) ? '@' : '#'); @@ -123,9 +123,9 @@ void GameIO::printBoardMultipliers2(ostream &out, const Game &iGame) out << " " << (char)(row - BOARD_MIN + 'A') << " "; for (col = BOARD_MIN; col <= BOARD_MAX; col++) { - char l = iGame.getBoardChar(row, col); - int wm = iGame.getBoardWordMultiplier(row, col); - int tm = iGame.getBoardLetterMultiplier(row, col); + char l = iGame.getBoard().getChar(row, col); + int wm = iGame.getBoard().getWordMultiplier(row, col); + int tm = iGame.getBoard().getLetterMultiplier(row, col); if (wm > 1) out << " " << ((wm == 3) ? '@' : '#'); @@ -147,7 +147,7 @@ void GameIO::printNonPlayed(ostream &out, const Game &iGame) for (it = allTiles.begin(); it != allTiles.end(); it++) { - if (iGame.getNCharInBag(it->toChar()) > 9) + if (iGame.getBag().in(it->toChar()) > 9) out << " "; out << setw(2) << it->toChar(); } @@ -155,7 +155,7 @@ void GameIO::printNonPlayed(ostream &out, const Game &iGame) for (it = allTiles.begin(); it != allTiles.end(); it++) { - out << " " << iGame.getNCharInBag(it->toChar()); + out << " " << iGame.getBag().in(it->toChar()); } out << endl; } diff --git a/utils/ncurses.cpp b/utils/ncurses.cpp index 83b5cc1..e225add 100644 --- a/utils/ncurses.cpp +++ b/utils/ncurses.cpp @@ -139,8 +139,8 @@ void CursesIntf::drawBoard(WINDOW *win, int y, int x) const for (int col = 1; col < 16; col++) { // Handle colors - int wm = m_game->getBoardWordMultiplier(row, col); - int lm = m_game->getBoardLetterMultiplier(row, col); + int wm = m_game->getBoard().getWordMultiplier(row, col); + int lm = m_game->getBoard().getLetterMultiplier(row, col); if (wm == 3) wattron(win, COLOR_PAIR(COLOR_RED)); else if (wm == 2) @@ -156,7 +156,7 @@ void CursesIntf::drawBoard(WINDOW *win, int y, int x) const mvwprintw(win, y + row + 1, x + 3 * col + 1, " "); // Now add the letter - char c = m_game->getBoardChar(row, col); + char c = m_game->getBoard().getChar(row, col); if (c) { if (islower(c)) diff --git a/wxwin/auxframes.cc b/wxwin/auxframes.cc index 142db4c..305b5ce 100644 --- a/wxwin/auxframes.cc +++ b/wxwin/auxframes.cc @@ -161,7 +161,7 @@ BagFrame::Refresh(refresh_t force) list::const_iterator it; for (index = 0, it = allTiles.begin(); it != allTiles.end(); index++, it++) { - buf.Printf(format, it->toChar(), m_game.getNCharInBag(*it)); + buf.Printf(format, it->toChar(), m_game.getBag().in(*it)); tiles->InsertItem(index,buf); } } diff --git a/wxwin/gfxboard.cc b/wxwin/gfxboard.cc index 7bfd0e6..d8e3557 100644 --- a/wxwin/gfxboard.cc +++ b/wxwin/gfxboard.cc @@ -287,29 +287,29 @@ GfxBoard::DrawBoard(wxDC *dc) { for (column = BOARD_MIN; column <= BOARD_MAX; column++) { - if (m_game.getBoardLetterMultiplier(row, column) == 2) + if (m_game.getBoard().getLetterMultiplier(row, column) == 2) { dc->SetBrush(*Lx2Brush); dc->SetTextBackground(colLx2); } - else if (m_game.getBoardLetterMultiplier(row, column) == 3) + else if (m_game.getBoard().getLetterMultiplier(row, column) == 3) { dc->SetBrush(*Lx3Brush); dc->SetTextBackground(colLx3); } - else if (m_game.getBoardWordMultiplier(row, column) == 2) + else if (m_game.getBoard().getWordMultiplier(row, column) == 2) { dc->SetBrush(*Wx2Brush); dc->SetTextBackground(colWx2); } - else if (m_game.getBoardWordMultiplier(row, column) == 3) + else if (m_game.getBoard().getWordMultiplier(row, column) == 3) { dc->SetBrush(*Wx3Brush); dc->SetTextBackground(colWx3); } - wxs = wxString((wxChar)m_game.getBoardChar(row, column)); - attr = m_game.getBoardCharAttr(row, column); + wxs = wxString((wxChar)m_game.getBoard().getChar(row, column)); + attr = m_game.getBoard().getCharAttr(row, column); if ((paintedboard_char[row - BOARD_MIN][column - BOARD_MIN] != wxs.GetChar(0)) || (paintedboard_attr[row - BOARD_MIN][column - BOARD_MIN] != attr)) {