Skip to content

Instantly share code, notes, and snippets.

@JFFail
Created April 26, 2016 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/f6b21f504d8f69577c358066d9a64ec4 to your computer and use it in GitHub Desktop.
Save JFFail/f6b21f504d8f69577c358066d9a64ec4 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer #260 - Garage Door Opener
//Reddit Daily Programmer #260 - Easy
//https://www.reddit.com/r/dailyprogrammer/comments/4cb7eh/20160328_challenge_260_easy_garage_door_opener/
using System;
namespace DoorOpener
{
class DoorOpener
{
//Create an enumerated set for my sanity in figuring the state of the door.
enum DoorState { open, closed, opening, closing, stoppedOpening, stoppedClosing, emergencyOpening, openBlocked };
//The only property that door has is the state.
public int state { get; set;}
//Default constructor; door always starts off closed.
public DoorOpener()
{
state = (int)DoorState.closed;
}
//Function to print the current state of the door.
public void PrintState()
{
if(this.state == (int)DoorState.open)
{
Console.WriteLine("Door: OPEN");
}
else if(this.state == (int)DoorState.closed)
{
Console.WriteLine("Door: CLOSED");
}
else if(this.state == (int)DoorState.opening)
{
Console.WriteLine("Door: OPENING");
}
else if(this.state == (int)DoorState.closing)
{
Console.WriteLine("Door: CLOSING");
}
else if(this.state == (int)DoorState.stoppedOpening)
{
Console.WriteLine("Door: STOPPED_WHILE_OPENING");
}
else if(this.state == (int)DoorState.stoppedClosing)
{
Console.WriteLine("Door: STOPPED_WHILE_CLOSING");
}
else if(this.state == (int)DoorState.emergencyOpening)
{
Console.WriteLine("Door: EMERGENCY_OPENING");
}
else if(this.state == (int)DoorState.openBlocked)
{
Console.WriteLine("Door: OPEN_BLOCKED");
}
}
//Function for acting when the button is clicked for the opener.
public void ToggleButtonClick()
{
if(this.state == (int)DoorState.open)
{
this.state = (int)DoorState.closing;
}
else if(this.state == (int)DoorState.closed)
{
this.state = (int)DoorState.opening;
}
else if(this.state == (int)DoorState.opening)
{
this.state = (int)DoorState.stoppedOpening;
}
else if(this.state == (int)DoorState.closing)
{
this.state = (int)DoorState.stoppedClosing;
}
else if(this.state == (int)DoorState.stoppedOpening)
{
this.state = (int)DoorState.closing;
}
else if(this.state == (int)DoorState.stoppedClosing)
{
this.state = (int)DoorState.opening;
}
//Don't actually do anything since it's unnecessary to re-assign the same value.
//Just putting it here so I don't think I missed it.
/*else if(this.state == (int)DoorState.emergencyOpening)
{
this.state = (int)DoorState.emergencyOpening;
}
else if(this.state == (int)DoorState.openBlocked)
{
this.state == (int)DoorState.openBlocked;
}*/
}
//Function for completing the cycle.
public void CompleteCycle()
{
if(this.state == (int)DoorState.opening)
{
this.state = (int)DoorState.open;
}
else if(this.state == (int)DoorState.closing)
{
this.state = (int)DoorState.closed;
}
else if(this.state == (int)DoorState.emergencyOpening)
{
this.state = (int)DoorState.openBlocked;
}
}
//Function for dealing with blocking items.
public void BlockDoor()
{
this.state = (int)DoorState.emergencyOpening;
}
//Function to clear a door block.
public void ClearBlock()
{
this.state = (int)DoorState.open;
}
static void Main() {
//Create the input array.
//Original array pre-bonus material.
/*string[] buttonInputs = new string[]
{
"button_clicked",
"cycle_complete",
"button_clicked",
"button_clicked",
"button_clicked",
"button_clicked",
"button_clicked",
"cycle_complete"
};*/
//Bonus array!
string[] buttonInputs = new string[]
{
"button_clicked",
"cycle_complete",
"button_clicked",
"block_detected",
"button_clicked",
"cycle_complete",
"button_clicked",
"block_cleared",
"button_clicked",
"cycle_complete"
};
//Create the object.
DoorOpener theDoor = new DoorOpener();
//Print the starting state of the door.
theDoor.PrintState();
//Loop through each input and calculate the result.
foreach(string buttonInput in buttonInputs)
{
//Act based on whether it was a click or a cycle completion.
if(buttonInput == "button_clicked")
{
Console.WriteLine("> Button clicked.");
theDoor.ToggleButtonClick();
}
else if(buttonInput == "cycle_complete")
{
Console.WriteLine("> Cycle complete.");
theDoor.CompleteCycle();
}
else if(buttonInput == "block_detected")
{
Console.WriteLine("> Block detected");
theDoor.BlockDoor();
}
else if(buttonInput == "block_cleared")
{
Console.WriteLine("> Block cleared");
theDoor.ClearBlock();
}
//Print the results.
theDoor.PrintState();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment