Example #1
0
        public static bool IsPassable(BoardArray boardArray, Position fromPosition,
                                      IEnumerable <Position> oponentPositions, Position toPosition)
        {
            if (!BasicValidator.IsWithinBoard(fromPosition) || !BasicValidator.IsWithinBoard(toPosition))
            {
                return(false);
            }
            var discovered = new HashSet <Position>();

            var s = new Stack <Position>();

            s.Push(fromPosition);

            while (s.Count > 0)
            {
                var pos = s.Pop();
                if (discovered.Contains(pos))
                {
                    continue;
                }

                discovered.Add(pos);

                foreach (var step in steps)
                {
                    var newPosition = pos + step;
                    if (MoveValidator.IsValid(boardArray, pos, oponentPositions, newPosition))
                    {
                        s.Push(newPosition);
                    }
                }
            }

            return(discovered.Contains(toPosition));
        }
Example #2
0
File: Board.cs Project: ylith/Chess
    public bool TestMoveForCheck(GameObject piece, Vector2 pos)
    {
        Dictionary <int, List <Vector2> >[] tempLocks = (Dictionary <int, List <Vector2> >[])locks.Clone();
        List <GameObject> tempPieces = new List <GameObject>(GameManager.Instance.GetPieces());
        BoardArray        tempBoard  = new BoardArray(8, 8);

        tempBoard.boardArray = (int[, ])board.boardArray.Clone();
        Piece      script     = piece.GetComponent <Piece>();
        Vector2Int currentPos = script.currentBoardPosition;

        if (GetIdByCoord(pos) != 0)
        {
            tempPieces.Remove(GetByCoord(pos));
        }
        board.RemovePiece(script.currentBoardPosition);
        board.SetPiece(piece, pos);
        script.currentBoardPosition = new Vector2Int((int)pos.x, (int)pos.y);
        SetLocks(tempPieces);
        //reset
        bool isInCheck = IsKingUnderCheck(script.player);

        locks                       = (Dictionary <int, List <Vector2> >[])tempLocks.Clone();
        board.boardArray            = (int[, ])tempBoard.boardArray.Clone();
        script.currentBoardPosition = currentPos;

        return(isInCheck);
    }
Example #3
0
        public static bool IsPassable(BoardArray boardArray, IEnumerable <QuoridorPlayer> players)
        {
            // return true;
            //var isPassableForPlayer = false;
            //var isPassableForOpponent = false;


            foreach (var player in players)
            {
                var isPassableForPlayer = false;
                for (var x = 0; x <= Board.BoardSize; x += 2)
                {
                    for (var y = 0; y <= Board.BoardSize; y += 2)
                    {
                        var pos = new Position(x, y);
                        if (player.IsWiningPosition(pos) &&
                            IsPassable(boardArray, player.CurrentPosition, players.Select(p => p.CurrentPosition), pos))
                        {
                            isPassableForPlayer = true;
                            break;
                        }
                    }
                    if (isPassableForPlayer)
                    {
                        break;
                    }
                }
                if (!isPassableForPlayer)
                {
                    return(false);
                }
            }

            return(true);
        }
Example #4
0
    public override void OnInspectorGUI()
    {
        boardArray = (target as BoardInitInfo).board;
        EditorGUILayout.PropertyField(countX);
        EditorGUILayout.PropertyField(countY);

        EditorGUILayout.PropertyField(randomBoard);
        if (randomBoard.boolValue)
        {
            EditorGUILayout.PropertyField(minValue);
            EditorGUILayout.PropertyField(maxValue);
        }
        else
        {
            serializedObject.ApplyModifiedProperties();
            if (oldX != countX.intValue || oldY != countY.intValue)
            {
                SaveBoard();
                ReloadBoard();
                serializedObject.Update();
            }
            oldX = countX.intValue;
            oldY = countY.intValue;
            EditorGUILayout.PropertyField(board);
        }
        serializedObject.ApplyModifiedProperties();
    }
Example #5
0
 void OnEnable()
 {
     randomBoard = serializedObject.FindProperty("randomBoard");
     countX      = serializedObject.FindProperty("countX");
     countY      = serializedObject.FindProperty("countY");
     minValue    = serializedObject.FindProperty("minValue");
     maxValue    = serializedObject.FindProperty("maxValue");
     board       = serializedObject.FindProperty("board");
     boardArray  = (target as BoardInitInfo).board;
     //InitBoard();
 }
Example #6
0
 private void CreateSquares(BoardArray board)
 {
     squares = new Square[countY, countX];
     for (int i = 0; i < countY; i++)
     {
         for (int j = 0; j < countX; j++)
         {
             CreateSquare(i, j, board.rows[i].row[j]);
         }
     }
     StartCoroutine(ShowCreatedSquares());
 }
    //private bool doingSetup = true;

    // Use this for initialization
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);

        arrayScript  = GetComponent <BoardArray>();
        boardScript  = GetComponent <BoardManager>();
        unitScript   = GetComponent <UnitManager>();
        cameraScript = GetComponent <CameraManager>();
        InitGame();
    }
