0

Domino simulation [SOLVED]

I am trying to write a code which essentially simulates tipping over dominoes sequentially. The best way to express this is through an example run: 0=empty , 1=upright, 2=tipping, 3=horizontal User input is 1 1 1 0 2 1 1 1 0 1 Making the initial configuration: ||| /|||| | So, the following runs would go as follows run 0: /|| _ /|| | (if position 1 is upright, it will always be tipped on the first run) run 1: _/| __/| | until we get to run 4 which looks like this: ___ ____ | The program will run 10 times to ensure all the dominoes are toppled (so in this case, run 4 will look identical to run 10 seeing as it has exhausted all actions). I’m having trouble getting this, some pointers would be greatly appreciated. Below I have written out a basic idea of how I am trying to do this. https://code.sololearn.com/czNfzYk2AJ91/?ref=app

2nd May 2021, 7:03 AM
Alejandro
3 Antworten
+ 1
Your conditionals are nested incorrectly. If state[ i ] is upright, it can never be tipping. I reformatted them to match the domino structure. If you want to repeat the simulation, you would need to put your logic into a loop. However, repeating a fixed number of times could result in many unnecessary runs. Since you only need to change something in the next iteration if a stone has started tipping, I would monitor this exact condition via a variable. You also want to treat the first stone slightly differently in the first iteration, which could be achieved via conditionals, but I found it simpler to use a sentinal tipping state at the start, that also ensures no out-of-bounds access happens, but is never actually used. Furthermore, the "next_state" array is somewhat redundant if you change the states in backwards order. This is what it could look like: https://code.sololearn.com/cXvRwYqnkHO3/?ref=app If you already know about functions, it would make sense to refactor the code to that effect.
2nd May 2021, 9:48 AM
Shadow
Shadow - avatar
+ 1
The puts() function simply prints the given string to the console, and appends a new line. It's pretty much the same as printf( "...\n" ); without any additional arguments. If you need a reference for C, I can recommend the following site. For example, more about puts(): https://en.cppreference.com/w/c/io/puts
2nd May 2021, 2:10 PM
Shadow
Shadow - avatar
0
Thank you, Shadow. I have not used “puts” yet nor have I seen it used in code to this point. I will look into what this does and then reaccess this. I have used functions about two or three times to this point, so once I figure this puts out, I can attempt to reconfigure the code...
2nd May 2021, 1:44 PM
Alejandro