(that He may requite those who do evil with that which they have done, and reward those who do good, with what is best.) 53:31. He did not create all that in vain or for (mere) play:
(And We created not the heaven and the earth and all that is between them without purpose! That is the consideration of those who disbelieve! Then woe to those who disbelieve from the Fire!) 38:27
بَلْ نَقْذِفُ بِالْحَقِّ عَلَى الْبَاطِلِ فَيَدْمَغُهُ فَإِذَا هُوَ زَاهِقٌ (Instead, We launch the truth against the falsehood, which smashes it, and in no time it is nothing. - 21:18) The literal meaning of قَذَف is to throw horizontally which has been translated above as launching) یدمَغُ means to hit on the head (to smash it) and زَاھِق means something which is gone or vanishes without leaving a trace.
This verse explains that Allah has not created this marvelous universe with earth and the sky for amusement. This creation is the result of a carefully thought out plan which aims at distinguishing right from wrong. Observation of Nature's creations leads people along the righteous path and protects them from evil. This idea is conveyed in the verse by saying that virtue is hurled against evil and smashes its head so completely that it disappears without a trace.
Tazkirul Quran
Maulana Wahiduddin KhanOn ayahs 18–19
Those who are not serious about the divine call, consider the present world as a plaything of God which has no purpose except temporal entertainment. But judging by the immense wisdom and meaningfulness underlying the working of the present world, it would appear to be impossible that the Creator of this world could be a being who has created it merely for the sake of diversion. In the present world, there is the special creation of God, namely, human beings, who by their very nature, have the ability to distinguish truth from falsehood. The existence in the world of such creatures, who can use their discretion and decide upon one path as that of truth and another as that of untruth, coupled with the frequent struggle between Truth and untruth, are all indications of the fact that a time is going to come when it will finally be clear as to what is really Truth and what is Untruth. And whoever sides with the Truth will attain success and whoever does not will be doomed to failure.
Uthmani text of the Madinah mushaf (King Fahd Complex). Translation: Saheeh International. Tafsir via Quran.com.