First backports from the branch (Game::getBoard and Game::getBag, mainly)

This commit is contained in:
Olivier Teulière 2005-11-04 23:26:03 +00:00
parent fe13a5e627
commit 50fc82f60c
11 changed files with 125 additions and 138 deletions

View file

@ -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 Tile Board::getTile(int iRow, int iCol) const
{ {
return m_tilesRow[iRow][iCol]; return m_tilesRow[iRow][iCol];

View file

@ -63,10 +63,29 @@ public:
Board(); Board();
virtual ~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; Tile getTile(int iRow, int iCol) const;
bool isJoker(int iRow, int iCol) const; bool isJoker(int iRow, int iCol) const;
bool isVacant(int iRow, int iCol) const; bool isVacant(int iRow, int iCol) const;
/*int score(Round);*/
void addRound(const Dictionary &iDic, const Round &iRound); void addRound(const Dictionary &iDic, const Round &iRound);
void removeRound(const Dictionary &iDic, const Round &iRound); void removeRound(const Dictionary &iDic, const Round &iRound);
int checkRound(Round &iRound, bool iFirstTurn); int checkRound(Round &iRound, bool iFirstTurn);

View file

@ -27,81 +27,104 @@
#include <string> #include <string>
#include "coord.h" #include "coord.h"
Coord::Coord() Coord::Coord()
{ {
m_row = 1; m_row = 1;
m_col = 1; m_col = 1;
m_dir = HORIZONTAL; m_dir = HORIZONTAL;
}
Coord::Coord(const string &iStr)
{
m_row = 1;
m_col = 1;
setFromString(iStr);
} }
Coord::~Coord() Coord::~Coord()
{ {
} }
Direction Direction Coord::getDir() const
Coord::getDir() const
{ {
return m_dir; return m_dir;
} }
int int Coord::getRow() const
Coord::getRow() const
{ {
return m_row; return m_row;
} }
int int Coord::getCol() const
Coord::getCol() const
{ {
return m_col; return m_col;
} }
void void Coord::setRow(int iRow)
Coord::setRow(int iRow)
{ {
m_row = iRow; m_row = iRow;
} }
void void Coord::setCol(int iCol)
Coord::setCol(int iCol)
{ {
m_col = iCol; m_col = iCol;
} }
void void Coord::setDir(Direction iDir)
Coord::setDir(Direction iDir)
{ {
m_dir = iDir; m_dir = iDir;
} }
void void Coord::operator=(const Coord &iOther)
Coord::operator=(const Coord &iOther)
{ {
m_dir = iOther.m_dir; m_dir = iOther.m_dir;
m_row = iOther.m_row; m_row = iOther.m_row;
m_col = iOther.m_col; m_col = iOther.m_col;
} }
std::string void Coord::setFromString(const string &iStr)
Coord::toString() const
{ {
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) if (getDir() == HORIZONTAL)
{ {
char s[5]; char s[5];
sprintf(s, "%d", m_col); sprintf(s, "%d", m_col);
rs = std::string(1, m_row + 'A' - 1) + s; rs = string(1, m_row + 'A' - 1) + s;
} }
else else
{ {
char s[5]; char s[5];
sprintf(s, "%d", m_col); sprintf(s, "%d", m_col);
rs = s + std::string(1, m_row + 'A' - 1); rs = s + string(1, m_row + 'A' - 1);
} }
return rs; return rs;
} }
/// Local Variables: /// Local Variables:
/// mode: hs-minor /// mode: hs-minor
/// c-basic-offset: 4 /// c-basic-offset: 4

View file

@ -27,6 +27,8 @@
#ifndef _COORD_H #ifndef _COORD_H
#define _COORD_H #define _COORD_H
using std::string;
class Coord class Coord
{ {
public: public:
@ -34,7 +36,7 @@ public:
enum Direction {VERTICAL, HORIZONTAL}; enum Direction {VERTICAL, HORIZONTAL};
Coord(); Coord();
Coord(const std::string& iCoord); Coord(const string &iStr);
virtual ~Coord(); virtual ~Coord();
void setRow(int iRow); void setRow(int iRow);
@ -46,9 +48,10 @@ public:
int getCol() const; int getCol() const;
void operator=(const Coord &iOther); void operator=(const Coord &iOther);
std::string toString() const; void setFromString(const string &iStr);
string toString() const;
private: private:
Direction m_dir; Direction m_dir;
int m_row, m_col; int m_row, m_col;

View file

@ -454,51 +454,15 @@ int Game::back(int n)
m_roundHistory.pop_back(); m_roundHistory.pop_back();
m_playerHistory.pop_back(); m_playerHistory.pop_back();
} }
else else
{ {
return 1; return 1;
} }
} }
return 0; 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 string Game::formatCoords(const Round &iRound) const
{ {
if (iRound.getDir() == HORIZONTAL) if (iRound.getDir() == HORIZONTAL)

View file

@ -41,14 +41,6 @@ using namespace std;
*************************/ *************************/
#define IDENT_STRING "Eliot" #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. * Parent class of all the Game types.
* It offers the common attributes (Board, Bag, etc...) as well as useful * It offers the common attributes (Board, Bag, etc...) as well as useful
@ -77,6 +69,9 @@ public:
kJOKER // Joker game kJOKER // Joker game
}; };
const Board& getBoard() const { return m_board; }
const Bag& getBag() const { return m_bag; }
/** /**
* Accessors for the variant of the game. * Accessors for the variant of the game.
* The variant can be changed during a game without any problem * The variant can be changed during a game without any problem
@ -108,28 +103,6 @@ public:
*************************/ *************************/
int back(int); 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 * Set the rack for searching
* *
@ -149,13 +122,6 @@ public:
static const int RACK_SIZE; static const int RACK_SIZE;
typedef enum {RACK_ALL, RACK_NEW} set_rack_mode; 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. * Methods to access already played words.
* The int parameter should be 0 <= int < getNRounds() * The int parameter should be 0 <= int < getNRounds()

