Wednesday, April 24, 2024
Coding STEM

How do those words game work???

Here I’ll show you the basic concepts of the most famous word games…and how they are actually coded. Obviously, I’m NOT going to show you advanced algorithms or any proprietary information here. Just the basics…you go party on with this information with your own creativity.

There are numerous games out there based on this concept. You are given a set of letters, and you have to construct a word, the “correct” word. Very similar to Wheel of Fortune.

Code:

I will show Python code here for now (but I can demo in many other languages e.g. C, C++, Go, Java, JavaScript, VBA, etc. etc. etc. but STOP!).

import random

str = ['C','O','C','O','N','U','T','F','R','U','I','T']
random.shuffle(str);
print("Shuffled:", str);

Explanation:

I have an array of characters (C/C++ guys get it!), or a list (Python guys get it!) of values. Now I can re-arrange the letters by using shuffle() function. The function comes from random module/library of Python. So, I use the import statement above. Within it, is the random.shuffle() method. So there we have it!

Everytime you run it, it’s going to be different 🙂 “Oh sir, one more thing….” (here)

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top