-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
228 lines (181 loc) · 6.31 KB
/
Main.java
File metadata and controls
228 lines (181 loc) · 6.31 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.util.Scanner;
public class Main{
public UTM tm;
public Main(){
char[] options = menu();
String response;
Scanner input;
//Switch on the selected option from the menu
switch(options[0]){
case '1': //Counter
/*
UTM Counting: count in binary.
int bits = number of bit places or power of 2 count to. i.e bits=4 counts to 1111, or 15
*/
int bits = 0;
System.out.println("|");
System.out.println("|Please enter the number of bits to use (i.e. bits=5 counts to 2^5 - 1)");
System.out.print("|(For effectively 'non-terminating' machine, enter large integer):");
input = new Scanner(System.in);
response = input.next();
while(!response.matches("^[1-9][0-9]{0,2}")){
System.out.print("|Must be an integer or entered integer is too large. Try again:");
input = new Scanner(System.in);
response = input.next();
}
bits = Integer.parseInt(response);
tm = new UTM(UTM.OPS.COUNTER, bits, options[1]);
break;
case '2': //Subtraction
/*
UTM subtraction: Subtracts N from M, i.e M-N. If N>M, 0 is returned.
*/
System.out.println("|Difference = M-N where M > N");
System.out.print("|Please enter M: ");
input = new Scanner(System.in);
response = input.next();
while(!response.matches("^[1-9][0-9]{0,}")){
System.out.print("|M must be an integer. Try again:");
input = new Scanner(System.in);
response = input.next();
}
int M = Integer.parseInt(response);
System.out.println("|");
System.out.print("|Please enter N: ");
input = new Scanner(System.in);
response = input.next();
while(!response.matches("^[1-9][0-9]{0,}")){
System.out.print("|N must be an integer. Try again:");
input = new Scanner(System.in);
response = input.next();
}
int N = Integer.parseInt(response);
tm = new UTM(UTM.OPS.SUBTRACTION, options[1],M, N);
//UTMsubtract(M,N, options[1]);
break;
case '3': //BB3
UTMbusybeaver(UTM.OPS.BUSYBEAVER3, options[1]);
break;
case '4': //BB4
UTMbusybeaver(UTM.OPS.BUSYBEAVER4, options[1]);
break;
case '5':
tm = new UTM(UTM.OPS.BUSYBEAVER5, options[1]);
break;
case '6':
System.out.println("|Addition = M+N");
System.out.print("|Please enter M: ");
input = new Scanner(System.in);
response = input.next();
while(!response.matches("^[1-9][0-9]{0,}")){
System.out.print("|M must be an integer. Try again:");
input = new Scanner(System.in);
response = input.next();
}
int A = Integer.parseInt(response);
System.out.println("|");
System.out.print("|Please enter N: ");
input = new Scanner(System.in);
response = input.next();
while(!response.matches("^[1-9][0-9]{0,}")){
System.out.print("|N must be an integer. Try again:");
input = new Scanner(System.in);
response = input.next();
}
int B = Integer.parseInt(response);
tm = new UTM(UTM.OPS.ADDITION, options[1],A, B);
break;
case '7':
System.out.println("|Multiplication = M*N");
System.out.print("|Please enter M: ");
input = new Scanner(System.in);
response = input.next();
while(!response.matches("^[1-9][0-9]{0,}")){
System.out.print("|M must be an integer. Try again:");
input = new Scanner(System.in);
response = input.next();
}
int C = Integer.parseInt(response);
System.out.println("|");
System.out.print("|Please enter N: ");
input = new Scanner(System.in);
response = input.next();
while(!response.matches("^[1-9][0-9]{0,}")){
System.out.print("|N must be an integer. Try again:");
input = new Scanner(System.in);
response = input.next();
}
int D = Integer.parseInt(response);
tm = new UTM(UTM.OPS.MULTIPLICATION, options[1],C, D);
break;
case '8':
tm = new UTM(UTM.OPS.NTCOUNTER, 1, options[1]);
break;
}//end: switch
System.out.print("\n||>>Program finished execution. Run another? (y/n):");
input = new Scanner(System.in);
response = input.next();
//input validation
while(response.length() > 1 || (response.charAt(0)!= 'y' && response.charAt(0)!='n')){
System.out.print("|Invalid option. Try again:");
input = new Scanner(System.in);
response = input.next();
}
if(response.charAt(0) == 'y')
new Main();
else{
System.out.println("||Thank you for using the Universal Turing Machine. Exiting.");
System.exit(0);
}
}//end: Main()
public void UTMbusybeaver(UTM.OPS bbType, char verbose){
tm = new UTM(bbType, verbose);
}
public char[] menu(){
char[] returnVals = {'0', '0'};
String response = "";
System.out.println("\n## Michael Toth - CS125 - Universal Turing Machine\n");
System.out.println("||Select a Program to run:");
System.out.println("|1: Counter");
System.out.println("|2: Subtraction");
System.out.println("|3: Busy Beaver 3");
System.out.println("|4: Busy Beaver 4");
System.out.println("|5: Busy Beaver 5 (Warning, will print 47 million lines unless verbose=n)");
System.out.println("|6: Addition");
System.out.println("|7: Multiplication");
System.out.println("|8: Non-terminating Counter (Looks cooler with verbose = n)");
System.out.println("|0: Exit");
System.out.println("|");
System.out.print("|Please Select an option: ");
Scanner input = new Scanner(System.in);
response = input.next();
//input validation
while(response.length() > 1 || response.charAt(0) > '8' || response.charAt(0) < '0'){
System.out.print("|Invalid option. Try again:");
input = new Scanner(System.in);
response = input.next();
}
returnVals[0] = response.charAt(0);
if(returnVals[0] != '0'){
System.out.println("|");
System.out.print("|Would you like to turn verbose mode on (displays every transition step)? (y/n): ");
input = new Scanner(System.in);
response = input.next();
//input validation
while(response.length() > 1 || (response.charAt(0)!= 'y' && response.charAt(0)!='n')){
System.out.print("|Invalid option. Try again:");
input = new Scanner(System.in);
response = input.next();
}
returnVals[1] = response.charAt(0);
}else{
System.out.println("|");
System.out.println("||Thank you for using the Universal Turing Machine. Exiting.");
System.exit(0);
}
return returnVals;
}
public static void main(String[] args){
new Main();
}//: main()
}//end: Main