diff --git a/bin/app/data/fallback/ApplicationSettings.xml b/bin/app/data/fallback/ApplicationSettings.xml index 10367d1d..94d9b9de 100644 --- a/bin/app/data/fallback/ApplicationSettings.xml +++ b/bin/app/data/fallback/ApplicationSettings.xml @@ -1,18 +1,19 @@ - + 500 500 - - 0 - + + 0 + ./projects/tutorial/tutorial.srctrlprj - ./projects/tictactoe/tictactoe.srctrlprj + ./projects/tictactoe_cpp/tictactoe_cpp.srctrlprj + ./projects/tictactoe_py/tictactoe_py.srctrlprj ./projects/javaparser/javaparser.srctrlprj - 6 + 7 diff --git a/bin/app/user/projects/tictactoe/src/artificial_player.cpp b/bin/app/user/projects/tictactoe_cpp/src/artificial_player.cpp similarity index 100% rename from bin/app/user/projects/tictactoe/src/artificial_player.cpp rename to bin/app/user/projects/tictactoe_cpp/src/artificial_player.cpp diff --git a/bin/app/user/projects/tictactoe/src/artificial_player.h b/bin/app/user/projects/tictactoe_cpp/src/artificial_player.h similarity index 100% rename from bin/app/user/projects/tictactoe/src/artificial_player.h rename to bin/app/user/projects/tictactoe_cpp/src/artificial_player.h diff --git a/bin/app/user/projects/tictactoe/src/field.cpp b/bin/app/user/projects/tictactoe_cpp/src/field.cpp similarity index 91% rename from bin/app/user/projects/tictactoe/src/field.cpp rename to bin/app/user/projects/tictactoe_cpp/src/field.cpp index d87128a8..6b3f0034 100644 --- a/bin/app/user/projects/tictactoe/src/field.cpp +++ b/bin/app/user/projects/tictactoe_cpp/src/field.cpp @@ -81,14 +81,16 @@ int Field::SameInRow( Token token, int amount ) const { for ( int i = 0; i < 3; i++ ) { if ( grid_[i][0] + grid_[i][1] + grid_[i][2] == sum ) { count++; - } else if ( grid_[0][i] + grid_[1][i] + grid_[2][i] == sum ) { + } + if ( grid_[0][i] + grid_[1][i] + grid_[2][i] == sum ) { count++; } } if ( grid_[0][0] + grid_[1][1] + grid_[2][2] == sum ) { count++; - } else if ( grid_[2][0] + grid_[1][1] + grid_[0][2] == sum ) { + } + if ( grid_[2][0] + grid_[1][1] + grid_[0][2] == sum ) { count++; } @@ -117,7 +119,7 @@ void Field::MakeMove( const Move& move, Token token ) { } void Field::ClearMove( const Move& move ) { - if ( !InRange( move ) || !IsEmpty( move ) || left_ == 9 ) { + if ( !InRange( move ) || IsEmpty( move ) || left_ == 9 ) { return; } diff --git a/bin/app/user/projects/tictactoe/src/field.h b/bin/app/user/projects/tictactoe_cpp/src/field.h similarity index 100% rename from bin/app/user/projects/tictactoe/src/field.h rename to bin/app/user/projects/tictactoe_cpp/src/field.h diff --git a/bin/app/user/projects/tictactoe/src/game_object.h b/bin/app/user/projects/tictactoe_cpp/src/game_object.h similarity index 100% rename from bin/app/user/projects/tictactoe/src/game_object.h rename to bin/app/user/projects/tictactoe_cpp/src/game_object.h diff --git a/bin/app/user/projects/tictactoe/src/human_player.cpp b/bin/app/user/projects/tictactoe_cpp/src/human_player.cpp similarity index 100% rename from bin/app/user/projects/tictactoe/src/human_player.cpp rename to bin/app/user/projects/tictactoe_cpp/src/human_player.cpp diff --git a/bin/app/user/projects/tictactoe/src/human_player.h b/bin/app/user/projects/tictactoe_cpp/src/human_player.h similarity index 100% rename from bin/app/user/projects/tictactoe/src/human_player.h rename to bin/app/user/projects/tictactoe_cpp/src/human_player.h diff --git a/bin/app/user/projects/tictactoe/src/io.cpp b/bin/app/user/projects/tictactoe_cpp/src/io.cpp similarity index 100% rename from bin/app/user/projects/tictactoe/src/io.cpp rename to bin/app/user/projects/tictactoe_cpp/src/io.cpp diff --git a/bin/app/user/projects/tictactoe/src/io.h b/bin/app/user/projects/tictactoe_cpp/src/io.h similarity index 100% rename from bin/app/user/projects/tictactoe/src/io.h rename to bin/app/user/projects/tictactoe_cpp/src/io.h diff --git a/bin/app/user/projects/tictactoe/src/main.cpp b/bin/app/user/projects/tictactoe_cpp/src/main.cpp similarity index 100% rename from bin/app/user/projects/tictactoe/src/main.cpp rename to bin/app/user/projects/tictactoe_cpp/src/main.cpp diff --git a/bin/app/user/projects/tictactoe/src/player.cpp b/bin/app/user/projects/tictactoe_cpp/src/player.cpp similarity index 100% rename from bin/app/user/projects/tictactoe/src/player.cpp rename to bin/app/user/projects/tictactoe_cpp/src/player.cpp diff --git a/bin/app/user/projects/tictactoe/src/player.h b/bin/app/user/projects/tictactoe_cpp/src/player.h similarity index 100% rename from bin/app/user/projects/tictactoe/src/player.h rename to bin/app/user/projects/tictactoe_cpp/src/player.h diff --git a/bin/app/user/projects/tictactoe/src/tictactoe.cpp b/bin/app/user/projects/tictactoe_cpp/src/tictactoe.cpp similarity index 100% rename from bin/app/user/projects/tictactoe/src/tictactoe.cpp rename to bin/app/user/projects/tictactoe_cpp/src/tictactoe.cpp diff --git a/bin/app/user/projects/tictactoe/src/tictactoe.h b/bin/app/user/projects/tictactoe_cpp/src/tictactoe.h similarity index 100% rename from bin/app/user/projects/tictactoe/src/tictactoe.h rename to bin/app/user/projects/tictactoe_cpp/src/tictactoe.h diff --git a/bin/app/user/projects/tictactoe/tictactoe.srctrlprj b/bin/app/user/projects/tictactoe_cpp/tictactoe_cpp.srctrlprj similarity index 100% rename from bin/app/user/projects/tictactoe/tictactoe.srctrlprj rename to bin/app/user/projects/tictactoe_cpp/tictactoe_cpp.srctrlprj diff --git a/bin/app/user/projects/tictactoe_py/src/tictactoe.py b/bin/app/user/projects/tictactoe_py/src/tictactoe.py new file mode 100644 index 00000000..3cd5a0c1 --- /dev/null +++ b/bin/app/user/projects/tictactoe_py/src/tictactoe.py @@ -0,0 +1,309 @@ +from enum import Enum +from random import randint + + +def numberIn(): + return int(input()) + + +def numberOut(num): + print(str(num), end="") + + +def stringOut(str): + print(str, end="") + + +class GameObject: + pass + + +class Field(GameObject): + class Token(Enum): + TOKEN_NONE = 0 + TOKEN_PLAYER_A = 1 + TOKEN_PLAYER_B = 4 + + + def opponent(self, token): + if token == Field.Token.TOKEN_PLAYER_A: + return Field.Token.TOKEN_PLAYER_B + elif token == Field.Token.TOKEN_PLAYER_B: + return Field.Token.TOKEN_PLAYER_A + return Field.Token.TOKEN_NONE + + + class Move: + def __init__(self): + self.row = None + self.col = None + + + def __init__(self): + self._grid = [] + for j in range(0, 3): + row = [] + for i in range(0, 3): + row.append(Field.Token.TOKEN_NONE) + self._grid.append(row) + self._left = 9 + + + def clone(self): + field = Field() + for j in range(0, 3): + for i in range(0, 3): + field._grid[i][j] = self._grid[i][j] + field._left = self._left + return field + + + def clear(self): + for j in range(0, 3): + for i in range(0, 3): + self._grid[i][j] = Field.Token.TOKEN_NONE + self._left = 9 + + + def show(self): + stringOut(" 1 2 3\n") + for row in range(0, 3): + numberOut(row + 1) + stringOut(" ") + for col in range(0, 3): + if self._grid[row][col] == Field.Token.TOKEN_PLAYER_A: + stringOut(" X ") + elif self._grid[row][col] == Field.Token.TOKEN_PLAYER_B: + stringOut(" O ") + else: + stringOut(" ") + if col < 2: + stringOut("|") + if row < 2: + stringOut("\n -----------\n") + stringOut("\n\n") + + + def sameInRow(self, token, amount): + total = amount * token.value + count = 0 + for i in range(0, 3): + if self._grid[i][0].value + self._grid[i][1].value + self._grid[i][2].value == sum: + count += 1 + if self._grid[0][i].value + self._grid[1][i].value + self._grid[2][i].value == sum: + count += 1 + if self._grid[0][0].value + self._grid[1][1].value + self._grid[2][2].value == sum: + count += 1 + if self._grid[2][0].value + self._grid[1][1].value + self._grid[0][2].value == sum: + count += 1 + return count + + + def inRange(self, move): + return move.row >= 0 and move.row < 3 and move.col >= 0 and move.col < 3 + + + def isEmpty(self, move): + return self._grid[move.row][move.col] == Field.Token.TOKEN_NONE + + + def isFull(self): + return self._left == 0 + + + def makeMove(self, move, token): + if not self.inRange(move): + return + if not self.isEmpty(move): + return + if token is Field.Token.TOKEN_NONE: + return + if self.isFull(): + return + self._grid[move.row][move.col] = token + self._left -= 1 + + + def clearMove(self, move): + if not self.inRange(move): + return + if self.isEmpty(move): + return + if self._left == 9: + return + self._grid[move.row][move.col] = Field.Token.TOKEN_NONE + self._left += 1 + + +class Player(GameObject): + def __init__(self, token, name): + self.token = token + self.name = name + + + def turn(field): + raise NotImplementedError + + +class HumanPlayer(Player): + def __init__(self, token, name): + Player.__init__(self, token, name) + + + def turn(self, field): + stringOut(self.name) + stringOut("\n") + while True: + move = self._input() + if self._check(field, move): + break + return move + + + def _input(self): + move = Field.Move() + stringOut("Insert row: ") + move.row = numberIn() - 1 + stringOut("Insert col: ") + move.col = numberIn() - 1 + stringOut("\n") + return move + + + def _check(self, field, move): + if not field.inRange(move): + stringOut("Wrong inpot!\n") + return False + elif not field.isEmpty(move): + stringOut("Is occupied!\n") + return False + return True + + +class ArtificialPlayer(Player): + class Node: + def __init__(self): + self.move = None + self.value = 0 + + + def __init__(self, token, name): + Player.__init__(self, token, name) + + + def turn(self, field): + tempField = field.clone() + node = self._minMax(tempField, self.token) + return node.move + + + def _minMax(self, field, token, prefix = ""): + node = ArtificialPlayer.Node() + node.value = -10000 + + move = Field.Move() + sameMove = 0 + + for j in range(0, 3): + move.row = j + for i in range(0, 3): + move.col = i + + if not field.isEmpty(move): + continue + + field.makeMove(move, token) + + turnValue = self._evaluate(field, token) + if turnValue == 0 and not field.isFull(): + turnValue = -self._minMax(field, field.opponent(token), prefix + " ").value + + field.clearMove(move) + + if turnValue > node.value: + node.move = move + node.value = turnValue + sameMove = 1 + elif turnValue == node.value: + sameMove += 1 + if randint(0, sameMove - 1) == 0: + node.move = move + return node + + + def _evaluate(self, field, token): + if field.sameInRow(token, 3): + return 2 + elif field.sameInRow(field.opponent(token), 2): + return -1 + elif field.sameInRow(token, 2) > 1: + return 1 + return 0 + + +class TicTacToe: + def __init__(self): + self._field = Field() + self._players = [None, None] + + + def start(self): + self._reset() + stringOut("Tic Tac Toe\n\n[1] Human\n[2] Computer\n[3] Quit\n\n") + + self._players[0] = self._selectPlayer(Field.Token.TOKEN_PLAYER_A, "Player A") + if self._players[0] is None: + return False + + self._players[1] = self._selectPlayer(Field.Token.TOKEN_PLAYER_B, "Player B") + if self._players[1] is None: + return False + + stringOut("\n") + return True + + + def run(self): + self._field.show() + playerIndex = 0 + for i in range(0, 9): + player = self._players[playerIndex] + self._field.makeMove(player.turn(self._field), player.token) + self._field.show() + if self._field.sameInRow(player.token, 3): + stringOut(player.name) + stringOut(" won!\n\n") + return + playerIndex = (playerIndex + 1) % 2 + stringOut("Game ends in draw!\n\n") + + + def _reset(self): + for i in range(0, 2): + self._players[i] = None + self._field.clear() + + + def _selectPlayer(self, token, name): + while (True): + stringOut("Choose ") + stringOut(name) + stringOut(": ") + selection = numberIn() + if selection == 1: + return HumanPlayer(token, name) + elif selection == 2: + return ArtificialPlayer(token, name) + elif selection == 3: + return None + stringOut("Wrong input!\n") + + +def main(): + tictactoe = TicTacToe() + while tictactoe.start(): + tictactoe.run() + + +if __name__ == '__main__': + main() diff --git a/bin/app/user/projects/tictactoe_py/tictactoe_py.srctrlprj b/bin/app/user/projects/tictactoe_py/tictactoe_py.srctrlprj new file mode 100644 index 00000000..c4fffb03 --- /dev/null +++ b/bin/app/user/projects/tictactoe_py/tictactoe_py.srctrlprj @@ -0,0 +1,17 @@ + + + + + TicTacToe Source Group + + .py + + + src + + enabled + Python Source Group + + + 7 + diff --git a/cmake/pre_install_linux.cmake b/cmake/pre_install_linux.cmake index 43182f44..04212e32 100644 --- a/cmake/pre_install_linux.cmake +++ b/cmake/pre_install_linux.cmake @@ -19,7 +19,11 @@ execute_process( ) execute_process( - COMMAND ${binPath} index --full ${projPath}/tictactoe/tictactoe.srctrlprj + COMMAND ${binPath} index --full ${projPath}/tictactoe_cpp/tictactoe_cpp.srctrlprj +) + +execute_process( + COMMAND ${binPath} index --full ${projPath}/tictactoe_py/tictactoe_py.srctrlprj ) execute_process( diff --git a/deployment/windows/wixSetup/build.sh b/deployment/windows/wixSetup/build.sh index 9eb9dee3..a43bf0de 100644 --- a/deployment/windows/wixSetup/build.sh +++ b/deployment/windows/wixSetup/build.sh @@ -41,11 +41,13 @@ mkdir -p $OUTPUT_DIR # user SAMPLE_JAVAPARSER_DIR=$ROOT_DIR/bin/app/user/projects/javaparser -SAMPLE_TICTACTOE_DIR=$ROOT_DIR/bin/app/user/projects/tictactoe +SAMPLE_TICTACTOE_CPP_DIR=$ROOT_DIR/bin/app/user/projects/tictactoe_cpp +SAMPLE_TICTACTOE_PY_DIR=$ROOT_DIR/bin/app/user/projects/tictactoe_py SAMPLE_TUTORIAL_DIR=$ROOT_DIR/bin/app/user/projects/tutorial heat.exe dir $SAMPLE_JAVAPARSER_DIR -cg SampleJavaparserComponentGroup -var var.SampleJavaparserSourceDir -out build/sampleJavaparser.wxs -gg -sfrag -g1 -dr SampleProjects -t $SCRIPT_DIR/HeatTransform.xslt -heat.exe dir $SAMPLE_TICTACTOE_DIR -cg SampleTictactoeComponentGroup -var var.SampleTictactoeSourceDir -out build/sampleTictactoe.wxs -gg -sfrag -g1 -dr SampleProjects -t $SCRIPT_DIR/HeatTransform.xslt +heat.exe dir $SAMPLE_TICTACTOE_CPP_DIR -cg SampleTictactoeCppComponentGroup -var var.SampleTictactoeCppSourceDir -out build/sampleTictactoeCpp.wxs -gg -sfrag -g1 -dr SampleProjects -t $SCRIPT_DIR/HeatTransform.xslt +heat.exe dir $SAMPLE_TICTACTOE_PY_DIR -cg SampleTictactoePyComponentGroup -var var.SampleTictactoePySourceDir -out build/sampleTictactoePy.wxs -gg -sfrag -g1 -dr SampleProjects -t $SCRIPT_DIR/HeatTransform.xslt heat.exe dir $SAMPLE_TUTORIAL_DIR -cg SampleTutorialComponentGroup -var var.SampleTutorialSourceDir -out build/sampleTutorial.wxs -gg -sfrag -g1 -dr SampleProjects -t $SCRIPT_DIR/HeatTransform.xslt @@ -72,7 +74,8 @@ heat.exe dir $DATA_SYNTAX_HIGHLIGHTING_RULES_DIR -cg DataSyntaxHighlightingRules candle.exe -dProductVersion="$VERSION_STRING" -dProductGuid="$PRODUCT_GUID" -dWin64="$WIN_64" -arch $X_ARCH -out build/ \ -dSampleJavaparserSourceDir="$SAMPLE_JAVAPARSER_DIR" build/sampleJavaparser.wxs \ - -dSampleTictactoeSourceDir="$SAMPLE_TICTACTOE_DIR" build/sampleTictactoe.wxs \ + -dSampleTictactoeCppSourceDir="$SAMPLE_TICTACTOE_CPP_DIR" build/sampleTictactoeCpp.wxs \ + -dSampleTictactoePySourceDir="$SAMPLE_TICTACTOE_PY_DIR" build/sampleTictactoePy.wxs \ -dSampleTutorialSourceDir="$SAMPLE_TUTORIAL_DIR" build/sampleTutorial.wxs \ -dDataColorSchemesSourceDir="$DATA_COLOR_SCHEMES_DIR" build/dataColorSchemes.wxs \ -dDataCxxSourceDir="$DATA_CXX_DIR" build/dataCxx.wxs \ @@ -88,7 +91,8 @@ candle.exe -dProductVersion="$VERSION_STRING" -dProductGuid="$PRODUCT_GUID" -dWi light.exe -ext WixUIExtension \ build/sampleJavaparser.wixobj \ - build/sampleTictactoe.wixobj \ + build/sampleTictactoeCpp.wixobj \ + build/sampleTictactoePy.wixobj \ build/sampleTutorial.wixobj \ build/dataColorSchemes.wixobj \ build/dataCxx.wixobj \ diff --git a/script/deploy_windows.sh b/script/deploy_windows.sh index eadb9ab9..a949111e 100644 --- a/script/deploy_windows.sh +++ b/script/deploy_windows.sh @@ -74,8 +74,9 @@ echo -e "$INFO building the app (64 bit)" if [ $UPDATE_DATABASES = true ]; then echo -e "$INFO creating databases" - rm bin/app/user/projects/tictactoe/tictactoe.srctrldb rm bin/app/user/projects/tutorial/tutorial.srctrldb + rm bin/app/user/projects/tictactoe_cpp/tictactoe_cpp.srctrldb + rm bin/app/user/projects/tictactoe_py/tictactoe_py.srctrldb rm bin/app/user/projects/javaparser/javaparser.srctrldb rm -rf temp @@ -87,12 +88,15 @@ if [ $UPDATE_DATABASES = true ]; then ../build/win32/Release/app/Sourcetrail.exe config -t 8 ../build/win32/Release/app/Sourcetrail.exe config -M 1024 - echo -e "$INFO creating database for tictactoe" - ../build/win32/Release/app/Sourcetrail.exe index --full --project-file ../bin/app/user/projects/tictactoe/tictactoe.srctrlprj - echo -e "$INFO creating database for tutorial" ../build/win32/Release/app/Sourcetrail.exe index --full --project-file ../bin/app/user/projects/tutorial/tutorial.srctrlprj + echo -e "$INFO creating database for tictactoe_cpp" + ../build/win32/Release/app/Sourcetrail.exe index --full --project-file ../bin/app/user/projects/tictactoe_cpp/tictactoe_cpp.srctrlprj + + echo -e "$INFO creating database for tictactoe_py" + ../build/win32/Release/app/Sourcetrail.exe index --full --project-file ../bin/app/user/projects/tictactoe_py/tictactoe_py.srctrlprj + echo -e "$INFO creating database for javaparser" ../build/win32/Release/app/Sourcetrail.exe index --full --project-file ../bin/app/user/projects/javaparser/javaparser.srctrlprj @@ -210,8 +214,10 @@ if [ $CREATE_PORTABLE_ZIP = true ]; then mkdir -p $PORTABLE_PACKAGE_APP_DIR/user/projects/javaparser/ cp -u -r bin/app/user/projects/javaparser/* $PORTABLE_PACKAGE_APP_DIR/user/projects/javaparser/ - mkdir -p $PORTABLE_PACKAGE_APP_DIR/user/projects/tictactoe/ - cp -u -r bin/app/user/projects/tictactoe/* $PORTABLE_PACKAGE_APP_DIR/user/projects/tictactoe/ + mkdir -p $PORTABLE_PACKAGE_APP_DIR/user/projects/tictactoe_cpp/ + cp -u -r bin/app/user/projects/tictactoe_cpp/* $PORTABLE_PACKAGE_APP_DIR/user/projects/tictactoe_cpp/ + mkdir -p $PORTABLE_PACKAGE_APP_DIR/user/projects/tictactoe_py/ + cp -u -r bin/app/user/projects/tictactoe_py/* $PORTABLE_PACKAGE_APP_DIR/user/projects/tictactoe_py/ mkdir -p $PORTABLE_PACKAGE_APP_DIR/user/projects/tutorial/ cp -u -r bin/app/user/projects/tutorial/* $PORTABLE_PACKAGE_APP_DIR/user/projects/tutorial/ diff --git a/src/lib/settings/ApplicationSettings.cpp b/src/lib/settings/ApplicationSettings.cpp index 6067d436..13056d45 100644 --- a/src/lib/settings/ApplicationSettings.cpp +++ b/src/lib/settings/ApplicationSettings.cpp @@ -13,7 +13,7 @@ #include "UserPaths.h" #include "Version.h" -const size_t ApplicationSettings::VERSION = 6; +const size_t ApplicationSettings::VERSION = 7; std::shared_ptr ApplicationSettings::s_instance; @@ -80,7 +80,26 @@ bool ApplicationSettings::load(const FilePath& filePath, bool readOnly) } } )); + migrator.addMigration(7, std::make_shared( + [](const SettingsMigration* migration, Settings* settings) + { + std::vector recentProjects; + recentProjects.push_back("./projects/tictactoe_py/tictactoe_py.srctrlprj"); + utility::append(recentProjects, migration->getValuesFromSettings( + settings, "user/recent_projects/recent_project", std::vector()) + ); + recentProjects.pop_back(); + for (size_t i = 0; i < recentProjects.size(); i++) + { + if (recentProjects[i] == "./projects/tictactoe/tictactoe.srctrlprj") + { + recentProjects[i] = "./projects/tictactoe_cpp/tictactoe_cpp.srctrlprj"; + } + } + migration->setValuesInSettings(settings, "user/recent_projects/recent_project", recentProjects); + } + )); bool migrated = migrator.migrate(this, ApplicationSettings::VERSION); if (migrated) {