Although he wasn't illegitimate, and he had an older brother, Tolstoy grew up in a similar way to Pierre. He was orphaned as a young man and inherited a ton of money from his parents. At first he just partied as much as possible, but then he got his act together and started to write short journalism pieces.
Before writing War and Peace Tolstoy spent time as a war correspondent during the 1857 Anglo-Russian war in Crimea. He saw battle up close and personal and started to describe what he was seeing, much to the shock of the people reading his articles back home. That experience would shape his approach to war in this novel.
Originally War and Peace was going to be about the Decembrist uprising, when a group of aristocrats wanted to kill the middle-aged Emperor Alexander. While researching the uprising, he became interested in Napoleon's campaign.