コード例 #1
0
ファイル: chessplayer.cpp プロジェクト: joergoster/cutechess
void ChessPlayer::setState(State state)
{
	if (state == m_state)
		return;
	if (m_state == Thinking)
		emit stoppedThinking();

	m_state = state;
}
コード例 #2
0
ファイル: mainwindow.cpp プロジェクト: thesmartwon/OpenChess
void MainWindow::setCurrentGame(const TabData& gameData)
{
	if (gameData.game == m_game && m_game != nullptr)
		return;

	for (int i = 0; i < 2; i++)
	{
		ChessPlayer* player(m_players[i]);
		if (player != nullptr)
		{
			disconnect(player, nullptr, m_engineDebugLog, nullptr);
			disconnect(player, nullptr, m_chessClock[0], nullptr);
			disconnect(player, nullptr, m_chessClock[1], nullptr);
		}
	}

	if (m_game != nullptr)
	{
		m_game->pgn()->setTagReceiver(nullptr);
		m_gameViewer->disconnectGame();
		disconnect(m_game, nullptr, m_moveList, nullptr);

		ChessGame* tmp = m_game;
		m_game = nullptr;

		// QObject::disconnect() is not atomic, so we need to flush
		// all pending events from the previous game before switching
		// to the next one.
		tmp->lockThread();
		CuteChessApplication::processEvents();
		tmp->unlockThread();

		// If the call to CuteChessApplication::processEvents() caused
		// a new game to be selected as the current game, then our
		// work here is done.
		if (m_game != nullptr)
			return;
	}

	m_game = gameData.game;

	lockCurrentGame();

	m_engineDebugLog->clear();

	m_moveList->setGame(m_game, gameData.pgn);

	if (m_game == nullptr)
	{
		m_gameViewer->setGame(gameData.pgn);

		for (int i = 0; i < 2; i++)
		{
			ChessClock* clock(m_chessClock[i]);
			clock->stop();
			clock->setInfiniteTime(true);
			clock->setPlayerName(gameData.pgn->playerName(Chess::Side::Type(i)));
		}

		updateWindowTitle();

		return;
	}
	else
		m_gameViewer->setGame(m_game);

	m_tagsModel->setTags(gameData.pgn->tags());
	gameData.pgn->setTagReceiver(m_tagsModel);

	for (int i = 0; i < 2; i++)
	{
		ChessPlayer* player(m_game->player(Chess::Side::Type(i)));
		m_players[i] = player;

		connect(player, SIGNAL(debugMessage(QString)),
			m_engineDebugLog, SLOT(appendPlainText(QString)));

		ChessClock* clock(m_chessClock[i]);

		clock->stop();
		clock->setPlayerName(player->name());
		connect(player, SIGNAL(nameChanged(QString)),
			clock, SLOT(setPlayerName(QString)));

		clock->setInfiniteTime(player->timeControl()->isInfinite());

		if (player->state() == ChessPlayer::Thinking)
			clock->start(player->timeControl()->activeTimeLeft());
		else
			clock->setTime(player->timeControl()->timeLeft());

		connect(player, SIGNAL(startedThinking(int)),
			clock, SLOT(start(int)));
		connect(player, SIGNAL(stoppedThinking()),
			clock, SLOT(stop()));
	}

	updateWindowTitle();
	unlockCurrentGame();
}
コード例 #3
0
ファイル: gamewall.cpp プロジェクト: MichaelB7/cutechess
void GameWall::addGame(ChessGame* game)
{
	Q_ASSERT(game != nullptr);

	QWidget* widget = new QWidget(this);

	ChessClock* clock[2] = { new ChessClock(), new ChessClock() };
	QHBoxLayout* clockLayout = new QHBoxLayout();
	for (int i = 0; i < 2; i++)
	{
		clock[i] = new ChessClock();
		clockLayout->addWidget(clock[i]);

		Chess::Side side = Chess::Side::Type(i);
		clock[i]->setPlayerName(side.toString());
	}
	clockLayout->insertSpacing(1, 20);

	BoardScene* scene = new BoardScene();
	BoardView* view = new BoardView(scene);

	QVBoxLayout* mainLayout = new QVBoxLayout();
	mainLayout->addLayout(clockLayout);
	mainLayout->addWidget(view);
	mainLayout->setContentsMargins(0, 0, 0, 0);

	widget->setLayout(mainLayout);
	layout()->addWidget(widget);

	game->lockThread();
	connect(game, SIGNAL(fenChanged(QString)),
		scene, SLOT(setFenString(QString)));
	connect(game, SIGNAL(moveMade(Chess::GenericMove, QString, QString)),
		scene, SLOT(makeMove(Chess::GenericMove)));
	connect(game, SIGNAL(humanEnabled(bool)),
		view, SLOT(setEnabled(bool)));

	for (int i = 0; i < 2; i++)
	{
		ChessPlayer* player(game->player(Chess::Side::Type(i)));

		if (player->isHuman())
			connect(scene, SIGNAL(humanMove(Chess::GenericMove, Chess::Side)),
				player, SLOT(onHumanMove(Chess::GenericMove, Chess::Side)));

		clock[i]->setPlayerName(player->name());
		connect(player, SIGNAL(nameChanged(QString)),
			clock[i], SLOT(setPlayerName(QString)));

		clock[i]->setInfiniteTime(player->timeControl()->isInfinite());

		if (player->state() == ChessPlayer::Thinking)
			clock[i]->start(player->timeControl()->activeTimeLeft());
		else
			clock[i]->setTime(player->timeControl()->timeLeft());

		connect(player, SIGNAL(startedThinking(int)),
			clock[i], SLOT(start(int)));
		connect(player, SIGNAL(stoppedThinking()),
			clock[i], SLOT(stop()));
	}

	scene->setBoard(game->pgn()->createBoard());
	scene->populate();

	foreach (const Chess::Move& move, game->moves())
		scene->makeMove(move);

	game->unlockThread();

	view->setEnabled(!game->isFinished() &&
			 game->playerToMove()->isHuman());
	m_games[game] = widget;

	cleanupWidgets();
}