Пример #1
0
    public void Init(CheckerBoard board, CheckerCoords checkerCoords, bool isWhite)
    {
        _board  = board;
        _coords = checkerCoords;
        IsWhite = isWhite;

        _spriteRenderer.sprite = isWhite ? _whiteCheckerSprite : _blackCheckerSprite;
    }
Пример #2
0
    Checker CheckerAt(CheckerCoords coords)
    {
        if (isDummy)
        {
            return(AllCheckersDummy.ContainsKey(coords) ? AllCheckersDummy[coords] : null);
        }

        return(_board.CheckerAt(coords));
    }
Пример #3
0
 public void SetCoords(CheckerCoords newCoords)
 {
     _coords = newCoords;
     // Reached end
     if (IsWhite ? newCoords.Y == 8 : newCoords.Y == 1 && !IsQueen)
     {
         BecomeQueen();
     }
     transform.position = _board.CheckerToWorldCoords(newCoords);
 }
Пример #4
0
    bool IsKillMoveValid(CheckerCoords newCoords, int deltaY, int deltaX)
    {
        if (CheckerAt(newCoords) == null)
        {
            var killCoords = _coords + new CheckerCoords(deltaY / 2, deltaX / 2);

            var killChecker = CheckerAt(killCoords);
            return(killChecker != null && killChecker.IsWhite != IsWhite);
        }

        return(false);
    }
Пример #5
0
    void CheckOutput()
    {
        try {
            var t     = output.text;
            var split = t.Split(new [] { " " }, StringSplitOptions.RemoveEmptyEntries);


            for (int i = 0; i < split.Length; ++i)
            {
                CheckerCoords coords = TextToCoords(split[i]);

                touchMoveProvider.Raycast(_board.CheckerToWorldCoords(coords));
            }
        }
        catch (Exception e) {
            androidDebug.AddLog(e.ToString());
        }
    }
Пример #6
0
    public bool IsValidMove(CheckerCoords newCoords)
    {
        if (_board.OutsideBoard(newCoords) || _board.isWhiteMove != IsWhite)
        {
            return(false);
        }
        int yRemainder = newCoords.Y % 2, xRemainder = newCoords.X % 2;

        if (yRemainder != xRemainder)
        {
            return(false);
        }

        var deltaY         = newCoords.Y - _coords.Y;
        var deltaX         = newCoords.X - _coords.X;
        var absoluteDeltaX = Mathf.Abs(deltaX);
        var absoluteDeltaY = Mathf.Abs(deltaY);

        if (IsQueen)
        {
            if (absoluteDeltaY == absoluteDeltaX)
            {
                return(IsQueenMoveValid(newCoords, !isDummy));
            }
        }
        else
        {
            if (absoluteDeltaY == 1 && absoluteDeltaX == 1)
            {
                return(IsSimpleMoveValid(newCoords));
            }
            if (absoluteDeltaY == 2 && absoluteDeltaX == 2)
            {
                return(IsKillMoveValid(newCoords, deltaY, deltaX));
            }
        }
        return(false);
    }
Пример #7
0
    void CreateCheckerRow(int y, bool isWhite)
    {
        int beginCell = y % 2 == 0 ? 2 : 1;

        for (int x = beginCell; x <= beginCell + 6; x += 2)
        {
            var createdChecker = Instantiate(_checkerPrefab, CheckerToWorldCoords(y, x),
                                             Quaternion.identity, transform);

            var checkerCoords = new CheckerCoords(y, x);
            createdChecker.Init(this, checkerCoords, isWhite);

            if (isWhite)
            {
                WhiteCheckers.Add(createdChecker);
            }
            else
            {
                BlackCheckers.Add(createdChecker);
            }

            _allCheckers.Add(checkerCoords, createdChecker);
        }
    }
