def performInitialSetup(): # Resets the display overlay & all the turtles & the click listeners & the piece data & the board matrix displayOut.reset() boardTurtle.reset() pieceTurtle.reset() ghostPieceTurtle.reset() displayOut.onclick(None) displayOut.onkey(None, "l") displayOut.onkey(None, "s") displayOut.bgcolor(BOARD_BACKGROUND_COLOUR) displayOut.title("Reversi By Group Reversi02") blankBoard = [[0 for i in range(8)] for i in range(8)] # Populates the board with the initial starting pieces & sends it off to the backend blankBoard[3][3] = 1 blankBoard[3][4] = 2 blankBoard[4][3] = 2 blankBoard[4][4] = 1 backend.writeBoard(blankBoard) # Scales the display overlay window based on the specified size of the board # Takes half the width of the board (global constant) and multiplies it by 2 to get the entire board's width # Then calculates 1/8th of the board's width and subtracts it from the total width to have empty spaces on the sides # Then does the same calculation for the board's height displayOut.setup(abs(((HALF_BOARD_WIDTH * 2) + (HALF_BOARD_WIDTH * 0.25))), abs(((HALF_BOARD_HEIGHT * 2) + (HALF_BOARD_HEIGHT * 0.25)))) # Hides the turtles & makes the animation speed / delay instantaneous pieceTurtle.hideturtle() pieceTurtle.speed(0) boardTurtle.hideturtle() boardTurtle.speed(0) ghostPieceTurtle.hideturtle() ghostPieceTurtle.speed(0) displayOut.delay(0) # Calls the functions to print out the intro & board printOutIntro() printOutTable() # Gets the game's difficulty, determine and performs the first random move, and updates the board & ghost pieces backend.setDifficulty(getGameDifficulty()) performFirstMove() updateBoardPieces(backend.getBoard()) addGhostPiecesToBoard() # Sets the function that will be called when the user clicks on the screen + for L is pressed + for S is pressed displayOut.onkey(importGameStateFromFile, "l") displayOut.onkey(saveGameStateToFile, "s") displayOut.onclick(graphicalOverlayClicked) displayOut.listen()
def importGameStateFromFile(): try: # Initialize a new list & the save game file reader utility & specifies that it is to be imported from saveGameFile = open("Reversi Save Game", "r") importedBoard = [[0 for importedMatrixIndex in range(8)] for importedMatrixIndex in range(8)] currentIndex = 0 fileData = saveGameFile.read() # Loops through the entire file except last spot & imports it into the temp board matrix, then sets the # game difficulty and closes the save file reader for rowCounter in range(8): for columnCounter in range(8): importedBoard[rowCounter][columnCounter] = int(fileData[currentIndex:currentIndex + 1]) currentIndex += 1 backend.setDifficulty(int(fileData[len(fileData) - 1])) saveGameFile.close() # Sends the newly populated game board to the backend and updates the GUI's game state as well by # first resetting the current board's pieces backend.writeBoard(importedBoard) pieceTurtle.clear() ghostPieceTurtle.clear() updateBoardPieces(backend.getBoard()) addGhostPiecesToBoard() except Exception: pass