-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.cs
104 lines (97 loc) · 3.3 KB
/
Factory.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
using System;
namespace Binoxxo_Solver
{
class Factory
{
public static Binoxxo CreateBinoxxo()
{
string input = "";
char[] line = new char[0];
int?[] init = new int?[0];
int gameSize = -1;
int fullSize = -1;
while (gameSize % 2 != 0)
{
if (gameSize < 0)
{
Console.WriteLine("Reading Binoxxo from user input");
} else
{
Console.WriteLine("Invalid length of line");
}
Console.Write("Line 1: ");
input = Console.ReadLine();
line = input.ToCharArray();
init = new int?[(int)Math.Pow(line.Length, 2)];
gameSize = line.Length;
fullSize = init.Length;
}
bool first = true;
for (int i = 0; i < fullSize; i += gameSize)
{
try
{
if (first)
{
first = false;
}
else
{
Console.Write("Line {0}: ", i / gameSize + 1);
input = Console.ReadLine();
line = input.ToCharArray();
}
if (input.Length == gameSize)
{
for (int j = 0; j < gameSize; j++)
{
string str = line[j].ToString();
int? value;
if (str.Equals(" "))
{
value = null;
}
else if (str.Equals("O") || str.Equals("o") || str.Equals("0"))
{
value = 0;
}
else if (str.Equals("X") || str.Equals("x") || str.Equals("1"))
{
value = 1;
}
else
{
throw new ArgumentException("Input invalid");
}
init[i + j] = value;
}
} else if (input.Length == 0)
{
for (int j = 0; j < gameSize; j++)
{
init[i + j] = null;
}
}
else
{
throw new IndexOutOfRangeException($"Input does not contain {gameSize} characters");
}
}
catch (Exception e)
{
if (e is IndexOutOfRangeException || e is ArgumentException)
{
Console.WriteLine(e.Message);
i -= gameSize;
}
else
{
throw;
}
}
}
Console.WriteLine("");
return new Binoxxo(init);
}
}
}