コード例 #1
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        public Form1()
        {
            InitializeComponent();
            graphic = CreateGraphics();


            Thread initSerialPort = new Thread(() => InitSerialport());

            initSerialPort.Start();

            tiles = new ImageList();
            AddImagesToList();

            mazeList = new List <ArrayHelper>();
            autobots = new List <Autobot>();

            optimus   = new Autobot("Optimus", Orientation.NORTH);
            bumblebee = new Autobot("Bumblebee", Orientation.NORTH);

            autobots.Add(optimus);
            autobots.Add(bumblebee);

            xOptimus   = 237;
            yOptimus   = 287;
            xBumblebee = 737;
            yBumblebee = 287;

            centerX = 475;
            centerY = 275;

            isMerged = false;

            lastMessage = getTimestamp();
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        /// <summary>
        /// Tell robot which way to go
        /// </summary>
        private void SendData(string data, Autobot robot)
        {
            string message;
            int    prevTs = 0;

            //getTimestamp() - lastMessage < 5
            while (!messageConfirmed)
            {
                // wait
                if (getTimestamp() != prevTs)
                {
                    Console.WriteLine(getTimestamp() + " Waiting..");
                    prevTs = getTimestamp();
                }
            }

            lastMessage      = getTimestamp();
            messageConfirmed = false;

            switch (robot.Name)
            {
            case "Optimus":
                message = "1" + data;
                serialport.Write(message);
                Console.WriteLine(lastMessage + " DataSend: " + message);
                break;

            case "Bumblebee":
                message = "2" + data;
                serialport.Write(message);
                Console.WriteLine(lastMessage + " DataSend: " + message);
                break;
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        /// <summary>
        /// Adds tile to the robot-specific array
        /// </summary>
        /// <param name="robot">The robot</param>
        /// <param name="tile">The tile</param>
        private void UpdateRobotArray(Autobot robot, ArrayHelper tile, string data)
        {
            if (robot.TileArray.Count > 0)
            {
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    robot.Y--;
                    break;

                case Orientation.EAST:
                    robot.X++;
                    break;

                case Orientation.SOUTH:
                    robot.Y++;
                    break;

                case Orientation.WEST:
                    robot.X--;
                    break;
                }
            }

            tile.X = robot.X;
            tile.Y = robot.Y;

            if (isMerged)
            {
                //set deltaX and deltaY
                switch (robot.Name)
                {
                case "Optimus":
                    tile.DeltaX = tile.X - optimusTile.DeltaX;
                    tile.DeltaY = tile.Y - optimusTile.DeltaY;
                    break;

                case "Bumblebee":
                    tile.DeltaX = tile.X - bumbleBTile.DeltaX;
                    tile.DeltaY = tile.Y - bumbleBTile.DeltaY;
                    break;
                }
            }

            robot.AddToArray(tile);
            DrawTile(robot, tile);

            if (!isMerged)
            {
                CompareMaze(robot);
                if (!isMerged)
                {
                    addMessageToSend("09", robot);
                }
            }
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        /// <summary>
        /// Draws the tiles
        /// </summary>
        private void DrawTile(Autobot robot, ArrayHelper helper)
        {
            int helperOffsetX = helper.X * 50;
            int helperOffsetY = helper.Y * 50;

            if (!isMerged)
            {
                switch (robot.Name)
                {
                case "Optimus":
                    graphic.DrawImage(helper.TileImage, xOptimus + helperOffsetX, yOptimus + helperOffsetY);
                    break;

                case "Bumblebee":
                    graphic.DrawImage(helper.TileImage, xBumblebee + helperOffsetX, yBumblebee + helperOffsetY);
                    break;
                }
            }
            else
            {
                graphic.DrawImage(helper.TileImage, centerX + helperOffsetX, centerY + helperOffsetY);

                switch (robot.Name)
                {
                case "Optimus":
                    graphic.DrawImage(tiles.Images[6], centerX + helperOffsetX, centerY + helperOffsetY);
                    break;

                case "Bumblebee":
                    graphic.DrawImage(tiles.Images[7], centerX + helperOffsetX, centerY + helperOffsetY);
                    break;
                }

                ArrayHelper prev = robot.TileArray[robot.TileArray.Count - 2];
                graphic.DrawImage(prev.TileImage, centerX + prev.DeltaX * 50, centerY + prev.DeltaY * 50);

                ArrayHelper temp = mazeList.Where(t => t.DeltaX == helper.DeltaX && t.DeltaY == helper.DeltaY).FirstOrDefault();

                if (temp == null)
                {
                    mazeList.Add(helper);
                }

                FindLooseEnds();
            }
        }
コード例 #5
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        private void addMessageToSend(string data, Autobot robot)
        {
            Thread t = new Thread(() => SendData(data, robot));

            t.Start();
        }
コード例 #6
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        /// <summary>
        /// Compares the maze of both robots to check for overlap
        /// </summary>
        private void CompareMaze(Autobot robot)
        {
            if (optimus.TileArray.Count != 0)
            {
                optimusTile = optimus.TileArray.Last();
                ArrayHelper optimusEast  = (ArrayHelper)optimus.TileArray.Where(o => o.X == optimusTile.X + 1 && o.Y == optimusTile.Y).FirstOrDefault();
                ArrayHelper optimusSouth = (ArrayHelper)optimus.TileArray.Where(o => o.Y == optimusTile.Y + 1 && o.X == optimusTile.X).FirstOrDefault();
                ArrayHelper optimusWest  = (ArrayHelper)optimus.TileArray.Where(o => o.X == optimusTile.X - 1 && o.Y == optimusTile.Y).FirstOrDefault();
                ArrayHelper optimusNorth = (ArrayHelper)optimus.TileArray.Where(o => o.Y == optimusTile.Y - 1 && o.X == optimusTile.X).FirstOrDefault();
                int         counter      = 0;

                // Arbitrary amount of tiles that need to have been scanned before the computer starts comparing the two mazes
                if (optimus.TileArray.Count >= 5 && bumblebee.TileArray.Count >= 5)
                {
                    foreach (ArrayHelper bumbleB in bumblebee.TileArray.Where(b => b.TileType.Equals(optimusTile.TileType) && b.TileOrientation.Equals(optimusTile.TileOrientation)))
                    {
                        ArrayHelper bumblebeeEast  = (ArrayHelper)bumblebee.TileArray.Where(b => b.X == bumbleB.X + 1 && b.Y == bumbleB.Y).FirstOrDefault();
                        ArrayHelper bumblebeeSouth = (ArrayHelper)bumblebee.TileArray.Where(b => b.Y == bumbleB.Y + 1 && b.X == bumbleB.X).FirstOrDefault();
                        ArrayHelper bumblebeeWest  = (ArrayHelper)bumblebee.TileArray.Where(b => b.X == bumbleB.X - 1 && b.Y == bumbleB.Y).FirstOrDefault();
                        ArrayHelper bumblebeeNorth = (ArrayHelper)bumblebee.TileArray.Where(b => b.Y == bumbleB.Y - 1 && b.X == bumbleB.X).FirstOrDefault();

                        // Is ugly, will remain ugly until an alternative is found
                        if (optimusEast != null && bumblebeeEast != null)
                        {
                            if (optimusEast.TileType.Equals(bumblebeeEast.TileType) && optimusEast.TileOrientation.Equals(bumblebeeEast.TileOrientation))
                            {
                                counter++;
                            }
                        }

                        if (optimusSouth != null && bumblebeeSouth != null)
                        {
                            if (optimusSouth.TileType.Equals(bumblebeeSouth.TileType) && optimusSouth.TileOrientation.Equals(bumblebeeSouth.TileOrientation))
                            {
                                counter++;
                            }
                        }


                        if (optimusWest != null && bumblebeeWest != null)
                        {
                            if (optimusWest.TileType.Equals(bumblebeeWest.TileType) && optimusWest.TileOrientation.Equals(bumblebeeWest.TileOrientation))
                            {
                                counter++;
                            }
                        }

                        if (optimusNorth != null && bumblebeeNorth != null)
                        {
                            if (optimusNorth.TileType.Equals(bumblebeeNorth.TileType) && optimusNorth.TileOrientation.Equals(bumblebeeNorth.TileOrientation))
                            {
                                counter++;
                            }
                        }

                        if (counter >= 3)
                        {
                            // If 3 or more tiles match, go ahead and merge the two mazes.
                            bumbleBTile = bumbleB;
                            MergeMaze();
                            break;
                        }
                        else
                        {
                            counter = 0;
                        }
                    }
                }
            }
        }
コード例 #7
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        /// <summary>
        /// Adjusts a robot's orientation.
        /// </summary>
        /// <param name="robot">The Robot</param>
        /// <param name="data">The data</param>
        private void UpdateRobotOrientation(Autobot robot, string data)
        {
            switch (data[2])
            {
            case '1':
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    robot.Orientation = Orientation.EAST;
                    break;

                case Orientation.EAST:
                    robot.Orientation = Orientation.SOUTH;
                    break;

                case Orientation.SOUTH:
                    robot.Orientation = Orientation.WEST;
                    break;

                case Orientation.WEST:
                    robot.Orientation = Orientation.NORTH;
                    break;
                }
                break;

            case '2':
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    robot.Orientation = Orientation.SOUTH;
                    break;

                case Orientation.EAST:
                    robot.Orientation = Orientation.WEST;
                    break;

                case Orientation.SOUTH:
                    robot.Orientation = Orientation.NORTH;
                    break;

                case Orientation.WEST:
                    robot.Orientation = Orientation.EAST;
                    break;
                }
                break;

            case '3':
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    robot.Orientation = Orientation.WEST;
                    break;

                case Orientation.EAST:
                    robot.Orientation = Orientation.NORTH;
                    break;

                case Orientation.SOUTH:
                    robot.Orientation = Orientation.EAST;
                    break;

                case Orientation.WEST:
                    robot.Orientation = Orientation.SOUTH;
                    break;
                }
                break;

            default:
                break;
            }
        }
