Exemple #1
0
 def Generate_Grid(self):
     tracker_y = 0
     for y in range(constants.SCREEN_SIZE[1]):
         tracker_x = 0
         for x in range(constants.SCREEN_SIZE[0]):
             square = Tile(pygame.Rect(x * self.block_size, y * self.block_size, self.block_size, self.block_size))
             square.row = y + 1
             square.col = x + 1
             self.tiles += [square]
             if tracker_x >= self.sections - 1:
                 break
             tracker_x += 1
         if tracker_y >= self.sections - 1:
             break
         tracker_y += 1
Exemple #2
0
def read_minefield():
    all_tiles = []
    for tile in TILES:
        positions = pyautogui.locateAllOnScreen(TILES[tile])
        for p in positions:
            all_tiles.append({'value': tile, 'position': p})
    all_tiles = sorted(all_tiles,
                       key=lambda x: (x['position'][1], x['position'][0]))
    width = len(set([x['position'][0] for x in all_tiles]))
    height = len(set([x['position'][1] for x in all_tiles]))
    tiles = [
        Tile(i, t['position'], t['value']) for i, t in enumerate(all_tiles)
    ]
    minefield = Minefield(width, height, tiles)
    return minefield
Exemple #3
0
def recv_msg(msg, grid, entities):
    if msg[0] == "T":
        curObj = msg
        newObj = TiledAtom(int(curObj[1]), int(curObj[2]), curObj[3])
        grid[newObj.gridY][newObj.gridX] = newObj
    elif msg[0] == "L":
        entities.append(
            Line((int(msg[1]), int(msg[2])), (int(msg[3]), int(msg[4])),
                 int(msg[5])))
    elif msg[0] == "TE":
        newObj = Tile(int(msg[1]), int(msg[2]))
        grid[newObj.gridY][newObj.gridX] = newObj
    elif msg[0] == "LE" or "LB":
        for obj in entities:
            sPos = (int(msg[1]), int(msg[2]))
            ePos = (int(msg[3]), int(msg[4]))
            if obj.startPos == sPos and obj.endPos == ePos:
                if msg[0] == "LE":
                    entities.remove(obj)
                elif msg[0] == "LB":
                    obj.update_Bond_Number()
                break

    return grid, entities
Exemple #4
0
def get_grid(frame):
    height, width, channels = frame.shape
    print("------Start getting grid ---------")

    # treshold to find contours
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 140, 255, cv2.THRESH_BINARY)
    # cv2.imshow("tresh", thresh)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                                cv2.CHAIN_APPROX_SIMPLE)

    # sort contours
    cntsSorted = sorted(contours,
                        key=lambda x: cv2.contourArea(x),
                        reverse=True)

    # make tiles array
    max_tile_width = 0
    max_tile_height = 0
    tiles = []
    for i in range(0, len(all_the_frames) * 2):
        # print("aantal contours "+str(len(cntsSorted)))
        contour = cntsSorted[i]
        orig = frame.copy()
        # ratio = frame.shape[0] / 300.0
        # frame = imutils.resize(frame, height = 300)
        peri = cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, 0.015 * peri, True)
        if (len(approx) == 4):
            screenCnt = approx

            pts = screenCnt.reshape(4, 2)
            rect = np.zeros((4, 2), dtype="float32")

            # the top-left point has the smallest sum whereas the
            # bottom-right has the largest sum
            s = pts.sum(axis=1)
            rect[0] = pts[np.argmin(s)]
            rect[2] = pts[np.argmax(s)]

            # compute the difference between the points -- the top-right
            # will have the minumum difference and the bottom-left will
            # have the maximum difference
            diff = np.diff(pts, axis=1)
            rect[3] = pts[np.argmin(diff)]
            rect[1] = pts[np.argmax(diff)]

            # the width and height of the new image
            (tl, tr, br, bl) = rect
            widthA = np.sqrt(((br[0] - bl[0])**2) + ((br[1] - bl[1])**2))
            widthB = np.sqrt(((tr[0] - tl[0])**2) + ((tr[1] - tl[1])**2))
            heightA = np.sqrt(((tr[0] - br[0])**2) + ((tr[1] - br[1])**2))
            heightB = np.sqrt(((tl[0] - bl[0])**2) + ((tl[1] - bl[1])**2))

            # take the maximum of the width and height values to reach
            # our final dimensions
            maxWidth = max(int(widthA), int(widthB))
            maxHeight = max(int(heightA), int(heightB))

            # construct our destination points which will be used to
            # map the screen to a top-down, "birds eye" view
            dst = np.array([[0, 0], [maxWidth - 1, 0],
                            [maxWidth - 1, maxHeight - 1], [0, maxHeight - 1]],
                           dtype="float32")

            # calculate the perspective transform matrix and warp
            # the perspective to grab the screen
            M = cv2.getPerspectiveTransform(rect, dst)
            warp = cv2.warpPerspective(orig, M, (maxWidth, maxHeight))

            # center of contour
            M = cv2.moments(contour)
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])

        else:
            rect = cv2.minAreaRect(contour)
            cX, cY = rect[0]
            cX = int(cX)
            cY = int(cY)
            w, h = rect[1]

            box = cv2.boxPoints(rect)
            box = np.int0(box)

            warp = crop_minAreaRect(frame, rect, box)

        # cv2.imshow(str(i), warp)

        tiles.append(Tile(i, warp, (cX, cY)))
        # cv2.drawContours(frame,[box],0,(0,0,255),2)

        if maxWidth > max_tile_width:
            max_tile_width = maxWidth
        if maxHeight > max_tile_height:
            max_tile_height = maxHeight

    glob_cols, glob_rows = add_position_in_grid(tiles, max_tile_height,
                                                max_tile_width)

    return tiles, glob_cols, glob_rows
