Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.61 KB | None | 0 0
  1. from board import Direction, Rotation, Block
  2. from random import Random
  3.  
  4.  
  5. class Player:
  6.     def choose_action(self, board):
  7.         raise NotImplementedError
  8.  
  9.  
  10. class RandomPlayer(Player):
  11.     def __init__(self, seed=None):
  12.         self.random = Random(seed)
  13.  
  14.     def choose_action(self, board):
  15.         #fallingCells = board.falling.cells
  16.         #print([x[1] for x in cells]) if you want to access [n]th item of each tuple. Eg { {2,1},{4,5} } with n=1 returns 1,5
  17.  
  18.         # for (x,y) in board.cells:
  19.         #     print("HERE: x:"+str(x)+"y:"+str(y)) #when a block lands, basically console displays {{8,22}, (7,2)...} so this function seperates it all out into like x:8, y:22, etc.
  20.  
  21.         #sandbox = board.clone()
  22.         #sandbox.rotate(Rotation.Anticlockwise)
  23.  
  24.  
  25.        
  26.         #print the bottom row
  27.         # for y in range(board.height):
  28.         #     for x in range(board.width):
  29.         #         if (x,y) not in board.cells and y==23:
  30.         #             pass
  31.  
  32.         # print(x, [y[1] for y in board.falling.cells]) <- prints the current iteration of x, and the current y value of the falling block
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.         # def determineDirection(x, leftXCord, clone):
  40.  
  41.         #     currentDifference = leftXCord - x
  42.         #     currentShape = clone.falling.shape
  43.         #     newClone = board.clone()
  44.         #     direction = None
  45.  
  46.         #     newLocationLeft = clone.move(Direction.Left)
  47.         #     newLeftXCord = calculateLeftX(currentShape, clone)
  48.         #     newDifferenceLeft = newLeftXCord - x
  49.  
  50.         #     newLocationRight = newClone.move(Direction.Right)
  51.         #     newLeftXCord = calculateLeftX(currentShape, newClone)
  52.         #     newDifferenceRight = newLeftXCord - x
  53.  
  54.         #     if newDifferenceLeft < newDifferenceRight:
  55.         #         direction = "left"
  56.         #     else:
  57.         #         direction = "right"
  58.  
  59.         #     while(leftXCord != x ):
  60.         #         if direction == "left":
  61.         #             clone.move(Direction.Left)
  62.         #         elif direction == "right":
  63.         #             clone.move(Direction.Right)
  64.  
  65.         #     clone.move(Direction.Drop)
  66.  
  67.         # def calculateLeftX(shape, clone):
  68.         #     lowestX = 10
  69.         #     cells = clone.falling.cells
  70.         #     xValues = ([y[0] for y in cells])
  71.         #     for x in xValues:
  72.         #         if x < lowestX:
  73.         #             lowestX = x
  74.  
  75.         #     return lowestX
  76.  
  77.         # def calculateRightX(shape, clone):
  78.         #     lowestX = -1
  79.         #     cells = clone.falling.cells
  80.         #     xValues = ([y[0] for y in cells])
  81.         #     for x in xValues:
  82.         #         if x > lowestX:
  83.         #             lowestX = x
  84.  
  85.         #     return lowestX
  86.  
  87.         rotateAmount = 0 #should check if these variables are actually
  88.         returnDirections = [] #being updated -e
  89.         bestPoints = 0
  90.         bestX = 0
  91.         bestRotate = 0
  92.  
  93.         def aggregateHeight(clone):
  94.             #We want to always maximize aggregateHeight
  95.  
  96.             #you'll want to calculate the height of a column
  97.             #instead of summing the y coordinate of the blocks
  98.             #and this height should be minimised
  99.             #the height can be calculated by looping through the
  100.             #board (x, y) and stopping the loop once a cell is occupied
  101.             #do this for each column and then sum the total -e
  102.             cells = clone.cells
  103.             totalHeight = 0
  104.             xValues = ([y[1] for y in cells])
  105.             for y in xValues:
  106.                 totalHeight += y
  107.             #an optimised way of summing a list is sum(xValues) - Cris Tomy
  108.             return totalHeight
  109.  
  110.  
  111.         def evaluateTwo(x, rotateAmount, clone):
  112.             cells = clone.cells
  113.             maxHeight = aggregateHeight(clone)
  114.             #using the total column height instead of sum of the y coordinates
  115.             #means you should use a negative constant here -e
  116.             points = 0.51 * maxHeight
  117.             if points > bestPoints:
  118.                 bestX = x
  119.                 bestRotate = rotateAmount
  120.  
  121.  
  122.            
  123.  
  124.         def evaluate(x, rotateAmount):
  125.             print("CURRENT X: "+str(x)+"\n")
  126.             dropped = False #I'm checking if I've already dropped the block, so I don't drop it twice. Read lines 128 and 134
  127.             clone = board.clone()
  128.             shape = clone.falling.shape
  129.  
  130.             # leftMostX = calculateLeftX(shape, clone) #Calculates Left most x value of the shape
  131.             # rightMostX = calculateRightX(shape, clone) #Calculates Right most x value of the shape
  132.  
  133.             #you should check if the falling block has landed before calling the
  134.             #next move - the move() and rotate() functions return True if the
  135.             #move caused the block to land and false otherwise
  136.             #you should also count how many of the specified moves were
  137.             #actually made instead of appending to returnDirections the
  138.             #number of moves it was intended to make -e
  139.             if x<0:
  140.                 for i in range(abs(x)):
  141.                     clone.move(Direction.Left)
  142.             elif x == 0:
  143.                 #there's also a down direction - you might want to use this
  144.                 #to move one block down instead of all the way as it's unlikely
  145.                 #the algorithm can decide the best position when it's at the top -e
  146.                 clone.move(Direction.Drop)
  147.                 dropped = True
  148.             else:
  149.                 for i in range(x):
  150.                     clone.move(Direction.Right)
  151.  
  152.             if dropped == False:
  153.                 clone.move(Direction.Drop) #So basically once it's moved correct number of times left OR right depending on the x value, drop the block
  154.            
  155.             evaluateTwo(x, rotateAmount, clone)
  156.  
  157.  
  158.         #Program 'starts' here
  159.             #when you implement rotations you need to consider which direction
  160.             #to rotate in (anticlockwise and clockwise) - one outer loop here -e
  161.         for x in range(-5, 5):
  162.             for rotateAmount in range(4):        
  163.                 evaluate(x, rotateAmount)
  164.  
  165.        
  166.             if bestX < 0 and x==4: #what does this do? -e
  167.                 for x in range(abs(bestX)):
  168.                     returnDirections.append(Direction.Left)
  169.             elif bestX == 0 and x==4:
  170.                 returnDirections.append(Direction.Drop)
  171.             elif bestX > 0 and x==4:
  172.                 for x in range(bestX):
  173.                     returnDirections.append(Direction.Right)
  174.  
  175.        
  176.         return returnDirections
  177.  
  178.  
  179. SelectedPlayer = RandomPlayer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement