-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cs
106 lines (104 loc) · 2.85 KB
/
Tree.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
using System.Collections.Generic;
namespace CheckersSolver
{
public class Tree
{
public Node Origin { get; private set; }
public Tree(bool whiteGo)
{
Origin = new Node(new MoveInfo(), whiteGo, null);
}
public bool WhiteCertainlyWin() {
return WhiteCertainlyWin(Origin);
}
bool WhiteCertainlyWin(Node CurrentNode)
{
if (CurrentNode.Children.Count == 0)
{
return (bool)CurrentNode.whiteHaveWon;
}
else if (CurrentNode.whiteGo)
{
bool result = false;
foreach (Node node in CurrentNode.Children)
{
result = result || WhiteCertainlyWin(node);
}
return result;
}
else
{
bool result = true;
foreach (Node node in CurrentNode.Children)
{
result = result && WhiteCertainlyWin(node);
}
return result;
}
}
}
public class Node
{
public List<Node> Children = new List<Node>();
public Node parent { get; private set; }
public bool? whiteHaveWon;
/*{
get
{
if (Children.Count == 0)
{
return whiteHaveWon;
}
else
{
throw new Exception("Attempted to get bool value of a non-leaf node.");
}
}
set
{
if (Children.Count == 0)
{
whiteHaveWon = value;
}
else
{
throw new Exception("Attempted to set bool value of a non-leaf node.");
}
}
}*/
public MoveInfo move { get; private set; }
public bool whiteGo { get; private set; }
public Node(MoveInfo move, bool whiteGo, Node parent, bool? whiteHaveWon = null)
{
this.move = move;
this.whiteHaveWon = whiteHaveWon;
this.whiteGo = whiteGo;
this.parent = parent;
}
public Node AddChild(MoveInfo move ,bool? whiteHaveWon = null)
{
Node childNode = new Node(move,!whiteGo, this, whiteHaveWon);
Children.Add(childNode);
return childNode;
}
}
public struct MoveInfo
{
int X;
int Y;
MoveType moveType;
bool left;
public MoveInfo(int X, int Y, MoveType moveType, bool left)
{
this.X = X;
this.Y = Y;
this.moveType = moveType;
this.left = left;
}
}
public enum MoveType
{
MOVE,
CAPTURE
}
}