How do I use a different template for the anonymous users? If the user is the anonymous user or an authenticated user, I want to use the page1.tpl.php; with other roles, I want to use page.tpl.php.
How do I write the code?
If you want to handle different templates for different scenarios the context module is a great tool. This case is quite simple, so you could handle it with code like suggested. But using the context module, makes it a bit more manageable, and allows you not only to decide what template to use, to also what blocks to insert. It's a great module and is one of those I use for almost all of the sites I make.