def stringToPiece(game_state): for y in range(len(game_state)): for x in range(len(game_state[y])): column = Constants.COLUMN_LETTERS[y] row = Constants.ROW_NUMBERS[x] piece = game_state[x][y] if piece == Constants.PIECE_BLACK: TurtleMove.placePiece(column, row, "Black") elif piece == Constants.PIECE_WHITE: TurtleMove.placePiece(column, row, "White") else: TurtleMove.resetSquare(column, row)
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")