-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_states.py
More file actions
135 lines (114 loc) · 4.72 KB
/
Copy pathcreate_states.py
File metadata and controls
135 lines (114 loc) · 4.72 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
LEFT = False
Right = True
class State:
# Attributes of a state, initialized to default values
def __init__(self, state_name, write_0, direction_0, state_0, write_1, direction_1, state_1):
self.name = state_name
self.write_after_0 = write_0
self.write_after_1 = write_1
self.direction_after_0 = direction_0
self.direction_after_1 = direction_1
self.next_state_after_0 = state_0
self.next_state_after_1 = state_1
self.next_state_index_0 = ""
self.next_state_index_1 = ""
# Outputs the attributes of the state as a string.
def __str__(self) -> str:
return self.write_after_0 + " " + self.direction_after_0 + " " + \
self.next_state_index_0 + " " + self.write_after_1 + " " + \
self.direction_after_1 + " " + self.next_state_index_1
# Asks user input and categorizes it into one of two given options.
def ask_input(option0, option1) -> str:
response = input().lower()
while response != option0 and response != option1:
print("Please type %s or %s"%(option0, option1))
response = input().lower()
if response == option0:
return "0"
return "1"
# If a new state's name has been used already, remove the other
# state with that name.
def remove_repeats(states):
new_state_name = states[len(states) - 1].name
for i in range(len(states) - 1):
if states[i].name == new_state_name:
states.pop(i)
break
# Repeatedly asks user questions whose answers create new states.
def submit_states(states):
print("Type the name of the state, or press enter to use existing states in states.txt:")
state_name = input().lower()
while state_name == "halt":
print("No need to include HALT state. Please type a different state or press enter:")
state_name = input().lower()
while (state_name != ""):
print("\nUpon reading 0...")
print("Write (0 or 1):")
write_0 = ask_input("0", "1")
print("Move (left or right):")
direction_0 = ask_input("left", "right")
print("Go to next state (by name, or HALT):")
state_0 = input().lower()
while state_0 == "":
print("Please include a state:")
state_0 = input().lower()
print("\nUpon reading 1...")
print("Write (0 or 1):")
write_1 = ask_input("0", "1")
print("Move (left or right):")
direction_1 = ask_input("left", "right")
print("Go to next state (by name, or HALT):")
state_1 = input().lower()
while state_1 == "":
print("Please include a state:")
state_1 = input().lower()
states.append(State(state_name, write_0, direction_0, state_0, write_1, direction_1, state_1))
print("\nType the name of the state, or press enter to continue to the next step:")
state_name = input().lower()
while state_name == "halt":
print("No need to include HALT state. Please type a different state or press enter:")
state_name = input().lower()
remove_repeats(states)
# Converts states to index numbers (as strings), with "-1"
# referring to the halt state.
def interpret_names(states) -> bool:
for s in states:
if s.next_state_after_0 == "halt":
s.next_state_index_0 = str(-1)
else:
for i in range(0, len(states)):
if states[i].name == s.next_state_after_0:
s.next_state_index_0 = str(i)
break
if s.next_state_index_0 == "":
print("There is a state mentioned that was not submitted")
return False
if s.next_state_after_1 == "halt":
s.next_state_index_1 = str(-1)
else:
for i in range(0, len(states)):
if states[i].name == s.next_state_after_1:
s.next_state_index_1 = str(i)
break
if s.next_state_index_1 == "":
print("There is a state mentioned that was not submitted")
return False
return True
# Creates a string containing the list of states.
def make_str(states):
result = str(states[0])
for i in range(1, len(states)):
result += "," + str(states[i])
return result
# Calls functions that request user input to create all states.
# Then saves the list of states into states.txt.
def main():
states = []
submit_states(states)
if len(states) > 0 and interpret_names(states):
file = open("states.txt", "w")
file.write(make_str(states))
file.close()
# Call main() if this file is run directly.
if __name__ == "__main__":
main()