def rotation_amount(block): blocks = [block] while True: block = rotate_clockwise(block) if block in blocks: return len(blocks) blocks.append(block)
def num_rotations(stone): """The number of unique rotated positions of stone""" stones = [stone] while True: stone = rotate_clockwise(stone) if stone in stones: return len(stones) stones.append(stone)
def find_best_move(self): all_moves = [] piece = self.tetris.stone #want to use the same rotation shape for each column, #saves time so we dont have to re-rotate a piece each time for rotation in range(self.rotations_per_piece(piece)): #now that we have a config of a piece/tetromino/stone, #check each column with it #print(len(piece[0])) for x in range((COLS - len(piece[0])) + 1): y = self.top_of_column(x, piece) #get a new board config after adding the current piece's rotation #to the current board. join the current board and place where the #piece would be placed new_board = join_matrixes(self.tetris.board, piece, (x, y)) all_moves.append(Move(x, rotation, new_board)) #checked every config with current configuration, #rotate and do it again piece = rotate_clockwise(piece) #at this point, all_moves contains the x_coord, the rotation config of the #current piece we are working on, and a representation of how the board looks #if we placed the piece in that specific configuration/rotation on the current #board #now we calculate the utility of each board state and find the max #heuristics takes only a board... keep this in mind when calling util_fcn max_util = float("-inf") best_board_state = None #check how good each placement is for each placement pop = self.tetris.genetics.population curOrgIndex = self.tetris.genetics.current_organism workingOrganism = pop[curOrgIndex] trained = [ -0.42288086451739565, -0.20602799801145819, -0.33535728489031424, 0.2413838337524669, -0.46690981318268, -0.06051542450985892, 0.28699447592371874, -0.16251189239909392, 0.023842910473495273, 0.45812789704427803, -0.25722297728881605, 0.03061740725841887 ] #iterate over the moves and choose the best one. #temp = float("-inf") for a_move in all_moves: temp = heuristics.Utility_Function(a_move[2], trained) if temp == max_util: if numpy.random.random() < .5: max_util = temp best_board_state = a_move elif temp > max_util: max_util = temp best_board_state = a_move #max_util has the utility of the best board state #best_board_state contains x_coord, rotation of the piece, and how the board #will look after the piece is placed on the current board #self.best_move = best_board_state return best_board_state
def all_possible_actions(self): actions = [] block = self.tetris.block for i in range(Player_AI.rotation_amount(block)): for x in range(self.max_xposition(block)+1): y = self.insert_point(x, block) board = self.new_board_with_blocks(x , y , block) actions.append(Action(x, i, board)) block = rotate_clockwise(block) return actions
def all_possible_moves(self): moves = [] stone = self.tetris.stone for r in range(AI.num_rotations(stone)): for x in range(self.max_x_pos_for_stone(stone)+1): y = self.intersection_point(x, stone) board = self.board_with_stone(x, y, stone) moves.append(Move(x, r, board)) stone = rotate_clockwise(stone) return moves
def do_action(self): tetris = self.tetris action = self.most_efficient_action() tetris.lock.acquire() for i in range(action.rotation): tetris.block = rotate_clockwise(tetris.block) tetris.move_to(action.x_position) if self.instant_play: tetris.block_y = self.insert_point(action.x_position, tetris.block) tetris.lock.release()
def getMoves(self, board, stone): maxMove = (0, 0, 0) score = 0 for value in stone[0]: if (value > 0): shapeNum = value for i in range(10): stonex = i stoney = 0 for j in range(tetris.tetris_rotations[shapeNum - 1]): newBoard = copy.deepcopy(board) if i > 10 - len(stone[0]): stone = tetris.rotate_clockwise(stone) continue newBoard = self.drop(newBoard, stone, stonex, stoney) score = self.scoreBoard(newBoard) if (score > maxMove[0]): maxMove = (score, i, j) stone = tetris.rotate_clockwise(stone) return maxMove
def make_move(self): """Move the current stone to the desired position by modifying TetrisApp's state""" tetris = self.tetris move = self.best_move() tetris.lock.acquire() for _ in range(move.rotation): tetris.stone = rotate_clockwise(tetris.stone) tetris.move_to(move.x_pos) if self.instant_play: tetris.stone_y = self.intersection_point(move.x_pos, tetris.stone) tetris.lock.release()
def finalPositions(self, board, piece): positions = [] #if shape is square, loop through finding final positions just once, don't care about rotations if piece == tetris_shapes[6]: for x in range(10): positions.append(x, 0, piece) for y in range(22): if not check_collision(board, piece, (x, y)): position = positions[x] if y > position[1]: positions.pop() positions.append(x, y, piece) #if shape is line, Z or S, loop through twice to take into account a rotation of the piece if piece == tetris_shapes[5] or piece == tetris_shapes[ 1] or piece == tetris_shapes[2]: for z in range(2): for x in range(10): positions.append(x, 0, piece) for y in range(22): if not check_collision(board, piece, (x, y)): position = positions[x] if y > position[1]: positions.pop() positions.append(x, y, piece) piece = tetris.rotate_clockwise(piece) #all other pieces (L/J/T shapes) run four times to take into account the 3 rotations else: for z in range(4): for x in range(10): positions.append(x, 0, piece) for y in range(22): if not check_collision(board, piece, (x, y)): position = positions[x] if y > position[1]: positions.pop() positions.append(x, y, piece) piece = tetris.rotate_clockwise(piece) return positions
def update_board(self): tetris = self.tetris #calculate the best config for the piece #move = [x_coord, rotation, new_board] move = self.find_best_move() #rotate the piece until it is in the config we want for r in range(move[1]): tetris.stone = rotate_clockwise(tetris.stone) #move the piece over to the correct column tetris.move_to(move.x_pos) #if we are still playing, not paused if self.instantPlay: tetris.stone_y = self.top_of_column(move.x_pos, tetris.stone)
def getSuccessors(self, state): successors = [] if not tetris.check_collision(state[2], state[5], (state[0] - 1, state[1])): successors.append( tetris.join_matrixes(state[2], state[5], (state[0] - 1, state[1])), 'LEFT') if not tetris.check_collision(state[2], state[5], (state[0] + 1, state[1])): successors.append( tetris.join_matrixes(state[2], state[5], (state[0] + 1, state[1])), 'RIGHT') state[5] = tetris.rotate_clockwise(state[5]) if not tetris.check_collision(state[2], state[5], (state[0], state[1])): successors.append( tetris.join_matrixes(state[2], state[5], (state[0], state[1])), 'UP') return successors