/
Side.cs
63 lines (54 loc) · 1.79 KB
/
Side.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using ChineseChessLib.Pieces;
namespace ChineseChessLib
{
class Side
{
public SideType sideType;
Piece[] pieces;
Jiang jiang;
private Game game;
public Side Opponent;
public Side(Game game)
{
this.game = game;
// Initialize pieces
this.pieces = (from piece in this.game.Pieces where piece.Side == this select piece).ToArray();
Debug.Assert(pieces.Length == 16);
this.jiang = (Jiang)(from p in this.pieces where p.GetType() == jiang.GetType() select p).First();
// Initialize Oppopent
int i = Array.IndexOf(this.game.Sides, this);
this.Opponent = this.game.Sides[Math.Abs(1 - i)]; // this = 1 then opponent = 0, and vice versa
}
public bool PointInAttackRange(Point point)
{
foreach (var piece in this.pieces)
{
if (!piece.IsAlive) continue;
if (piece.PointInAttackRange(point))
return true;
}
return false;
}
public void Move(Piece piece, Point targetLocation)
{
this.game.Board.Move(piece, targetLocation);
}
/// <summary>
/// Transform the position specified as if the player is bottom to the absolute position
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public Point AbsolutePosition(Point p)
{
if (this.sideType == SideType.Bottom) return p;
int x = Board.NumRows - p.X - 1;
int y = Board.NumCols - p.Y - 1;
return new Point(x, y);
}
}
}