/
Logic.cs
92 lines (76 loc) · 2.31 KB
/
Logic.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
using System;
using System.Collections.Generic;
using System.Linq;
using DeltaEngine.Datatypes;
namespace Drench.Logics
{
public abstract class Logic
{
protected Logic(int width, int height, Vector2D[] homeSquares)
{
Board = new Board(width, height);
this.homeSquares = homeSquares;
availableColorFinder = new AvailableColorFinder(Board, homeSquares);
turns = new int[homeSquares.Length];
}
internal Board Board { get; set; }
internal readonly AvailableColorFinder availableColorFinder;
protected readonly Vector2D[] homeSquares;
protected readonly int[] turns;
protected Logic(Board.Data boardData, Vector2D[] homeSquares)
{
Board = new Board(boardData);
this.homeSquares = homeSquares;
availableColorFinder = new AvailableColorFinder(Board, homeSquares);
turns = new int[homeSquares.Length];
}
public void Reset()
{
Board.Randomize();
}
public abstract void Pass();
public abstract void MakeMove(Color color);
public class CannotMakeMoveWhenGameIsOver : Exception {}
protected void CheckForGameOver()
{
for (int i = 0; i < homeSquares.Length; i++)
if (HasPlayerAnyValidMoves(i))
return;
GameOver();
}
protected void GameOver()
{
IsGameOver = true;
if (GameFinished != null)
GameFinished();
}
public bool IsGameOver { get; private set; }
public event Action GameFinished;
public int GetPlayerScore(int player)
{
return Board.GetConnectedColorsCount(homeSquares[player]);
}
public int ActivePlayer { get; protected set; }
public bool LastPlayerPassed { get; protected set; }
internal bool HasPlayerAnyValidMoves(int player)
{
return GetPlayerValidMoves(player).Any();
}
internal List<Color> GetPlayerValidMoves(int player)
{
List<Color> availableColors = availableColorFinder.GetAvailableColors();
return availableColors.Where(color => IsValidMove(player, color)).ToList();
}
private bool IsValidMove(int player, Color color)
{
Board clone = Board.Clone();
clone.SetColor(homeSquares[player], color);
return clone.GetConnectedColorsCount(homeSquares[player]) >
Board.GetConnectedColorsCount(homeSquares[player]);
}
public int GetPlayerTurnsTaken(int player)
{
return turns[player];
}
}
}