/*public void OnDrawGizmos()
        {
            if (squareGrid == null) return;
            for (var x = 0; x < squareGrid.Squares.GetLength(0); x++)
            {
                for (var y = 0; y < squareGrid.Squares.GetLength(1); y++)
                {
                    Gizmos.color = (squareGrid.Squares[x, y].TopLeft.Active) ? Color.black : Color.white;
                    Gizmos.DrawCube(squareGrid.Squares[x, y].TopLeft.Position, Vector3.one * 0.4f);

                    Gizmos.color = (squareGrid.Squares[x, y].TopRight.Active) ? Color.black : Color.white;
                    Gizmos.DrawCube(squareGrid.Squares[x, y].TopRight.Position, Vector3.one * 0.4f);

                    Gizmos.color = (squareGrid.Squares[x, y].BottomLeft.Active) ? Color.black : Color.white;
                    Gizmos.DrawCube(squareGrid.Squares[x, y].BottomLeft.Position, Vector3.one * 0.4f);

                    Gizmos.color = (squareGrid.Squares[x, y].BottomRight.Active) ? Color.black : Color.white;
                    Gizmos.DrawCube(squareGrid.Squares[x, y].BottomRight.Position, Vector3.one * 0.4f);

                    Gizmos.color = Color.gray;
                    Gizmos.DrawCube(squareGrid.Squares[x, y].CenterTop.Position, Vector3.one * 0.15f);
                    Gizmos.DrawCube(squareGrid.Squares[x, y].CenterRight.Position, Vector3.one * 0.15f);
                    Gizmos.DrawCube(squareGrid.Squares[x, y].CenterBottom.Position, Vector3.one * 0.15f);
                    Gizmos.DrawCube(squareGrid.Squares[x, y].CenterLeft.Position, Vector3.one * 0.15f);
                }
            }
        }*/
        void TriangulateSquare(Square square)
        {
            switch (square.Configuration)
            {
                case 0:
                    break;

                // 1 points:
                case 1:
                    MeshFromPoints(square.CenterLeft, square.CenterBottom, square.BottomLeft);
                    break;
                case 2:
                    MeshFromPoints(square.BottomRight, square.CenterBottom, square.CenterRight);
                    break;
                case 4:
                    MeshFromPoints(square.TopRight, square.CenterRight, square.CenterTop);
                    break;
                case 8:
                    MeshFromPoints(square.TopLeft, square.CenterTop, square.CenterLeft);
                    break;

                // 2 points:
                case 3:
                    MeshFromPoints(square.CenterRight, square.BottomRight, square.BottomLeft, square.CenterLeft);
                    break;
                case 6:
                    MeshFromPoints(square.CenterTop, square.TopRight, square.BottomRight, square.CenterBottom);
                    break;
                case 9:
                    MeshFromPoints(square.TopLeft, square.CenterTop, square.CenterBottom, square.BottomLeft);
                    break;
                case 12:
                    MeshFromPoints(square.TopLeft, square.TopRight, square.CenterRight, square.CenterLeft);
                    break;
                case 5:
                    MeshFromPoints(square.CenterTop, square.TopRight, square.CenterRight, square.CenterBottom, square.BottomLeft, square.CenterLeft);
                    break;
                case 10:
                    MeshFromPoints(square.TopLeft, square.CenterTop, square.CenterRight, square.BottomRight, square.CenterBottom, square.CenterLeft);
                    break;

                // 3 point:
                case 7:
                    MeshFromPoints(square.CenterTop, square.TopRight, square.BottomRight, square.BottomLeft, square.CenterLeft);
                    break;
                case 11:
                    MeshFromPoints(square.TopLeft, square.CenterTop, square.CenterRight, square.BottomRight, square.BottomLeft);
                    break;
                case 13:
                    MeshFromPoints(square.TopLeft, square.TopRight, square.CenterRight, square.CenterBottom, square.BottomLeft);
                    break;
                case 14:
                    MeshFromPoints(square.TopLeft, square.TopRight, square.BottomRight, square.CenterBottom, square.CenterLeft);
                    break;

                // 4 point:
                case 15:
                    MeshFromPoints(square.TopLeft, square.TopRight, square.BottomRight, square.BottomLeft);
                    checkedVertices.Add(square.TopLeft.VertexIndex);
                    checkedVertices.Add(square.TopRight.VertexIndex);
                    checkedVertices.Add(square.BottomRight.VertexIndex);
                    checkedVertices.Add(square.BottomLeft.VertexIndex);
                    break;
            }
        }
        public SquareGrid(int[,] map, float squareSize)
        {
            var nodeCountX = map.GetLength(0);
            var nodeCountY = map.GetLength(1);
            var mapWidth = nodeCountX * squareSize;
            var mapHeight = nodeCountY * squareSize;

            var controlNodes = new ControlNode[nodeCountX, nodeCountY];
            for (var x = 0; x < nodeCountX; x++)
            {
                for (var y = 0; y < nodeCountY; y++)
                {
                    var pos = new Vector3(-mapWidth / 2 + x * squareSize + squareSize / 2, 0,
                        -mapHeight / 2 + y * squareSize + squareSize / 2);
                    controlNodes[x, y] = new ControlNode(pos, map[x, y] == 1, squareSize);
                }
            }
            Squares = new Square[nodeCountX - 1, nodeCountY - 1];
            for (var x = 0; x < nodeCountX - 1; x++)
            {
                for (var y = 0; y < nodeCountY - 1; y++)
                {
                    Squares[x, y] = new Square(controlNodes[x, y + 1], controlNodes[x + 1, y + 1],
                        controlNodes[x, y], controlNodes[x + 1, y]);
                }

            }
        }
