Hamlet acts mad, or crazy, mainly to hide that he is investigating Claudius for murdering his father and to buy time before taking revenge.
After the ghost tells Hamlet that Claudius killed King Hamlet, calling it a “foul and most unnatural murder” and saying “the serpent that did sting thy father’s life now wears his crown,” Hamlet does not immediately kill Claudius. He worries about whether the ghost is telling the truth and whether revenge itself is morally right. So he begins behaving strangely as a cover. At the end of his encounter with the ghost, he makes Horatio and Marcellus swear that, no matter how oddly he behaves, they will never reveal what they know about the ghost. His madness begins as a strategy.
That strategy lets Hamlet confuse the people around him. Polonius thinks Hamlet has gone mad because Ophelia rejected him. Claudius and Gertrude summon Rosencrantz and Guildenstern to spy on him. Hamlet uses strange speech, insults, riddles, and theatrical behavior to keep others off balance while he watches them. His most important plan is the play he stages to test Claudius’s guilt: “The play’s the thing, / Wherein I’ll catch the conscience of the king.” By seeming unstable, Hamlet can act suspiciously without everyone immediately understanding his purpose.
Still, the act becomes hard to separate from real distress. Hamlet is grieving his father, disgusted by Gertrude’s quick marriage to Claudius, fixated on death, and repeatedly drawn toward thoughts of suicide. His cruelty toward Ophelia and Gertrude, his reckless killing of Polonius, and Gertrude’s inability to see the ghost in her room all make his madness feel increasingly genuine. The ambiguity is intentional: Hamlet performs madness to uncover the truth, but the performance also exposes how deeply grief, suspicion, and revenge have damaged him.