Пример #8
0
    List <CheckerMove> GetQueenMoves(bool includeSimpleMoves)
    {
        var moves = new List <CheckerMove>();

        var leftTop1     = new CheckerCoords(1, -1);
        var rightTop1    = new CheckerCoords(1, 1);
        var rightBottom1 = new CheckerCoords(-1, 1);
        var leftBottom1  = new CheckerCoords(-1, -1);

        var currentCheckPosition = _coords;
        var enemyBehind          = false;
        var enemyCoords          = new CheckerCoords();

        while (true)
        {
            currentCheckPosition += leftTop1;
            var checkResult = CheckQueenMove(moves, currentCheckPosition, ref enemyBehind,
                                             ref enemyCoords, includeSimpleMoves);
            if (checkResult == false)
            {
                break;
            }
        }

        currentCheckPosition = _coords;
        enemyBehind          = false;
        enemyCoords          = new CheckerCoords();

        while (true)
        {
            currentCheckPosition += rightTop1;
            var checkResult = CheckQueenMove(moves, currentCheckPosition, ref enemyBehind,
                                             ref enemyCoords, includeSimpleMoves);
            if (checkResult == false)
            {
                break;
            }
        }

        currentCheckPosition = _coords;
        enemyBehind          = false;
        enemyCoords          = new CheckerCoords();

        while (true)
        {
            currentCheckPosition += rightBottom1;
            var checkResult = CheckQueenMove(moves, currentCheckPosition, ref enemyBehind,
                                             ref enemyCoords, includeSimpleMoves);
            if (checkResult == false)
            {
                break;
            }
        }

        currentCheckPosition = _coords;
        enemyBehind          = false;
        enemyCoords          = new CheckerCoords();

        while (true)
        {
            currentCheckPosition += leftBottom1;
            var checkResult = CheckQueenMove(moves, currentCheckPosition, ref enemyBehind,
                                             ref enemyCoords, includeSimpleMoves);
            if (checkResult == false)
            {
                break;
            }
        }


        return(moves);
    }
Пример #9
0
    public bool CheckQueenMove(List <CheckerMove> movesList, CheckerCoords newCoords,
                               ref bool enemyBehind, ref CheckerCoords enemyCoords,
                               bool includeSimpleMoves)
    {
        if (_board.OutsideBoard(newCoords) || _board.isWhiteMove != IsWhite)
        {
            return(false);
        }
        int yRemainder = newCoords.Y % 2, xRemainder = newCoords.X % 2;

        if (yRemainder != xRemainder)
        {
            return(false);
        }

        var deltaY         = newCoords.Y - _coords.Y;
        var deltaX         = newCoords.X - _coords.X;
        var absoluteDeltaX = Mathf.Abs(deltaX);
        var absoluteDeltaY = Mathf.Abs(deltaY);

        if (absoluteDeltaY == absoluteDeltaX)
        {
            var checker = CheckerAt(newCoords);
            if (checker == null)
            {
                if (enemyBehind)
                {
                    if (IsQueenMoveValid(newCoords, true))
                    {
                        movesList.Add(new CheckerMove {
                            destinationCoords = newCoords,
                            isKill            = true,
                            killCoords        = enemyCoords,
                            oldCoords         = _coords
                        });
                    }
                }
                else if (includeSimpleMoves && !SomeoneMustKill())
                {
                    movesList.Add(new CheckerMove {
                        destinationCoords = newCoords,
                        oldCoords         = _coords
                    });
                }

                return(true);
            }
            // Friend.
            if (checker.IsWhite == IsWhite)
            {
                return(false);
            }
            // Enemy.
            if (checker.IsWhite != IsWhite)
            {
                if (enemyBehind)
                {
                    return(false);
                }

                enemyBehind = true;
                enemyCoords = newCoords;
                return(true);
            }
        }

        return(false);
    }
Пример #10
0
    List <CheckerMove> GetCheckerMoves(bool includeSimpleMoves)
    {
        var moves = new List <CheckerMove>();

        var ySign = IsWhite ? 1 : -1;

        var leftDiagonalCell  = new CheckerCoords(ySign, -1);
        var rightDiagonalCell = new CheckerCoords(ySign, 1);

        var oneForwardLeft = _coords + leftDiagonalCell;
        var twoForwardLeft = oneForwardLeft + leftDiagonalCell;

        var oneForwardRight = _coords + rightDiagonalCell;
        var twoForwardRight = oneForwardRight + rightDiagonalCell;

        var twoBackwardsLeft  = _coords + new CheckerCoords(-2 * ySign, -2);
        var twoBackwardsRight = _coords + new CheckerCoords(-2 * ySign, 2);

        if (includeSimpleMoves && IsValidMove(oneForwardLeft))
        {
            moves.Add(new CheckerMove {
                destinationCoords = oneForwardLeft, oldCoords = _coords
            });
        }
        else if (IsValidMove(twoForwardLeft))
        {
            moves.Add(new CheckerMove()
            {
                destinationCoords = twoForwardLeft,
                killCoords        = oneForwardLeft,
                isKill            = true,
                oldCoords         = _coords
            });
        }

        if (includeSimpleMoves && IsValidMove(oneForwardRight))
        {
            moves.Add(new CheckerMove {
                destinationCoords = oneForwardRight, oldCoords = _coords
            });
        }
        else if (IsValidMove(twoForwardRight))
        {
            moves.Add(new CheckerMove {
                destinationCoords = twoForwardRight,
                killCoords        = oneForwardRight,
                isKill            = true,
                oldCoords         = _coords
            });
        }

        if (IsValidMove(twoBackwardsLeft))
        {
            moves.Add(new CheckerMove {
                destinationCoords = twoBackwardsLeft,
                isKill            = true,
                killCoords        = _coords + new CheckerCoords(-ySign, -1),
                oldCoords         = _coords
            });
        }

        if (IsValidMove(twoBackwardsRight))
        {
            moves.Add(new CheckerMove {
                destinationCoords = twoBackwardsRight,
                isKill            = true,
                killCoords        = _coords + new CheckerCoords(-ySign, 1),
                oldCoords         = _coords
            });
        }

        return(moves);
    }
