Exemplo n.º 1
0
        public static FlowerzGame CreateGameFromFile(string fileLocation)
        {
            var game = new FlowerzGame();

            using (var stringReader = new StreamReader(new FileStream(fileLocation, FileMode.Open))) {
                string s;
                var    lineNumber = 1;
                while ((s = stringReader.ReadLine()) != null)
                {
                    if (lineNumber == 1)
                    {
                        //Line 1 -> Queue, Items are separates by spaces
                        //Cannot contain Rock (X) or empty field (_)
                        var queueItems = s.Split(' ');
                        foreach (var queueItem in queueItems)
                        {
                            if (queueItem == PieceStrings.Empty || queueItem == PieceStrings.Rock)
                            {
                                throw new InvalidQueueException("'" + queueItem + "' is not a valid piece for the queue");
                            }

                            game.AddToQueue(PieceTranslator.StringToPiece(queueItem));
                        }
                    }
                    else if (lineNumber > 1)
                    {
                        //Lines 2-8 -> board layout
                        //Cannot contain shovel (S) or Butterfly (*F)
                        var fieldItems = s.Split(' ');
                        for (int i = 0; i < fieldItems.Length; i++)
                        {
                            if (fieldItems[i] != "S")
                            {
                                game.SetField(lineNumber - 2, i, PieceTranslator.StringToPiece(fieldItems[i]));
                            }
                        }
                    }

                    lineNumber++;
                }
            }

            return(game);
        }
Exemplo n.º 2
0
        public byte[] ToByteArray()
        {
            var queueBytes = new List <Byte> {
                0x70,
                0x69,
                0x65,
                0x63,
                0x65,
                0x73,
                0x51,
                0x75,
                0x65,
                0x75,
                0x65,
                0x08,
                0x00,
                0x00,
                0x00,
            };

            queueBytes.Add(Convert.ToByte(_piecesInQueue.Count));
            queueBytes.Add(0x00);
            queueBytes.Add(0x01);

            var tens        = 0;
            var digit       = 0;
            var boardNumber = 0;

            foreach (var piece in _piecesInQueue)
            {
                if (tens > 0)
                {
                    queueBytes.Add(DigitTranslator.IntToByte(tens));
                }

                queueBytes.Add(DigitTranslator.IntToByte(digit));

                if (digit == 9)
                {
                    tens++;
                    digit = 0;
                }
                else
                {
                    digit++;
                }

                var pieceBytes = PieceTranslator.PieceToByte(piece);
                queueBytes.AddRange(pieceBytes);

                queueBytes.Add(0);
                queueBytes.Add(0);
                queueBytes.Add(0);
                queueBytes.Add(0);
                queueBytes.Add(0);
                queueBytes.Add(0);

                if (boardNumber == _piecesInQueue.Count - 1)
                {
                    //Laatste
                    queueBytes.Add(0x00);
                    queueBytes.Add(0x09);
                    queueBytes.Add(0x00);
                    queueBytes.Add(0x00);
                    queueBytes.Add(0x11);
                }
                else if (boardNumber <= 8)
                {
                    queueBytes.Add(0x01);
                }
                else
                {
                    queueBytes.Add(0x02);
                }

                boardNumber++;
            }

            return(queueBytes.ToArray());
        }