Beispiel #1
0
        public void SnakeGame_GameIteration_SnakeCrash()
        {
            Position head = new Position(), tail = new Position();

            eCellType[] field = new eCellType[64];

            FieldMatrix.Seed(field, head, tail);
            eDirectionType currentDirection = eDirectionType.None, nextDirection = eDirectionType.Right;

            FieldMatrix.SetCellTypeByPosition(field, new Position()
            {
                row = head.row, col = (byte)(head.col + 1)
            }, eCellType.SnakeDown);

            Assert.ThrowsException <CrashedInSnakeException>(() => GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded), "Did not crash moving to the right");

            FieldMatrix.Seed(field, head, tail);

            FieldMatrix.SetCellTypeByPosition(field, new Position()
            {
                row = (byte)(head.row + 1), col = head.col
            }, eCellType.SnakeHead);
            nextDirection = eDirectionType.Down;

            Assert.ThrowsException <CrashedInSnakeException>(() => GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded), "Did not crash moving down");
        }
Beispiel #2
0
        public static async Task Aggregator(
            // banks
            FPGA.OutputSignal <bool> Bank1,
            FPGA.OutputSignal <bool> Bank2,

            // blinker
            FPGA.OutputSignal <bool> LED1,

            // WS2812
            FPGA.OutputSignal <bool> DOUT
            )
        {
            QuokkaBoard.OutputBank(Bank1);
            QuokkaBoard.InputBank(Bank2);

            IsAlive.Blink(LED1);

            bool internalDOUT = false;

            FPGA.Config.Link(internalDOUT, DOUT);

            byte state = 0;

            eCellType[] fieldMatrix = new eCellType[64];

            Sequential handler = () =>
            {
                state++;
                FieldMatrix.Reset(fieldMatrix);

                switch (state)
                {
                case 1:
                    fieldMatrix[0]  = eCellType.RedCross;
                    fieldMatrix[63] = eCellType.GreenCross;
                    break;

                case 2:
                    FieldMatrix.DrawCross(fieldMatrix, eCellType.GreenCross);
                    break;

                case 3:
                    FieldMatrix.DrawCross(fieldMatrix, eCellType.RedCross);
                    break;

                case 4:
                    Position head = new Position(), tail = new Position();
                    FieldMatrix.Seed(fieldMatrix, head, tail);
                    break;

                default:
                    state = 0;
                    break;
                }

                Graphics.DrawFieldMatrix(1, fieldMatrix, out internalDOUT);
            };

            FPGA.Config.OnTimer(TimeSpan.FromSeconds(1), handler);
        }
        public static void DrawFieldMatrix(
            uint baseColor,
            eCellType[] matrix,
            out bool DOUT)
        {
            uint[] buff  = new uint[matrix.Length];
            uint   color = 0;

            for (byte idx = 0; idx < buff.Length; idx++)
            {
                eCellType value = eCellType.None;
                value = matrix[idx];

                FPGA.Collections.ReadOnlyDictionary <eCellType, uint> mapColors = new FPGA.Collections.ReadOnlyDictionary <eCellType, uint>()
                {
                    { eCellType.SnakeUp, baseColor },
                    { eCellType.SnakeDown, baseColor },
                    { eCellType.SnakeLeft, baseColor },
                    { eCellType.SnakeRight, baseColor },
                    { eCellType.SnakeHead, baseColor * 2 },
                    { eCellType.NextPart, baseColor << 8 },
                    { eCellType.RedCross, baseColor << 16 },
                    { eCellType.GreenCross, baseColor << 8 },
                };

                color     = mapColors[value];
                buff[idx] = color;
            }

            Draw(buff, out DOUT);
        }
        public static void SetCellTypeByPosition(
            eCellType[] fieldMatrix,
            Position position,
            eCellType cellType)
        {
            byte offset = 0;

            Lookups.PositionToOffset(position, ref offset);
            fieldMatrix[offset] = cellType;
        }
        public static void GetCellTypeByPosition(
            eCellType[] fieldMatrix,
            Position position,
            out eCellType cellType)
        {
            byte offset = 0;

            Lookups.PositionToOffset(position, ref offset);
            cellType = fieldMatrix[offset];
        }
        public static void ApplyChange(
            eCellType[] fieldMatrix,
            Position head,
            Position tail,
            eDirectionType currentDirection,
            eDirectionType nextDirection,
            out bool expanded)
        {
            expanded = false;

            if (nextDirection == eDirectionType.None)
            {
                return;
            }

            eCellType nextDirectionCellType = eCellType.None;

            Lookups.DirectionTypeToCellType(nextDirection, ref nextDirectionCellType);

            Position nextHeadPosition = new Position();

            Lookups.ApplyDirection(head, nextHeadPosition, nextDirection);

            ThrowIfCrashed(fieldMatrix, nextHeadPosition);

            eCellType tailCellType, nextHeadCellType;

            FieldMatrix.GetCellTypeByPosition(fieldMatrix, tail, out tailCellType);
            FieldMatrix.GetCellTypeByPosition(fieldMatrix, nextHeadPosition, out nextHeadCellType);

            // move head
            FieldMatrix.SetCellTypeByPosition(fieldMatrix, head, nextDirectionCellType);
            FieldMatrix.SetCellTypeByPosition(fieldMatrix, nextHeadPosition, eCellType.SnakeHead);

            FPGA.Runtime.DeepCopy(head, nextHeadPosition);

            if (nextHeadCellType == eCellType.NextPart)
            {
                expanded = true;
                return;
            }

            // move tail
            eDirectionType tailDirection = eDirectionType.None;

            // get value at current tail

            Lookups.CellTypeToDirectionType(tailCellType, ref tailDirection);

            // clear current tail
            FieldMatrix.SetCellTypeByPosition(fieldMatrix, tail, eCellType.None);

            // move tail
            Lookups.ApplyDirection(tail, tail, tailDirection);
        }
Beispiel #7
0
 /// <summary>
 /// Removes all occurences of specific cell type.
 /// </summary>
 /// <param name="cellType">Cell type.</param>
 private void RemoveAllOccurences(eCellType cellType)
 {
     for (int x = 0; x < AREA_SIZE; x++)
     {
         for (int y = 0; y < AREA_SIZE; y++)
         {
             if (_area[x, y] == cellType)
             {
                 _area[x, y] = eCellType.None;
             }
         }
     }
 }
Beispiel #8
0
        public static void CellTypeToDirectionType(
            eCellType cellType,
            ref eDirectionType directionType)
        {
            var lookup = new FPGA.Collections.ReadOnlyDictionary <eCellType, eDirectionType>()
            {
                { eCellType.SnakeUp, eDirectionType.Up },
                { eCellType.SnakeDown, eDirectionType.Down },
                { eCellType.SnakeLeft, eDirectionType.Left },
                { eCellType.SnakeRight, eDirectionType.Right }
            };

            directionType = lookup[cellType];
        }
Beispiel #9
0
        public void SnakeGame_GameIteration_TurnUp()
        {
            Position head = new Position(), tail = new Position();

            eCellType[] field = new eCellType[64];

            FieldMatrix.Seed(field, head, tail);
            eDirectionType currentDirection = eDirectionType.None, nextDirection = eDirectionType.Up;

            GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded);

            Assert.AreEqual(3, field.Count(c => c != eCellType.None));

            Assert.AreEqual(eCellType.SnakeRight, field[TestOffset(3, 3)], "Tail did not move to the right");
            Assert.AreEqual(eCellType.SnakeUp, field[TestOffset(3, 4)], "Body did not move to the right");
            Assert.AreEqual(eCellType.SnakeHead, field[TestOffset(2, 4)], "Head did not move up");
        }
Beispiel #10
0
        public void SnakeGame_GameIteration_SimpleMove()
        {
            Position head = new Position(), tail = new Position();

            eCellType[] field = new eCellType[64];
            field[0] = eCellType.SnakeHead;
            eDirectionType currentDirection = eDirectionType.None, nextDirection = eDirectionType.None;

            GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded);
            Assert.AreEqual(eCellType.SnakeHead, field[0], "Head should not move");

            nextDirection = eDirectionType.Right;
            GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out expanded);

            Assert.AreEqual(eCellType.None, field[0], "Tail did not move");
            Assert.AreEqual(eCellType.SnakeHead, field[1], "Head did not move");
        }
Beispiel #11
0
        public void SnakeGame_GameIteration_WallCrash(int row, int col, eDirectionType direction)
        {
            Position head = new Position()
            {
                row = (byte)row, col = (byte)col
            },
                     tail = new Position()
            {
                row = (byte)row, col = (byte)col
            };

            eCellType[] field = new eCellType[64];

            byte offset = 0;

            Lookups.PositionToOffset(head, ref offset);
            field[offset] = eCellType.SnakeHead;

            Assert.ThrowsException <CrashedInWallException>(() => GameEngine.ApplyChange(field, head, tail, eDirectionType.None, direction, out bool expanded));
        }
        public static void DrawCross(eCellType[] fieldMatrix, eCellType color)
        {
            for (byte row = 0; row < 8; row++)
            {
                for (byte col = 0; col < 8; col++)
                {
                    byte offset = 0;
                    Lookups.RowColToOffset(row, col, ref offset);

                    eCellType value = eCellType.None;

                    if (row == col || (7 - row) == col)
                    {
                        value = color;
                    }

                    fieldMatrix[offset] = value;
                }
            }
        }
Beispiel #13
0
        public void SnakeGame_PlaceNextPiece()
        {
            Position head = new Position(), tail = new Position();

            eCellType[] field = new eCellType[64];

            FieldMatrix.Seed(field, head, tail);
            var rnd = new Random();

            while (field.Any(f => f == eCellType.None))
            {
                var currentEmpty = field.Count(f => f == eCellType.None);

                FieldMatrix.PlaceNextPiece(field, (byte)rnd.Next());

                var nextEmpty = field.Count(f => f == eCellType.None);

                Assert.AreEqual(currentEmpty - 1, nextEmpty, "Did not place next piece");
            }

            Assert.ThrowsException <GameCompletedException>(() => FieldMatrix.PlaceNextPiece(field, 10));
        }
Beispiel #14
0
        public void SnakeGame_GameIteration_Expanded()
        {
            Position head = new Position(), tail = new Position();

            eCellType[] field = new eCellType[64];

            FieldMatrix.Seed(field, head, tail);

            field[TestOffset(3, 5)] = eCellType.NextPart;
            eDirectionType currentDirection = eDirectionType.None, nextDirection = eDirectionType.Right;

            GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded);

            Assert.IsTrue(expanded, "Snake did not expand");

            Assert.AreEqual(4, field.Count(c => c != eCellType.None));

            Assert.AreEqual(eCellType.SnakeRight, field[TestOffset(3, 2)], "Tail moved");
            Assert.AreEqual(eCellType.SnakeRight, field[TestOffset(3, 3)], "Body moved");
            Assert.AreEqual(eCellType.SnakeRight, field[TestOffset(3, 4)], "Body moved");
            Assert.AreEqual(eCellType.SnakeHead, field[TestOffset(3, 5)], "Head did not extend");
        }
Beispiel #15
0
        public bool SetMove(int i_Row, int i_Col, eCellType i_Value)
        {
            bool isLegalMove = false;

            if (i_Row >= 0 && i_Row < 3 && i_Col >= 0 && i_Col < 3 && i_Value != eCellType.Empty)
            {
                if (m_GameBoard[i_Row, i_Col] == eCellType.Empty)
                {
                    m_GameBoard[i_Row, i_Col] = i_Value;
                    checkGameOver();
                    isLegalMove = true;
                }
                else
                {
                    Console.WriteLine("Cell occupied");
                }
            }
            else
            {
                Console.WriteLine("Invalid Input");
            }

            return(isLegalMove);
        }
Beispiel #16
0
 public void SetCellType(eCellType _type)
 {
     m_type = _type;
 }
        public static void SnakeControl(
            GameControlsState controlsState,
            FPGA.OutputSignal <bool> DOUT,
            FPGA.OutputSignal <bool> TXD)
        {
            bool internalDOUT = false;

            FPGA.Config.Link(internalDOUT, DOUT);

            FPGA.Register <int> randomValue = 0;
            RandomValueGenerator.MakeRandomValue(controlsState, randomValue);

            eCellType[]    fieldMatrix      = new eCellType[64];
            var            head             = new Position();
            var            tail             = new Position();
            eDirectionType currentDirection = eDirectionType.None;
            byte           baseColor        = 0x1;
            eGameMode      gameMode         = eGameMode.Setup;

            // drawing
            Sequential drawHandler = () =>
            {
                try
                {
                    switch (gameMode)
                    {
                    case eGameMode.Setup:
                    {
                        GameEngine.Setup(
                            controlsState,
                            fieldMatrix,
                            head,
                            tail,
                            ref currentDirection,
                            randomValue,
                            ref gameMode,
                            ref baseColor);
                    }
                    break;

                    case eGameMode.Play:
                    {
                        GameEngine.GameIteration(
                            controlsState,
                            fieldMatrix,
                            head,
                            tail,
                            ref currentDirection,
                            randomValue,
                            TXD);
                    }
                    break;

                    default:
                        if (controlsState.keyCode == Drivers.KeypadKeyCode.PWR)
                        {
                            gameMode = eGameMode.Setup;
                        }

                        break;
                    }
                }
                catch (GameCompletedException)
                {
                    FieldMatrix.DrawCross(fieldMatrix, eCellType.GreenCross);
                    gameMode = eGameMode.Completed;
                }
                catch (Exception)
                {
                    FieldMatrix.DrawCross(fieldMatrix, eCellType.RedCross);
                    gameMode = eGameMode.Failed;
                }

                Graphics.DrawFieldMatrix(baseColor, fieldMatrix, out internalDOUT);
            };

            FPGA.Config.OnTimer(TimeSpan.FromMilliseconds(400), drawHandler);
        }
Beispiel #18
0
 public Cell(int x, int y)
 {
     _coordinateX = x;
     _coordinateY = y;
     _cellType    = eCellType.Empty;
 }
Beispiel #19
0
 public void SetCellType(eCellType cellType)
 {
     _cellType = cellType;
 }
Beispiel #20
0
 public Cell(int x, int y, eCellType cellType)
 {
     _coordinateX = x;
     _coordinateY = y;
     _cellType    = cellType;
 }