The incident is described in the Alf Layla wa Layla, ‘Thousand Nights and a Night’, a set of interconnected tales whose literary setting is made Baghdad, under the Caliph Haroun al Rashid, who ruled at the time of Charlemagne.
However, the ‘Nights’ consists of what were known as ‘market-place tales’; they are characteristic of Egypt where the collection is thought to have gained essentially its present form (c.12thC) and include some stories which are plainly descended from dynastic Egyptian belief.
Secondary sources often repeat the old story that ‘card-games were known in Baghdad’ without actually checking the source, the history of the Nights or the context in which the reference occurs.
The story of the slave girl Tawaddud makes clear, however:
(i) that the practice was not native to Islam, but probably brought to the Muslim world by older communities – the context suggests a Christian origin.
(ii) that the purpose of the games was to aid memory of the curriculum of studies, though it appears also to include some sort of ‘ordinary’ card-play as the last exercise.
Otherwise, Tawaddud uses them as aids to memory (as she explains) when set against specialists in every one of the disciplines which constituted the medieval curriculum of studies – grammar and rhetoric, medicine (where the display of virtuosity is still astonishing), astronomy, the calendar, religious commentary and so on.
In answer to the Caliph’s demand to know the secret of her encyclopaedic knowledge (as a woman and slave her education was unexpected), she answers that ‘the almanac-makers have certain tokens, whereby ordinary people may learn somewhat by practice. The modest response is formal; the girl refutes the highest specialists in every discipline including a vain and ‘muttering’ Frank.
So card-use is attested by this story in Egypt (not its fictional setting, Baghdad) and by the time of the Nights’ compilation ~ round about the 12thC,
Hope that clears things up.
PS There may still be floating somewhere on the net an old essay of mine on this subject. Signe ‘Diane O’Donovan’ . It will be about 15 yrs old or so.