-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
147 lines (114 loc) · 3.59 KB
/
main.cs
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
using System;
using std = System.Console;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
namespace a1{
public static class GlobalVar {
public static readonly int[] GOAL = new int[] {1,2,3,4,5,6,7,8,0};
}
public class program{
public static void Main(string[] args){
std.WriteLine("Enter path for puzzle file");
var user_input = std.ReadLine();
int[] output;
if(File.Exists(user_input)){
var lines = ReadFile(user_input);
if(lines == null) return;
RunMultiplePuzzles(lines);
}else{
std.WriteLine("File does not appear to exist, trying to parse input");
output = ParseInput(user_input);
if(output == null)return;
var algo = AlgorithmFactory(WriteOutputForAlgorithm(), output );
if(algo == null)return;
var p = new Puzzle(algo);
p.Begin();
Console.Read();
}
}
///<summary>
///If the user selects to read a file, the program can be ran for each entry in the file
///this method handles such a scenario
///</summary>
public static void RunMultiplePuzzles(List<int[]> InitialStates){
string algo_input = WriteOutputForAlgorithm();
if(algo_input == null)return;
foreach(var item in InitialStates){
var algo = AlgorithmFactory(algo_input,item);
var p = new Puzzle(algo);
p.Begin();
}
}
///<summary>
///converts a string representation of a puzzle's initial state into an array
///</summary>
public static int[] ParseInput(string input){
int[] parsed_values = new int[9];
var entries = input.Split(default(string[]),StringSplitOptions.RemoveEmptyEntries);
if(entries.Length < 8){
std.WriteLine("Length was incorrect {0}", entries.Length);
return null;
}
for(int i = 0; i < 9; i++){
var entry = entries[i];
int temp;
int.TryParse(entry, out temp);
parsed_values[i] = temp;
}
return parsed_values;
}
///<summary>
///Creates an algorithm based on user's input, passes null if invalid which is handled
///in the calling function by simply returning.
///</summary>
public static Algorithm AlgorithmFactory(string input, int[] arr){
switch(input){
case "1":
return new BreadthFirst(arr);
case "2":
return new DepthFirstSearch(arr);
case "3":
return new DepthFirstLimited(arr);
case "4":
return new IterativeDepth(arr);
case "5":
return new BiDirectional(arr);
case "6":
return new astar(arr, true);
case "7":
return new astar(arr, false);
case "8":
return new greedy(arr);
}
std.WriteLine("Invalid Choice, input was: {0}", input);
return null;
}
public static string WriteOutputForAlgorithm(){
//since we have multiple choices for algorithms, we have to check the user input for their choice
std.WriteLine("Pick a search algorithm:");
std.WriteLine("1. Breadth first");
std.WriteLine("2. Depth first");
std.WriteLine("3. Depth-limited");
std.WriteLine("4. Iterative-deepening");
std.WriteLine("5. Bi-directional");
std.WriteLine("6. A* using manhattan distances");
std.WriteLine("7. A* using hamming distances");
std.WriteLine("8. Greedy");
return std.ReadLine();
}
///<summary>
///Reads each line of a file and creates a collection of the initial states.
///</summary>
public static List<int[]> ReadFile(string Path){
var rv = new List<int[]>();
string[] lines = System.IO.File.ReadAllLines(Path);
foreach(var item in lines){
var output = ParseInput(item);
if(output == null) return null;
rv.Add(output);
}
return rv;
}
}
}