Error version
This commit is contained in:
parent
9ebb4a0f15
commit
56c8fbfc07
@ -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.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.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.cell_expense = [0 for i in range(self.rows * self.columns)]
|
||||||
|
self.visited_mines = []
|
||||||
|
|
||||||
# Modified by Artem to search in the status area
|
# Modified by Artem to search in the status area
|
||||||
self.canvas_small_images = []
|
self.canvas_small_images = []
|
||||||
self.rectangle = 0
|
self.rectangle = 0
|
||||||
|
|
||||||
|
self.mines_coord = []
|
||||||
|
|
||||||
self.main_frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=0)
|
self.main_frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=0)
|
||||||
self.main_frame.pack(anchor=NW)
|
self.main_frame.pack(anchor=NW)
|
||||||
self.small_field_canvas = Canvas(self.main_frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, highlightthickness=0,
|
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')
|
bg='gray')
|
||||||
self.large_image_canvas.place(x=FRAME_WIDTH + 5, y=3)
|
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
|
# Clear Canvases
|
||||||
def Moving(self):
|
def Moving(self):
|
||||||
self.large_image_canvas.delete('all')
|
self.large_image_canvas.delete('all')
|
||||||
@ -58,6 +63,7 @@ class Field(object):
|
|||||||
# Putting small images
|
# Putting small images
|
||||||
for i in range(self.columns):
|
for i in range(self.columns):
|
||||||
for j in range(self.rows):
|
for j in range(self.rows):
|
||||||
|
|
||||||
small_image_name = self.small_image_array[column][row]
|
small_image_name = self.small_image_array[column][row]
|
||||||
|
|
||||||
self.small_field_canvas.image = small_image_name
|
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))
|
||||||
# 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
|
x += self.image_size + self.x_start
|
||||||
row += 1
|
row += 1
|
||||||
y += self.image_size + self.y_start
|
y += self.image_size + self.y_start
|
||||||
x = self.x_start
|
x = self.x_start
|
||||||
column += 1
|
column += 1
|
||||||
row = 0
|
row = 0
|
||||||
|
# print(self.mines_coord)
|
||||||
|
|
||||||
def PuttingLargeImage(self, large_img_name):
|
def PuttingLargeImage(self, large_img_name):
|
||||||
self.large_image_canvas.image = large_img_name
|
self.large_image_canvas.image = large_img_name
|
||||||
|
5
bin/Classess/Track.py
Normal file
5
bin/Classess/Track.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Track:
|
||||||
|
def __init__(self, road, distance):
|
||||||
|
self.road = road
|
||||||
|
self.distance = distance
|
||||||
|
|
20
bin/Classess/Travel.py
Normal file
20
bin/Classess/Travel.py
Normal file
@ -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))
|
||||||
|
|
Binary file not shown.
BIN
bin/Classess/__pycache__/Travel.cpython-38.pyc
Normal file
BIN
bin/Classess/__pycache__/Travel.cpython-38.pyc
Normal file
Binary file not shown.
@ -5,8 +5,10 @@ from tkinter import *
|
|||||||
|
|
||||||
from bin.Classess.Field import Field
|
from bin.Classess.Field import Field
|
||||||
from bin.Classess.Mine import Mine
|
from bin.Classess.Mine import Mine
|
||||||
|
from bin.Classess.Travel import Travel
|
||||||
from bin.Classess.Player import Player
|
from bin.Classess.Player import Player
|
||||||
import bin.Classess.Node as nd
|
import bin.Classess.Node as nd
|
||||||
|
import bin.Classess.Travel as tr
|
||||||
from resources.Globals import *
|
from resources.Globals import *
|
||||||
|
|
||||||
# WINDOW_X = 533 + 1200
|
# WINDOW_X = 533 + 1200
|
||||||
@ -24,6 +26,7 @@ from resources.Globals import *
|
|||||||
# Creating objects
|
# Creating objects
|
||||||
player = Player()
|
player = Player()
|
||||||
field = Field()
|
field = Field()
|
||||||
|
travel = Travel()
|
||||||
|
|
||||||
fringe = []
|
fringe = []
|
||||||
explored = []
|
explored = []
|
||||||
@ -51,9 +54,21 @@ def Fill(bool):
|
|||||||
if bool:
|
if bool:
|
||||||
field.PuttingSmallImages()
|
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)):
|
for i in range(0, len(field.canvas_small_images)):
|
||||||
images_coord.append(field.small_field_canvas.coords(field.canvas_small_images[i]))
|
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)
|
nd.init_data(images_coord, field.cell_expense)
|
||||||
|
|
||||||
@ -144,11 +159,15 @@ def ImagesInArray(directory, array):
|
|||||||
if column == 10:
|
if column == 10:
|
||||||
column = 0
|
column = 0
|
||||||
row += 1
|
row += 1
|
||||||
|
if row == 10:
|
||||||
|
break
|
||||||
|
|
||||||
column += 1
|
column += 1
|
||||||
if column == 10:
|
if column == 10:
|
||||||
column = 0
|
column = 0
|
||||||
row += 1
|
row += 1
|
||||||
|
if row == 10:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
def CellDesignation(array, color):
|
def CellDesignation(array, color):
|
||||||
@ -244,6 +263,8 @@ def MouseClickEvent(event):
|
|||||||
|
|
||||||
print(action_list)
|
print(action_list)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Start moving
|
# Start moving
|
||||||
AutoMove()
|
AutoMove()
|
||||||
|
|
||||||
@ -261,15 +282,18 @@ def PutMines(mines_array):
|
|||||||
if mine.array_x == x and mine.array_y == y:
|
if mine.array_x == x and mine.array_y == y:
|
||||||
is_equal = True
|
is_equal = True
|
||||||
if not is_equal:
|
if not is_equal:
|
||||||
mine = Mine(x, y)
|
if x == 0 and y == 0:
|
||||||
mines_array.append(mine)
|
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
|
counter = 0
|
||||||
|
|
||||||
temp_array = []
|
temp_array = []
|
||||||
@ -308,6 +332,29 @@ def MinesInArrays(mines_array, directory, imgs_array):
|
|||||||
# Add images in image array
|
# Add images in image array
|
||||||
imgs_array[mines_array[i].array_x][mines_array[i].array_y] = temp_array[i]
|
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():
|
def AutoMove():
|
||||||
for action in action_list:
|
for action in action_list:
|
||||||
@ -315,6 +362,8 @@ def AutoMove():
|
|||||||
time.sleep(DELAY_TIME)
|
time.sleep(DELAY_TIME)
|
||||||
# Move once
|
# Move once
|
||||||
Action(action)
|
Action(action)
|
||||||
|
# Check if player on mine and if yes, draw flag
|
||||||
|
IsItMine()
|
||||||
# Update main window
|
# Update main window
|
||||||
field.win.update()
|
field.win.update()
|
||||||
|
|
||||||
@ -333,7 +382,7 @@ def DrawRectangle():
|
|||||||
color = "yellow"
|
color = "yellow"
|
||||||
elif field.cell_expense[i] == 40:
|
elif field.cell_expense[i] == 40:
|
||||||
color = "dodger blue"
|
color = "dodger blue"
|
||||||
elif field.cell_expense[i] == 80:
|
elif field.cell_expense[i] == 5:
|
||||||
color = "green4"
|
color = "green4"
|
||||||
if color != "None":
|
if color != "None":
|
||||||
field.small_field_canvas.create_rectangle(x, y, x + IMAGE_SIZE + 2, y + IMAGE_SIZE + 2, width=2, outline=color)
|
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()
|
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():
|
def main():
|
||||||
# Creating the main window of an application
|
# Creating the main window of an application
|
||||||
win_size = f'{WINDOW_X}x{WINDOW_Y}'
|
win_size = f'{WINDOW_X}x{WINDOW_Y}'
|
||||||
field.win.title("Sapper")
|
field.win.title("Sapper")
|
||||||
field.win.configure(bg='gray')
|
field.win.configure(bg='gray')
|
||||||
field.win.geometry(win_size)
|
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
|
# Create array with mines objects
|
||||||
mines_array = []
|
mines_array = []
|
||||||
# Put mines on coordinates
|
# Put mines on coordinates
|
||||||
PutMines(mines_array)
|
PutMines(mines_array)
|
||||||
|
|
||||||
MinesInArrays(mines_array, "../../files/small_mines_images", field.small_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)
|
MinesInArrays(mines_array, "../../files/large_mines_images", field.large_image_array, False)
|
||||||
|
|
||||||
# Filling image arrays
|
# Filling image arrays
|
||||||
small_directory = "../../files/small_images"
|
small_directory = "../../files/small_images"
|
||||||
|
BIN
files/flag/Flaga.png
Normal file
BIN
files/flag/Flaga.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 372 B |
@ -1,3 +1,5 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
FRAME_WIDTH = 555
|
FRAME_WIDTH = 555
|
||||||
FRAME_HEIGHT = 555
|
FRAME_HEIGHT = 555
|
||||||
|
|
||||||
@ -7,7 +9,9 @@ WINDOW_Y = 950
|
|||||||
# Size of small image
|
# Size of small image
|
||||||
IMAGE_SIZE = 50
|
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
|
DELAY_TIME = 0.5
|
||||||
|
|
||||||
@ -22,7 +26,7 @@ amount_of_water_cells = 10
|
|||||||
water_cell_cost = 40
|
water_cell_cost = 40
|
||||||
|
|
||||||
amount_of_swamp_cells = 10
|
amount_of_swamp_cells = 10
|
||||||
swamp_cell_cost = 80
|
swamp_cell_cost = 5
|
||||||
|
|
||||||
x_start = 5
|
x_start = 5
|
||||||
y_start = 5
|
y_start = 5
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user