2008-01-20 19:40:12 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
|
|
|
* Copyright (C) 2008 Olivier Teulière
|
|
|
|
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-12-30 19:33:48 +01:00
|
|
|
#include <QtCore/QSettings>
|
2008-01-20 19:40:12 +01:00
|
|
|
|
|
|
|
#include "new_game.h"
|
2012-02-16 21:59:01 +01:00
|
|
|
#include "players_table_helper.h"
|
2008-01-20 19:40:12 +01:00
|
|
|
#include "qtcommon.h"
|
2011-12-30 19:33:48 +01:00
|
|
|
#include "prefs_dialog.h"
|
2008-01-20 19:40:12 +01:00
|
|
|
#include "game_factory.h"
|
|
|
|
#include "game.h"
|
2008-11-30 21:53:44 +01:00
|
|
|
#include "public_game.h"
|
2008-01-20 19:40:12 +01:00
|
|
|
#include "training.h"
|
|
|
|
#include "freegame.h"
|
|
|
|
#include "duplicate.h"
|
|
|
|
#include "player.h"
|
2008-01-28 20:17:33 +01:00
|
|
|
#include "ai_percent.h"
|
2008-01-20 19:40:12 +01:00
|
|
|
|
|
|
|
|
2012-02-10 20:14:15 +01:00
|
|
|
INIT_LOGGER(qt, NewGame);
|
|
|
|
|
2008-08-31 13:48:11 +02:00
|
|
|
const char * NewGame::kHUMAN = _("Human");
|
|
|
|
const char * NewGame::kAI = _("Computer");
|
2008-01-20 19:40:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
NewGame::NewGame(QWidget *iParent)
|
|
|
|
: QDialog(iParent)
|
|
|
|
{
|
|
|
|
setupUi(this);
|
2008-09-03 19:28:27 +02:00
|
|
|
lineEditName->setText(_q("Player %1").arg(2));
|
2008-01-20 19:40:12 +01:00
|
|
|
|
2011-08-27 19:21:26 +02:00
|
|
|
checkBoxJoker->setToolTip(_q(
|
|
|
|
"In a joker game, each rack contains a joker.\n"
|
|
|
|
"When a word containing the joker is played on the grid, the joker is then replaced\n"
|
|
|
|
"with the corresponding letter from the bag, and the joker stays in the rack.\n"
|
|
|
|
"When the corresponding letter is not present in the bag, the joker is placed on the board.\n"
|
|
|
|
"This variant, particularly interesting in Duplicate mode, is good to train using the joker."));
|
|
|
|
checkBoxExplosive->setToolTip(_q(
|
|
|
|
"An explosive game is a bit like a joker game, except that when the computer chooses the rack\n"
|
|
|
|
"(containing a joker), it performs a search and finds the best word possible with the rack.\n"
|
|
|
|
"Then, if possible, it replaces the joker in the rack with the letter allowing to play this best word.\n"
|
|
|
|
"This variant, unlike the joker game, allows playing with a normal-looking rack, but it usually gives\n"
|
|
|
|
"much higher scores than in a normal game."));
|
|
|
|
checkBox7Among8->setToolTip(_q(
|
|
|
|
"With this variant, the rack contains 8 letters instead of 7,\n"
|
|
|
|
"but at most 7 can be played at the same time.\n"
|
|
|
|
"This allows for more combinations during the game, and thus higher scores."));
|
|
|
|
|
2012-02-16 21:59:01 +01:00
|
|
|
m_helper = new PlayersTableHelper(this, tablePlayers, NULL, pushButtonRemove);
|
|
|
|
|
|
|
|
// Retrieve the default computer level
|
|
|
|
QSettings qs;
|
|
|
|
int defLevel = qs.value(PrefsDialog::kINTF_DEFAULT_AI_LEVEL, 100).toInt();
|
|
|
|
// Ensure a valid range
|
|
|
|
if (defLevel < 0)
|
|
|
|
defLevel = 0;
|
|
|
|
if (defLevel > 100)
|
|
|
|
defLevel = 100;
|
|
|
|
|
2008-01-20 19:40:12 +01:00
|
|
|
// Initialize the model of the default players
|
2012-02-16 21:59:01 +01:00
|
|
|
m_helper->addRow(_q("Player %1").arg(1), _q(kHUMAN), "");
|
|
|
|
m_helper->addRow(_q("Eliot"), _q(kAI), QString("%1").arg(defLevel));
|
2008-09-03 19:28:27 +02:00
|
|
|
|
|
|
|
// Enable the Level spinbox only when the player is a computer
|
|
|
|
QObject::connect(comboBoxType, SIGNAL(currentIndexChanged(int)),
|
|
|
|
this, SLOT(enableLevelSpinBox(int)));
|
2011-08-27 20:00:37 +02:00
|
|
|
// Enable the Ok button only if there are enough players for the
|
2008-01-20 19:40:12 +01:00
|
|
|
// current mode
|
2012-02-16 21:59:01 +01:00
|
|
|
QObject::connect(m_helper, SIGNAL(rowCountChanged()),
|
2008-01-20 19:40:12 +01:00
|
|
|
this, SLOT(enableOkButton()));
|
2011-08-27 20:00:37 +02:00
|
|
|
QObject::connect(radioButtonDuplicate, SIGNAL(toggled(bool)),
|
2008-01-20 19:40:12 +01:00
|
|
|
this, SLOT(enableOkButton()));
|
2011-08-27 20:00:37 +02:00
|
|
|
QObject::connect(radioButtonFreeGame, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(enableOkButton()));
|
|
|
|
QObject::connect(radioButtonTraining, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(enableOkButton()));
|
2012-02-10 20:14:15 +01:00
|
|
|
|
2011-08-27 20:00:37 +02:00
|
|
|
QObject::connect(radioButtonDuplicate, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(enablePlayers(bool)));
|
|
|
|
QObject::connect(radioButtonFreeGame, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(enablePlayers(bool)));
|
|
|
|
QObject::connect(radioButtonTraining, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(enablePlayers(bool)));
|
2011-12-30 19:33:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
PublicGame * NewGame::createGame(const Dictionary &iDic) const
|
2008-01-20 19:40:12 +01:00
|
|
|
{
|
2011-08-27 19:21:26 +02:00
|
|
|
// Game parameters
|
2011-08-28 19:16:59 +02:00
|
|
|
GameParams params(iDic);
|
2011-08-28 18:20:00 +02:00
|
|
|
if (radioButtonTraining->isChecked())
|
2011-08-28 19:16:59 +02:00
|
|
|
params.setMode(GameParams::kTRAINING);
|
2011-08-28 18:20:00 +02:00
|
|
|
else if (radioButtonFreeGame->isChecked())
|
2011-08-28 19:16:59 +02:00
|
|
|
params.setMode(GameParams::kFREEGAME);
|
2011-08-28 18:20:00 +02:00
|
|
|
else
|
2011-08-28 19:16:59 +02:00
|
|
|
params.setMode(GameParams::kDUPLICATE);
|
2011-08-28 18:20:00 +02:00
|
|
|
|
2011-08-27 19:21:26 +02:00
|
|
|
if (checkBoxJoker->isChecked())
|
2011-08-28 19:24:37 +02:00
|
|
|
params.addVariant(GameParams::kJOKER);
|
2011-08-27 19:21:26 +02:00
|
|
|
if (checkBoxExplosive->isChecked())
|
2011-08-28 19:24:37 +02:00
|
|
|
params.addVariant(GameParams::kEXPLOSIVE);
|
2011-08-27 19:21:26 +02:00
|
|
|
if (checkBox7Among8->isChecked())
|
2011-08-28 19:24:37 +02:00
|
|
|
params.addVariant(GameParams::k7AMONG8);
|
2011-08-27 19:21:26 +02:00
|
|
|
|
2008-01-20 19:40:12 +01:00
|
|
|
// Create the game
|
2011-08-28 19:16:59 +02:00
|
|
|
Game *tmpGame = GameFactory::Instance()->createGame(params);
|
2008-11-30 21:53:44 +01:00
|
|
|
PublicGame *game = new PublicGame(*tmpGame);
|
2008-01-20 19:40:12 +01:00
|
|
|
|
|
|
|
// Add the players
|
2011-08-27 20:00:37 +02:00
|
|
|
if (!radioButtonTraining->isChecked())
|
2008-01-20 19:40:12 +01:00
|
|
|
{
|
2012-02-16 21:59:01 +01:00
|
|
|
const QList<PlayersTableHelper::PlayerDef> &players = m_helper->getPlayers(false);
|
2008-01-26 17:58:46 +01:00
|
|
|
set<QString> allNames;
|
2012-02-16 21:59:01 +01:00
|
|
|
for (int num = 0; num < players.size(); ++num)
|
2008-01-20 19:40:12 +01:00
|
|
|
{
|
2012-02-16 21:59:01 +01:00
|
|
|
QString name = players.at(num).name;
|
2008-01-20 19:40:12 +01:00
|
|
|
if (name == "")
|
|
|
|
name = _q("Player %1").arg(num + 1);
|
2008-01-26 17:58:46 +01:00
|
|
|
// Ensure unicity of the players names
|
|
|
|
if (allNames.find(name) != allNames.end())
|
|
|
|
{
|
|
|
|
int n = 2;
|
|
|
|
while (allNames.find(name + QString(" (%1)").arg(n)) != allNames.end())
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
name += QString(" (%1)").arg(n);
|
|
|
|
}
|
|
|
|
allNames.insert(name);
|
|
|
|
|
2012-02-16 21:59:01 +01:00
|
|
|
QString type = players.at(num).type;
|
2008-01-28 20:17:33 +01:00
|
|
|
Player *player;
|
2008-08-31 13:48:11 +02:00
|
|
|
if (type == _q(kHUMAN))
|
2008-01-28 20:17:33 +01:00
|
|
|
player = new HumanPlayer;
|
2008-01-20 19:40:12 +01:00
|
|
|
else
|
2008-09-03 19:28:27 +02:00
|
|
|
{
|
2012-02-16 21:59:01 +01:00
|
|
|
double level = players.at(num).level;
|
2008-09-03 19:28:27 +02:00
|
|
|
player = new AIPercent(level / 100.);
|
|
|
|
}
|
2011-07-30 21:45:18 +02:00
|
|
|
player->setName(wfq(name));
|
2008-01-28 20:17:33 +01:00
|
|
|
game->addPlayer(player);
|
2008-01-20 19:40:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-29 17:01:31 +01:00
|
|
|
game->addPlayer(new HumanPlayer);
|
2008-01-20 19:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-03 19:28:27 +02:00
|
|
|
void NewGame::enableLevelSpinBox(int index)
|
|
|
|
{
|
|
|
|
spinBoxLevel->setEnabled(index == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-20 19:40:12 +01:00
|
|
|
void NewGame::enableOkButton()
|
|
|
|
{
|
|
|
|
// Enable the "Ok" button:
|
|
|
|
// - always in training mode
|
|
|
|
// - if there is at least one player in duplicate mode
|
|
|
|
// - if there are at least 2 players in free game mode
|
|
|
|
bool disable =
|
2012-02-16 21:59:01 +01:00
|
|
|
(radioButtonDuplicate->isChecked() && m_helper->getRowCount() < 1) ||
|
|
|
|
(radioButtonFreeGame->isChecked() && m_helper->getRowCount() < 2);
|
2008-01-20 19:40:12 +01:00
|
|
|
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!disable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-27 20:00:37 +02:00
|
|
|
void NewGame::enablePlayers(bool checked)
|
2008-01-20 19:40:12 +01:00
|
|
|
{
|
2011-08-27 20:00:37 +02:00
|
|
|
// Testing the "checked" variable prevents from doing the work twice
|
|
|
|
if (checked)
|
|
|
|
{
|
|
|
|
groupBoxPlayers->setEnabled(!radioButtonTraining->isChecked());
|
|
|
|
}
|
2008-01-20 19:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void NewGame::on_pushButtonAdd_clicked()
|
|
|
|
{
|
|
|
|
// Add a new row
|
2012-02-16 21:59:01 +01:00
|
|
|
m_helper->addRow(lineEditName->displayText(),
|
|
|
|
comboBoxType->currentText(),
|
|
|
|
spinBoxLevel->isEnabled() ?
|
|
|
|
QString("%1").arg(spinBoxLevel->value()) : "");
|
2008-09-03 19:28:27 +02:00
|
|
|
|
|
|
|
// Increment the player ID
|
|
|
|
static int currPlayer = 2;
|
|
|
|
if (lineEditName->displayText() == _q("Player %1").arg(currPlayer))
|
|
|
|
{
|
|
|
|
++currPlayer;
|
|
|
|
lineEditName->setText(_q("Player %1").arg(currPlayer));
|
|
|
|
}
|
2008-01-20 19:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 19:33:32 +01:00
|
|
|
void NewGame::on_checkBoxJoker_stateChanged(int newState)
|
|
|
|
{
|
|
|
|
if (newState == Qt::Checked)
|
|
|
|
checkBoxExplosive->setChecked(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void NewGame::on_checkBoxExplosive_stateChanged(int newState)
|
|
|
|
{
|
|
|
|
if (newState == Qt::Checked)
|
|
|
|
checkBoxJoker->setChecked(false);
|
|
|
|
}
|
2008-01-20 19:40:12 +01:00
|
|
|
|