0

Game: nine heads and tails

Description of Game: Nine coins are placed in a 3x3 matrix with some face up and some face down. You can represent the state of the coins with the values 0(heads) and 1(tails). (Here are some examples: 000 101 110 101 010 001 100 110 000 100 001 100) Each state can also be represented using a binary number. (For example, the preceding matrices correspond to the numbers: 000010000 101001100 110100001 101110100). There are a total of 512 possibilities. So, you can use the decimal numbers 0, 1, 2, 3, ..., and 511 to represent all states of the matrix. My goal is to write a program that prompts the user to enter a number between 0 and 511 and displays the corresponding 3x3 matrix with the characters H and T. (ex. "Enter a # between 0 and 511: " (7) '''the user entered 7, which corresponds to 000000111. Since 0 stand for H and 1 for T, the output is:''' >>> H H H H H H T T T ) I'm finding this a bit complicated, so I'd appreciate if anyone could help me out with figuring out how to start this out or complete it. Any suggestions or tips are appreciated!

12th Sep 2021, 11:46 PM
Michael Aguirre
4 Antworten
+ 2
here is a quick code, long time ago I made a code here for transposition encryption. just for fun. your description reminded me of writing that function. so I quickly modified my code to fit your project. it doesn't necessarily have to be that complicated. it's a quick edit of my old code. you can remove a lot. my code can work with greater matrices not only 3x3. check my transposition code and compare it with the modified one here. https://code.sololearn.com/ctuofjkk5Ri4/?ref=app
13th Sep 2021, 12:37 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 2
Convert the decimal number to binary. Then format as three by three matrix.
13th Sep 2021, 12:00 AM
Simon Sauter
Simon Sauter - avatar
+ 2
easier approach. get a number in the specified range convert it to 9 bit digits to get 3x3 grid replace 0 and 1 with H T convert the result to a two dimensional list. with 3 rows and 3 columns. remember my code is not the best solution. it's just a hack of what I already wrote for another purpose.
13th Sep 2021, 12:41 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Thanks guys, appreciate it!
13th Sep 2021, 2:12 PM
Michael Aguirre