def before_turn(self): board = self.board # 1. get all the empty slots and choose randomly between them # 2. choose randomly(0.8) between putting 2 or 4 there emptySlots = [(x,y) for x in range(4) for y in range(4) if board[x][y] == None] numberToPutInSlot = weighted_choice() x,y = choose_uni_from_seq(emptySlots) self.create_new_tile(x, y, numberToPutInSlot) # check that there left some moves here, or it's a game over noMoreMoves = True for move in Directions.generator(): if self.is_legal_turn(move): noMoreMoves = False if noMoreMoves: self.display_score_and_exit() if self.agent: self.after(AFTER_FOR_NEW_TURN, self.update_turn, self.agent.getAction(self.board)) self.ignoreKeys = False
def getNextStatesOfMyTurn(board): ''' gets a state - a board - and yields tuples (move,nextState) ''' for direction in Directions.generator(): if Miniboard.isLegalAction(board, direction): nextBoard = Miniboard.calculateNextBoardUsing(direction, board) yield(direction, nextBoard)
def test_legal_turn(self): self.create_from_list([0,0,0,0,2,4,2,4]) self.debug_board() for d in Directions.generator(): print(d, self.is_legal_turn(d))