def generate_hand(pool=Pool(), amount=14):
    hand = []
    for _ in range(0, amount):
        tile = random.choice(pool.available_tiles())
        tile.available = False
        hand.append(tile)
    return Pool(hand)


# def get_first_set(tiles_in_hand, barrier=30):
#     all_sets = get_sets(tiles_in_hand)
#     if sum(list(map(sum, all_sets))) >= barrier:
#         return all_sets
#     else:
#         return False

# my_hand = generate_hand()
example = [
    Tile(2, 1),
    Tile(3, 1),
    Tile(4, 2),
    Tile(2, 3),
    Tile(1, 3),
    Tile(4, 3),
    Tile(4, 5)
]
my_hand = Pool(example)

print(f"Hand:\t{my_hand.tiles}")
print(f"Groups:\t{my_hand.get_groups()}")
print(f"Runs:\t{my_hand.get_runs()}")
Exemple #6
0
from classes import Tile, Route
from classes import Side, SideType, EndPoints

import json

start_tile = Tile()
start_tile.id = 1
start_tile.order = 1
start_tile.image = "base64Image"
start_tile.cloister = False
start_tile.sides[Side.North] = SideType.City
start_tile.sides[Side.East] = SideType.Road
start_tile.sides[Side.South] = SideType.Farm
start_tile.sides[Side.West] = SideType.Road

newRoute = Route()
newRoute.rid = "1-1"
newRoute.route_type = SideType.City
newRoute.endpoints.append(EndPoints.TopLeft)
newRoute.endpoints.append(EndPoints.TopMiddle)
newRoute.endpoints.append(EndPoints.TopRight)
newRoute.meeple_pos = (100, 100)
start_tile.routes.append(newRoute)

newRoute = Route()
newRoute.rid = "1-2"
newRoute.route_type = SideType.Road
newRoute.endpoints.append(EndPoints.LeftMiddle)
newRoute.endpoints.append(EndPoints.RightMiddle)
newRoute.meeple_pos = (200, 200)
start_tile.routes.append(newRoute)
Exemple #7
0
import math

from classes import Tile, Generation
from constants import *

import pygame
from pygame.locals import *

# Initialization
pygame.init()
generation = Generation([[Tile((i, j)) for i in range(n_cells)] for j in range(n_cells)])
# Empty window creation
window = pygame.display.set_mode((window_size, window_size), 0, 32)
# Simulation controller
simulate = False
# Drawing controller
drawing = False
# Infinite loop controller
run = True
# Main loop
while run:
    # Handle user inputs here
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False

        if event.type == KEYDOWN:
            if event.key == K_SPACE:
                simulate = not simulate
            if event.key == K_c:
                generation.reset()
Exemple #8
0
def init_world_tiles(w, h, size, data):
    tiles_per_width = int(np.ceil(w / size))
    tiles_per_height = int(np.ceil(h / size))
    return [[Tile((x * size, y * size), data) for x in range(tiles_per_width)]
            for y in range(tiles_per_height)]