Initial commit on develop branch

This commit is contained in:
2026-04-17 20:07:57 +03:30
commit 9b68b97ae6
52 changed files with 3499 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
{
GAME_HISTORY_LIMIT = 100
BOARD_SIZE = 8
BOARD_COLOR = black
BOARD_BORDER_COLOR = white
SOUND = 0
SHOW_AVAILABLE_PLACES_FOR_PIECES = 1
HINT = 0
HINT_LIMIT = 3
}
+201
View File
@@ -0,0 +1,201 @@
#include <iostream>
#include <fstream>
#include "../Utilities/StrToInt.h"
#include "../utilities/IntToStr.h"
#include "ConfigManager.h"
using namespace std;
string getElementOfConfig(int index);
int getGameHistoryLimit();
int getBoardSize();
std::string getBoardColor();
std::string getBoardBorderColor();
bool getSound();
bool getShowAvailablePlacesForPieces();
bool getHint();
int getHintLimit();
void modifyElementOfConfig(int index, string newValue);
void modifyGameHistoryLimit(int);
void modifyBoardSize(int);
void modifyBoardColor(std::string);
void modifyBoardBorderColor(std::string);
void modifySound(bool);
void modifyShowAvailablePlacesForPieces(bool);
void modifyHint(bool);
void modifyHintLimit(int);
string getElementOfConfig(int index)
{
if (index > 7)
{
throw 2; // out of bond
}
ifstream file ("codes/Database/Config.txt");
if (!file.is_open())
{
throw 1; // failed to read the file
}
string line;
for (int i{0}; i <= index + 1; i++)
{
getline(file, line);
}
int startingIndex = line.find("=") + 2;
string result = line.substr(startingIndex, line.size());
file.close();
return result;
}
void modifyElementOfConfig(int index, string newValue)
{
if (index > 7)
{
throw 2; // out of bounds
}
std::ifstream fileToRead("codes/Database/Config.txt");
if (!fileToRead.is_open())
{
throw 1; // failed to read the file
}
std::string lines[10];
for (int i{0}; i < 10; i++)
{
getline(fileToRead, lines[i]);
}
fileToRead.close();
string targetLine = lines[index + 1];
int startingIndex = targetLine.find("=") + 2;
targetLine = targetLine.substr(0, startingIndex) + newValue;
lines[index + 1] = targetLine;
std::ofstream fileToWrite ("codes/Database/Config.txt");
if (!fileToWrite.is_open())
{
throw 1; // failed to read the file
}
for (std::string l : lines)
{
fileToWrite << l << "\n";
}
fileToWrite.close();
}
int getGameHistoryLimit()
{
return strToInt(getElementOfConfig(0));
}
int getBoardSize()
{
return strToInt(getElementOfConfig(1));
}
string getBoardColor()
{
return getElementOfConfig(2);
}
string getBoardBorderColor()
{
return getElementOfConfig(3);
}
bool getSound()
{
int intBool = strToInt(getElementOfConfig(4));
return static_cast<bool>(intBool);
}
bool getShowAvailablePlacesForPieces()
{
int intBool = strToInt(getElementOfConfig(5));
return static_cast<bool>(intBool);
}
bool getHint()
{
int intBool = strToInt(getElementOfConfig(6));
return static_cast<bool>(intBool);
}
int getHintLimit()
{
return strToInt(getElementOfConfig(7));
}
void modifyGameHistoryLimit(int gameHistoryLimit)
{
modifyElementOfConfig(0, intToStr(gameHistoryLimit));
}
void modifyBoardSize(int boardSize)
{
modifyElementOfConfig(1, intToStr(boardSize));
}
void modifyBoardColor(string color)
{
modifyElementOfConfig(2, color);
}
void modifyBoardBorderColor(string color)
{
modifyElementOfConfig(3, color);
}
void modifySound(bool sound)
{
int soundToSave = static_cast<int>(sound);
modifyElementOfConfig(4, intToStr(soundToSave));
}
void modifyShowAvailablePlacesForPieces(bool newValue)
{
int newValueToSave = static_cast<int>(newValue);
modifyElementOfConfig(5, intToStr(newValueToSave));
}
void modifyHint(bool hint)
{
int hintToSave = static_cast<int>(hint);
modifyElementOfConfig(6, intToStr(hintToSave));
}
void modifyHintLimit(int hintLimit)
{
modifyElementOfConfig(7, intToStr(hintLimit));
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <string>
int getGameHistoryLimit();
int getBoardSize();
std::string getBoardColor();
std::string getBoardBorderColor();
bool getSound();
bool getShowAvailablePlacesForPieces();
bool getHint();
int getHintLimit();
void modifyGameHistoryLimit(int);
void modifyBoardSize(int);
void modifyBoardColor(std::string);
void modifyBoardBorderColor(std::string);
void modifySound(bool);
void modifyShowAvailablePlacesForPieces(bool);
void modifyHint(bool);
void modifyHintLimit(int);
+3
View File
@@ -0,0 +1,3 @@
[0][.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|W|B|.|.|.|.|.|.|W|B|B|.|.|.|.|.|W|B|W|.|.|.|.|.|B|.|.|.|.|.|.|B|W|.|.|.|.|.|.|.|.|.|.|.|.|.][2Player][ramtin|W][farzad|W][W][0]
[1][.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|W|B|.|.|.|.|.|B|B|B|B|.|.|.|.|W|W|W|W|.|.|.|.|.|.|B|B|W|.|.|.|.|W|B|B|.|.|.|.|.|.|.|.|.|.|.][2Player][ramtin|W][farzad|B][B][0]
[2][W|W|W|W|W|W|W|W|W|B|B|B|B|W|B|W|W|W|W|W|B|B|W|W|W|B|W|W|W|W|B|W|W|B|W|W|W|W|B|W|W|W|W|W|B|W|W|W|W|B|B|B|W|W|W|W|W|W|W|W|W|W|B|W][1Player][ramtin|B][Hard Bot|3][W][2]
@@ -0,0 +1,391 @@
// a saved game will follow this patter:
// [gameId][.|.|.|.][1Player][PlayerName|PlayerColor][BotName or PlayerName|BotColor or PlayerColor]['B'][0] and winner at the end
// GameBoard mode Player1 Player2 or Bot current turn
#include "../Structures/Config.h"
#include "../Structures/SinglePlayerGame.h"
#include "../Structures/MultiPlayerGame.h"
#include "../Utilities/StrToInt.h"
#include "../Utilities/IntSqrt.h"
#include "../Utilities/StrToSymbol.h"
#include "GameHistoryManager.h"
#include <fstream>
// file managing
void writeTempoToGameHistory();
void updateLine(int lineIndex, std::string str);
void addLine(std::string str);
void freeLastLine();
// internal uses
int getLinesCount();
std::string getProperty(int propertyId, std::string game);
int getGameId(std::string game);
int getSavedBoardElementCount(std::string board);
// adding data
void saveGame(SinglePlayerGame* game);
void saveGame(MultiPlayerGame* game);
// extracting data
std::string getGameById(int gameId);
int getLineIndexById(int gameId);
std::string getLineByLineIndex(int lineIndex);
// external uses
SinglePlayerGame getSinglePlayerGame(int id);
MultiPlayerGame getMultiPlayerGame(int id);
void syncGameHistoryWithLimit();
int getFirstGameId();
int getLastGameId();
//----------------------------------- file managing --------------------------------------------------------------
void writeTempoToGameHistory()
{
std::ifstream tempoFileToRead("codes/Database/tempo.txt");
std::ofstream GameHistoryToWrite("codes/Database/GameHistory.txt");
if (!GameHistoryToWrite.is_open() && !tempoFileToRead.is_open())
{
throw 1; // faild to read file
}
std::string line;
while (getline(tempoFileToRead, line))
{
GameHistoryToWrite << line << "\n";
}
tempoFileToRead.close();
GameHistoryToWrite.close();
}
void freeLastLine()
{
std::ifstream GameHistoryToRead("codes/Database/GameHistory.txt");
std::ofstream tempoFileToWrite("codes/Database/tempo.txt");
if (!GameHistoryToRead.is_open() && !tempoFileToWrite.is_open())
{
throw 1; // faild to read file
}
std::string line;
getline(GameHistoryToRead, line); // skipping the first line
while (getline(GameHistoryToRead, line))
{
tempoFileToWrite << line << "\n";
}
tempoFileToWrite.close();
GameHistoryToRead.close();
writeTempoToGameHistory();
}
void updateLine(int lineIndex, std::string str)
{
std::ifstream GameHistoryToRead("codes/Database/GameHistory.txt");
std::ofstream tempoFileToWrite("codes/Database/tempo.txt");
if (!GameHistoryToRead.is_open() && !tempoFileToWrite.is_open())
{
throw 1; // faild to read file
}
int curLineIndex = 0;
std::string line;
while (getline(GameHistoryToRead, line))
{
if (curLineIndex == lineIndex)
{
tempoFileToWrite << str << "\n";
}
else
{
tempoFileToWrite << line << "\n";
}
curLineIndex++;
}
tempoFileToWrite.close();
GameHistoryToRead.close();
writeTempoToGameHistory();
}
void addLine(std::string str)
{
std::ofstream GameHistory("codes/Database/GameHistory.txt", std::ios::app);
GameHistory << str << "\n";
GameHistory.close();
}
//----------------------------------- internal use -----------------------------------------------
int getLinesCount()
{
int count = 0;
std::string line;
std::ifstream fileToRead("codes/Database/GameHistory.txt");
if (!fileToRead.is_open())
{
throw 1; // faild to read file
}
while (getline(fileToRead, line))
{
if (!line.empty()) count++;
}
return count;
}
std::string getProperty(int propertyId, std::string game)
{
for (int i = 0; i < propertyId; i++)
{
int endingIndex = game.find("]");
game = game.substr(endingIndex + 1, game.size());
}
int EndingIndex = game.find("]");
std::string property = game.substr(
1, EndingIndex - 1
);
return property;
}
int getGameId(std::string game)
{
return strToInt(getProperty(0, game));
}
void syncGameHistoryWithLimit()
// has to be called when limit changes, so GameHistory.txt stays within its new limit
{
int curCount = getLinesCount();
int limit = Config::getInstance() -> GAME_HISTORY_LIMIT;
if (curCount > limit)
{
int toRemove = curCount - limit;
for (int i = 0; i < toRemove; i++)
{
freeLastLine();
}
}
}
int getSavedBoardElementCount(std::string board)
{
int count = 0;
for (char element : board)
{
if (element == '|') count++;
}
return count + 1;
}
//--------------------------------------------- adding data --------------------------------------------------
void saveGame(SinglePlayerGame* game)
{
int thisGameId = game -> id;
std::string gameToSave = game -> retrieveGame();
int lineIndex = getLineIndexById(thisGameId);
if (lineIndex == -1)
{
addLine(gameToSave);
syncGameHistoryWithLimit();
}
else
{
updateLine(lineIndex, gameToSave);
}
}
void saveGame(MultiPlayerGame* game)
{
int thisGameId = game -> id;
std::string gameToSave = game -> retrieveGame();
int lineIndex = getLineIndexById(thisGameId);
if (lineIndex == -1)
{
addLine(gameToSave);
syncGameHistoryWithLimit();
}
else
{
updateLine(lineIndex, gameToSave);
}
}
//--------------------------------------------- extraxting data --------------------------------------------------
std::string getGameById(int gameId)
{
std::string line;
std::ifstream fileToRead("codes/Database/GameHistory.txt");
if (!fileToRead.is_open())
{
throw 1; // faild to read file
}
while (getline(fileToRead, line))
{
if (getGameId(line) == gameId)
{
return line;
}
}
return "";
}
int getLineIndexById(int gameId)
{
std::string line;
std::ifstream fileToRead("codes/Database/GameHistory.txt");
if (!fileToRead.is_open())
{
throw 1; // faild to read file
}
int id = 0;
while (getline(fileToRead, line))
{
if (getGameId(line) == gameId)
{
return id;
}
id++;
}
return -1;
}
std::string getLineByLineIndex(int lineIndex)
{
std::string line;
std::ifstream fileToRead("codes/Database/GameHistory.txt");
if (!fileToRead.is_open())
{
throw 1; // faild to read file
}
int linesCount = getLinesCount();
for (int i = 0; i < linesCount; i++)
{
getline(fileToRead, line);
if (i == lineIndex) return line;
}
return "";
}
//-------------------------------------------- external uses -----------------------------------------
int getFirstGameId()
{
int liensCount = getLinesCount();
if (liensCount == 0) return -1; // file is empty
std::string game = getLineByLineIndex(0);
return getGameId(game);
}
int getLastGameId()
{
int liensCount = getLinesCount();
if (liensCount == 0) return -1; // file is empty
std::string game = getLineByLineIndex(liensCount - 1);
return getGameId(game);
}
SinglePlayerGame getSinglePlayerGame(int id)
{
std::string savedGame = getGameById(id);
std::string savedBoard = getProperty(1, savedGame);
int boardSize = intSqrt(
getSavedBoardElementCount(savedBoard)
);
Board board {boardSize};
board.loadBoard(savedBoard);
Player player {"", 0};
player.loadPlayer(getProperty(3, savedGame));
Bot bot {"", 0};
bot.loadBot(getProperty(4, savedGame));
char turn = strToSymbol(getProperty(5, savedGame));
SinglePlayerGame game {&board, &player, &bot, turn};
game.id = id;
return game;
}
MultiPlayerGame getMultiPlayerGame(int id)
{
std::string savedGame = getGameById(id);
std::string savedBoard = getProperty(1, savedGame);
int boardSize = intSqrt(
getSavedBoardElementCount(savedBoard)
);
Board board {boardSize};
board.loadBoard(savedBoard);
Player player1 {"", 0};
player1.loadPlayer(getProperty(3, savedGame));
Player player2 {"", 0};
player2.loadPlayer(getProperty(4, savedGame));
char turn = strToSymbol(getProperty(5, savedGame));
MultiPlayerGame game {&board, &player1, &player2, turn};
game.id = id;
return game;
}
@@ -0,0 +1,17 @@
#pragma once
#include <string>
#include "../Structures/MultiPlayerGame.h"
#include "../Structures/SinglePlayerGame.h"
void saveGame(SinglePlayerGame* game);
void saveGame(MultiPlayerGame* game);
SinglePlayerGame getSinglePlayerGame(int id);
MultiPlayerGame getMultiPlayerGame(int id);
std::string getGameById(int gameId);
void syncGameHistoryWithLimit();
std::string getProperty(int propertyId, std::string game);
int getSavedBoardElementCount(std::string board);
int getGameId(std::string game);
int getFirstGameId();
int getLastGameId();
+2
View File
@@ -0,0 +1,2 @@
1 3 B
1 4 W
+77
View File
@@ -0,0 +1,77 @@
#include "../Structures/Move.h"
#include <fstream>
#include <string>
void clearGameLog();
void addLog(Move move);
int countLogs();
Move getLog(int logIndex);
void clearGameLog()
{
std::ofstream file ("codes/Database/GameLog.txt", std::ios::trunc);
if (!file.is_open())
{
throw 1; // failed to read file
}
file.close();
}
void addLog(Move move)
{
std::ofstream file("codes/Database/GameLog.txt", std::ios::app);
if (!file.is_open())
{
throw 1; // failed to read file
}
file << move.retrieveMove() << "\n";
file.close();
}
int countLogs()
{
int logCount = 0;
std::ifstream file("codes/Database/GameLog.txt");
std::string line;
while(std::getline(file, line))
{
if (line.empty()) continue;
logCount++;
}
return logCount;
}
Move getLog(int logIndex)
{
std::ifstream file ("codes/Database/GameLog.txt");
if (!file.is_open())
{
throw 1; // failed to read the file
}
std::string line;
for (int i{0}; i <= logIndex; i++)
{
std::getline(file, line);
}
Move move{0, 0, '0'};
move.loadMove(line);
return move;
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include "../Structures/Move.h"
void clearGameLog();
void addLog(Move move);
int countLogs();
Move getLog(int logIndex);
+3
View File
@@ -0,0 +1,3 @@
[0][.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|W|B|.|.|.|.|.|.|W|B|B|.|.|.|.|.|W|B|W|.|.|.|.|.|B|.|.|.|.|.|.|B|W|.|.|.|.|.|.|.|.|.|.|.|.|.][2Player][ramtin|W][farzad|W][W][0]
[1][.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|.|W|B|.|.|.|.|.|B|B|B|B|.|.|.|.|W|W|W|W|.|.|.|.|.|.|B|B|W|.|.|.|.|W|B|B|.|.|.|.|.|.|.|.|.|.|.][2Player][ramtin|W][farzad|B][B][0]
[2][W|W|W|W|W|W|W|W|W|B|B|B|B|W|B|W|W|W|W|W|B|B|W|W|W|B|W|W|W|W|B|W|W|B|W|W|W|W|B|W|W|W|W|W|B|W|W|W|W|B|B|B|W|W|W|W|W|W|W|W|W|W|B|W][1Player][ramtin|B][Hard Bot|3][W][2]