Question: Finite state machines also play important roles in software design, including digital control system implementation and event-driven software design (most web services, user interfaces, and
Finite state machines also play important roles in software design, including digital control system implementation and event-driven software design (most web services, user interfaces, and a growing number of games are designed in this way) as well as parts of compilers. In this problem, you will draw a state transition diagram corresponding to an adventure game. In the game, each room is a state, and the input values (0, 1, or 2 for our game) correspond to transitions. Download the program dungeon.c from our class web page, play the game, analyze the code, and draw a state transition diagram. Use the room number from the code to label the states, and the input value (0, 1, or 2) to label transitions between states. Note that the game ends in some of the rooms, so your diagram should not have transition arcs leaving these states.
Code:
/* * dungeon.c * Douglas L. Jones * University of Illinois at Urbana-Champaign * September 29, 2012 * * (c) 2012 by Douglas L. Jones * This work is made available according to the * Creative Commons "Attribution" license * http://creativecommons.org/licenses/by/3.0/ * * edited by S.S. Lumetta, 11 October 2012 * edited by Yuchen He, March 2015 * * dungeon.c: a simple maze game */ #include/* Define constants */ int main () { /*********************/ /* Declare variables */ /*********************/ int flag_play; /* indicates whether to play the game again */ int room; /* indicates current room */ int key; /* indicates whether key is found */ int choice; flag_play = 1; while ( flag_play == 1 ) { /***************/ /* Play a game */ /***************/ key = 0; room = 0; while ( room < 3 ) /* Explore the maze */ { if ( room == 0 ) { printf("You wake in a dungeon, chained to the wall! "); printf("The chains are rusty, and you manage to break free. "); printf("You search the room carefully, "); printf("and you discover two hidden exits; "); printf("a tunnel under a large flagstone, "); printf("and an air vent large enough to crawl through. "); printf("Which exit do you wish to explore? "); printf("Enter 0 for flagstone, or 1 for air vent. "); scanf("%d", &choice); if ( choice == 0 ) { room = 3; } if ( choice == 1 ) { room = 1; } } else if ( room == 1 ) { printf("You emerge in what appears to be a large cavern. "); printf("A bat brushes your head as it flies past. "); printf("Groping around in the darkness, you discover a "); printf("crawlway and a fissure you think you can fit through. "); printf("There also seems to be an opening behind a large "); printf("stalagmite. "); printf("Which exit do you wish to explore? "); printf("Enter 0 for crawlway, 1 for fissure, or 2 for behind "); printf("stalagmite. "); scanf("%d", &choice); if ( choice == 0 ) { if (!key) { printf("A locked door stopped you from moving forward."); room = 1; } else { room = 2; } } if ( choice == 1 ) { if (!key) { printf("A locked door stopped you from moving forward."); room = 1; } else { room = 3; } } if ( choice == 2 ) { if (!key) { printf("You found a shiny key on the floor."); key = 1; } room = 1; } } else if ( room == 2 ) { printf("The passage expands into a small cave with an "); printf("underground waterfall. "); printf("The passage continues on, but you feel another passage "); printf("behind the waterfall. "); printf("Enter 0 for behind waterfall, or 1 for continue "); printf("along passage. "); scanf("%d", &choice); if ( choice == 0 ) { room = 1; } if ( choice == 1 ) { room = 4; } } printf(" "); } /* End of game conditions */ if ( room == 3 ) { printf("You feel the passage widening and a draft of fresh air. "); printf("On your next step, you fall into a deep pit and die. "); } if ( room == 4 ) { printf("You see a light in the distance and climb toward it. "); printf("You emerge on the surface in a beautiful forest. "); printf("You have successfully escaped; you are free! "); } printf(" "); printf(" Would you like to play again? Enter 1 for yes, 0 for no. "); scanf("%d", &flag_play); printf(" "); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
