2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
2012-01-17 00:27:32 +01:00
|
|
|
* Copyright (C) 1999-2012 Eliot
|
2005-02-05 12:14:56 +01:00
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
|
|
|
|
* Olivier Teuliere <ipkiss@via.ecp.fr>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-23 16:53:42 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-02-05 12:14:56 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2009-11-29 17:01:31 +01:00
|
|
|
#include <boost/format.hpp>
|
2008-11-22 14:09:28 +01:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
#include <iomanip>
|
2005-02-26 23:57:34 +01:00
|
|
|
#include <string>
|
2008-01-08 14:52:32 +01:00
|
|
|
#include <stdlib.h>
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2005-02-26 23:57:34 +01:00
|
|
|
#include "game_io.h"
|
2011-08-27 19:21:26 +02:00
|
|
|
#include "game_params.h"
|
2012-12-26 14:14:44 +01:00
|
|
|
#include "dic.h"
|
2008-11-30 21:53:44 +01:00
|
|
|
#include "public_game.h"
|
|
|
|
#include "bag.h"
|
|
|
|
#include "board.h"
|
2012-12-26 14:14:44 +01:00
|
|
|
#include "board_layout.h"
|
2008-11-30 21:53:44 +01:00
|
|
|
#include "results.h"
|
2005-11-05 16:48:59 +01:00
|
|
|
#include "player.h"
|
2006-01-22 13:23:52 +01:00
|
|
|
#include "encoding.h"
|
2009-11-29 17:01:31 +01:00
|
|
|
#include "history.h"
|
2012-10-06 01:50:58 +02:00
|
|
|
#include "turn_data.h"
|
2009-11-29 17:01:31 +01:00
|
|
|
#include "move.h"
|
|
|
|
#include "round.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2005-02-26 23:57:34 +01:00
|
|
|
using namespace std;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
using boost::format;
|
|
|
|
using boost::wformat;
|
|
|
|
|
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(utils, GameIO);
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printBoard(ostream &out, const PublicGame &iGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
int nbRows = iGame.getBoard().getLayout().getRowCount();
|
|
|
|
int nbCols = iGame.getBoard().getLayout().getColCount();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
out << " ";
|
2012-12-26 14:14:44 +01:00
|
|
|
for (int col = 1; col <= nbCols; ++col)
|
|
|
|
out << setw(3) << col;
|
2005-02-05 12:14:56 +01:00
|
|
|
out << endl;
|
2012-12-26 14:14:44 +01:00
|
|
|
for (int row = 1; row <= nbRows; ++row)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
out << " " << (char)(row + 'A' - 1) << " ";
|
|
|
|
for (int col = 1; col <= nbCols; ++col)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-06-23 15:21:19 +02:00
|
|
|
if (iGame.getBoard().isVacant(row, col))
|
2009-06-23 23:36:33 +02:00
|
|
|
out << " - ";
|
2008-01-08 14:52:32 +01:00
|
|
|
else
|
2009-06-23 23:36:33 +02:00
|
|
|
out << centerAndConvert(iGame.getBoard().getDisplayStr(row, col), 3);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 00:55:18 +02:00
|
|
|
|
2006-11-05 14:27:49 +01:00
|
|
|
/* this mode is used for regression tests */
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printBoardDebug(ostream &out, const PublicGame &iGame)
|
2006-11-05 14:27:49 +01:00
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
int nbRows = iGame.getBoard().getLayout().getRowCount();
|
|
|
|
int nbCols = iGame.getBoard().getLayout().getColCount();
|
2006-11-05 14:27:49 +01:00
|
|
|
|
|
|
|
/* first printf row cell contents */
|
2012-12-26 14:14:44 +01:00
|
|
|
for (int row = 1; row <= nbRows; ++row)
|
2006-11-05 14:27:49 +01:00
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
out << " " << (char)(row + 'A' - 1) << "r ";
|
|
|
|
for (int col = 1; col <= nbCols; ++col)
|
2006-11-05 14:27:49 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
out << iGame.getBoard().getCellContent_row(row, col);
|
2006-11-05 14:27:49 +01:00
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
out << " -" << endl;
|
2012-12-26 14:14:44 +01:00
|
|
|
for (int row = 1; row <= nbRows; ++row)
|
2006-11-05 14:27:49 +01:00
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
out << " " << (char)(row + 'A' - 1) << "c ";
|
|
|
|
for (int col = 1; col <= nbCols; ++col)
|
2006-11-05 14:27:49 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
out << iGame.getBoard().getCellContent_col(row, col);
|
2006-11-05 14:27:49 +01:00
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printBoardMultipliers(ostream &out, const PublicGame &iGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
int nbRows = iGame.getBoard().getLayout().getRowCount();
|
|
|
|
int nbCols = iGame.getBoard().getLayout().getColCount();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
out << " ";
|
2012-12-26 14:14:44 +01:00
|
|
|
for (int col = 1; col <= nbCols; ++col)
|
|
|
|
out << setw(3) << col;
|
2005-02-05 12:14:56 +01:00
|
|
|
out << endl;
|
|
|
|
|
2012-12-26 14:14:44 +01:00
|
|
|
const BoardLayout & boardLayout = iGame.getBoard().getLayout();
|
|
|
|
for (int row = 1; row <= nbRows; ++row)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
out << " " << (char)(row + 'A' - 1) << " ";
|
|
|
|
for (int col = 1; col <= nbCols; ++col)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-06-23 15:21:19 +02:00
|
|
|
if (!iGame.getBoard().isVacant(row, col))
|
|
|
|
out << padAndConvert(iGame.getBoard().getDisplayStr(row, col), 3);
|
2005-02-05 12:14:56 +01:00
|
|
|
else
|
|
|
|
{
|
2012-12-26 14:14:44 +01:00
|
|
|
int wm = boardLayout.getWordMultiplier(row, col);
|
|
|
|
int tm = boardLayout.getLetterMultiplier(row, col);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
if (wm > 1)
|
|
|
|
out << " " << ((wm == 3) ? '@' : '#');
|
|
|
|
else if (tm > 1)
|
|
|
|
out << " " << ((tm == 3) ? '*' : '+');
|
|
|
|
else
|
|
|
|
out << " -";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printNonPlayed(ostream &out, const PublicGame &iGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
const Bag &bag = iGame.getBag();
|
|
|
|
BOOST_FOREACH(const Tile &tile, iGame.getDic().getAllTiles())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2013-01-17 17:14:38 +01:00
|
|
|
if (bag.count(tile) > 9)
|
2005-02-05 12:14:56 +01:00
|
|
|
out << " ";
|
2011-07-30 21:48:05 +02:00
|
|
|
out << setw(2) << lfw(tile.getDisplayStr());
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, iGame.getDic().getAllTiles())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2013-01-17 17:14:38 +01:00
|
|
|
out << " " << bag.count(tile);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-26 15:59:57 +01:00
|
|
|
void GameIO::printPlayedRack(ostream &out, const PublicGame &iGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-03-26 07:48:33 +02:00
|
|
|
out << lfw(iGame.getCurrentRack().toString(PlayedRack::RACK_SIMPLE)) << endl;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printAllRacks(ostream &out, const PublicGame &iGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-30 21:53:44 +01:00
|
|
|
for (unsigned int j = 0; j < iGame.getNbPlayers(); j++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "Rack " << j << ": ";
|
2011-07-30 21:48:05 +02:00
|
|
|
out << lfw(iGame.getPlayer(j).getCurrentRack().toString(PlayedRack::RACK_EXTRA)) << endl;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
static void searchResultLine(ostream &out, const Results &iResults, int num)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-30 21:53:44 +01:00
|
|
|
const Round &r = iResults.get(num);
|
|
|
|
const wstring &word = r.getWord();
|
2009-11-29 17:01:31 +01:00
|
|
|
if (word.empty())
|
2005-02-05 12:14:56 +01:00
|
|
|
return;
|
2011-07-30 21:48:05 +02:00
|
|
|
out << lfw(word) << string(16 - word.size(), ' ')
|
2005-12-27 00:35:03 +01:00
|
|
|
<< (r.getBonus() ? '*' : ' ')
|
|
|
|
<< setw(4) << r.getPoints()
|
2011-07-30 21:48:05 +02:00
|
|
|
<< ' ' << lfw(r.getCoord().toString());
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printSearchResults(ostream &out, const Results &iResults, int num)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-30 21:53:44 +01:00
|
|
|
for (int i = 0; i < num && i < (int)iResults.size(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
out << setw(3) << i + 1 << ": ";
|
2008-11-30 21:53:44 +01:00
|
|
|
searchResultLine(out, iResults, i);
|
2005-02-05 12:14:56 +01:00
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printPoints(ostream &out, const PublicGame &iGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-10-05 18:27:59 +02:00
|
|
|
out << iGame.getPlayer(0).getTotalScore() << endl;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printAllPoints(ostream &out, const PublicGame &iGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-30 21:53:44 +01:00
|
|
|
for (unsigned int i = 0; i < iGame.getNbPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "Score " << i << ": "
|
2012-10-05 18:27:59 +02:00
|
|
|
<< setw(4) << iGame.getPlayer(i).getTotalScore() << endl;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-05 14:27:49 +01:00
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void GameIO::printGameDebug(ostream &out, const PublicGame &iGame)
|
2006-11-05 14:27:49 +01:00
|
|
|
{
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "Game: player " << iGame.getCurrentPlayer().getId() + 1
|
|
|
|
<< " out of " << iGame.getNbPlayers() << endl;
|
2011-08-28 18:27:48 +02:00
|
|
|
if (iGame.getParams().getMode() == GameParams::kDUPLICATE)
|
|
|
|
out << "Game: mode=Duplicate" << endl;
|
|
|
|
else if (iGame.getParams().getMode() == GameParams::kFREEGAME)
|
|
|
|
out << "Game: mode=Free game" << endl;
|
|
|
|
else if (iGame.getParams().getMode() == GameParams::kTRAINING)
|
|
|
|
out << "Game: mode=Training" << endl;
|
2012-03-05 01:27:56 +01:00
|
|
|
else if (iGame.getParams().getMode() == GameParams::kARBITRATION)
|
|
|
|
out << "Game: mode=Arbitration" << endl;
|
2012-12-25 17:30:03 +01:00
|
|
|
else if (iGame.getParams().getMode() == GameParams::kTOPPING)
|
|
|
|
out << "Game: mode=Topping" << endl;
|
2011-08-28 19:24:37 +02:00
|
|
|
if (iGame.getParams().hasVariant(GameParams::kJOKER))
|
2011-08-28 17:29:39 +02:00
|
|
|
out << "Game: variant=joker" << endl;
|
2011-08-28 19:24:37 +02:00
|
|
|
if (iGame.getParams().hasVariant(GameParams::kEXPLOSIVE))
|
2011-08-28 17:29:39 +02:00
|
|
|
out << "Game: variant=explosive" << endl;
|
2011-08-28 19:24:37 +02:00
|
|
|
if (iGame.getParams().hasVariant(GameParams::k7AMONG8))
|
2011-08-28 17:29:39 +02:00
|
|
|
out << "Game: variant=7among8" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "Game: history:" << endl;
|
2013-01-16 14:26:05 +01:00
|
|
|
out << " N | RACK | SOLUTION | REF | PTS | BONUS" << endl;
|
|
|
|
out << " ===|==========|================|=====|=====|======" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
for (unsigned int i = 0; i < iGame.getHistory().getSize(); ++i)
|
|
|
|
{
|
2012-10-06 01:57:39 +02:00
|
|
|
const TurnData &turn = iGame.getHistory().getTurn(i);
|
2009-11-29 17:01:31 +01:00
|
|
|
const Move &move = turn.getMove();
|
2013-01-16 14:26:05 +01:00
|
|
|
format fmter("%1% | %2% | %3% | %4% | %5% | %6%");
|
2009-11-29 17:01:31 +01:00
|
|
|
fmter % padAndConvert(str(wformat(L"%1%") % (i + 1)), 5);
|
|
|
|
fmter % padAndConvert(turn.getPlayedRack().toString(), 8);
|
2012-04-30 21:21:38 +02:00
|
|
|
if (move.isValid())
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
const Round &round = move.getRound();
|
|
|
|
fmter % padAndConvert(round.getWord(), 14, false);
|
|
|
|
fmter % padAndConvert(round.getCoord().toString(), 3);
|
|
|
|
fmter % padAndConvert(str(wformat(L"%1%") % round.getPoints()), 3);
|
|
|
|
fmter % padAndConvert(round.getBonus() ? L"*": L"", 1, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-30 21:21:38 +02:00
|
|
|
if (move.isInvalid())
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
fmter % padAndConvert(L"#" + move.getBadWord() + L"#", 14, false);
|
|
|
|
fmter % padAndConvert(move.getBadCoord(), 3);
|
|
|
|
}
|
2012-04-30 21:21:38 +02:00
|
|
|
else if (move.isChangeLetters())
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
fmter % padAndConvert(L"[" + move.getChangedLetters() + L"]", 14, false) % " - ";
|
|
|
|
}
|
2012-04-30 21:21:38 +02:00
|
|
|
else if (move.isPass())
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
fmter % padAndConvert(L"(PASS)", 14, false) % " - ";
|
|
|
|
}
|
2012-01-17 00:27:32 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fmter % padAndConvert(L"(NO MOVE)", 14, false) % " - ";
|
|
|
|
}
|
2009-11-29 17:01:31 +01:00
|
|
|
fmter % " 0" % " ";
|
|
|
|
}
|
|
|
|
out << fmter.str() << endl;
|
|
|
|
}
|
|
|
|
out << endl << endl;
|
2006-11-05 14:27:49 +01:00
|
|
|
}
|