예제 #1
0
        public bool SetBoardFromStringArray(string[] data, bool force_creation = false)
        {
            if (data.Length != this.State.Length || data.Any(x => !ICellTools.IsStringICell(x)))
            {
                return(false);
            }

            if (force_creation)
            {
                this.State = data.Select(id => ICellTools.ICellFromString(id)).ToArray();
                return(true);
            }

            this.State = data.Zip(this.State,
                                  (new_id, exsisting_cell) =>
                                  (exsisting_cell == null || exsisting_cell.ToString().CompareTo(new_id) != 0
                    ? ICellTools.ICellFromString(new_id)
                    : exsisting_cell)
                                  ).ToArray();
            return(true);
        }
예제 #2
0
        public Boolean InitBoardFromUser(string[] input, Ownership player)
        {
            // make sure size is correct and all are all indeed valid
            if (input.Length != DefaultPiecesRanks.Length || input.Any(id => !ICellTools.IsStringICell(id)))
            {
                return(false);
            }

            // make sure all pieces are of the right type and Ownership
            var user_icells          = input.Select(x => ICellTools.ICellFromString(x)).ToArray();
            var user_icells_ranklist = new List <Rank>();
            var enumerator           = user_icells.GetEnumerator();

            enumerator.Reset();
            if (!enumerator.MoveNext())
            {
                throw  new EvaluateException("InitBoardFromUser");
            }
            ICell item;

            while ((item = (ICell)enumerator.Current) != null)
            {
                switch (item)
                {
                case Piece p when p.GetOwnership() == player:
                    user_icells_ranklist.Add(p.GetRank());

                    break;

                default:
                    return(false);
                }

                if (!enumerator.MoveNext())
                {
                    break;
                }
            }

            // make sure all pieces that should be there - are there
            var ordered_user_ranklist = user_icells_ranklist.OrderBy(rank => (int)rank);

            if (!DefaultPiecesRanks
                .Zip(ordered_user_ranklist, (expected_rank, given_rank) => expected_rank == given_rank).All(x => x))
            {
                return(false);
            }


            switch (player)
            {
            case Ownership.FirstPlayer:
                user_icells.CopyTo(State, 0);
                break;

            case Ownership.SecondPlayer:
                int insert_index = Position.CoordinatesToIndex(0, DefaultBoardSize - 4);
                user_icells.Reverse().ToArray().CopyTo(State, insert_index);
                break;

            default:
                return(false);
            }

            return(true);
        }