-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefusingBomb.cpp
81 lines (71 loc) · 1.59 KB
/
DefusingBomb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
/* Assign colors for readability */
#define WHITE 0
#define RED 1
#define ORANGE 2
#define BLACK 3
#define GREEN 4
using namespace std;
int main()
{
int result, state = 0, wireColor = 6;
/* Matrix to represent Finite Stae Machine
Rows = State
Columns = Wire Colors (White, Red, Orange, Black, Green)
-1 = wrong choice
6 = defused
0-5 = Next State*/
int fsm[6][5] = { 1, 2,-1,-1,-1,
2,-1,3,-1,-1,
-1,0,-1,3,-1,
-1,-1,4,3,5,
-1,-1,-1,-1,6,
-1,-1,6,-1,-1 };
string color;
ifstream infile("SampleInput2.txt"); // edit file to read input from as needed
while (infile >> color)
{
/* get wire color from input */
if (!color.compare("white"))
wireColor = WHITE;
else if (!color.compare("red"))
wireColor = RED;
else if (!color.compare("orange"))
wireColor = ORANGE;
else if (!color.compare("black"))
wireColor = BLACK;
else if (!color.compare("green"))
wireColor = GREEN;
else
{
cout << "Invalid wire color detected. Exiting." << endl;
return 0;
}
/* obtain state after wire cut */
result = fsm[state][wireColor];
/* interpret result of wire cut */
switch (result) {
case 6:
cout << "Bomb defused!" << endl;
getchar();
return 0;
break;
case -1:
cout << "BOOM!" << endl;
getchar();
return 0;
break;
default:
state = result;
cout << "ok" << endl;
break;
}
}
cout << "BOOM!" << endl;
getchar();
return 0;
}