This repository was archived by the owner on Aug 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeafBoard.cs
141 lines (126 loc) · 4.74 KB
/
LeafBoard.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab3
{
public class LeafBoard : ComponentBoard
{
public Player[] Grid;
public LeafBoard()
{
Grid = new Player[9] { Player.None, Player.None, Player.None, Player.None, Player.None, Player.None, Player.None, Player.None, Player.None };
WonBy = Player.None;
winList = new List<Coordinate>();
PlayerXMoves = new List<Coordinate>();
PlayerOMoves = new List<Coordinate>();
}
/// <summary>
/// Changes Player.None to provided player in grid element according to coordinate.
/// </summary>
/// <param name="player">Player enum</param>
/// <param name="coordinate">LeafBoard coordinate</param>
/// <returns>If successful</returns>
public override bool MakeMove(Player player, Coordinate coordinate)
{
if (this.WonBy == Player.None && Grid[(int)coordinate] == Player.None)
{
Grid[(int)coordinate] = player;
switch (player)
{
case Player.X:
PlayerXMoves.Add(coordinate);
break;
case Player.O:
PlayerOMoves.Add(coordinate);
break;
}
return true;
}
return false;
}
public override bool MakeMove(Player player, Coordinate largeCoordinate, Coordinate smallCoordinate)
{
Console.WriteLine("Leaf cannot handle multiple coordinates!");
throw new NotImplementedException();
}
/// <summary>
/// Returns whether 3 "Player"-attributes in the Grid collection match each other if so add those coordinates to winList from PlayerMoves.
/// </summary>
/// <param name="coord1">First Coordinate</param>
/// <param name="coord2">Second Coordinate</param>
/// <param name="coord3">Third Coordinate</param>
/// <returns>If coordinates matched</returns>
public override bool TripleCoordinateMatch(Coordinate coord1, Coordinate coord2, Coordinate coord3)
{
if (Grid[(int)coord1] == Grid[(int)coord2] && Grid[(int)coord2] == Grid[(int)coord3] && Grid[(int)coord1] != Player.None)
{
WonBy = Grid[(int)coord1];
switch (WonBy)
{
case Player.X:
winList = PlayerXMoves.Where(x => x == coord1 || x == coord2 || x == coord3).ToList();
break;
case Player.O:
winList = PlayerOMoves.Where(x => x == coord1 || x == coord2 || x == coord3).ToList();
break;
}
return true;
}
return false;
}
public override bool CheckPatternWin()
{
// Horizontal 1 - 0 1 2
if (TripleCoordinateMatch(Coordinate.NW, Coordinate.NC, Coordinate.NE))
{
return true;
}
// Horizontal 2 - 3 4 5
else if (TripleCoordinateMatch(Coordinate.CW, Coordinate.CC, Coordinate.CE))
{
return true;
}
// Horizontal 3 - 6 7 8
else if (TripleCoordinateMatch(Coordinate.SW, Coordinate.SC, Coordinate.SE))
{
return true;
}
// Vertical 1 - 0 3 6
else if (TripleCoordinateMatch(Coordinate.NW, Coordinate.CW, Coordinate.SW))
{
return true;
}
// Vertical 2 - 1 4 7
else if (TripleCoordinateMatch(Coordinate.NC, Coordinate.CC, Coordinate.SC))
{
return true;
}
// Vertical 3 - 2 5 8
else if (TripleCoordinateMatch(Coordinate.NE, Coordinate.CE, Coordinate.SE))
{
return true;
}
// Diagonal 1 - 0 4 8
else if (TripleCoordinateMatch(Coordinate.NW, Coordinate.CC, Coordinate.SE))
{
return true;
}
// Diagonal 2 - 2 4 6
else if (TripleCoordinateMatch(Coordinate.NE, Coordinate.CC, Coordinate.SW))
{
return true;
}
return false;
}
public override bool CheckWin()
{
return CheckPatternWin();
}
public override void Add(ComponentBoard componentboard)
{
Console.WriteLine("Leaf nodes can't add components!");
throw new NotImplementedException();
}
}
}