def createBoard(numBlocks): """Returns a new board with three pegs. The first peg has numBlocks number of blocks (from 1 to numBlocks). View the assignment page for examples and further details.""" def getNumBlocks(board): """Returns the number of blocks contained within the specified board. For instance, if board = createBoard(6), getNumBlocks(board) should return 6.""" def isTowerComplete(board, pegNo): """Returns True if all of the blocks on the board are on the specified peg, in proper order.""" def isGameComplete(board): """Returns True if the goal of the game has been satisfied (ie, all of the blocks have been moved to their proper destination), where peg 2 is the proper destination. Hint: you can do this in one line of code!""" def isValidMove(board, fromPeg, toPeg): """fromPeg and toPeg are the indices of the respective pegs. Returns True if the fromPeg and the toPeg are on the board, the fromPeg has a piece to move, and the piece being moved is smaller than (less than) the piece (if any) on the toPeg.""" def makeMove(board, fromPeg, toPeg): """Changes and returns the board after moving the smallest block from the top of fromPeg to the top of toPeg. This function will only be used on valid moves (ie, isValidMove(board, fromPeg, toPeg) returns True), so you do not need to perform additional checks.""" def inputInt(prompt): """Asks the user to give input with the given prompt until the user has entered an integer. Hint: use try and except to test if the input can be converted to an integer.""" def main(): boardSize = 3 board = createBoard(boardSize) while not isGameComplete(board): printBoard(board) fromPeg = inputInt("From which peg? ") toPeg = inputInt("To which peg? ") while not isValidMove(board, fromPeg, toPeg): print("INVALID MOVE! Try again!") fromPeg = inputInt("From which peg? ") toPeg = inputInt("To which peg? ") board = makeMove(board, fromPeg, toPeg) printBoard(board) print("CONGRATULATIONS!") def printBoard(board): nb = max([b for p in board for b in p]) formatStr = "%ds" % nb print("+", "=" * ((2 + nb) * len(board) + len(board) - 1), \ "+", sep="") print("|", end="") for pegNo in range(len(board)): print(" ", format(str(pegNo), formatStr), sep="", end = " |") print("\n", end="") print("+", "=" * ((2 + nb) * len(board) + len(board) - 1), \ "+", sep="") for rowNo in range(nb): print("|", end = '') for peg in board: if nb - len(peg) <= rowNo: print(" ", format(str(peg[rowNo - nb + len(peg)]) * peg[rowNo - nb + len(peg)], formatStr), sep="", end = ' |') else: print(" ", format("0" * 0, formatStr), sep="", end = ' |') print("\n", end = '') print("+", "-" * ((2 + nb) * len(board) + len(board) - 1), \ "+", sep="") main()