This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessMove.cs
81 lines (69 loc) · 2.18 KB
/
ChessMove.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChessGame
{
internal class ChessMove
{
public ChessPosition? startposition { get; set; }
public ChessPosition? endposition { get; set; }
public static bool TryParse(string s, out ChessMove move)
{
move = new ChessMove();
s = s.Trim().ToLower();
if(s.Length == 5 && s[2] == '-')
{
String[] position = s.Split('-');
for(int i = 0; i < position.Length; i++)
{
bool compareL = CheckLetters(position[i]);
bool compareN = CheckNumbers(position[i]);
if(compareL==false || compareN == false)
{
move = null;
return false;
}
}
ChessPosition chessPosition = new ChessPosition();
if (ChessPosition.TryParse(position[0], out var startPosition) && ChessPosition.TryParse(position[1], out var endPosition))
{
move.startposition = startPosition;
move.endposition = endPosition;
return true;
}
else { move = null; return false; }
}
else
{
move = null;
return false;
}
}
public static bool CheckLetters(String s)
{
string lettersToCheck = "abcdefgh";
foreach (char letter in lettersToCheck)
{
if (s.Contains(letter))
{
return true;
}
}
return false;
}
public static bool CheckNumbers(String s)
{
string lettersToCheck = "12345678";
foreach (char letter in lettersToCheck)
{
if (s.Contains(letter))
{
return true;
}
}
return false;
}
}
}