Example #8
0
        public static bool AreValid(BoardArray boardArray, IEnumerable <QuoridorPlayer> players,
                                    QuoridorPlayer player, params Position[] wallPositions)
        {
            if (player.NumberOfWallsAvalaible <= 0)
            {
                return(false);
            }

            //var boardCopy = new Board(board);
            foreach (var wallPosition in wallPositions)
            {
                if (!BasicValidator.IsWithinBoard(wallPosition)) //just in case, it doesnt cost much
                {
                    return(false);
                }

                if (boardArray[wallPosition] != BoardElementType.EmptyForWall)
                {
                    return(false);
                }
            }

            var elements = new BoardElementType[wallPositions.Length];

            for (var index = 0; index < wallPositions.Length; index++)
            {
                elements[index] = boardArray[wallPositions[index]];
                boardArray[wallPositions[index]] = BoardElementType.Wall;
            }

            var isPassable = IsPassable(boardArray, players);

            for (var index = 0; index < wallPositions.Length; index++)
            {
                boardArray[wallPositions[index]] = elements[index];
            }

            return(isPassable);
        }
        public static bool IsValid(BoardArray board, Position playerPosition, IEnumerable <Position> playersPositions, Position newPosition)
        {
            //cant move out of board
            if (!BasicValidator.IsWithinBoard(newPosition)) //just in case, it doesnt cost much
            {
                return(false);
            }

            //cant only move to empty position
            if (board[newPosition] != BoardElementType.Empty)
            {
                return(false);
            }


            //cant move to the same position or to the position of any of the oponents
            if (playersPositions.Any(p => p.Equals(newPosition)))
            {
                return(false);
            }

            //cant move diagonal
            if (playerPosition.X != newPosition.X && playerPosition.Y != newPosition.Y)
            {
                return(false);
            }


            foreach (var oponentPosition in playersPositions)
            {
                if (oponentPosition == playerPosition)
                {
                    continue;
                }
                //can only move two fields if jumping over another pawn and there is no wall between
                if (oponentPosition.IsAdjacent(playerPosition) && oponentPosition.IsAdjacent(newPosition))
                {
                    if (playerPosition.Y == newPosition.Y &&
                        ((playerPosition.X - newPosition.X == 4 && playerPosition.X - oponentPosition.X == 2 && board[playerPosition.Y, playerPosition.X - 1] != BoardElementType.Wall && board[playerPosition.Y, playerPosition.X - 3] != BoardElementType.Wall) ||
                         (playerPosition.X - newPosition.X == -4 && playerPosition.X - oponentPosition.X == -2 && board[playerPosition.Y, playerPosition.X + 1] != BoardElementType.Wall && board[playerPosition.Y, playerPosition.X + 3] != BoardElementType.Wall)))
                    {
                        return(true);
                    }


                    if (playerPosition.X == newPosition.X &&
                        ((playerPosition.Y - newPosition.Y == 4 && playerPosition.Y - oponentPosition.Y == 2 && board[playerPosition.Y - 1, playerPosition.X] != BoardElementType.Wall && board[playerPosition.Y - 3, playerPosition.X] != BoardElementType.Wall) ||
                         (playerPosition.Y - newPosition.Y == -4 && playerPosition.Y - oponentPosition.Y == -2 && board[playerPosition.Y + 1, playerPosition.X] != BoardElementType.Wall && board[playerPosition.Y + 3, playerPosition.X] != BoardElementType.Wall)))
                    {
                        return(true);
                    }
                }
            }

            //cant move more than one field in x or y direction if not jumping over another pawn
            if (playerPosition.X == newPosition.X &&
                Math.Abs(playerPosition.Y - newPosition.Y) != 2)
            {
                return(false);
            }
            if (playerPosition.Y == newPosition.Y &&
                Math.Abs(playerPosition.X - newPosition.X) != 2)
            {
                return(false);
            }

            //cant move if wall is placed between current and new position
            if (playerPosition.X == newPosition.X &&
                ((playerPosition.Y + 2 == newPosition.Y &&
                  board[playerPosition.Y + 1, playerPosition.X] == BoardElementType.Wall) ||
                 (playerPosition.Y - 2 == newPosition.Y &&
                  board[playerPosition.Y - 1, playerPosition.X] == BoardElementType.Wall)))
            {
                return(false);
            }

            if (playerPosition.Y == newPosition.Y &&
                ((playerPosition.X + 2 == newPosition.X &&
                  board[playerPosition.Y, playerPosition.X + 1] == BoardElementType.Wall) ||
                 (playerPosition.X - 2 == newPosition.X &&
                  board[playerPosition.Y, playerPosition.X - 1] == BoardElementType.Wall)))
            {
                return(false);
            }

            return(true);
        }
