From 56c8fbfc07b003e11ddf2973119d1227a19b5932 Mon Sep 17 00:00:00 2001 From: Art-cyber520 Date: Sun, 23 May 2021 22:32:00 +0200 Subject: [PATCH] Error version --- bin/Classess/Field.py | 12 +++ bin/Classess/Track.py | 5 + bin/Classess/Travel.py | 20 ++++ bin/Classess/__pycache__/Field.cpython-38.pyc | Bin 3223 -> 3489 bytes .../__pycache__/Travel.cpython-38.pyc | Bin 0 -> 841 bytes bin/main/main.py | 86 ++++++++++++++++-- files/flag/Flaga.png | Bin 0 -> 372 bytes resources/Globals.py | 8 +- resources/__pycache__/Globals.cpython-38.pyc | Bin 579 -> 673 bytes 9 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 bin/Classess/Track.py create mode 100644 bin/Classess/Travel.py create mode 100644 bin/Classess/__pycache__/Travel.cpython-38.pyc create mode 100644 files/flag/Flaga.png diff --git a/bin/Classess/Field.py b/bin/Classess/Field.py index 65e254f..2df8c04 100644 --- a/bin/Classess/Field.py +++ b/bin/Classess/Field.py @@ -29,11 +29,14 @@ class Field(object): self.small_image_array = [[0 for i in range(self.rows)] for j in range(self.columns)] self.large_image_array = [[0 for i in range(self.rows)] for j in range(self.columns)] self.cell_expense = [0 for i in range(self.rows * self.columns)] + self.visited_mines = [] # Modified by Artem to search in the status area self.canvas_small_images = [] self.rectangle = 0 + self.mines_coord = [] + self.main_frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=0) self.main_frame.pack(anchor=NW) self.small_field_canvas = Canvas(self.main_frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, highlightthickness=0, @@ -44,6 +47,8 @@ class Field(object): bg='gray') self.large_image_canvas.place(x=FRAME_WIDTH + 5, y=3) + self.flag_img = PhotoImage(master=self.small_field_canvas, file="../../files/flag/Flaga.png") + # Clear Canvases def Moving(self): self.large_image_canvas.delete('all') @@ -58,6 +63,7 @@ class Field(object): # Putting small images for i in range(self.columns): for j in range(self.rows): + small_image_name = self.small_image_array[column][row] self.small_field_canvas.image = small_image_name @@ -65,12 +71,18 @@ class Field(object): self.small_field_canvas.create_image(x, y, anchor=NW, image=small_image_name)) # self.small_field_canvas.create_image(x, y, anchor=NW, image=small_image_name) + for k in range(0, len(self.mines_coord)): + if self.mines_coord[k][0] == i and self.mines_coord[k][1] == j: + new_mine_coord = self.small_field_canvas.coords(self.canvas_small_images[len(self.canvas_small_images) - 1]) + self.mines_coord[k] = new_mine_coord + x += self.image_size + self.x_start row += 1 y += self.image_size + self.y_start x = self.x_start column += 1 row = 0 + # print(self.mines_coord) def PuttingLargeImage(self, large_img_name): self.large_image_canvas.image = large_img_name diff --git a/bin/Classess/Track.py b/bin/Classess/Track.py new file mode 100644 index 0000000..98e191b --- /dev/null +++ b/bin/Classess/Track.py @@ -0,0 +1,5 @@ +class Track: + def __init__(self, road, distance): + self.road = road + self.distance = distance + diff --git a/bin/Classess/Travel.py b/bin/Classess/Travel.py new file mode 100644 index 0000000..72c27c0 --- /dev/null +++ b/bin/Classess/Travel.py @@ -0,0 +1,20 @@ +import queue +from itertools import permutations, islice, combinations + + +class Travel: + def __init__(self): + self.points_coord = [] + self.points_map = {} + + +def genetic_algorithm(travel_map): + population = queue.PriorityQueue() + road_map = list(travel_map.keys()) + points_permutation = list(map(list, islice(permutations(road_map), 10))) + # for i in range(0, len(points_permutation)): + # distance = + # subject = Track() + # print(points_permutation) + # print(len(points_permutation)) + diff --git a/bin/Classess/__pycache__/Field.cpython-38.pyc b/bin/Classess/__pycache__/Field.cpython-38.pyc index 8cf8c4b571ec150d00a58de12d5e59d0e4a1e05d..87c9362eb915bd4f4dc1ae9c879fe2bed23a704e 100644 GIT binary patch delta 784 zcmY+B&ubGw6vyYyZnm4vW|QXE=0|Id)wXMEE22bc@n=yKTMHtBpfNaUOxh;xW>Juh z6>=#;gfa*5*5J*97xmy-&;9{9_29{ie}ISjUK#{<_I>8f_q{jFym_1H=B#_+u)*+) zfBx|JkGX5@!}9L$uiF*2!-`R1B8U5a%!_*oFX^R(DZ(8Ub$f@^>{{B(2&)6YyezE; z84(ff1TZIuT5d1hWrH|)BVstsL<}vCJ0%8rqnI~ZJK`mUYBP}(xm1T^$rzOjTckT2 zH#;iWUFKvyP4G7=^yC-q_VkPF?Cc!=tJU>#b8dCLRGGVuD9vuxD-L%#UaY55(8)_> zsB49vRhw0>EQ%Y|dbugz59Q5J-z`3AG+ZIS4ZTeDjFqPiuW@suR4Mm#+Ot^QsK_h& z`22b56zHJ+HxiKTj}g!mjBt&|zycHafvy;kpqVG+JN*{S%b)rroRenoM1;Cdb`7nm zg1j8eNA_C*kNd2qwqcPy{3thrXSY2rz%x+{x1r5ZUT*2M^7RZ8kYRpcjl1qYML|Uy zDB7Ndy56tW+aQ!Q%djrAf`0HYe}QeeKA;~#V>T8Lw} z=X4#yFkzCABFqpj5Ecoi*cz8hCHp`-ljW&3&(KNl$a+RlaGvcI;uO5)8SEW kkXMi^p#``vJE5iMB@(U@mI!y`uh3LrTvzllX`8S90v8IeNB{r; delta 483 zcmXYsJ!>055Qb;&R??mBPT$h$BN38qMaZ$i*fGN32!c&WNUX-B5d_+0v9y3? zA5B$f%b;@{d7e~Q`fBW}4Ih}iRw3x3E=3lChGXc2{m%zhq;NoY@z+-8A-zZ}=ue_S zEoVw!oK@VWd#7&OaU^sL6uK|5OJ{Byx9Hk!r5#>Jb~@k~e**gEHq*0d%vci`s`q#Z zZ~OGiebSg93qA*g(1j^ZY;AM4-)&69>eC|GdmM}&@t9FtkQKB9CBeF2SI`q|Qqrr2 zJ5sJOI?kvpbwkjkBX5I@pP>(4zA2vZ3Ii~XaZ91HQ^?nH_?`Q!6#ekd!>8Qus8N^G o{|}x?wL=4cA79bjKg#ThurD|iJg18wBrnLu>W=M{sTs`v0oli6*8l(j diff --git a/bin/Classess/__pycache__/Travel.cpython-38.pyc b/bin/Classess/__pycache__/Travel.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..12b91093aea7b06e41ca61c258808be508b3e6c6 GIT binary patch literal 841 zcmZ8f&5qMB5VoB(&6X~IS|Q#5<$^#+91ucCJ#eeKTJ{n}GA1@%FHX|69i&$6srw+i zM|cI^#8*!H3Y?fpiWG3xI`%?LwlR z(WMI0vZ+4ny1y;acDMjlkKhCjisV$1oJlGf#E)bI@#BzuwBnmGB^@~88GUE4-9r#d zvL&2oln?{_=saTpsnNA}ylfgP!{neiFgC{rimh7l!TXGZKl0Drq&wLDfKDvuKF4_DJQ)S~Yx@uLc z!tuiD#%jMl?bO(4=R^EetzC$KU5kTd5lPon$DABTs^MgRZ+ literal 0 HcmV?d00001 diff --git a/bin/main/main.py b/bin/main/main.py index d318f41..6032f77 100644 --- a/bin/main/main.py +++ b/bin/main/main.py @@ -5,8 +5,10 @@ from tkinter import * from bin.Classess.Field import Field from bin.Classess.Mine import Mine +from bin.Classess.Travel import Travel from bin.Classess.Player import Player import bin.Classess.Node as nd +import bin.Classess.Travel as tr from resources.Globals import * # WINDOW_X = 533 + 1200 @@ -24,6 +26,7 @@ from resources.Globals import * # Creating objects player = Player() field = Field() +travel = Travel() fringe = [] explored = [] @@ -51,9 +54,21 @@ def Fill(bool): if bool: field.PuttingSmallImages() + travel.points_coord.append(field.small_field_canvas.coords(field.canvas_small_images[0])) + travel.points_coord.extend(field.mines_coord) + print(travel.points_coord) + + for i in range(0, len(travel.points_coord)): + travel.points_map[i + 1] = travel.points_coord[i] + # print(travel.points_map) + # key = list(travel.points_map.keys()) + # print(key) + tr.genetic_algorithm(travel.points_map) + + for i in range(0, len(field.canvas_small_images)): images_coord.append(field.small_field_canvas.coords(field.canvas_small_images[i])) - print("Coords List: ", images_coord) + # print("Coords List: ", images_coord) nd.init_data(images_coord, field.cell_expense) @@ -144,11 +159,15 @@ def ImagesInArray(directory, array): if column == 10: column = 0 row += 1 + if row == 10: + break column += 1 if column == 10: column = 0 row += 1 + if row == 10: + break def CellDesignation(array, color): @@ -244,6 +263,8 @@ def MouseClickEvent(event): print(action_list) + + # Start moving AutoMove() @@ -261,15 +282,18 @@ def PutMines(mines_array): if mine.array_x == x and mine.array_y == y: is_equal = True if not is_equal: - mine = Mine(x, y) - mines_array.append(mine) + if x == 0 and y == 0: + continue + else: + mine = Mine(x, y) + mines_array.append(mine) - field.field_state_array[x][y] = True + field.field_state_array[x][y] = True - counter += 1 + counter += 1 -def MinesInArrays(mines_array, directory, imgs_array): +def MinesInArrays(mines_array, directory, imgs_array, bool_mines_coord): counter = 0 temp_array = [] @@ -308,6 +332,29 @@ def MinesInArrays(mines_array, directory, imgs_array): # Add images in image array imgs_array[mines_array[i].array_x][mines_array[i].array_y] = temp_array[i] + if bool_mines_coord: + for i in range(len(mines_array)): + field.mines_coord.append([mines_array[i].array_x, mines_array[i].array_y]) + + +def DrawFlag(): + field.small_field_canvas.create_image(player.current_x, player.current_y, anchor=NW, image=field.flag_img) + + +def IsItMine(): + visited = 0 # 0 - not mine; 1 - on this mine for the first time; 2 - already been on this mine + + # Checks if the player is on the mine + for i in field.mines_coord: + if i[0] == player.current_x and i[1] == player.current_y: + visited = 1 + # Checks if the player has already been on this mine + for y in field.visited_mines: + if y[0] == player.current_x and y[1] == player.current_y: + visited = 2 + if visited == 1: + DrawFlag() + def AutoMove(): for action in action_list: @@ -315,6 +362,8 @@ def AutoMove(): time.sleep(DELAY_TIME) # Move once Action(action) + # Check if player on mine and if yes, draw flag + IsItMine() # Update main window field.win.update() @@ -333,7 +382,7 @@ def DrawRectangle(): color = "yellow" elif field.cell_expense[i] == 40: color = "dodger blue" - elif field.cell_expense[i] == 80: + elif field.cell_expense[i] == 5: color = "green4" if color != "None": field.small_field_canvas.create_rectangle(x, y, x + IMAGE_SIZE + 2, y + IMAGE_SIZE + 2, width=2, outline=color) @@ -364,20 +413,39 @@ def CostingOfCells(): DrawRectangle() +def click_button(): + btn.destroy() + label = Label(field.win, text='Prepod lox\nPrepod lox\nPrepod lox\nPrepod lox\nPrepod lox\nPrepod lox\n', fg='black') + label.place(x=50, y=570) + + def main(): # Creating the main window of an application win_size = f'{WINDOW_X}x{WINDOW_Y}' field.win.title("Sapper") field.win.configure(bg='gray') field.win.geometry(win_size) + print(f'Amount of mines: {AMOUNT_OF_MINES}') + global btn + btn = Button(field.win, + text="Search for mines", # текст кнопки + background="#555", # фоновый цвет кнопки + foreground="#ccc", # цвет текста + padx="20", # отступ от границ до содержимого по горизонтали + pady="8", # отступ от границ до содержимого по вертикали + font="16", # высота шрифта + command=click_button + ) + + btn.place(x=50, y=570) # Create array with mines objects mines_array = [] # Put mines on coordinates PutMines(mines_array) - MinesInArrays(mines_array, "../../files/small_mines_images", field.small_image_array) - MinesInArrays(mines_array, "../../files/large_mines_images", field.large_image_array) + MinesInArrays(mines_array, "../../files/small_mines_images", field.small_image_array, True) + MinesInArrays(mines_array, "../../files/large_mines_images", field.large_image_array, False) # Filling image arrays small_directory = "../../files/small_images" diff --git a/files/flag/Flaga.png b/files/flag/Flaga.png new file mode 100644 index 0000000000000000000000000000000000000000..3e6bba9d30f43605016f9a5565d04373acf56147 GIT binary patch literal 372 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1|;R|J2nC-&H|6fVg?4G5)fv*mnL7qz`&^H z>Eakt5%+eQBi|tf0oU%Aymxke`KSKs(%NthCe5~_zyL#@l|zMQe#ZT`(#`PlAE z_ibEpyPoJbXKlReD6~5yCVVU75etRno)ZbC8w120#05IFcWQbtButmKy7ow6Vy#7k ze7d2)l{e4d#eOvXu3Ye^sH1PbkN(w(X$(7MwQbe^?V6{>QB^!8Ncp@lOQx_O_g%qv zC+@A=pj^$nXnwl8kemPJH_I6v`|HYVzVxN2KXa5?zz9V@n3?t87C5a`S-^G$7>o>_ Lu6{1-oD!M<;-i!4 literal 0 HcmV?d00001 diff --git a/resources/Globals.py b/resources/Globals.py index f3167a8..5addf56 100644 --- a/resources/Globals.py +++ b/resources/Globals.py @@ -1,3 +1,5 @@ +import random + FRAME_WIDTH = 555 FRAME_HEIGHT = 555 @@ -7,7 +9,9 @@ WINDOW_Y = 950 # Size of small image IMAGE_SIZE = 50 -AMOUNT_OF_MINES = 10 +MIN_AMOUNT_OF_MINES = 0 +MAX_AMOUNT_OF_MINES = 11 +AMOUNT_OF_MINES = random.randint(MIN_AMOUNT_OF_MINES, MAX_AMOUNT_OF_MINES) DELAY_TIME = 0.5 @@ -22,7 +26,7 @@ amount_of_water_cells = 10 water_cell_cost = 40 amount_of_swamp_cells = 10 -swamp_cell_cost = 80 +swamp_cell_cost = 5 x_start = 5 y_start = 5 diff --git a/resources/__pycache__/Globals.cpython-38.pyc b/resources/__pycache__/Globals.cpython-38.pyc index e0de283ec4cbeec5ce6aa903bda67a7594e9d6e9..b52aa34e2f735f18b960301e14dfc23eed3fcbda 100644 GIT binary patch delta 323 zcmX@ivXHetl$V!_0SL}-UX>uo#K7!!XH6W9NiG`7ciGz`anFYjRfoXzLBEZ0z(Hf(TZ* zg>NgJ<$G`6G;hxSgZGOhi3xLi9+xLW^l{;TEW#M!BgLFy!hH6@Anc?xYRCyhnu(E1FKa lPTV2Wv~hS1^NG2mk7(P