Thread: Scrabble rack generator (not really)

  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612

    Scrabble rack generator (not really)

    How about a dumb program?
    It turns out that words are legible if you put the letters in any order, as long as the first and last letter are kept correct. So I quickly threw something together that will scramble any sentence I typed in, and played a prank on a friend.

    Code:
    #include <string>
    #include <iostream>
    #include <cctype>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
      string word;
      bool done = false;
      
      while (cin >> word) {
        string::iterator last = word.begin() + word.length() - 1;
    
        if (ispunct(*last)) {
          if (*last == '.' || *last == '?' || *last == '!') {
            done = true;
          }
    
          last--;
        }
    
        random_shuffle(word.begin()+1, last);
        cout << word << ' ';
    
        if (done) {
          cout << '\n';
          break;
        }
      }
    
      return 0;
    }
    only made it for my use, but if anyone wants to have some fun/ make it better feel free.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your code segfaults on single-letter words for me.
    Also, you should seed the rng so it's different each run.
    random_shuffle() uses rand() so you seed it with srand().
    Code:
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <cstdlib>
    #include <ctime>
     
    using namespace std;
     
    int main()
    {
      srand(time(0));
     
      string word;
      bool done = false;
     
      while (!done && cin >> word) {
        auto last = word.end() - 1;
     
        if (ispunct(*last)) {
          done = (*last == '.' || *last == '?' || *last == '!');
          --last;
        }
     
        auto dist = distance(word.begin()+1, last);
     
        if (dist == 2) // always swap middle letters of 4-letter words (maybe?)
          swap(word.begin()[1], last[-1]);
        else if (dist > 2)
          random_shuffle(word.begin()+1, last);
     
        cout << word << ' ';
      }
     
      cout << '\n';
     
      return 0;
    }
    random_shuffle() has been removed from C++ since C++17.
    You can use shuffle() instead which uses the <random> library instead of rand().
    Code:
    #include <algorithm>
    #include <iostream>
    #include <random>
    #include <string>
    #include <cctype>
     
    using namespace std;
     
    int main()
    {
      random_device rd;
      minstd_rand g(rd());
     
      string word;
      bool done = false;
     
      while (!done && cin >> word) {
        auto last = word.end() - 1;
     
        if (ispunct(*last)) {
          done = (*last == '.' || *last == '?' || *last == '!');
          --last;
        }
     
        auto dist = distance(word.begin()+1, last);
     
        if (dist == 2) // always swap middle letters of 4-letter words (maybe?)
          swap(word.begin()[1], last[-1]);
        else if (dist > 2)
          shuffle(word.begin()+1, last, g);
     
        cout << word << ' ';
      }
     
      cout << '\n';
     
      return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Hello John, thanks for your comments. I figured that supplying a generator would be one of the first changes people would make. I didn't want to bother so I used an older standard. Next time I write the program I'll have to be more careful about words like I or a. Nice find. There are definitely things I would have done different if I was interested in making the program more robust... Like letting it work on multiple lines.

    It's just the kind of shoddy quality you get from me if I only spend 30 minutes on something.

    Something I learned from your rewrite now, actually:
    Code:
        if (dist == 2) // always swap middle letters of 4-letter words (maybe?)
          swap(word.begin()[1], last[-1]);

    I didn't know operator[] was defined for string iterators!

    Although, to respond to the comment, personally I thought leaving some words alone by chance was an even better prank.
    Last edited by whiteflags; 07-20-2023 at 03:04 PM.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    What about a 5-letter word? There's a 1 in 6 chance that it'll be untouched too.

    So there's a good chance that one of the words in these sentences will stay the same:

    Alice reads books. Bobby reads books.
    I'd also just let the shuffle do its job and let it produce untouched results some of the time.

    If you wanted to guarantee that a word will always be different than the original, you could put the shuffling in a loop, and if the word is 4 letters or longer and the shuffled version is the same as the original, repeat the loop. That way you don't have to handle 4-letter words specifically.

    Edit: I didn't consider cases where all of the inside letters are the same, as in "book" or "booooook". That would make the loop infinitely repeat. So you would need another test to see if all the letters are the same; if they are, just break out of the loop (or don't enter the loop in the first place).
    Last edited by christop; 07-21-2023 at 12:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New 1/2 rack colo - signup question
    By ginoitalo in forum Tech Board
    Replies: 0
    Last Post: 02-12-2010, 09:49 AM
  2. scrabble counting
    By kees in forum C Programming
    Replies: 6
    Last Post: 10-03-2009, 09:43 AM
  3. Scrabble Solver
    By Coder87C in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2005, 03:36 PM
  4. Scrabble AI
    By vex_helix in forum C Programming
    Replies: 9
    Last Post: 04-03-2004, 03:03 PM
  5. Player's input in Scrabble?
    By vex_helix in forum C Programming
    Replies: 5
    Last Post: 03-26-2004, 02:38 AM

Tags for this Thread