Пример #11
0
    bool IsQueenMoveValid(CheckerCoords newCoords, bool shouldCheckDummy)
    {
        if (CheckerAt(newCoords) == null)
        {
            var checkers = new List <Checker>();
            var yMove    = (int)Mathf.Sign(newCoords.Y - _coords.Y);
            var xMove    = (int)Mathf.Sign(newCoords.X - _coords.X);
            var yAbs     = Mathf.Abs(newCoords.Y - _coords.Y);

            for (var i = 1; i < yAbs; ++i)
            {
                checkers.Add(CheckerAt(_coords + new CheckerCoords(i * yMove, i * xMove)));
            }

            var count = checkers.Count(checker => checker != null);

            if (count == 1 && isDummy)
            {
                return(true);
            }

            if (count == 1)
            {
                var skippedChecker = checkers.First(checker => checker != null);
                if (skippedChecker.IsWhite != IsWhite)
                {
                    // Check if kills can be more.
                    List <CheckerCoords> dummyCheckCoords = new List <CheckerCoords>();
                    CheckerCoords        tempCoordinate   = skippedChecker.GetCoords();
                    while (true)
                    {
                        tempCoordinate = tempCoordinate + new CheckerCoords(yMove, xMove);
                        var checker = CheckerAt(tempCoordinate);

                        if (checker == null && !_board.OutsideBoard(tempCoordinate))
                        {
                            dummyCheckCoords.Add(tempCoordinate);
                        }
                        else
                        {
                            break;
                        }
                    }

                    AllCheckersDummy = new Dictionary <CheckerCoords, Checker>(_board.AllCheckers);
                    AllCheckersDummy.Remove(skippedChecker.GetCoords());
                    List <CheckerCoords> coordsWhichCanKillMore = new List <CheckerCoords>();

                    Dummy.IsWhite = IsWhite;

                    for (int j = 0; j < dummyCheckCoords.Count; ++j)
                    {
                        Dummy.SetCoords(dummyCheckCoords[j]);
                        var moves = Dummy.GetValidMoves(false);
                        if (moves.Any(move => move.isKill))
                        {
                            coordsWhichCanKillMore.Add(dummyCheckCoords[j]);
                        }
                    }

                    if (coordsWhichCanKillMore.Contains(newCoords) || coordsWhichCanKillMore.Count == 0)
                    {
                        return(true);
                    }
                }
            }
            else if (count == 0)
            {
                return(IsSimpleMoveValid(newCoords));
            }
        }

        return(false);
    }
Пример #12
0
    bool IsSimpleMoveValid(CheckerCoords newCoords)
    {
        bool someOneMustKill = SomeoneMustKill();

        return(CheckerAt(newCoords) == null && !someOneMustKill);
    }
Пример #13
0
 public Vector2 CheckerToWorldCoords(CheckerCoords coords)
 {
     return(CheckerToWorldCoords(coords.Y, coords.X));
 }
Пример #14
0
 public bool OutsideBoard(CheckerCoords coords)
 {
     return(coords.Y < 1 || coords.Y > 8 || coords.X < 1 || coords.X > 8);
 }
Пример #15
0
 public Checker CheckerAt(CheckerCoords coords)
 {
     return(!_allCheckers.ContainsKey(coords) ? null : _allCheckers[coords]);
 }