Bug 334107 - solution word sometimes precedes anagram string
Summary: solution word sometimes precedes anagram string
Status: RESOLVED FIXED
Alias: None
Product: kanagram
Classification: Applications
Component: general (show other bugs)
Version: 0.2
Platform: Gentoo Packages Linux
: NOR normal
Target Milestone: ---
Assignee: Jeremy Whiting
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-04-30 00:15 UTC by Martin Walch
Modified: 2014-05-04 03:04 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In: 4.13.1


Attachments
proposal for a fix (672 bytes, patch)
2014-05-03 19:58 UTC, Martin Walch
Details
fix the problem and avoid infinite loop (1.70 KB, patch)
2014-05-03 21:44 UTC, Martin Walch
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Walch 2014-04-30 00:15:30 UTC
in src/engine/kanagramgame.cpp:

void KanagramGame::createAnagram()
{
    QString anagram;
    QString letters;
    int randomIndex;

    do {
        letters = m_originalWord;
        while (!letters.isEmpty())
        {
            randomIndex = m_random.getLong(letters.count());
            anagram.append(letters.at(randomIndex));
            letters.remove(randomIndex, 1);
        }
    } while (anagram == m_originalWord);

    m_anagram = anagram;
}

the do while loop does not reset the QString anagram if anagram == m_originalWord, but instead appends the anagram to the duplicated original word. As a result, sometimes a bad anagram shows up. Most of the time this happens for particularly short words like "mir".

Reproducible: Sometimes
Comment 1 Martin Walch 2014-05-03 19:58:41 UTC
Created attachment 86439 [details]
proposal for a fix

Simple idea for fixing: just insert anagram.clear() at the beginning of the do while loop. This is no particularly pretty solution as it performs a second initialization of the string.
Comment 2 Martin Walch 2014-05-03 21:44:30 UTC
Created attachment 86440 [details]
fix the problem and avoid infinite loop

While it does not make much sense in an anagram game to have a word that is only of a series of the same character, I still think this should be handled well. Without the previous patch this will *always* trigger the bug this report is about, i. e. if the word is e.g. the Finnish town Ii, the displayed anagram is iiii. After applying the previous patch, no anagram will be shown in that case and instead the program runs into an infinite loop, not responding anymore.

This revised patch takes that case into account.
Comment 3 Jeremy Whiting 2014-05-03 23:19:01 UTC
This does look like a better fix indeed. Do you have an account to commit this fix? If not I'll commit it for you, just let me know.

thanks,
Jeremy
Comment 4 Martin Walch 2014-05-04 01:03:04 UTC
(In reply to comment #3)
> This does look like a better fix indeed. Do you have an account to commit
> this fix? If not I'll commit it for you, just let me know.

I do not have an account. Thank you for committing.
Comment 5 Jeremy Whiting 2014-05-04 03:04:15 UTC
Git commit 94d899c449a91e8e3863c94316401b74a13bdae2 by Jeremy Whiting.
Committed on 04/05/2014 at 03:02.
Pushed by whiting into branch 'master'.

Fix anagram creation duplicating letters when word is short.
Thanks to <walch.martin@web.de> for the patch.
FIXED-IN: 4.13.1

M  +20   -15   src/engine/kanagramgame.cpp

http://commits.kde.org/kanagram/94d899c449a91e8e3863c94316401b74a13bdae2
Comment 6 Jeremy Whiting 2014-05-04 03:04:43 UTC
Git commit bf0bffd42c971e0a96a9e2297c2573a9db18546a by Jeremy Whiting.
Committed on 04/05/2014 at 03:02.
Pushed by whiting into branch 'KDE/4.13'.

Fix anagram creation duplicating letters when word is short.
Thanks to <walch.martin@web.de> for the patch.
FIXED-IN: 4.13.1
(cherry picked from commit 94d899c449a91e8e3863c94316401b74a13bdae2)

M  +20   -15   src/engine/kanagramgame.cpp

http://commits.kde.org/kanagram/bf0bffd42c971e0a96a9e2297c2573a9db18546a