def run(state, move_num, isPlayerMove): #Allow the computer to make it's move if not isPlayerMove: state = computerTurn(state, move_num) move_num += 1 #Update the scoreboard black_score = VictoryStatus.countPieces(Constants.PIECE_BLACK, state) white_score = VictoryStatus.countPieces(Constants.PIECE_WHITE, state) ScreenWriter.writeScore(black_score, white_score) #Display valid moves valid_moves = SquareValidator.mainSquareValidator( Converter.toString(state), ListInterpret.whoseTurn(move_num)) TurtleMove.displayValidMoves(valid_moves) #Save variables to be used later FileHandler.saveVariable("State", Converter.toString(state)) FileHandler.saveVariable("Move", str(move_num)) FileHandler.saveVariable("Moving", "False") #Set up the window wn = Constants.WINDOW wn.onclick(placePiece) wn.onkey(saveGame, "s") wn.onkey(loadGame, "l") wn.onkey(quitGame, "space") wn.listen() wn.mainloop()
def loadGame(): #Load game info saved_state = FileHandler.loadVariable("State", "save.txt") saved_move_num = FileHandler.loadVariable("Move", "save.txt") #Write loaded variables to variables file FileHandler.saveVariable("State", saved_state) FileHandler.saveVariable("Move", saved_move_num) #Convert state from string to list saved_state = Converter.toList(saved_state) #Load board configuration ListInterpret.stringToPiece(saved_state) #Update the scoreboard black_score = VictoryStatus.countPieces(Constants.PIECE_BLACK, saved_state) white_score = VictoryStatus.countPieces(Constants.PIECE_WHITE, saved_state) ScreenWriter.writeScore(black_score, white_score) #Reset valid moves from TurtleMove TurtleMove.SHOWN_MOVES = [] #Display valid moves valid_moves = SquareValidator.mainSquareValidator( Converter.toString(saved_state), ListInterpret.whoseTurn(int(saved_move_num))) TurtleMove.displayValidMoves(valid_moves)
def placePiece(x, y): #Make sure a move is not being made if FileHandler.loadVariable("Moving") == "False": #Save variable FileHandler.saveVariable("Moving", "True") #Load the variables we need game_state = Converter.toList(FileHandler.loadVariable("State")) move_num = int(FileHandler.loadVariable("Move")) #Make sure the game is not over if VictoryStatus.endGameStatus(game_state) != True: #Check if the point is valid if isValidSquare(x, y): #Convert the x and y to the coordinate of a cell x = convertXToBoard(x) y = convertYToBoard(y) #Convert x and y to column and row letter = Constants.COLUMN_LETTERS[x] number = Constants.ROW_NUMBERS[Constants.NUM_OF_ROWS - (y + 1)] #The move being made move = letter + str(number) #Make sure a piece is not in this location and the move is valid if StringMove.validateMoveLocation( game_state, move) and MoveValidator.isValidMove( move, Converter.toString(game_state) ) and move in SquareValidator.mainSquareValidator( Converter.toString(game_state), ListInterpret.whoseTurn(move_num)): #Clear space at move TurtleMove.resetSquare(letter, number) #Update the game state game_state = ListInterpret.stringInterpret( game_state, letter + str(number), move_num) move_num += 1 #Allow the computer to place a piece if the game is not over if VictoryStatus.endGameStatus(game_state) != True: game_state = computerTurn(game_state, move_num) move_num += 1 #Save the variables FileHandler.saveVariable("State", Converter.toString(game_state)) FileHandler.saveVariable("Move", str(move_num)) #Update the scoreboard black_score = VictoryStatus.countPieces( Constants.PIECE_BLACK, game_state) white_score = VictoryStatus.countPieces( Constants.PIECE_WHITE, game_state) ScreenWriter.writeScore(black_score, white_score) #Display valid moves valid_moves = SquareValidator.mainSquareValidator( Converter.toString(game_state), ListInterpret.whoseTurn(move_num)) TurtleMove.displayValidMoves(valid_moves) else: #Print who won game_status = PlayerVictory.playerWon(game_state) ScreenWriter.writeMessage(game_status) #Wait for user Constants.WINDOW.exitonclick() #Reset state and move FileHandler.saveVariable("State", "") FileHandler.saveVariable("Move", "") #Save variable FileHandler.saveVariable("Moving", "False")