コード例 #8
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        /// <summary>
        /// Do things with robots and tiles
        /// </summary>
        /// <param name="robot">The robot that sent the data</param>
        /// <param name="data">The data</param>
        private void DoRobotyStuff(Autobot robot, string data)
        {
            switch (data[1])
            {
            case '0':
                ArrayHelper tileStraight = new ArrayHelper(tiles.Images[1], TileType.STRAIGHT);
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    tileStraight.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    //Orientation is relative to the default position of the tile as depicted in the images
                    tileStraight.TileOrientation = Orientation.EAST;
                    tileStraight.AddDirections(new List <string> {
                        "up", "down"
                    });
                    break;

                case Orientation.EAST:
                    tileStraight.TileOrientation = Orientation.NORTH;
                    tileStraight.AddDirections(new List <string> {
                        "left", "right"
                    });
                    break;

                case Orientation.SOUTH:
                    tileStraight.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    tileStraight.TileOrientation = Orientation.EAST;
                    tileStraight.AddDirections(new List <string> {
                        "up", "down"
                    });
                    break;

                case Orientation.WEST:
                    tileStraight.TileOrientation = Orientation.NORTH;
                    tileStraight.AddDirections(new List <string> {
                        "left", "right"
                    });
                    break;
                }
                UpdateRobotArray(robot, tileStraight, data);
                UpdateRobotOrientation(robot, data);
                break;

            case '1':
                ArrayHelper tileTLeft = new ArrayHelper(tiles.Images[3], TileType.TTILE);
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    tileTLeft.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    tileTLeft.TileOrientation = Orientation.EAST;
                    tileTLeft.AddDirections(new List <string> {
                        "up", "left", "down"
                    });
                    break;

                case Orientation.EAST:
                    tileTLeft.TileImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    tileTLeft.TileOrientation = Orientation.SOUTH;
                    tileTLeft.AddDirections(new List <string> {
                        "left", "right", "up"
                    });
                    break;

                case Orientation.SOUTH:
                    tileTLeft.TileImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    tileTLeft.TileOrientation = Orientation.WEST;
                    tileTLeft.AddDirections(new List <string> {
                        "up", "down", "right"
                    });
                    break;

                case Orientation.WEST:
                    tileTLeft.TileOrientation = Orientation.NORTH;
                    tileTLeft.AddDirections(new List <string> {
                        "left", "right", "down"
                    });
                    break;
                }
                UpdateRobotArray(robot, tileTLeft, data);
                UpdateRobotOrientation(robot, data);
                break;

            case '2':
                ArrayHelper tileTRight = new ArrayHelper(tiles.Images[3], TileType.TTILE);
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    tileTRight.TileImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    tileTRight.TileOrientation = Orientation.WEST;
                    tileTRight.AddDirections(new List <string> {
                        "up", "right", "down"
                    });
                    break;

                case Orientation.EAST:
                    tileTRight.TileOrientation = Orientation.NORTH;
                    tileTRight.AddDirections(new List <string> {
                        "left", "right", "down"
                    });
                    break;

                case Orientation.SOUTH:
                    tileTRight.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    tileTRight.TileOrientation = Orientation.EAST;
                    tileTRight.AddDirections(new List <string> {
                        "up", "left", "down"
                    });
                    break;

                case Orientation.WEST:
                    tileTRight.TileImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    tileTRight.TileOrientation = Orientation.SOUTH;
                    tileTRight.AddDirections(new List <string> {
                        "left", "right", "up"
                    });
                    break;
                }
                UpdateRobotArray(robot, tileTRight, data);
                UpdateRobotOrientation(robot, data);
                break;

            case '3':
                ArrayHelper tileX = new ArrayHelper(tiles.Images[4], TileType.XTILE);
                tileX.TileOrientation = Orientation.NORTH;
                tileX.AddDirections(new List <string> {
                    "left", "right", "down", "up"
                });
                UpdateRobotArray(robot, tileX, data);
                UpdateRobotOrientation(robot, data);
                break;

            case '4':
                ArrayHelper tileCornerLeft = new ArrayHelper(tiles.Images[2], TileType.CORNER);
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    tileCornerLeft.TileImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    tileCornerLeft.TileOrientation = Orientation.SOUTH;
                    tileCornerLeft.AddDirections(new List <string> {
                        "left", "down"
                    });
                    break;

                case Orientation.EAST:
                    tileCornerLeft.TileImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    tileCornerLeft.TileOrientation = Orientation.WEST;
                    tileCornerLeft.AddDirections(new List <string> {
                        "left", "up"
                    });
                    break;

                case Orientation.SOUTH:
                    tileCornerLeft.TileOrientation = Orientation.NORTH;
                    tileCornerLeft.AddDirections(new List <string> {
                        "up", "right"
                    });
                    break;

                case Orientation.WEST:
                    tileCornerLeft.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    tileCornerLeft.TileOrientation = Orientation.EAST;
                    tileCornerLeft.AddDirections(new List <string> {
                        "right", "down"
                    });
                    break;
                }
                UpdateRobotArray(robot, tileCornerLeft, data);
                UpdateRobotOrientation(robot, data);
                break;

            case '5':
                ArrayHelper tileCornerRight = new ArrayHelper(tiles.Images[2], TileType.CORNER);
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    tileCornerRight.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    tileCornerRight.TileOrientation = Orientation.EAST;
                    tileCornerRight.AddDirections(new List <string> {
                        "right", "down"
                    });
                    break;

                case Orientation.EAST:
                    tileCornerRight.TileImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    tileCornerRight.TileOrientation = Orientation.SOUTH;
                    tileCornerRight.AddDirections(new List <string> {
                        "left", "down"
                    });
                    break;

                case Orientation.SOUTH:
                    tileCornerRight.TileImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    tileCornerRight.TileOrientation = Orientation.WEST;
                    tileCornerRight.AddDirections(new List <string> {
                        "left", "up"
                    });
                    break;

                case Orientation.WEST:
                    tileCornerRight.TileOrientation = Orientation.NORTH;
                    tileCornerRight.AddDirections(new List <string> {
                        "up", "right"
                    });
                    break;
                }
                UpdateRobotArray(robot, tileCornerRight, data);
                UpdateRobotOrientation(robot, data);
                break;

            case '6':
                ArrayHelper tileT = new ArrayHelper(tiles.Images[3], TileType.TTILE);
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    tileT.TileOrientation = Orientation.NORTH;
                    tileT.AddDirections(new List <string> {
                        "left", "right", "down"
                    });
                    break;

                case Orientation.EAST:
                    tileT.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    tileT.TileOrientation = Orientation.EAST;
                    tileT.AddDirections(new List <string> {
                        "left", "up", "down"
                    });
                    break;

                case Orientation.SOUTH:
                    tileT.TileImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    tileT.TileOrientation = Orientation.SOUTH;
                    tileT.AddDirections(new List <string> {
                        "left", "right", "up"
                    });
                    break;

                case Orientation.WEST:
                    tileT.TileImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    tileT.TileOrientation = Orientation.WEST;
                    tileT.AddDirections(new List <string> {
                        "up", "right", "down"
                    });
                    break;
                }
                UpdateRobotArray(robot, tileT, data);
                UpdateRobotOrientation(robot, data);
                break;

            case '7':
                ArrayHelper tileDeadEnd = new ArrayHelper(tiles.Images[0], TileType.DEADEND);
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    tileDeadEnd.TileOrientation = Orientation.NORTH;
                    tileDeadEnd.AddDirections(new List <string> {
                        "down"
                    });
                    break;

                case Orientation.EAST:
                    tileDeadEnd.TileImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    tileDeadEnd.TileOrientation = Orientation.EAST;
                    tileDeadEnd.AddDirections(new List <string> {
                        "left"
                    });
                    break;

                case Orientation.SOUTH:
                    tileDeadEnd.TileImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    tileDeadEnd.TileOrientation = Orientation.SOUTH;
                    tileDeadEnd.AddDirections(new List <string> {
                        "up"
                    });
                    break;

                case Orientation.WEST:
                    tileDeadEnd.TileImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    tileDeadEnd.TileOrientation = Orientation.WEST;
                    tileDeadEnd.AddDirections(new List <string> {
                        "right"
                    });
                    break;
                }
                UpdateRobotArray(robot, tileDeadEnd, data);
                UpdateRobotOrientation(robot, data);
                break;
            }
        }
