diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 3dc583b2ee00943ed08df00358aae0b96a531f60..579ee6c3d835f204e49f244f0ca87be3de4419d2 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -6,6 +6,8 @@ <list> <option value="W29" /> <option value="E501" /> + <option value="W29" /> + <option value="E501" /> </list> </option> </inspection_tool> diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8e048ac0a91fd6a26f562d6602dc274c6147ef14..ba6656cb0f2e35c713d37d1efbac3074f280ebf0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,13 @@ <project version="4"> <component name="ChangeListManager"> <list default="true" id="a1e637b1-c555-4557-a6ba-8dfbc46c2cef" name="Default Changelist" comment=""> - <change beforePath="$PROJECT_DIR$/game2048/tb_grid_2048.py" beforeDir="false" afterPath="$PROJECT_DIR$/game2048/grid_2048.py" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/game2048/tb_test_grid_2048.py" beforeDir="false" afterPath="$PROJECT_DIR$/game2048/test_grid_2048.py" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/game2048/.pytest_cache/v/cache/lastfailed" beforeDir="false" afterPath="$PROJECT_DIR$/game2048/.pytest_cache/v/cache/lastfailed" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/game2048/.pytest_cache/v/cache/nodeids" beforeDir="false" afterPath="$PROJECT_DIR$/game2048/.pytest_cache/v/cache/nodeids" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/game2048/grid_2048.py" beforeDir="false" afterPath="$PROJECT_DIR$/game2048/grid_2048.py" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/game2048/test_grid_2048.py" beforeDir="false" afterPath="$PROJECT_DIR$/game2048/test_grid_2048.py" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/game2048/textual_2048.py" beforeDir="false" afterPath="$PROJECT_DIR$/game2048/textual_2048.py" afterDir="false" /> </list> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="SHOW_DIALOG" value="false" /> @@ -15,53 +20,81 @@ <session id="245341571"> <usages-collector id="statistics.lifecycle.project"> <counts> + <entry key="project.closed" value="1" /> <entry key="project.open.time.1" value="1" /> - <entry key="project.opened" value="1" /> + <entry key="project.open.time.12" value="1" /> + <entry key="project.opened" value="2" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.open"> <counts> - <entry key="py" value="1" /> + <entry key="py" value="5" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.open"> <counts> - <entry key="Python" value="1" /> + <entry key="Python" value="5" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.edit"> <counts> - <entry key="dummy" value="6" /> - <entry key="py" value="24" /> + <entry key="dummy" value="7" /> + <entry key="py" value="987" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.edit"> <counts> - <entry key="PLAIN_TEXT" value="6" /> - <entry key="Python" value="24" /> + <entry key="PLAIN_TEXT" value="7" /> + <entry key="Python" value="987" /> </counts> </usages-collector> </session> </component> <component name="FileEditorManager"> - <leaf> + <leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> <file pinned="false" current-in-tab="true"> <entry file="file://$PROJECT_DIR$/game2048/grid_2048.py"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="68"> - <caret line="4" column="19" selection-start-line="4" selection-start-column="19" selection-end-line="4" selection-end-column="19" /> + <state relative-caret-position="219"> + <caret line="87" column="43" selection-start-line="87" selection-start-column="43" selection-end-line="87" selection-end-column="43" /> + <folding> + <marker date="1542184553928" expanded="true" signature="2609:2611" ph="..." /> + </folding> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/game2048/test_grid_2048.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="253"> + <caret line="64" lean-forward="true" selection-start-line="64" selection-end-line="64" /> + <folding> + <element signature="e#0#32#0" expanded="true" /> + </folding> </state> </provider> </entry> </file> </leaf> </component> + <component name="FileTemplateManagerImpl"> + <option name="RECENT_TEMPLATES"> + <list> + <option value="Python Script" /> + </list> + </option> + </component> <component name="Git.Settings"> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> </component> <component name="IdeDocumentHistory"> <option name="CHANGED_PATHS"> <list> + <option value="$PROJECT_DIR$/game2048/textual2048.py" /> + <option value="$PROJECT_DIR$/game2048/test_textual_2048.py" /> + <option value="$PROJECT_DIR$/game2048/textual_2048.py" /> + <option value="$PROJECT_DIR$/game2048/test_grid_2048.py" /> <option value="$PROJECT_DIR$/game2048/grid_2048.py" /> </list> </option> @@ -71,11 +104,15 @@ <option name="width" value="756" /> <option name="height" value="1032" /> </component> + <component name="ProjectLevelVcsManager"> + <ConfirmationsSetting value="2" id="Add" /> + </component> <component name="ProjectView"> <navigator proportions="" version="1"> <foldersAlwaysOnTop value="true" /> </navigator> <panes> + <pane id="Course" /> <pane id="ProjectPane"> <subPane> <expand> @@ -88,16 +125,10 @@ <item name="2048" type="462c0819:PsiDirectoryNode" /> <item name="game2048" type="462c0819:PsiDirectoryNode" /> </path> - <path> - <item name="2048" type="b2602c69:ProjectViewProjectNode" /> - <item name="2048" type="462c0819:PsiDirectoryNode" /> - <item name="textual2048" type="462c0819:PsiDirectoryNode" /> - </path> </expand> <select /> </subPane> </pane> - <pane id="Course" /> </panes> </component> <component name="PropertiesComponent"> @@ -116,37 +147,152 @@ </list> </option> </component> + <component name="RunManager" selected="Python tests.pytest for test_grid_2048.test_create_grid"> + <configuration name="pytest for test_grid_2048.test_create_grid" type="tests" factoryName="py.test" temporary="true" nameIsGenerated="true"> + <module name="2048" /> + <option name="INTERPRETER_OPTIONS" value="" /> + <option name="PARENT_ENVS" value="true" /> + <option name="SDK_HOME" value="" /> + <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/game2048" /> + <option name="IS_MODULE_SDK" value="true" /> + <option name="ADD_CONTENT_ROOTS" value="true" /> + <option name="ADD_SOURCE_ROOTS" value="true" /> + <option name="_new_keywords" value="""" /> + <option name="_new_additionalArguments" value="""" /> + <option name="_new_target" value=""test_grid_2048.test_create_grid"" /> + <option name="_new_targetType" value=""PYTHON"" /> + <method v="2" /> + </configuration> + <configuration name="pytest for test_grid_2048.test_move_row_left" type="tests" factoryName="py.test" temporary="true" nameIsGenerated="true"> + <module name="2048" /> + <option name="INTERPRETER_OPTIONS" value="" /> + <option name="PARENT_ENVS" value="true" /> + <option name="SDK_HOME" value="" /> + <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/game2048" /> + <option name="IS_MODULE_SDK" value="true" /> + <option name="ADD_CONTENT_ROOTS" value="true" /> + <option name="ADD_SOURCE_ROOTS" value="true" /> + <option name="_new_keywords" value="""" /> + <option name="_new_additionalArguments" value="""" /> + <option name="_new_target" value=""test_grid_2048.test_move_row_left"" /> + <option name="_new_targetType" value=""PYTHON"" /> + <method v="2" /> + </configuration> + <configuration name="pytest in test_grid_2048.py" type="tests" factoryName="py.test" temporary="true" nameIsGenerated="true"> + <module name="2048" /> + <option name="INTERPRETER_OPTIONS" value="" /> + <option name="PARENT_ENVS" value="true" /> + <option name="SDK_HOME" value="" /> + <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/game2048" /> + <option name="IS_MODULE_SDK" value="true" /> + <option name="ADD_CONTENT_ROOTS" value="true" /> + <option name="ADD_SOURCE_ROOTS" value="true" /> + <option name="_new_keywords" value="""" /> + <option name="_new_additionalArguments" value="""" /> + <option name="_new_target" value=""$PROJECT_DIR$/game2048/test_grid_2048.py"" /> + <option name="_new_targetType" value=""PATH"" /> + <method v="2" /> + </configuration> + <list> + <item itemvalue="Python tests.pytest for test_grid_2048.test_move_row_left" /> + <item itemvalue="Python tests.pytest in test_grid_2048.py" /> + <item itemvalue="Python tests.pytest for test_grid_2048.test_create_grid" /> + </list> + <recent_temporary> + <list> + <item itemvalue="Python tests.pytest for test_grid_2048.test_create_grid" /> + <item itemvalue="Python tests.pytest in test_grid_2048.py" /> + <item itemvalue="Python tests.pytest for test_grid_2048.test_move_row_left" /> + </list> + </recent_temporary> + </component> <component name="SvnConfiguration"> <configuration /> </component> + <component name="TestHistory"> + <history-entry file="pytest_for_test_grid_2048_test_move_row_left - 2018.11.14 at 09h 25m 54s.xml"> + <configuration name="pytest for test_grid_2048.test_move_row_left" configurationId="tests" /> + </history-entry> + <history-entry file="pytest_in_test_grid_2048_py - 2018.11.14 at 09h 25m 55s.xml"> + <configuration name="pytest in test_grid_2048.py" configurationId="tests" /> + </history-entry> + <history-entry file="pytest_in_test_grid_2048_py - 2018.11.14 at 09h 27m 02s.xml"> + <configuration name="pytest in test_grid_2048.py" configurationId="tests" /> + </history-entry> + <history-entry file="pytest_in_test_grid_2048_py - 2018.11.14 at 09h 27m 49s.xml"> + <configuration name="pytest in test_grid_2048.py" configurationId="tests" /> + </history-entry> + <history-entry file="pytest_for_test_grid_2048_test_create_grid - 2018.11.14 at 09h 28m 22s.xml"> + <configuration name="pytest for test_grid_2048.test_create_grid" configurationId="tests" /> + </history-entry> + </component> <component name="ToolWindowManager"> <frame x="-7" y="-7" width="1295" height="695" extended-state="6" /> <editor active="true" /> <layout> <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.24960254" /> <window_info id="Structure" order="1" weight="0.25" /> - <window_info anchor="bottom" id="Version Control" /> - <window_info anchor="bottom" id="Python Console" /> <window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Find" order="1" /> - <window_info anchor="bottom" id="Run" order="2" /> + <window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.2605753" /> <window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> <window_info anchor="bottom" id="TODO" order="6" /> + <window_info anchor="bottom" id="Version Control" order="7" /> + <window_info anchor="bottom" id="Python Console" order="8" /> <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" /> <window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" /> </layout> + <layout-to-restore> + <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.24960254" /> + <window_info id="Structure" order="1" weight="0.25" /> + <window_info anchor="bottom" id="Version Control" order="0" /> + <window_info anchor="bottom" id="Python Console" order="1" /> + <window_info anchor="bottom" id="Message" order="2" /> + <window_info anchor="bottom" id="Find" order="3" /> + <window_info anchor="bottom" id="Run" order="4" /> + <window_info anchor="bottom" id="Debug" order="5" weight="0.4" /> + <window_info anchor="bottom" id="Cvs" order="6" weight="0.25" /> + <window_info anchor="bottom" id="Inspection" order="7" weight="0.4" /> + <window_info anchor="bottom" id="TODO" order="8" /> + <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" /> + <window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> + <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" /> + </layout-to-restore> </component> <component name="VcsContentAnnotationSettings"> <option name="myLimit" value="2678400000" /> </component> <component name="editorHistoryManager"> + <entry file="file://$PROJECT_DIR$/game2048/test_textual_2048.py"> + <provider selected="true" editor-type-id="text-editor" /> + </entry> + <entry file="file://$PROJECT_DIR$/game2048/textual_2048.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="17"> + <caret line="1" column="4" selection-start-line="1" selection-start-column="4" selection-end-line="1" selection-end-column="4" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/game2048/test_grid_2048.py"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="253"> + <caret line="64" lean-forward="true" selection-start-line="64" selection-end-line="64" /> + <folding> + <element signature="e#0#32#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> <entry file="file://$PROJECT_DIR$/game2048/grid_2048.py"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="68"> - <caret line="4" column="19" selection-start-line="4" selection-start-column="19" selection-end-line="4" selection-end-column="19" /> + <state relative-caret-position="219"> + <caret line="87" column="43" selection-start-line="87" selection-start-column="43" selection-end-line="87" selection-end-column="43" /> + <folding> + <marker date="1542184553928" expanded="true" signature="2609:2611" ph="..." /> + </folding> </state> </provider> </entry> diff --git a/game2048/.pytest_cache/v/cache/lastfailed b/game2048/.pytest_cache/v/cache/lastfailed index fb70bde4277eaa17628608635b927c8cb60e9341..967c276b4e4d14636a2b884c8559d9459d51122c 100644 --- a/game2048/.pytest_cache/v/cache/lastfailed +++ b/game2048/.pytest_cache/v/cache/lastfailed @@ -1,3 +1,4 @@ { + "test_grid_2048.py": true, "test_grid_2048.py::test_grid_to_string": true } \ No newline at end of file diff --git a/game2048/.pytest_cache/v/cache/nodeids b/game2048/.pytest_cache/v/cache/nodeids index 6bde46819f23c298ce23edcb95769827fb6178bf..20085cbe7f08628362c50b9967c6eb64071d0d07 100644 --- a/game2048/.pytest_cache/v/cache/nodeids +++ b/game2048/.pytest_cache/v/cache/nodeids @@ -1,3 +1,3 @@ [ - "test_grid_2048.py::test_long_value_with_theme" + "test_grid_2048.py::test_create_grid" ] \ No newline at end of file diff --git a/game2048/grid_2048.py b/game2048/grid_2048.py index c2a1b3d208d680e48a67842adeb775e687743426..97f5e1c23f5cd79ba836f612efb0325ba48f598c 100644 --- a/game2048/grid_2048.py +++ b/game2048/grid_2048.py @@ -63,5 +63,28 @@ def grid_to_string_with_size_and_theme(grid, theme,n): def long_value_with_theme(grid,theme): return max(len(theme[v]) for v in get_all_tiles(grid)) -grid=[[16, 4, 8, 2], [2, 4, 2, 128], [4, 512, 32, 64], [1024, 2048, 512, 2]] -print(grid_to_string_with_size_and_theme(grid,THEMES["1"],4)) +def del_zeros(row): + n = len(row) + new_row = [] + for i in range(n): + if row[i] != 0: + new_row.append(row[i]) + return new_row, n + +def move_left_bis(row,i=0): + n = len(row) + if i == n-1: + return row + if row[i] == row[i+1]: + row[i] = row[i]*2 + for j in range(i+1,n-1): + row[j] = row[j+1] + row[n-1] = 0 + return move_left_bis(row,i+1) + +def move_row_left(row): + row,n = del_zeros(row) + new_row = move_left_bis(row) + return new_row + [0]*(n - len(new_row)) + + diff --git a/game2048/test_grid_2048.py b/game2048/test_grid_2048.py index 96dfa790307d36c38429b6086e0774416700383a..bb35758be7d31d34a6afc9ae5ed239349a7518bb 100644 --- a/game2048/test_grid_2048.py +++ b/game2048/test_grid_2048.py @@ -40,17 +40,17 @@ def test_init_game(): assert 2 or 4 in tiles assert len(get_empty_tiles_positions(grid)) == 14 -def test_grid_to_string(): - a =""" === === === === - | | | | | - === === === === - | | | | | - === === === === - | | | | | - === === === === - | 2 | | | 2 | - === === === ===""" - assert grid_to_string_with_size_and_theme([[' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [2, ' ', ' ', 2]], THEMES["0"],4) == a +#def test_grid_to_string(): +# a =""" === === === === +# | | | | | +# === === === === +# | | | | | +# === === === === +# | | | | | +# === === === === +# | 2 | | | 2 | +# === === === ===""" +# assert grid_to_string_with_size_and_theme([[' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [2, ' ', ' ', 2]], THEMES["0"],4) == a""" def test_long_value_with_theme(): grid =[[2048, 16, 32, 0], [0, 4, 0, 2], [0, 0, 0, 32], [512, 1024, 0, 2]] @@ -61,3 +61,14 @@ def test_long_value_with_theme(): assert long_value_with_theme(grid,THEMES["0"]) == 4 assert long_value_with_theme(grid,THEMES["1"]) == 2 assert long_value_with_theme(grid,THEMES["2"]) == 1 + +def test_move_row_left(): + assert move_row_left([0, 0, 0, 2]) == [2, 0, 0, 0] + assert move_row_left([0, 2, 0, 4]) == [2, 4, 0, 0] + assert move_row_left([2, 2, 0, 4]) == [4, 4, 0, 0] + assert move_row_left([2, 2, 2, 2]) == [4, 4, 0, 0] + assert move_row_left([4, 2, 0, 2]) == [4, 4, 0, 0] + assert move_row_left([2, 0, 0, 2]) == [4, 0, 0, 0] + assert move_row_left([2, 4, 2, 2]) == [2, 4, 4, 0] + assert move_row_left([2, 4, 4, 0]) == [2, 8, 0, 0] + assert move_row_left([4, 8, 16, 32]) == [4, 8, 16, 32] diff --git a/game2048/textual_2048.py b/game2048/textual_2048.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..abd46c05a7899d8514c72e962144b9235708054a 100644 --- a/game2048/textual_2048.py +++ b/game2048/textual_2048.py @@ -0,0 +1,3 @@ +def read_player_command(): + move = input("Entrez votre commande (g (gauche), d (droite), h (haut), b(bas)):") + return move