Exemple #1
0
        /// <summary>
        /// Called when leaving this page/view
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            while (BoardPositions.Count > 0)
            {
                PositionInstance pos = BoardPositions.First <PositionInstance>();
                BoardPositions.Remove(pos);
                pos.Reinitialize();
                _LocalPersistentObject.queuePositionInstances.Enqueue(pos);
            }

            BoardPositions = null;
        }
Exemple #2
0
        /// <summary>
        /// Processes the Board image into individual pieces that can be selected and identified seprately.
        /// </summary>
        /// <param name="_bmp"></param>
        private async void ProcessBoard(WriteableBitmap _bmp)
        {
            if (BoardPositions == null)
            {
                return;
            }

            String[] verticalPos = { "a", "b", "c", "d", "e", "f", "g", "h" };

            WriteableBitmap originalBitmap = _bmp;
            int             posSize        = 64;

            UpdateConsole("Processing Board Image into Pieces...");

            originalBitmap = originalBitmap.Resize(512, 512, WriteableBitmapExtensions.Interpolation.Bilinear);

            BoardPositions.Clear();
            for (int v = 7; v >= 0; v--)
            {
                for (int h = 1; h <= 8; h++)
                {
                    int x0 = (h - 1) * posSize;
                    int y0 = (8 - (v + 1)) * posSize;
                    //x1 = h * posSize;
                    //y1 = (8-v) * posSize;

                    PositionInstance bPos = _LocalPersistentObject.queuePositionInstances.Dequeue();
                    bPos.PositionID    = verticalPos[(h - 1)] + (v + 1);
                    bPos.PositionImage = new WriteableBitmap(posSize, posSize);
                    WriteableBitmap modifiedBitmap = originalBitmap.Crop(x0, y0, posSize, posSize);
                    bPos.PositionImage = modifiedBitmap;
                    bPos.PieceID       = (int)PositionInstance.PieceEnum.EPY;

                    modifiedBitmap = modifiedBitmap.Resize(64, 64, WriteableBitmapExtensions.Interpolation.Bilinear);
                    //modifiedBitmap = modifiedBitmap.Gray();
                    bPos.PositionImageByte = await EncodeImage(modifiedBitmap);

                    bPos.PositionImage = modifiedBitmap;
                    bPos.PieceName     = "Empty";
                    BoardPositions.Add(bPos);
                }
            }
            UpdateConsole("Processing Done...");

            //PredictBoardPieces();
        }
Exemple #3
0
        /// <summary>
        /// Instantiates a list of PositionInstances and Queues them for future use
        /// </summary>
        private void PositionInstancesFactory()
        {
            // Only instantiate new objects in the queue if it is empty
            if (_LocalPersistentObject.queuePositionInstances != null)
            {
                if (_LocalPersistentObject.queuePositionInstances.Count > 0)
                {
                    return;
                }
            }

            _LocalPersistentObject.queuePositionInstances = new Queue <PositionInstance>();

            for (int i = 1; i <= 64; i++)
            {
                PositionInstance bPos = new PositionInstance();
                _LocalPersistentObject.queuePositionInstances.Enqueue(bPos);
            }
        }
Exemple #4
0
        /// <summary>
        /// Predicts the pieces on the board for each position
        /// </summary>
        private async void PredictBoardPieces()
        {
            UpdateConsole("Starting board prediction...");
            String url = _LocalPersistentObject.predictionURLString;
            String str = SerializeTable();

            HttpResponseMessage response;

            // Send request
            UpdateConsole("Sending board configuration to service...");
            using (var client = new HttpClient())
            {
                try
                {
                    Uri uri = new Uri(url);
                    response = await client.PostAsync(
                        uri, new StringContent(str, Encoding.UTF8, "application/json"));
                }
                catch (Exception ex)
                {
                    UpdateConsole("An network related error occured: " + ex.Message);
                    return;
                }
            }
            UpdateConsole("Response received! Processing...");
            List <PredictedPiece> predictionResults;

            try
            {
                // Deserialize Response
                string json = await response.Content.ReadAsStringAsync();

                predictionResults = JsonConvert.DeserializeObject <List <PredictedPiece> >(json);
            }
            catch (Exception ex)
            {
                UpdateConsole("An error occured when parsing the response: " + ex.Message);
                throw new Exception("JSON deserialization error:", ex);
            }

            int pos = 0;

            // Update the Observable Collection with the predicted values
            foreach (PositionInstance position in BoardPositions)
            {
                PredictedPiece predictedPiece = predictionResults.ElementAt <PredictedPiece>(pos);
                if (String.Compare(predictedPiece.PositionID, position.PositionID, true) == 0)
                {
                    position.PieceID          = predictedPiece.PredictedPieceID;
                    position.PredictedPieceID = predictedPiece.PredictedPieceID;
                    position.PieceKeyID       = PositionInstance.GetPieceKeyFromPieceID(position.PieceID);
                    position.PieceName        = PositionInstance.GetPieceNameFromPieceID(position.PieceID);
                    position.IsPredicted      = true;
                    UpdateConsole(new StringBuilder().AppendFormat("We Predicted the Piece at {0} is a {1}={2}", position.PositionID,
                                                                   position.PredictedPieceID,
                                                                   PositionInstance.PiecesNames[position.PredictedPieceID]).ToString());
                }
                pos++;
            }

            UpdateConsole("Board prediction finished...");
        }