added first draft litris

This commit is contained in:
2023-07-22 09:44:48 +02:00
parent c2b5416542
commit 3176bf5269
4 changed files with 507 additions and 19 deletions

View File

@@ -35,6 +35,10 @@ from utils import mse
from game_base_class import GameBase
import random
from pynput.keyboard import Key, Controller
from field import Field
from tetromino import Tetromino
from optimizer import Optimizer
import time
BLOCK_FULL = [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]
@@ -89,24 +93,14 @@ class Litris(GameBase):
self.fill_data_coordinates()
self.field = Field()
#self.sd_reset_board = cv.imread("control_elements/sodoku_reset_button.jpg", cv.IMREAD_COLOR)
self.needles = {1: cv.imread("litris/blue_needle.jpg", cv.IMREAD_COLOR)
# 2: cv.imread("sodoku/2.jpg", cv.IMREAD_COLOR),
}
self.full_stones_dic = {1: BLOCK_FULL,
2: L1_FULL,
3: LINE_FULL,
4: DOT_FULL
}
self.col_stones_dic = {1: BLOCK_COL,
2: L1_COL,
3: LINE_COL,
4: DOT_COL
}
def fill_data_coordinates(self):
# 610 to 1950 = 1340 - 76 / 20 = 63
# 40 to 1380 = 1340 - 76 / 20 = 63
@@ -126,14 +120,31 @@ class Litris(GameBase):
#if self.check_for_button_and_execute(self.capture_window.get_screenshot(), self.sd_reset_board):
# cv.waitKey(2000)
#current_stone = self.new_stone_detection_and_identification()
#new_observation, new_screenshot = self.get_current_board_state()
#col = self.find_place_for_stone(current_stone, new_observation)
#self.move_stone(col)
#field = Field()
#current_tetromino = Tetromino.create("O")
#next_tetromino = None
#time.sleep(2)
#stone_list = ["L","S","Z","J"]
#ier = 0
current_stone = self.new_stone_detection_and_identification()
new_observation, new_screenshot = self.get_current_board_state()
col = self.find_place_for_stone(current_stone, new_observation)
self.move_stone(col)
current_tetromino = Tetromino.create(self.get_letter_for_stone(current_stone))
opt = Optimizer.get_optimal_drop(self.field, current_tetromino)
rotation = opt['tetromino_rotation']
column = opt['tetromino_column']
current_tetromino.rotate(rotation)
offset_col = current_tetromino.get_offset_column(rotation)
self.field.drop(current_tetromino, column)
self.move_stone(column - offset_col, rotation)
self.observation = new_observation
return new_observation
#time.sleep(0.2)
#self.observation = new_observation
#return new_observation
def get_current_board_state(self):
# get an updated image of the game
@@ -203,6 +214,32 @@ class Litris(GameBase):
#cv.waitKey(150)
return stone_coords
def get_letter_for_stone(self, stone):
if np.array_equal(stone, BLOCK_FULL):
return "O"
elif np.array_equal(stone, BL3_FULL):
return "D"
elif np.array_equal(stone, L1_FULL):
return "L"
elif np.array_equal(stone, L2_FULL):
return "J"
elif np.array_equal(stone, LINE_FULL):
return "I"
elif np.array_equal(stone, DOT_FULL):
return "C"
elif np.array_equal(stone, DDOT_FULL) :
return "B"
elif np.array_equal(stone, DDDOT_FULL) :
return "A"
elif np.array_equal(stone, Z1_FULL):
return "S"
elif np.array_equal(stone, Z2_FULL):
return "Z"
elif np.array_equal(stone, T1_FULL):
return "T"
def find_place_for_stone(self, stone, current_board):
if np.array_equal(stone, BLOCK_FULL):
@@ -291,13 +328,27 @@ class Litris(GameBase):
for i in range(0, 18, 1):
if current_board[e][i] == 1 and current_board[e][i + 1] == 0 and current_board[e][i + 2] == 1 and current_board[e - 1][i] == 0 and current_board[e - 1][i + 1] == 0 and current_board[e - 1][i + 2] == 0:
return i - T1_COL
def move_stone(self, col_movement):
def move_stone(self, col_movement, rotation):
if col_movement is None:
return
# Press and release space
self.keyboard.press(Key.down)
self.keyboard.release(Key.down)
cv.waitKey(250)
if rotation == 1:
self.keyboard.press('e')
self.keyboard.release('e')
elif rotation == 2:
self.keyboard.press('e')
self.keyboard.release('e')
cv.waitKey(40)
self.keyboard.press('e')
self.keyboard.release('e')
elif rotation == 3:
self.keyboard.press('q')
self.keyboard.release('q')
if col_movement < 0:
for i in range(0, col_movement, - 1):
self.keyboard.press(Key.left)