Commit cb270840 authored by Denis Kuplyakov's avatar Denis Kuplyakov

Extracted all common functions to Utils namespace

parent f445b350
......@@ -22,6 +22,7 @@ include_directories( ${CMAKE_SOURCE_DIR}/libkdegames/highscore )
########### next target ##############
set(kreversi_SRCS
commondefs.cpp
colorscheme.cpp
kreversigame.cpp
kreversiview.cpp
......
......@@ -398,7 +398,7 @@ KReversiMove Engine::computeMove(const KReversiGame& game, bool competitive)
m_bc_score->set(Black, CalcBcScore(Black));
quint64 colorbits = ComputeOccupiedBits(color);
quint64 opponentbits = ComputeOccupiedBits(opponentColorFor(color));
quint64 opponentbits = ComputeOccupiedBits(Utils::opponentColorFor(color));
int maxval = -LARGEINT;
int max_x = 0;
......@@ -524,7 +524,7 @@ int Engine::ComputeMove2(int xplay, int yplay, ChipColor color, int level,
{
int number_of_turned = 0;
SquareStackEntry mse;
ChipColor opponent = opponentColorFor(color);
ChipColor opponent = Utils::opponentColorFor(color);
m_nodes_searched++;
......@@ -687,7 +687,7 @@ int Engine::EvaluatePosition(ChipColor color)
{
int retval;
ChipColor opponent = opponentColorFor(color);
ChipColor opponent = Utils::opponentColorFor(color);
int score_color = m_score->score(color);
int score_opponent = m_score->score(opponent);
......
......@@ -20,9 +20,10 @@
* Boston, MA 02110-1301, USA.
*
********************************************************************/
#include "colorscheme.h"
#include <KColorScheme>
ColorScheme::ColorScheme(QDeclarativeItem *parent) :
QDeclarativeItem(parent)
{
......
......@@ -25,7 +25,6 @@
#define COLORSCHEME_H
#include <QDeclarativeItem>
#include <KColorScheme>
#include <QColor>
class ColorScheme : public QDeclarativeItem
......
/*******************************************************************
*
* Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
* Copyright 2013 Denis Kuplyakov <dener.kup@gmail.com>
*
* This file is part of the KDE project "KReversi"
*
* KReversi 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, or (at your option)
* any later version.
*
* KReversi 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 KReversi; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
********************************************************************/
#include <commondefs.h>
#include <KLocale>
ChipColor Utils::opponentColorFor(ChipColor color)
{
if (color == NoColor)
return NoColor;
else
return (color == White ? Black : White);
}
QString Utils::colorToString(const ChipColor &color) {
if (Preferences::useColoredChips())
return (color == Black ? i18n("Blue") : i18n("Red"));
return (color == Black ? i18n("Black") : i18n("White"));
}
QString Utils::moveToString(const KReversiMove& move)
{
QString moveString = colorToString(move.color);
const char labelsHor[] = "ABCDEFGH";
const char labelsVer[] = "12345678";
moveString += QLatin1Char(' ');
moveString += QLatin1Char(labelsHor[move.col]);
moveString += QLatin1Char(labelsVer[move.row]);
return moveString;
}
int Utils::difficultyLevelToInt() {
for (int i = 0; i < Kg::difficulty()->levels().size(); i++)
if (Kg::difficultyLevel()
== Kg::difficulty()->levels()[i]->standardLevel())
return i;
return -1;
}
const KgDifficultyLevel *Utils::intToDifficultyLevel(int skill) {
return Kg::difficulty()->levels()[skill];
}
......@@ -24,6 +24,12 @@
#ifndef COMMONDEFS_H
#define COMMONDEFS_H
#include <QString>
#include <KgDifficulty>
#include <KLocale>
#include "preferences.h"
// noColor = empty
enum ChipColor {White = 0, Black = 1, NoColor = 2};
......@@ -55,15 +61,14 @@ struct KReversiMove: public KReversiPos {
}
};
static ChipColor opponentColorFor(ChipColor color)
{
if (color == NoColor)
return NoColor;
else
return (color == White ? Black : White);
namespace Utils {
ChipColor opponentColorFor(ChipColor color);
QString colorToString(const ChipColor &color);
QString moveToString(const KReversiMove& move);
int difficultyLevelToInt();
const KgDifficultyLevel *intToDifficultyLevel(int skill);
}
typedef QList<KReversiMove> MoveList;
#endif
......@@ -98,7 +98,7 @@ void KReversiGame::makeMove(const KReversiMove &move)
void KReversiGame::startNextTurn()
{
m_curPlayer = opponentColorFor(m_lastPlayer);
m_curPlayer = Utils::opponentColorFor(m_lastPlayer);
emit moveFinished(); // previous move has just finished
......@@ -149,7 +149,7 @@ int KReversiGame::undo()
// and change back the color of the rest chips
foreach(const KReversiMove & pos, lastUndo) {
ChipColor opponentColor = opponentColorFor(m_cells[pos.row][pos.col]);
ChipColor opponentColor = Utils::opponentColorFor(m_cells[pos.row][pos.col]);
setChipColor(KReversiMove(opponentColor, pos.row, pos.col));
}
......@@ -221,7 +221,7 @@ bool KReversiGame::hasChunk(int dirNum, const KReversiMove& move) const
//
// Well, I wrote this description from my head, now lets produce some code for that ;)
ChipColor opColor = opponentColorFor(move.color);
ChipColor opColor = Utils::opponentColorFor(move.color);
int opponentChipsNum = 0;
bool foundPlayerColor = false;
......
......@@ -28,39 +28,7 @@ static const int BLACK_STATUSBAR_ID = 1;
static const int WHITE_STATUSBAR_ID = 2;
static const int COMMON_STATUSBAR_ID = 0;
static QString colorToString(const ChipColor &color) {
if (Preferences::useColoredChips())
return (color == Black ? i18n("Blue") : i18n("Red"));
return (color == Black ? i18n("Black") : i18n("White"));
}
static QString moveToString(const KReversiMove& move)
{
QString moveString = colorToString(move.color);
const char labelsHor[] = "ABCDEFGH";
const char labelsVer[] = "12345678";
moveString += QLatin1Char(' ');
moveString += QLatin1Char(labelsHor[move.col]);
moveString += QLatin1Char(labelsVer[move.row]);
return moveString;
}
static int difficultyLevelToInt() {
for (int i = 0; i < Kg::difficulty()->levels().size(); i++)
if (Kg::difficultyLevel()
== Kg::difficulty()->levels()[i]->standardLevel())
return i;
return -1;
}
static const KgDifficultyLevel *intToDifficultyLevel(int skill) {
return Kg::difficulty()->levels()[skill];
}
KReversiMainWindow::KReversiMainWindow(QWidget* parent, bool startDemo)
: KXmlGuiWindow(parent), m_view(0), m_game(0),
......@@ -188,7 +156,7 @@ void KReversiMainWindow::levelChanged()
// we are assuming that level can be changed here only when it is
// USER-AI or AI-USER match
int skill = difficultyLevelToInt();
int skill = Utils::difficultyLevelToInt();
if (m_nowPlayingInfo.type[White] == GameStartInformation::AI)
((KReversiComputerPlayer *)(m_player[White]))->setSkill(skill);
......@@ -311,16 +279,16 @@ void KReversiMainWindow::slotGameOver()
if (blackScore == whiteScore) {
res = i18n("Game is drawn!");
} else if (blackScore > whiteScore) {
res = i18n("%1 has won!", colorToString(Black));
res = i18n("%1 has won!", Utils::colorToString(Black));
} else {
res = i18n("%1 has won!", colorToString(White));
res = i18n("%1 has won!", Utils::colorToString(White));
}
}
if (m_nowPlayingInfo.type[Black] == GameStartInformation::AI
&& m_nowPlayingInfo.type[White] == GameStartInformation::AI) {
res += i18n("\n%1: %2", colorToString(Black), blackScore);
res += i18n("\n%1: %2", colorToString(White), whiteScore);
res += i18n("\n%1: %2", Utils::colorToString(Black), blackScore);
res += i18n("\n%1: %2", Utils::colorToString(White), whiteScore);
} else {
res += i18n("\n%1: %2", m_nowPlayingInfo.name[Black], blackScore);
res += i18n("\n%1: %2", m_nowPlayingInfo.name[White], whiteScore);
......@@ -347,7 +315,7 @@ void KReversiMainWindow::updateHistory() {
for (int i = 0; i < history.size(); i++) {
QString numStr = QString::number(i + 1) + QLatin1String(". ");
m_historyView->addItem(numStr + moveToString(history.at(i)));
m_historyView->addItem(numStr + Utils::moveToString(history.at(i)));
}
QListWidgetItem *last = m_historyView->item(m_historyView->count() - 1);
......@@ -399,15 +367,15 @@ void KReversiMainWindow::updateStatusBar()
if (m_nowPlayingInfo.type[Black] == GameStartInformation::AI
&& m_nowPlayingInfo.type[White] == GameStartInformation::AI) { // using Black White names
statusBar()->changeItem(i18n("%1: %2",
colorToString(Black),
Utils::colorToString(Black),
m_game->playerScore(Black)), BLACK_STATUSBAR_ID);
statusBar()->changeItem(i18n("%1: %2",
colorToString(White),
Utils::colorToString(White),
m_game->playerScore(White)), WHITE_STATUSBAR_ID);
if (!m_game->isGameOver()) {
statusBar()->changeItem(i18n("%1 turn",
colorToString(m_game->currentPlayer())),
Utils::colorToString(m_game->currentPlayer())),
COMMON_STATUSBAR_ID);
}
} else { // using player's names
......@@ -429,7 +397,7 @@ void KReversiMainWindow::startDemo() {
GameStartInformation info;
info.name[0] = info.name[1] = i18n("Computer");
info.type[0] = info.type[1] = GameStartInformation::AI;
info.skill[0] = info.skill[1] = difficultyLevelToInt();
info.skill[0] = info.skill[1] = Utils::difficultyLevelToInt();
receivedGameStartInformation(info);
}
......@@ -479,12 +447,12 @@ void KReversiMainWindow::receivedGameStartInformation(GameStartInformation info)
if (info.type[White] == GameStartInformation::AI
&& info.type[Black] == GameStartInformation::Human) {
Kg::difficulty()->setEditable(true);
Kg::difficulty()->select(intToDifficultyLevel(info.skill[White]));
Kg::difficulty()->select(Utils::intToDifficultyLevel(info.skill[White]));
}
else if (info.type[White] == GameStartInformation::Human
&& info.type[Black] == GameStartInformation::AI) {
Kg::difficulty()->setEditable(true);
Kg::difficulty()->select(intToDifficultyLevel(info.skill[Black]));
Kg::difficulty()->select(Utils::intToDifficultyLevel(info.skill[Black]));
}
else
Kg::difficulty()->setEditable(false);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment