Thread: State machine Slot machine

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    4

    State machine Slot machine

    Hi I need to produce a skeleton state machine c program code for the following:

    A slot machine which displays 3 numbers which change in random fashion. It has 2 buttons. A start number to change the numbers and a Stop button to stop one of the three numbers (one after the other).
    The slot machine starts changing all three numbers after the start button has been pressed. Then pressing the Stop button repeatedly stops one at a time. A win is indicated on the display if all numbers are equal after all the numbers have been stopped. The game can be restarted after pressing the start button again.

    Can anyone please provide a skeleton State Machine C code for the slot machine description above? Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    248
    I don't usually support gambling, but in this case I bet you are asking us to do your homework for you.

  3. #3
    Registered User
    Join Date
    Mar 2021
    Posts
    4
    Quote Originally Posted by hamster_nz View Post
    I don't usually support gambling, but in this case I bet you are asking us to do your homework for you.
    Lol, nope. But it is on the subject of education! Itís a past exam question and it would be great if anyone can help me with the answer so it can help me with my upcoming exams!

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    248
    Here's a state machine for traffic lights. It might give you ideas:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    
    enum State { state_all_stop, state_NS_go, state_NS_stopping, state_WE_go, state_WE_stopping };
    enum Light { light_red   = 0, light_amber = 1, light_green = 2 };
    char *colours[] = {" red ", "amber", "green"};
    
    
    // Display the traffic lights
    void lights(enum Light we, enum Light ns) {
       printf("---------------\n");
       printf("\n");
       printf("     %s     \n", colours[ns]);
       printf("       |\n");
       printf("%s--+--%s\n", colours[we], colours[we]);
       printf("       |\n");
       printf("     %s     \n", colours[ns]);
    }
    
    
    int main(void) {
       enum State state = state_all_stop;
       while(1) { // Forever
    
    
          // For each state, decide what to do and
          // what the next next state should be
          switch(state) {
             case state_all_stop:
                 lights(light_red,light_red);
                 sleep(3);
                 state = state_NS_go;
                 break;
             case state_NS_go:
                 lights(light_green,light_red);
                 sleep(5);
                 state = state_NS_stopping;
                 break;
             case state_NS_stopping:
                 lights(light_amber,light_red);
                 sleep(2);
                 state = state_WE_go;
                 break;
             case state_WE_go:
                 lights(light_red,light_green);
                 sleep(5);
                 state = state_WE_stopping;
                 break;
             case state_WE_stopping:
                 lights(light_red,light_amber);
                 sleep(2);
                 state = state_NS_go;
                 break;
             default:
                 state = state_all_stop;
                 break;
          }
       }
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    39
    The states seem obvious:

    - Initial state
    - Spinning, with 0 frozen numbers
    - Spinning, with 1 frozen number
    - Spinning, with 2 frozen numbers
    - Displaying result

    Depending on what other requirement you have, the initial state and the final state may be the same. Or, if there's some complexity behind payouts, maybe there are more states and more buttons: pay me now, let it ride, put it on my account, etc.

    The events are somewhat tricky. First, there are the obvious two:

    - start button pressed
    - stop button pressed

    Note that they're exclusive - you could implement this with just one button.

    But how to implement the randomly-changing numbers? You could leave that to an external process, like a separate thread, or you could implement a "timer tick" event that would sometimes update the numbers.

    Finally, there is the win/lose output. You don't mention anything about the losing case, but it seems obvious: "You won!" and "Better luck next time!"

    So draw a state transition table:
    From State Start button Stop button Timer tick Notes/Actions
    Initial spinning0 -- -- "Press 'Start' to play"
    Spinning0 -- spinning1 update_digits "Press 'Stop' to stop a wheel"
    Spinning1 -- spinning2 update_digits
    Spinning2 -- game_over -- Won/lost message
    Game_over initial initial --



    You'll have to decide whether Game_over is the same as the Initial state, or if they are different. Basically, if you want to put two messages on the screen (you lost / start to play) or put one message up, wait for an acknowledgement, then put the other message up.

    If you're printing a long page of "how to play" rules, you might want two different states.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please! Need help with slot machine code
    By petertrieu in forum C Programming
    Replies: 3
    Last Post: 11-24-2015, 06:46 PM
  2. How can I get this slot machine code to run?
    By Nick Petraglia in forum C Programming
    Replies: 15
    Last Post: 11-21-2015, 10:27 AM
  3. Slot Machine Problem
    By DecoratorFawn82 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2013, 07:11 PM
  4. Slot Machine
    By nomi in forum C Programming
    Replies: 9
    Last Post: 01-03-2004, 10:13 AM
  5. IDEA: A Slot Machine (aka a fruit machine)
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:13 PM

Tags for this Thread