Alan Turing, the figure at the center of the already much praised film, “The Imitation Game,” which opens Nov. 28, is probably better known to computer scientists than he is to most moviegoers.Turing, a British mathematician, is now widely credited with helping to develop the theoretical underpinnings formodern computing. He was also a war hero of sorts,largely responsible for cracking the notoriously difficult Enigma code, which the Germans used for virtually all their military communication in World War II. Churchill believed that his was the single biggest contribution to Allied victory.

But Turing, obsessed with ciphers all his life, was himself a puzzle and a bundle of contradictions.He was famously eccentric and antisocial, more at home with numbers than with people. He was also forthrightly gay at a time when homosexual activity was illegal in Britain, and in 1952, he was convicted on charges of indecency.Instead of prison, he chose to be chemically castrated, and two years later, at the age of 41, miserable over the changes the hormones had wreaked in him, he bit into an apple laced with cyanide.His death was almost certainly a suicide, possibly even a re-enactment of a scene in his favorite movie, “Snow White,” though his mother insisted that he was just sloppy with chemicals, and to this day there are conspiracy theorists who believe that he was assassinated by MI6, fearful that he was a security risk.

How do you dramatize such a life, so much of it, the wartime years especially, conducted in secret? The 2001 movie “Enigma,” with a Tom Stoppard script adapted from Robert Harris’s novel of the same name, is a version of the Turing story, but it turns the Turing character into a heterosexual enmeshed in a love triangle and adds an espionage plot for good measure.Hollywood is “not really designed around making movies about gay English mathematicians,” Graham Moore, who wrote the script for “The Imitation Game,” said in a recent email.

In 2011, his script was No. 1 on the Black List, an informal straw poll of highly regarded but unproduced screenplays. It bounced around for years before Teddy Schwarzman (a producer of last year’s “All Is Lost”) took it over from Warner Bros. and got the movie made for a mere $15 million.“It just felt like a story that needed to be told,” Mr. Schwarzman said.

Mr. Moore, who is 33, had never written a movie script before; he is probably best known for his 2010 novel “The Sherlockian,” a best-selling mystery about modern-day Sherlock Holmes obsessives.The movie’s director, Morten Tyldum, is also a first-timer of sorts: He’s a Norwegian, known for blackly comic thrillers like “Headhunters,” based on the Jo Nesbo novel (which includes possibly the most outrageous outhouse sequence in movie history). “The Imitation Game” is his first film in English.The way the movie evolved has a lot to do with the particular interestboth he and Mr. Moore brought to the Turing story, and with the zeal of Benedict Cumberbatch, who plays Turing with an intensity that has already made him an Oscar front-runner.

“I surprised myself,” Mr. Tyldum said recently about his decision to make “The Imitation Game,” explaining that he never imagined his first English-language movie would be a period piece, with lots of dialogue, set in Bletchley Park, the Buckinghamshire estate where the British code-breaking effort was headquartered.He laughed and added, “It meant I couldn’t hide behind explosions and crazy action.”What won him over, he went on to say, was the script, and the chance it gave him “not to put myself in a box.”

“I was looking for a story and a character, and this had both,” he said.“There were so many layers.Turing was very strong and driven, and at the same so awkward and fragile.”

Mr. Schwarzman said he picked up the phone and called Mr. Tyldum on a hunch after watching “Headhunters” on Netflix.“I looked at a lot of high-profile Brit directors,” he explained. “But I didn’t find anyone who was passionate. I didn’t want a traditional biopic that would put people to sleep.” What he loved about “Headhunters,” he said, “was that it showed such a range of emotion.It was a thriller and almost a screwball comedy at times.”

A confessed computer nerd, Mr. Moore said he had been obsessed with the Turing story since he was a teenager, when Turing was an object of almost cultlike fascination among him and his friends in Chicago. He kept circling around the idea of a film about Turing, and finally, while touring for “The Sherlockian,” he had a couple of Scotches on a plane and wrote the first scene.

