If you're developing a game that's very similar to a game that's already published, both the game mechanics and the graphics but you're not publishing it, instead you're keeping it in your own computer. Is it legal to do so?
Generally speaking, copyright law does not have an exclusion for "private use" or similar (depending, perhaps, on your jurisdiction). Violating copyright is violating copyright, regardless of whether or not you then further redistribute the work by making additional copies for "publication."
So no, it is not legal.
Of course, that doesn't stop people from doing it. From a practical perspective, of course, it's unlikely anybody could discover your violation. That doesn't make it "okay," but it significantly decreases the risk factor in many peoples' minds. Often you will not see entire games copied, but people using bits and pieces of artwork as "placeholders," et cetera.
Personally I wouldn't recommend this - it's very easy to accidentally keep a piece of copyrighted artwork in a game when you actually ship it, for example.
There's also "fair use" doctrine in some places, like the US, which is a defense for certain limited kinds of use such as parody or education. It's important to note that this is a defense, which means you have to be taken to court to prove your case, so it's not something you should rely on if your goal is to avoid being sued.
Note that none of this applies to imitating "game mechanics" or ideas. Those cannot be covered by copyright. Copyright covers work in fixed form, such as art and sound.
I don't see why not. If you are keeping the game to yourself (not publishing it) it would be fine. Some companies may even allow you to publish an imitated copy of their game as long as you aren't making money off of it (ad revenue, making the game cost money, etc). It would be helpful if you could edit your question so that we know what game you are planning to imitate, but overall it should be legal, as long as you don't publish it.