View file

@ -90,8 +90,7 @@ Round Turn::getRound() const
} }
void void Turn::operator=(const Turn &iOther)
Turn::operator=(const Turn &iOther)
{ {
num = iOther.num; num = iOther.num;
pldrack = iOther.pldrack; pldrack = iOther.pldrack;
@ -99,20 +98,18 @@ Turn::operator=(const Turn &iOther)
} }
string string Turn::toString(bool showExtraSigns) const
Turn::toString(bool showExtraSigns) const
{ {
string rs = ""; string rs = "";
if (showExtraSigns) if (showExtraSigns)
{ {
rs = ""; // TODO rs = ""; // TODO
} }
rs = rs + pldrack.toString() + " " + round.toString(); rs = rs + pldrack.toString() + " " + round.toString();
return rs; return rs;
} }
/// Local Variables: /// Local Variables:
/// mode: hs-minor /// mode: hs-minor
/// c-basic-offset: 4 /// c-basic-offset: 4

View file

@ -41,7 +41,7 @@ void GameIO::printBoard(ostream &out, const Game &iGame)
out << " " << (char)(row - BOARD_MIN + 'A') << " "; out << " " << (char)(row - BOARD_MIN + 'A') << " ";
for (col = BOARD_MIN; col <= BOARD_MAX; col++) 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 << setw(3) << (l ? l : '-');
} }
out << endl; out << endl;
@ -63,8 +63,8 @@ void GameIO::printBoardJoker(ostream &out, const Game &iGame)
out << " " << (char)(row - BOARD_MIN + 'A') << " "; out << " " << (char)(row - BOARD_MIN + 'A') << " ";
for (col = BOARD_MIN; col <= BOARD_MAX; col++) for (col = BOARD_MIN; col <= BOARD_MAX; col++)
{ {
char l = iGame.getBoardChar(row, col); char l = iGame.getBoard().getChar(row, col);
bool j = (iGame.getBoardCharAttr(row, col) & ATTR_JOKER); bool j = (iGame.getBoard().getCharAttr(row, col) & ATTR_JOKER);
out << " " << (j ? '.' : (l ? ' ' : '-')); out << " " << (j ? '.' : (l ? ' ' : '-'));
out << (l ? l : '-'); out << (l ? l : '-');
@ -88,13 +88,13 @@ void GameIO::printBoardMultipliers(ostream &out, const Game &iGame)
out << " " << (char)(row - BOARD_MIN + 'A') << " "; out << " " << (char)(row - BOARD_MIN + 'A') << " ";
for (col = BOARD_MIN; col <= BOARD_MAX; col++) for (col = BOARD_MIN; col <= BOARD_MAX; col++)
{ {
char l = iGame.getBoardChar(row, col); char l = iGame.getBoard().getChar(row, col);
if (l != 0) if (l != 0)
out << " " << l; out << " " << l;
else else
{ {
int wm = iGame.getBoardWordMultiplier(row, col); int wm = iGame.getBoard().getWordMultiplier(row, col);
int tm = iGame.getBoardLetterMultiplier(row, col); int tm = iGame.getBoard().getLetterMultiplier(row, col);
if (wm > 1) if (wm > 1)
out << " " << ((wm == 3) ? '@' : '#'); out << " " << ((wm == 3) ? '@' : '#');
@ -123,9 +123,9 @@ void GameIO::printBoardMultipliers2(ostream &out, const Game &iGame)
out << " " << (char)(row - BOARD_MIN + 'A') << " "; out << " " << (char)(row - BOARD_MIN + 'A') << " ";
for (col = BOARD_MIN; col <= BOARD_MAX; col++) for (col = BOARD_MIN; col <= BOARD_MAX; col++)
{ {
char l = iGame.getBoardChar(row, col); char l = iGame.getBoard().getChar(row, col);
int wm = iGame.getBoardWordMultiplier(row, col); int wm = iGame.getBoard().getWordMultiplier(row, col);
int tm = iGame.getBoardLetterMultiplier(row, col); int tm = iGame.getBoard().getLetterMultiplier(row, col);
if (wm > 1) if (wm > 1)
out << " " << ((wm == 3) ? '@' : '#'); out << " " << ((wm == 3) ? '@' : '#');
@ -147,7 +147,7 @@ void GameIO::printNonPlayed(ostream &out, const Game &iGame)
for (it = allTiles.begin(); it != allTiles.end(); it++) for (it = allTiles.begin(); it != allTiles.end(); it++)
{ {
if (iGame.getNCharInBag(it->toChar()) > 9) if (iGame.getBag().in(it->toChar()) > 9)
out << " "; out << " ";
out << setw(2) << it->toChar(); 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++) for (it = allTiles.begin(); it != allTiles.end(); it++)
{ {
out << " " << iGame.getNCharInBag(it->toChar()); out << " " << iGame.getBag().in(it->toChar());
} }
out << endl; out << endl;
} }

View file

@ -139,8 +139,8 @@ void CursesIntf::drawBoard(WINDOW *win, int y, int x) const
for (int col = 1; col < 16; col++) for (int col = 1; col < 16; col++)
{ {
// Handle colors // Handle colors
int wm = m_game->getBoardWordMultiplier(row, col); int wm = m_game->getBoard().getWordMultiplier(row, col);
int lm = m_game->getBoardLetterMultiplier(row, col); int lm = m_game->getBoard().getLetterMultiplier(row, col);
if (wm == 3) if (wm == 3)
wattron(win, COLOR_PAIR(COLOR_RED)); wattron(win, COLOR_PAIR(COLOR_RED));
else if (wm == 2) 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, " "); mvwprintw(win, y + row + 1, x + 3 * col + 1, " ");
// Now add the letter // Now add the letter
char c = m_game->getBoardChar(row, col); char c = m_game->getBoard().getChar(row, col);
if (c) if (c)
{ {
if (islower(c)) if (islower(c))

View file

@ -161,7 +161,7 @@ BagFrame::Refresh(refresh_t force)
list<Tile>::const_iterator it; list<Tile>::const_iterator it;
for (index = 0, it = allTiles.begin(); it != allTiles.end(); index++, 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); tiles->InsertItem(index,buf);
} }
} }

View file

@ -287,29 +287,29 @@ GfxBoard::DrawBoard(wxDC *dc)
{ {
for (column = BOARD_MIN; column <= BOARD_MAX; column++) 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->SetBrush(*Lx2Brush);
dc->SetTextBackground(colLx2); dc->SetTextBackground(colLx2);
} }
else if (m_game.getBoardLetterMultiplier(row, column) == 3) else if (m_game.getBoard().getLetterMultiplier(row, column) == 3)
{ {
dc->SetBrush(*Lx3Brush); dc->SetBrush(*Lx3Brush);
dc->SetTextBackground(colLx3); dc->SetTextBackground(colLx3);
} }
else if (m_game.getBoardWordMultiplier(row, column) == 2) else if (m_game.getBoard().getWordMultiplier(row, column) == 2)
{ {
dc->SetBrush(*Wx2Brush); dc->SetBrush(*Wx2Brush);
dc->SetTextBackground(colWx2); dc->SetTextBackground(colWx2);
} }
else if (m_game.getBoardWordMultiplier(row, column) == 3) else if (m_game.getBoard().getWordMultiplier(row, column) == 3)
{ {
dc->SetBrush(*Wx3Brush); dc->SetBrush(*Wx3Brush);
dc->SetTextBackground(colWx3); dc->SetTextBackground(colWx3);
} }
wxs = wxString((wxChar)m_game.getBoardChar(row, column)); wxs = wxString((wxChar)m_game.getBoard().getChar(row, column));
attr = m_game.getBoardCharAttr(row, column); attr = m_game.getBoard().getCharAttr(row, column);
if ((paintedboard_char[row - BOARD_MIN][column - BOARD_MIN] != wxs.GetChar(0)) || if ((paintedboard_char[row - BOARD_MIN][column - BOARD_MIN] != wxs.GetChar(0)) ||
(paintedboard_attr[row - BOARD_MIN][column - BOARD_MIN] != attr)) (paintedboard_attr[row - BOARD_MIN][column - BOARD_MIN] != attr))
{ {