Example #10
0
        /// <summary>
        /// Makes the board.
        /// </summary>
        /// <param name="routedEventHandler">The routed event handler.</param>
        public void MakeBoard(RoutedEventHandler routedEventHandler)
        {
            int count = 0;

            for (int row = 0; row < 8; row++)
            {
                BoardArray.Add(new List <CheckersSquareUserControl>());
                for (int column = 0; column < 8; column++)
                {
                    CheckersSquareUserControl checkerSquareUC;
                    if (row % 2 == 0)
                    {
                        if (column % 2 == 0)
                        {
                            checkerSquareUC = new CheckersSquareUserControl(
                                Brushes.White,
                                new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                routedEventHandler);
                        }
                        else
                        {
                            if (row < 3)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.BlackPawn),
                                    routedEventHandler);
                            }
                            else if (row > 4)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.RedPawn),
                                    routedEventHandler);
                            }
                            else
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                    routedEventHandler);
                            }
                        }
                    }
                    else
                    {
                        if (column % 2 == 0)
                        {
                            if (row < 3)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.BlackPawn),
                                    routedEventHandler);
                            }
                            else if (row > 4)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.RedPawn),
                                    routedEventHandler);
                            }
                            else
                            {
                                //empty middle spot
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                    routedEventHandler);
                            }
                        }
                        else
                        {
                            checkerSquareUC = new CheckersSquareUserControl(
                                Brushes.White,
                                new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                routedEventHandler);
                        }
                    }

                    count++;
                    BoardArray[row].Add(checkerSquareUC);
                }
            }
        }
Example #11
0
 void Awake()
 {
     board = new BoardArray(transform.position);
     logic = new MovementLogic(board);
 }
Example #12
0
 public MovementLogic(BoardArray board)
 {
     this.board = board;
 }
Example #13
0
    public void NextMove()
    {
        List <GameObject> pieces    = GameManager.Instance.GetPiecesByColor(Constants.Black);
        Vector2           bestPos   = new Vector2(0, 0);
        GameObject        bestPiece = pieces[0];
        int bestValue = 0;

        foreach (GameObject piece in pieces)
        {
            Piece          script         = piece.GetComponent <Piece>();
            List <Vector2> availableMoves = script.GetAvailableMoves();
            Board          board          = GameManager.Instance.board;

            foreach (Vector2 move in availableMoves) // Get all available moves, test them and report the result board values by weights
            {
                int currentValue = 0;
                Dictionary <int, List <Vector2> >[] tempLocks = (Dictionary <int, List <Vector2> >[])board.locks.Clone();
                List <GameObject> tempPieces = new List <GameObject>(GameManager.Instance.GetPieces());
                BoardArray        tempBoard  = new BoardArray(8, 8);
                tempBoard.boardArray = (int[, ])board.BoardObj.boardArray.Clone();
                Vector2Int currentPos = script.currentBoardPosition;
                if (board.GetIdByCoord(move) != 0)
                {
                    tempPieces.Remove(board.GetByCoord(move));
                }
                board.RemovePiece(script.currentBoardPosition);
                board.SetPiece(piece, move);
                script.currentBoardPosition = new Vector2Int((int)move.x, (int)move.y);
                board.SetLocks(tempPieces);

                foreach (GameObject tempPiece in tempPieces)
                {
                    Piece tempScrip = tempPiece.GetComponent <Piece>();
                    if (tempScrip.player == Constants.Black)
                    {
                        currentValue += Constants.weights[tempScrip.type];
                    }
                    else
                    {
                        currentValue -= Constants.weights[tempScrip.type];
                    }
                }
                bool isInCheck = board.IsKingUnderCheck(script.player);

                if (isInCheck)
                {
                    continue;
                }

                float rand = Random.Range(0f, 100f) / 100; // get a random 0-1 float and setup 30% chance to change move for moves with the same result

                if (bestPos == Vector2.zero)               //initialize
                {
                    bestPos   = move;
                    bestPiece = piece;
                    bestValue = currentValue;
                }
                else if (currentValue > bestValue || (currentValue == bestValue && rand > 0.3))
                {
                    bestPos   = move;
                    bestPiece = piece;
                    bestValue = currentValue;
                }

                //reset
                board.locks = (Dictionary <int, List <Vector2> >[])tempLocks.Clone();
                board.BoardObj.boardArray   = (int[, ])tempBoard.boardArray.Clone();
                script.currentBoardPosition = currentPos;
            }
        }

        if (bestPos == Vector2.zero) // game over if error or no moves
        {
            GameManager.Instance.gameOverText.text = "Something went wrong";
            GameManager.Instance.PassTurn();
            GameManager.Instance.GameOver();
        }
        else
        {
            Vector3    move     = GameManager.Instance.ConvertBoardToScreenCoords(bestPos);
            Vector2Int boardPos = new Vector2Int((int)bestPos.x, (int)bestPos.y);
            GameManager.Instance.board.RemovePieceAt(boardPos);
            bestPiece.GetComponent <Piece>().Move(move);
            GameManager.Instance.PassTurn();
        }
    }