Part of the difficulty in writing about Turing, he went on, is that until fairly recently, much of his work was classified. There are no recordings of him, no videotapes, and from childhood on, his naturewas always hard to fathom.Turing was notoriously odd and awkward, the kind of person who stuttered,kept his tea mug chained to a radiator and would walk away in the middle of a conversation if it failed to interest him.

At the same time, Mr. Moore noted, Turing was not a doddering, absent-minded professor type. He was a strong personality,and though driven by logic, he was also capable of deep feeling, especially for a boyhood friend whose death he never stopped mourning. And he was unabashed about having a sex life. At his trial, he expressed no remorse and did not contest the facts.

Hugh Whitemore’s 1986 play “Breaking the Code” (based, like “The Imitation Game” on Andrew Hodges’s 1983 Turing biography) features a Turing who is both tragic and endearing, a sort of innocent eccentric. He was played to great acclaim (in London, on Broadway and in a 1996 BBC movie version) by Derek Jacobi, recycling his “I, Claudius” stammer.Mr. Moore’s Turing is from the beginning more of a mystery, to the viewer and perhaps even to himself. Unlike “Breaking the Code,” which in fact deals very little with the actual code breaking, Mr. Moore’s screenplay doesn’t hesitate to plunge Turing into what amounts to an espionage thriller. Historically, there was no single breakthrough moment, as the movie suggests; the Enigma code had to be cracked several times as the Germans kept refining it. But noting that, for the most part, the camera moves only when Turing does — so that the viewer sees the world mainly through his eyes — Mr. Moore insisted that the movie is conceptually faithful to Turing’s experience. “That’s what it would have felt like to him,” he said. “He would have felt he was in a spy thriller.”

Speaking from his home in London, Mr. Cumberbatch talked at length and with unusual feeling about Turing, who in his version is more complicated, less cozy than in Mr. Jacobi’s. At one point, he even demonstrated Turing’s well-known stammer over the phone: not a full-fledged stutter, but something lighter and more rapid: a voice trying to catch up with the mind whirring behind it.

Mr. Cumberbatch, probably best known as the title character in the BBC series “Sherlock,” said that he was wary of being typecast these days in roles about people who were overly clever, but that he eagerly sought the part of Turing. He knew something of the story to begin with, he explained, from seeing “Breaking the Code,” among other things. And when he read Mr. Moore’s script, he found that it was just as good as its reputation.“I thought, ‘My God, how important this story is, how relevant,’ ” he said.“It sounds trite, but it’s a story that highlights how careful we have to be in a world where people seem different.We’re all different.”

In hindsight, several commentators have suggested that Turing’s personality traits probably placed him somewhere on the Asperger’s spectrum.Mr. Cumberbatch, who prepared for the part by talking to Turing’s relatives and some of his colleagues, said he was wary of that label. “The more I researched it, the more I came to think he was just a very brilliant, very sensitive human being,” he said.

Pointing out that Turing had grown up apart from his parents, who spent much of his childhood living in India, Mr. Cumberbatch said: “I think he was created an outsider. The world failed him in a way that meant he had to be cut off.”At the same time, “Turing was not insular,” he said.“He was physically and emotionally rugged.He became a world-class marathoner and he was sexually alive.He knew love, thank God.”

Mr. Moore said: “The way to seem really smart is to have Benedict Cumberbatch deliver what you’ve written.I could have typed out the phone book, and it would have worked.”In an email he mentioned the film’s skimpybudget (just enough to pay for limousines on some movies, he claimed) and said: “Would it have been nice to have had craft services, or even heat on our soundstage? Yes. (I cannot overemphasize the chilliness of an English November.) But then we wouldn’t have gotten to make our movie, the way we wanted.”

A version of this article appears in print on , on Page AR28 of the New York edition with the headline: The Riddle Who Unlocked the Enigma. Order Reprints | Today’s Paper | Subscribe