File size: 1,951 Bytes
0813b4d
 
 
 
 
 
 
 
 
46dc1ab
 
 
 
 
 
 
 
0813b4d
 
46dc1ab
 
 
 
 
 
 
 
 
 
 
0813b4d
 
 
 
 
46dc1ab
0813b4d
 
 
 
 
 
 
 
 
 
 
 
 
46dc1ab
 
 
 
 
 
 
 
0813b4d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gym
import math
import numpy as np

class MyWrapper(gym.Wrapper):
    def __init__(self, env):
        super().__init__(env)
        self.prevBoardState = np.zeros((self.env.height, self.env.width))

    def check_cleared_rows(self, board):
        to_delete = []
        for i, row in enumerate(board[::-1]):
            if 0 not in row:
                to_delete.append(len(board) - 1 - i)
        if len(to_delete) > 0:
            board = self.remove_row(board, to_delete)
        return len(to_delete)


    def get_bumpiness_and_height(self, board):
        board = np.array(board)
        mask = board != 0
        invert_heights = np.where(mask.any(axis=0), np.argmax(mask, axis=0), self.height)
        heights = self.height - invert_heights
        total_height = np.sum(heights)
        currs = heights[:-1]
        nexts = heights[1:]
        diffs = np.abs(currs - nexts)
        total_bumpiness = np.sum(diffs)
        return total_bumpiness, total_height, np.max(heights)

    def get_holes(self, board):
        num_holes = 0
        for col in zip(*board):
            row = 0
            while row < self.height and col[row] == 0:
                row += 1
            num_holes += len([x for x in col[row + 1:] if x == 0])
        return num_holes


    def step(self, action):
        
        ##Eredeti  step fv meghívása

        obs, reward, done, info = self.env.step(action)
        ##Itt tudom felül írni a reward számítást.

        currBoard = obs['board'].copy()
        cleared_lines = self.check_cleared_rows(currBoard)
        bumpiness, total_height, max_height = self.get_bumpiness_and_height(currBoard)
        num_holes = self.get_holes(currBoard)

        reward = 1 + math.pow(cleared_lines, 4)  * obs['board'].shape[1] - 0.28 * bumpiness - 0.36 * num_holes

        if done:
            reward -= 1000

        self.prevBoardState = obs['board'].copy()

        return obs, reward, done, info