Dr. Pulaski eggs on Geordi's frustration with trying to test Data's intelligence to grow beyond rote memorization when she challenges La Forge to a battle of wits on the holodeck. The test will be to see if Data's cognitive abilities will allow him to solve a Holmes-style mystery set in the time period, but the ship's computer will create a totally new plot, circumstances, and denouement to the story.
Geordi accidentally programs the ship to have an adversary capable of defeating Data as opposed to Holmes. Suddenly, Dr. Moriarty, the nemesis of Holmes, has the same powers over the ship as Data has with one exception: he can't leave the holodeck. He changes the weather, modifies life support, and pilots the Enterprise until he gets what he wants.
Picard finally agrees to meet him and explains to him on intellectual terms that Moriarty is trapped in a fictional world by his very nature. Picard convinces Moriarty that he can live a peaceful life inside the Enterprise's main computer and promises that his artificial intelligence will be preserved. Upon this agreement, Moriarty returns control of the ship to Picard.