forked from OdedRadi/CheckersGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Move.cs
94 lines (79 loc) · 2.31 KB
/
Move.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
using System;
using System.Text;
namespace CheckersGame
{
public struct Move
{
public enum eMoveResult
{
Skipping,
Moving,
Invalid
}
private Coordinate m_FromCoordinate;
private Coordinate m_ToCoordinate;
public Move(Coordinate i_FromCoordinate, Coordinate i_ToCoordinate)
{
m_FromCoordinate = i_FromCoordinate;
m_ToCoordinate = i_ToCoordinate;
}
public static bool TryParse(string i_InputString, out Move o_Move)
{
bool validInput = false;
o_Move = new Move();
if (i_InputString.Contains(">"))
{
string[] inputSplitedStrings = i_InputString.Split('>');
if (inputSplitedStrings.Length == 2)
{
string fromCoordianteString = inputSplitedStrings[0];
string toCoordianteString = inputSplitedStrings[1];
if (fromCoordianteString.Length == 2 && toCoordianteString.Length == 2)
{
char fromColCharatcter = fromCoordianteString[0];
char fromRowCharatcter = fromCoordianteString[1];
char toColCharatcter = toCoordianteString[0];
char toRowCharatcter = toCoordianteString[1];
if (char.IsUpper(fromColCharatcter) &&
char.IsLower(fromRowCharatcter) &&
char.IsUpper(toColCharatcter) &&
char.IsLower(toRowCharatcter))
{
int fromColNum = fromColCharatcter - 'A';
int fromRowNum = fromRowCharatcter - 'a';
int toColNum = toColCharatcter - 'A';
int toRowNum = toRowCharatcter - 'a';
validInput = true;
o_Move = new Move(new Coordinate(fromRowNum, fromColNum), new Coordinate(toRowNum, toColNum));
}
}
}
}
return validInput;
}
public Coordinate FromCoordinate
{
get
{
return m_FromCoordinate;
}
}
public Coordinate ToCoordinate
{
get
{
return m_ToCoordinate;
}
}
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(char.ConvertFromUtf32(FromCoordinate.Column + 'A'));
stringBuilder.Append(char.ConvertFromUtf32(FromCoordinate.Row + 'a'));
stringBuilder.Append('>');
stringBuilder.Append(char.ConvertFromUtf32(ToCoordinate.Column + 'A'));
stringBuilder.Append(char.ConvertFromUtf32(ToCoordinate.Row + 'a'));
return stringBuilder.ToString();
}
}
}