Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from board import Direction, Rotation, Block
- from random import Random
- class Player:
- def choose_action(self, board):
- raise NotImplementedError
- class RandomPlayer(Player):
- def __init__(self, seed=None):
- self.random = Random(seed)
- def choose_action(self, board):
- #fallingCells = board.falling.cells
- #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
- # for (x,y) in board.cells:
- # 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.
- #sandbox = board.clone()
- #sandbox.rotate(Rotation.Anticlockwise)
- #print the bottom row
- # for y in range(board.height):
- # for x in range(board.width):
- # if (x,y) not in board.cells and y==23:
- # pass
- # 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
- # def determineDirection(x, leftXCord, clone):
- # currentDifference = leftXCord - x
- # currentShape = clone.falling.shape
- # newClone = board.clone()
- # direction = None
- # newLocationLeft = clone.move(Direction.Left)
- # newLeftXCord = calculateLeftX(currentShape, clone)
- # newDifferenceLeft = newLeftXCord - x
- # newLocationRight = newClone.move(Direction.Right)
- # newLeftXCord = calculateLeftX(currentShape, newClone)
- # newDifferenceRight = newLeftXCord - x
- # if newDifferenceLeft < newDifferenceRight:
- # direction = "left"
- # else:
- # direction = "right"
- # while(leftXCord != x ):
- # if direction == "left":
- # clone.move(Direction.Left)
- # elif direction == "right":
- # clone.move(Direction.Right)
- # clone.move(Direction.Drop)
- # def calculateLeftX(shape, clone):
- # lowestX = 10
- # cells = clone.falling.cells
- # xValues = ([y[0] for y in cells])
- # for x in xValues:
- # if x < lowestX:
- # lowestX = x
- # return lowestX
- # def calculateRightX(shape, clone):
- # lowestX = -1
- # cells = clone.falling.cells
- # xValues = ([y[0] for y in cells])
- # for x in xValues:
- # if x > lowestX:
- # lowestX = x
- # return lowestX
- rotateAmount = 0 #should check if these variables are actually
- returnDirections = [] #being updated -e
- bestPoints = 0
- bestX = 0
- bestRotate = 0
- def aggregateHeight(clone):
- #We want to always maximize aggregateHeight
- #you'll want to calculate the height of a column
- #instead of summing the y coordinate of the blocks
- #and this height should be minimised
- #the height can be calculated by looping through the
- #board (x, y) and stopping the loop once a cell is occupied
- #do this for each column and then sum the total -e
- cells = clone.cells
- totalHeight = 0
- xValues = ([y[1] for y in cells])
- for y in xValues:
- totalHeight += y
- #an optimised way of summing a list is sum(xValues) - Cris Tomy
- return totalHeight
- def evaluateTwo(x, rotateAmount, clone):
- cells = clone.cells
- maxHeight = aggregateHeight(clone)
- #using the total column height instead of sum of the y coordinates
- #means you should use a negative constant here -e
- points = 0.51 * maxHeight
- if points > bestPoints:
- bestX = x
- bestRotate = rotateAmount
- def evaluate(x, rotateAmount):
- print("CURRENT X: "+str(x)+"\n")
- dropped = False #I'm checking if I've already dropped the block, so I don't drop it twice. Read lines 128 and 134
- clone = board.clone()
- shape = clone.falling.shape
- # leftMostX = calculateLeftX(shape, clone) #Calculates Left most x value of the shape
- # rightMostX = calculateRightX(shape, clone) #Calculates Right most x value of the shape
- #you should check if the falling block has landed before calling the
- #next move - the move() and rotate() functions return True if the
- #move caused the block to land and false otherwise
- #you should also count how many of the specified moves were
- #actually made instead of appending to returnDirections the
- #number of moves it was intended to make -e
- if x<0:
- for i in range(abs(x)):
- clone.move(Direction.Left)
- elif x == 0:
- #there's also a down direction - you might want to use this
- #to move one block down instead of all the way as it's unlikely
- #the algorithm can decide the best position when it's at the top -e
- clone.move(Direction.Drop)
- dropped = True
- else:
- for i in range(x):
- clone.move(Direction.Right)
- if dropped == False:
- 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
- evaluateTwo(x, rotateAmount, clone)
- #Program 'starts' here
- #when you implement rotations you need to consider which direction
- #to rotate in (anticlockwise and clockwise) - one outer loop here -e
- for x in range(-5, 5):
- for rotateAmount in range(4):
- evaluate(x, rotateAmount)
- if bestX < 0 and x==4: #what does this do? -e
- for x in range(abs(bestX)):
- returnDirections.append(Direction.Left)
- elif bestX == 0 and x==4:
- returnDirections.append(Direction.Drop)
- elif bestX > 0 and x==4:
- for x in range(bestX):
- returnDirections.append(Direction.Right)
- return returnDirections
- SelectedPlayer = RandomPlayer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement