コード例 #1
0
        public static void Setup(
            GameControlsState controlsState,
            eCellType[] fieldMatrix,
            Position head,
            Position tail,
            ref eDirectionType currentDirection,
            int randomValue,
            ref eGameMode gameMode,
            ref byte baseColor)
        {
            // seed initial state
            FieldMatrix.Seed(fieldMatrix, head, tail);
            currentDirection = eDirectionType.None;

            switch (controlsState.keyCode)
            {
            case KeypadKeyCode.GO:
                FieldMatrix.PlaceNextPiece(fieldMatrix, randomValue);
                gameMode = eGameMode.Play;
                break;

            case KeypadKeyCode.D2:
                baseColor = (byte)(baseColor < 10 ? baseColor + 1 : baseColor);
                break;

            case KeypadKeyCode.D8:
                baseColor = (byte)(baseColor > 1 ? baseColor - 1 : baseColor);
                break;
            }
        }
コード例 #2
0
 public static void JoystickPositionToDirection(
     int xAxis,
     int yAxis,
     ref eDirectionType direction)
 {
     if (xAxis < 20000)
     {
         direction = eDirectionType.Up;
     }
     else if (xAxis > 40000)
     {
         direction = eDirectionType.Down;
     }
     else if (yAxis < 20000)
     {
         direction = eDirectionType.Right;
     }
     else if (yAxis > 40000)
     {
         direction = eDirectionType.Left;
     }
     else
     {
         direction = eDirectionType.None;
     }
 }
コード例 #3
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");
        }
コード例 #4
0
        public static void ApplyDirection(
            Position current,
            Position next,
            eDirectionType direction)
        {
            FPGA.Runtime.DeepCopy(next, current);

            switch (direction)
            {
            case eDirectionType.Up:
                next.row--;
                break;

            case eDirectionType.Down:
                next.row++;
                break;

            case eDirectionType.Left:
                next.col--;
                break;

            case eDirectionType.Right:
                next.col++;
                break;
            }
        }
コード例 #5
0
        public static void GameIteration(
            GameControlsState controlsState,
            eCellType[] fieldMatrix,
            Position head,
            Position tail,
            ref eDirectionType currentDirection,
            int randomValue,
            FPGA.Signal <bool> TXD)
        {
            eDirectionType nextDirectionFromKeypad   = eDirectionType.None;
            eDirectionType nextDirectionFromJoystick = eDirectionType.None;
            eDirectionType nextDirection             = eDirectionType.None;

            Lookups.KeyCodeToDirection(
                controlsState.keyCode,
                ref nextDirectionFromKeypad);

            Lookups.JoystickPositionToDirection(
                controlsState.adcChannel1,
                controlsState.adcChannel2,
                ref nextDirectionFromJoystick);

            bool isReverse = false;

            Lookups.IsReverse(currentDirection, nextDirection, ref isReverse);

            // TODO: variable declaration from conditional expression;
            nextDirection = isReverse
                            ? currentDirection
                            : nextDirectionFromKeypad != eDirectionType.None
                                    ? nextDirectionFromKeypad
                                        : nextDirectionFromJoystick != eDirectionType.None
                                            ? nextDirectionFromJoystick
                                            : currentDirection;

            Diagnostics.ReportState(
                controlsState,
                nextDirectionFromKeypad,
                nextDirectionFromJoystick,
                nextDirection,
                TXD);

            bool expanded = false;

            ApplyChange(
                fieldMatrix,
                head,
                tail,
                currentDirection, nextDirection,
                out expanded);

            if (expanded)
            {
                FieldMatrix.PlaceNextPiece(fieldMatrix, randomValue);
            }

            currentDirection = nextDirection;
        }
コード例 #6
0
 public PlayerDetails(eSymboleType i_Symbole, eSymboleType i_KingSymbole, string i_PlayerName)
 {
     r_PlayerName    = i_PlayerName;
     r_PlayerSymbole = i_Symbole;
     r_KingSymbole   = i_KingSymbole;
     initializeScores();
     r_Direction       = getPlayerDirection();
     r_ListOfGamePawns = new List <CheckersCell <eSymboleType> >();
 }
コード例 #7
0
        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);
        }
コード例 #8
0
 public static void IsReverse(
     eDirectionType currentDirection,
     eDirectionType nextDirection,
     ref bool shouldReverse)
 {
     shouldReverse =
         currentDirection == eDirectionType.Up && nextDirection == eDirectionType.Down ||
         currentDirection == eDirectionType.Down && nextDirection == eDirectionType.Up ||
         currentDirection == eDirectionType.Left && nextDirection == eDirectionType.Right ||
         currentDirection == eDirectionType.Right && nextDirection == eDirectionType.Left;
 }
コード例 #9
0
        public static void KeyCodeToDirection(
            KeypadKeyCode keyCode,
            ref eDirectionType direction)
        {
            var lookup = new FPGA.Collections.ReadOnlyDictionary <KeypadKeyCode, eDirectionType>()
            {
                { KeypadKeyCode.D2, eDirectionType.Up },
                { KeypadKeyCode.D8, eDirectionType.Down },
                { KeypadKeyCode.D4, eDirectionType.Left },
                { KeypadKeyCode.D6, eDirectionType.Right }
            };

            direction = lookup[keyCode];
        }
コード例 #10
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];
        }
コード例 #11
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");
        }
コード例 #12
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");
        }
コード例 #13
0
        public static void ReportState(
            GameControlsState controlsState,
            eDirectionType nextDirectionFromKeypad,
            eDirectionType nextDirectionFromJoystick,
            eDirectionType nextDirection,
            FPGA.Signal <bool> TXD
            )
        {
            SnakeDBG dbg = new SnakeDBG();

            dbg.C1 = controlsState.adcChannel1;
            dbg.C2 = controlsState.adcChannel2;

            dbg.KD = nextDirectionFromKeypad;
            dbg.JD = nextDirectionFromJoystick;
            dbg.ND = nextDirection;

            JSON.SerializeToUART(ref dbg, TXD);
        }
コード例 #14
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));
        }
コード例 #15
0
    /// <summary>
    /// 移动
    /// </summary>
    /// <param name="distance">移动距离</param>
    /// <param name="time">时间</param>
    /// <param name="direction">方向,x,y,z轴</param>
    public void Move(float distance, float time, eDirectionType direction)
    {
        Vector3 deltaPos = Vector3.zero;//移动的距离

        if (direction == eDirectionType.x)
        {
            deltaPos.x = 1;
        }
        else if (direction == eDirectionType.y)
        {
            deltaPos.y = 1;
        }
        else
        {
            deltaPos.z = 1;
        }
        deltaPos = deltaPos * distance;
        //移动的目标位置
        Vector3 destPos = transform.localPosition + deltaPos;

        iTween.MoveTo(gameObject, iTween.Hash("position", destPos, "time", time, "islocal", true, "easetype", iTween.EaseType.linear));
    }
コード例 #16
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");
        }
コード例 #17
0
 /// <summary>
 /// 获取对应方向的数组下标
 /// </summary>
 /// <param name="dir"></param>
 /// <param name="tempColumn"></param>
 /// <param name="tempRow"></param>
 private void GetRowAndColumnIndex(eDirectionType dir, ref int tempColumn, ref int tempRow)
 {
     switch (dir)
     {
         case eDirectionType.Up:
             tempRow = -1;
             break;
         case eDirectionType.Down:
             tempRow = 1;
             break;
         case eDirectionType.Left:
             tempColumn = -1;
             break;
         case eDirectionType.Right:
             tempColumn = 1;
             break;
         default: break;
     }
 }
コード例 #18
0
 /// <summary>
 /// 获取随机移动方向
 /// </summary>
 public static eDirectionType[] GetRandomDirection()
 {
     eDirectionType[] direction = new eDirectionType[] { eDirectionType.Up, eDirectionType.Down, eDirectionType.Left, eDirectionType.Right };
     return direction;
 }
コード例 #19
0
        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);
        }
コード例 #20
0
ファイル: GoBangBLL.cs プロジェクト: nik2011/NikTest
        /// <summary>
        ///按方向计算是否赢
        /// </summary>
        /// <param name="list"></param>
        /// <param name="currentEnt"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static bool CatulateByDirection(List <GoBangEntity> list, GoBangEntity currentEnt, eDirectionType type)
        {
            bool result = false;
            int  count  = 0;
            int  beginX = currentEnt.PositionX - 5;
            int  beginY = currentEnt.PositionY - 5;
            int  cx     = 0;
            int  cy     = 0;

            for (int i = 0; i < 11; i++)
            {
                if (type == eDirectionType.横轴)
                {
                    cx = beginX + i;
                    cy = currentEnt.PositionY;
                }
                else if (type == eDirectionType.斜45 || type == eDirectionType.斜135)
                {
                    cx = beginX + i;
                    cy = beginY + i;
                }
                else if (type == eDirectionType.纵轴)
                {
                    cx = currentEnt.PositionX;
                    cy = beginY + i;
                }

                GoBangEntity selectEnt = list.Where(x => x.PositionX == cx && x.PositionY == cy).FirstOrDefault();
                if (selectEnt.Status == currentEnt.Status)
                {
                    count++;
                    if (count >= 5)
                    {
                        result = true;
                        break;
                    }
                }
                else
                {
                    count = 0;
                    continue;
                }
            }
            return(result);
        }
コード例 #21
0
    /// <summary>
    /// 旋转主角
    /// </summary>
    /// <param name="direction"></param>
    /// <param name="player"></param>
    private void RotatePlayer(eDirectionType direction,Transform player)
    {
        int angle = 0;
        switch(direction)
        {
            case eDirectionType.Left:
                angle = -135;
                break;
            case eDirectionType.Right:
                angle = 45;
                break;
            case eDirectionType.Up:
                angle = -45;
                break;
            case eDirectionType.Down:
                angle = 135;
                break;
        }

        player.rotation = Quaternion.Euler(new Vector3(player.eulerAngles.x, angle, 0));
    }