コード例 #9
0
ファイル: Form1.cs プロジェクト: Jeroeny/Arduino
        private int getTurnDirection(Orientation targetOrientation, Autobot robot)
        {
            int direction = 0;

            switch (targetOrientation)
            {
            case Orientation.NORTH:
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    direction = 0;
                    break;

                case Orientation.EAST:
                    direction = 1;
                    break;

                case Orientation.SOUTH:
                    direction = 2;
                    break;

                case Orientation.WEST:
                    direction = 3;
                    break;
                }
                break;

            case Orientation.EAST:
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    direction = 3;
                    break;

                case Orientation.EAST:
                    direction = 0;
                    break;

                case Orientation.SOUTH:
                    direction = 1;
                    break;

                case Orientation.WEST:
                    direction = 2;
                    break;
                }
                break;

            case Orientation.SOUTH:
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    direction = 2;
                    break;

                case Orientation.EAST:
                    direction = 3;
                    break;

                case Orientation.SOUTH:
                    direction = 0;
                    break;

                case Orientation.WEST:
                    direction = 1;
                    break;
                }
                break;

            case Orientation.WEST:
                switch (robot.Orientation)
                {
                case Orientation.NORTH:
                    direction = 1;
                    break;

                case Orientation.EAST:
                    direction = 2;
                    break;

                case Orientation.SOUTH:
                    direction = 3;
                    break;

                case Orientation.WEST:
                    direction = 0;
                    break;
                }
                break;
            }

            return(direction);
        }