Example #3
0
 public IMove GetMove(Square start, Square landing)
 {
     return(PossibleMoveList.FirstOrDefault(x => x.StartSquare.Equals(start) && x.LandingSquare.Equals(landing)));
 }
Example #4
0
 public Move(Square start, Square end)
 {
     StartSquare   = start;
     LandingSquare = end;
 }
Example #5
0
        private void GenerateMoveList()
        {
            HashSet <IMove> resp = new HashSet <IMove>();

            for (int i = 0; i < 64; i++)
            {
                HashSet <Square> landingPositions = new HashSet <Square>();
                Chessman         chessman         = Board[i];
                if (chessman == null || chessman.Color != CurrentPlayer)
                {
                    continue;
                }

                // Start calculate possible landing position, based on move patterns and pieces on board
                int[] currentPosition = { i / 8 + 1, i % 8 + 1 };

                if (chessman.Type == PieceType.Rook || chessman.Type == PieceType.Queen)
                {
                    int[][] movePatterns = { new[] { 0, 1 }, new[] { 0, -1 }, new[] { 1, 0 }, new[] { -1, 0 } };

                    for (int j = 0; j < 4; j++)
                    {
                        var   mvPtrn            = movePatterns[j];
                        int[] evaluatedPosition = { 0, 0 };
                        evaluatedPosition[0] = currentPosition[0];
                        evaluatedPosition[1] = currentPosition[1];
                        while (true)
                        {
                            evaluatedPosition[0] = evaluatedPosition[0] + mvPtrn[0];
                            evaluatedPosition[1] = evaluatedPosition[1] + mvPtrn[1];
                            // out of board
                            if (!IsValidPosition(evaluatedPosition))
                            {
                                break;
                            }
                            var s        = new Square(evaluatedPosition[0], evaluatedPosition[1]);
                            var occupant = this[s];
                            if (occupant == null ||
                                occupant.Color != CurrentPlayer && occupant.Type != PieceType.King)
                            {
                                landingPositions.Add(s);
                            }
                            if (occupant != null)
                            {
                                break;
                            }
                        }
                    }
                }
                if (chessman.Type == PieceType.Bishop || chessman.Type == PieceType.Queen)
                {
                    int[][] movePatterns = { new[] { 1, 1 }, new[] { -1, -1 }, new[] { 1, -1 }, new[] { -1, 1 } };

                    for (int j = 0; j < 4; j++)
                    {
                        var   mvPtrn            = movePatterns[j];
                        int[] evaluatedPosition = { 0, 0 };
                        evaluatedPosition[0] = currentPosition[0];
                        evaluatedPosition[1] = currentPosition[1];
                        while (true)
                        {
                            evaluatedPosition[0] = evaluatedPosition[0] + mvPtrn[0];
                            evaluatedPosition[1] = evaluatedPosition[1] + mvPtrn[1];
                            // out of board
                            if (!IsValidPosition(evaluatedPosition))
                            {
                                break;
                            }
                            var s        = new Square(evaluatedPosition[0], evaluatedPosition[1]);
                            var occupant = this[s];
                            if (occupant == null ||
                                occupant.Color != CurrentPlayer && occupant.Type != PieceType.King)
                            {
                                landingPositions.Add(s);
                            }
                            if (occupant != null)
                            {
                                break;
                            }
                        }
                    }
                }
                if (chessman.Type == PieceType.Knight)
                {
                    int[][] movePatterns =
                    {
                        new[] {  2, 1 }, new[] {  2, -1 },
                        new[] { -2, 1 }, new[] { -2, -1 },
                        new[] {  1, 2 }, new[] {  1, -2 },
                        new[] { -1, 2 }, new[] { -1, -2 }
                    };

                    for (int j = 0; j < 8; j++)
                    {
                        var   mvPtrn            = movePatterns[j];
                        int[] evaluatedPosition = { 0, 0 };
                        evaluatedPosition[0] = currentPosition[0];
                        evaluatedPosition[1] = currentPosition[1];
                        evaluatedPosition[0] = evaluatedPosition[0] + mvPtrn[0];
                        evaluatedPosition[1] = evaluatedPosition[1] + mvPtrn[1];
                        if (!IsValidPosition(evaluatedPosition))
                        {
                            continue;
                        }
                        var s        = new Square(evaluatedPosition[0], evaluatedPosition[1]);
                        var occupant = this[s];
                        if (occupant == null ||
                            occupant.Color != CurrentPlayer && occupant.Type != PieceType.King)
                        {
                            landingPositions.Add(s);
                        }
                    }
                }
                if (chessman.Type == PieceType.King)
                {
                    int[][] movePatterns =
                    {
                        new[] { -1, -1 }, new[] { -1, 0 }, new[] { -1, 1 },
                        new[] {  0, -1 }, new[] {  0, 1 },
                        new[] {  1, -1 }, new[] {  1, 0 }, new[] {  1, 1 }
                    };

                    for (int j = 0; j < 8; j++)
                    {
                        var   mvPtrn            = movePatterns[j];
                        int[] evaluatedPosition = { 0, 0 };
                        evaluatedPosition[0] = currentPosition[0];
                        evaluatedPosition[1] = currentPosition[1];
                        evaluatedPosition[0] = evaluatedPosition[0] + mvPtrn[0];
                        evaluatedPosition[1] = evaluatedPosition[1] + mvPtrn[1];
                        if (!IsValidPosition(evaluatedPosition))
                        {
                            continue;
                        }
                        var s        = new Square(evaluatedPosition[0], evaluatedPosition[1]);
                        var occupant = this[s];
                        if (occupant == null ||
                            occupant.Color != CurrentPlayer && occupant.Type != PieceType.King)
                        {
                            landingPositions.Add(s);
                        }
                    }
                }
                if (chessman.Type == PieceType.Pawn)
                {
                    int[] evaluatedPosition = { 0, 0 };
                    evaluatedPosition[0] = currentPosition[0];
                    evaluatedPosition[1] = currentPosition[1];

                    if (chessman.Color == PlayerColor.Black)
                    {
                        evaluatedPosition[0] = evaluatedPosition[0] - 1;
                    }
                    else
                    {
                        evaluatedPosition[0] = evaluatedPosition[0] + 1;
                    }
                    if (!IsValidPosition(evaluatedPosition))
                    {
                        continue;
                    }
                    var s        = new Square(evaluatedPosition[0], evaluatedPosition[1]);
                    var occupant = this[s];
                    if (occupant == null)
                    {
                        landingPositions.Add(s);
                    }
                }

                var mvs = landingPositions.Select(x => new Move(new Square(currentPosition[0], currentPosition[1]), x));
                foreach (var move in mvs)
                {
                    resp.Add(move);
                }
            }

            PossibleMoveList = resp;
        }
Example #6
0
        private void SetPiece(Square square, PieceType type, PlayerColor color)
        {
            var idx = (square.Row - 1) * 8 + square.Column - 1;

            Board[idx] = new Chessman(type, color);
        }
Example #7
0
 public bool CanMove(Square square)
 {
     // TODO : if "square" is empty, would CurrentPlayer's king be attacked ?
     return(this[square] != null && this[square].Color == CurrentPlayer);
 }