public PlayerCoordinate(string str)
 {
     if (str.Length != 2)
     {
         throw new Exception("Invalid coordinate format");
     }
     Row = BoardUtil.GetInteralPlayerRow(str[1]);
     Col = BoardUtil.GetInternalPlayerCol(str[0]);
 }
Exemple #2
0
        private bool CanPlayersReachGoal(WallCoordinate wallCoordinate)
        {
            // Make a copy of the board, we don't want to change the original yet
            char[,] copy = board.Clone() as char[, ];
            copy[wallCoordinate.StartRow, wallCoordinate.StartCol] = copy[wallCoordinate.EndRow, wallCoordinate.EndCol] = WALL;

            bool canPlayerOneReachGoal = BoardUtil.CanReachGoal(copy, 0, playerOneLocation.Row, playerOneLocation.Col);
            bool canPlayerTwoReachGoal = BoardUtil.CanReachGoal(copy, 16, playerTwoLocation.Row, playerTwoLocation.Col);

            return(canPlayerOneReachGoal && canPlayerTwoReachGoal);
        }
Exemple #3
0
        public bool MovePiece(PlayerEnum player, PlayerCoordinate destinationCoordinate)
        {
            if (gameOver || player != whoseTurn)
            {
                return(false);
            }

            bool             retValue        = false;
            PlayerCoordinate startCoordinate = null;

            switch (player)
            {
            case PlayerEnum.ONE:
                startCoordinate = playerOneLocation;
                break;

            case PlayerEnum.TWO:
                startCoordinate = playerTwoLocation;
                break;
            }

            string move = Convert.ToChar(97 + (destinationCoordinate.Col / 2)).ToString() + (9 - destinationCoordinate.Row / 2).ToString();

            if (possibleMoves[whoseTurn == 0 ? 0 : 1].Contains(move) /*IsValidPlayerMove(player, startCoordinate, destinationCoordinate)*/)
            {
                board[startCoordinate.Row, startCoordinate.Col] = PLAYER_SPACE;
                switch (player)
                {
                case PlayerEnum.ONE:
                    playerOneLocation.Row = destinationCoordinate.Row;
                    playerOneLocation.Col = destinationCoordinate.Col;

                    lastStart[0] = lastMove[0];

                    if (BoardUtil.IsMoveAdjacentToPosition(move, playerOneLocation))
                    {
                        lastMove[0] = BoardUtil.PlayerCoordinateToString(playerTwoLocation);
                    }
                    else
                    {
                        lastMove[0] = BoardUtil.PlayerCoordinateToString(playerOneLocation);
                    }

                    possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition();
                    whoseTurn = PlayerEnum.TWO;
                    possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition();
                    break;

                case PlayerEnum.TWO:
                    playerTwoLocation.Row = destinationCoordinate.Row;
                    playerTwoLocation.Col = destinationCoordinate.Col;

                    lastStart[1] = lastMove[1];

                    if (BoardUtil.IsMoveAdjacentToPosition(move, playerTwoLocation))
                    {
                        lastMove[1] = BoardUtil.PlayerCoordinateToString(playerOneLocation);
                    }
                    else
                    {
                        lastMove[1] = BoardUtil.PlayerCoordinateToString(playerTwoLocation);
                    }


                    possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition();
                    whoseTurn = PlayerEnum.ONE;
                    possibleMoves[whoseTurn == 0 ? 0 : 1] = PossibleMovesFromPosition();
                    break;
                }
                retValue = true;
            }

            // check for win
            if (playerOneLocation.Row == 0)
            {
                playerOneWin = true;
            }
            if (playerTwoLocation.Row == (TOTAL_ROWS - 1))
            {
                playerTwoWin = true;
            }
            gameOver = playerOneWin || playerTwoWin;

            return(retValue);
        }