Example #1
0
        /// <summary>
        /// check and make all possible moves from current coordinate
        /// </summary>
        /// <param name="newCoordinate">current coordinate to iterate</param>
        void makemoves(coordinate newCoordinate)
        {
            int X, Y, Iteration;

            // initialize position of current coordinate
            X         = newCoordinate.x;
            Y         = newCoordinate.y;
            Iteration = newCoordinate.iteration;

            // remove processing coordinate from pending list
            pendingList.Remove(newCoordinate);

            // Move 1 (UP -> LEFT)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X - 1 >= 0 && X - 1 < length) && (Y - 2 >= 0 && Y - 2 < width)) && (tmpBoard[X - 1, Y - 2] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X - 1, Y - 2]    = Iteration;
                tmpBoard[X - 1, Y - 2] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X - 1, y = Y - 2, iteration = Iteration + 1
                });
            }

            // Move 2 (UP -> RIGHT)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X + 1 >= 0 && X + 1 < length) && (Y - 2 >= 0 && Y - 2 < width)) && (tmpBoard[X + 1, Y - 2] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X + 1, Y - 2]    = Iteration;
                tmpBoard[X + 1, Y - 2] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X + 1, y = Y - 2, iteration = Iteration + 1
                });
            }

            // Move 3 (Right -> UP)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X + 2 >= 0 && X + 2 < length) && (Y - 1 >= 0 && Y - 1 < width)) && (tmpBoard[X + 2, Y - 1] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X + 2, Y - 1]    = Iteration;
                tmpBoard[X + 2, Y - 1] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X + 2, y = Y - 1, iteration = Iteration + 1
                });
            }

            // Move 4 (RIGHT -> DOWN)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X + 2 >= 0 && X + 2 < length) && (Y + 1 >= 0 && Y + 1 < width)) && (tmpBoard[X + 2, Y + 1] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X + 2, Y + 1]    = Iteration;
                tmpBoard[X + 2, Y + 1] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X + 2, y = Y + 1, iteration = Iteration + 1
                });
            }

            // Move 5 (DOWN -> RIGHT)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X + 1 >= 0 && X + 1 < length) && (Y + 2 >= 0 && Y + 2 < width)) && (tmpBoard[X + 1, Y + 2] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X + 1, Y + 2]    = Iteration;
                tmpBoard[X + 1, Y + 2] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X + 1, y = Y + 2, iteration = Iteration + 1
                });
            }

            // Move 6 (DOWN -> LEFT)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X - 1 >= 0 && X - 1 < length) && (Y + 2 >= 0 && Y + 2 < width)) && (tmpBoard[X - 1, Y + 2] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X - 1, Y + 2]    = Iteration;
                tmpBoard[X - 1, Y + 2] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X - 1, y = Y + 2, iteration = Iteration + 1
                });
            }

            // Move 7 (LEFT -> DOWN)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X - 2 >= 0 && X - 2 < length) && (Y + 1 >= 0 && Y + 1 < width)) && (tmpBoard[X - 2, Y + 1] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X - 2, Y + 1]    = Iteration;
                tmpBoard[X - 2, Y + 1] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X - 2, y = Y + 1, iteration = Iteration + 1
                });
            }

            // Move 8 (LEFT -> UP)
            // validate if move is valid from Knight's current position, and also Knight is reaching on that co-ordinate first time
            if (((X - 2 >= 0 && X - 2 < length) && (Y - 1 >= 0 && Y - 1 < width)) && (tmpBoard[X - 2, Y - 1] == false))
            {
                // if yes, then add number of iteration (number of moves) Knight took to reach this co-ordinate
                board[X - 2, Y - 1]    = Iteration;
                tmpBoard[X - 2, Y - 1] = true;

                // add new co-ordinate in pending list to make future moves from
                pendingList.Add(new coordinate {
                    x = X - 2, y = Y - 1, iteration = Iteration + 1
                });
            }
        }
 GetControl(coordinate, sizeMap);
Example #3
0
File: Form1.cs Project: xnzaa/ship
 //计算两个向量的夹角
 public static double get_angle(coordinate a, coordinate b)
 {
     double angle = Math.Acos((a.x * b.x + a.y * b.y) / (Math.Sqrt(a.x * a.x + a.y * a.y) * Math.Sqrt(b.x * b.x + b.y * b.y))) / 3.1415926 * 180;
     if (angle < 0.0001)
         angle = 0;
     return angle;
 }
Example #4
0
 void Start()
 {
     objName = gameObject.name;
     obj     = GameObject.Find(objName);
     co      = StaticMethodLib.stringTOcoordinate(objName);
 }
Example #5
0
 (int, int)playerVisibility = FindPlayerAndEnemies(coordinate, NPC.Player);
Example #6
0
File: Form1.cs Project: xnzaa/ship
 //计算速度初始化函数 必须调用一次
 public static void init(coordinate[] axis)
 {
     coordinate temp;
     temp.x = 1;
     temp.y = 0;
     axis[0] = temp;		//(1,0)
     temp.x = 0;
     temp.y = 1;
     axis[1] = temp;		//(0,1)
     temp.x = -1;
     temp.y = 0;
     axis[2] = temp;		//(-1,0)
     temp.x = 0;
     temp.y = -1;
     axis[3] = temp;		//(0,-1)
 }
Example #7
0
 double euclideanDistance(coordinate u, coordinate v)
 {
     return(Mathf.Sqrt(Mathf.Abs(Mathf.Pow(u.i - v.i, 2) + Mathf.Pow(u.j - v.j, 2))));
 }
Example #8
0
File: Form1.cs Project: xnzaa/ship
        //主串口字符串处理函数
        private void main_sp_receive(string str)
        {
            try
            {
                if (is_first_receive)       //第一次接收数据
                {
                    is_first_receive = false;
                    timer1.Start();
                    timer2.Start();
                    receive_time.Start();
                    label7.Text      = "已连接";
                    label7.BackColor = Color.White;
                }
                else                        //不是第一次接收
                {
                    //显示接收到的数据
                    label12.Text = str;

                    //接收到的数据保存在文本中
                    swr.WriteLine(str);

                    //处理含有$GPRMC的GPS数据
                    if (str.Contains("$GPRMC"))
                    {
                        string[] str_gps = str.Split(',');

                        //纬度转换并计算正确值
                        double latitude = Convert.ToDouble(str_gps[3]) / 100 - latitude_check;

                        //经度转换并计算正确值
                        double longitude = Convert.ToDouble(str_gps[5]) / 100 - longitude_check;

                        /***************************计算速度和航向********没事不要改***************************/
                        latlogtime temp;
                        temp.latlog.y = latitude;
                        temp.latlog.x = longitude;
                        temp.time     = time_passed;
                        latlogtime_queue.Enqueue(temp);
                        if (latlogtime_queue.Count == 2)
                        {
                            latlogtime temp1 = (latlogtime)latlogtime_queue.Dequeue();
                            latlogtime temp2 = (latlogtime)latlogtime_queue.Dequeue();
                            /***采用数学方法计算球面两点间距离****/
                            point  A        = temp1.latlog;
                            point  B        = temp2.latlog;
                            double distance = Math.Sqrt(Math.Pow((B.y - A.y) * 111700, 2) + Math.Pow((6371000 * 2 * Math.PI * Math.Cos(A.y * 180 / Math.PI) * (B.x - A.x) / 360), 2));
                            /*************************************/
                            double time  = Math.Abs(temp2.time - temp1.time) / 1000.0;      //毫秒转化为秒
                            double speed = distance / time;
                            label33.Text = speed.ToString().Substring(0, 5);
                            coordinate temp_coor = get_direction(temp1.latlog, temp2.latlog);
                            double[]   degree    = new double[4];
                            for (int i = 0; i < 4; ++i)
                            {
                                degree[i] = get_angle(temp_coor, axis[i]);
                            }
                            if (degree[0] <= 90 && degree[1] <= 90)
                            {
                                label35.Text = "北偏东" + degree[1].ToString().Substring(0, 5) + "°";
                            }
                            else if (degree[1] <= 90 && degree[2] <= 90)
                            {
                                label35.Text = "北偏西" + degree[1].ToString().Substring(0, 5) + "°";
                            }
                            else if (degree[2] <= 90 && degree[3] <= 90)
                            {
                                label35.Text = "南偏西" + degree[3].ToString().Substring(0, 5) + "°";
                            }
                            else if (degree[3] <= 90 && degree[4] <= 90)
                            {
                                label35.Text = "南偏东" + degree[3].ToString().Substring(0, 5) + "°";
                            }
                        }
                        /**************************************************************************************/

                        //显示当前坐标
                        label2.Text = latitude.ToString();
                        label3.Text = longitude.ToString();

                        //此处有一个未解决的疑问:每次调用InvokeScript之后,会交换objArray的值
                        //故每次均需重新构造objArray

                        //防止点数量过于密集,每接收三个丢弃一个point_counter_for_abandon % 3 == 0
                        if (point_counter_for_abandon % 3 == 0)
                        {
                            point_counter_for_abandon = 0;
                            objArray[0] = (object)latitude;
                            objArray[1] = (object)longitude;
                            webBrowser1.Document.InvokeScript("mark", objArray);
                        }

                        //船讯网显示船只信息函数
                        if (radioButton3.Checked)
                        {
                            objArray[0] = (object)latitude;
                            objArray[1] = (object)longitude;
                            webBrowser1.Document.InvokeScript("show_ships", objArray);
                        }

                        //地图居中
                        if (checkbox1.Checked)
                        {
                            objArray[0] = (object)latitude;
                            objArray[1] = (object)longitude;
                            webBrowser1.Document.InvokeScript("center", objArray);
                        }
                    }

                    //处理温湿度距离数据
                    if (str.Contains("#TEMP"))
                    {
                        //#TEMP22.7RH46.5endDIS65.68

                        /*
                         * th
                         * {string[6]}
                         *  [0]: "#TEM"
                         *  [1]: "22.7"
                         *  [2]: ""
                         *  [3]: "46.5"
                         *  [4]: "ndDI"
                         *  [5]: "65.68"
                         */

                        string[] th          = str.Split('P', 'R', 'H', 'e', 'S', '\r');
                        string   humidity    = th[3];           //湿度
                        string   temperature = th[1];           //温度

                        //温度标签
                        label18.Text       = temperature + "摄氏度";
                        progressBar1.Value = Convert.ToInt32(Convert.ToDouble(temperature));

                        //湿度标签
                        label19.Text       = humidity + "%";
                        progressBar2.Value = Convert.ToInt32(Convert.ToDouble(humidity));


                        //红外检测
                        if (str.Contains("!##!"))
                        {
                            label21.Text      = "发现目标";
                            label21.BackColor = Color.Red;
                        }
                        else
                        {
                            label21.Text      = "正常";
                            label21.BackColor = Color.White;
                        }

                        //障碍物距离
                        if (th[5] == "-1")
                        {
                            label9.Text        = "前方无目标";
                            progressBar3.Value = 1200;
                        }
                        else
                        {
                            label9.Text        = Convert.ToDouble(th[5]) / 100 + "m";
                            progressBar3.Value = Convert.ToInt32(Convert.ToDouble(th[5]));
                        }
                    }

                    //TODO: 命令返回值
                    if (str.Contains(":") || str.Contains("!"))
                    {
                        if (str.Contains(":"))
                        {
                            string[] strr = str.Split(':', '$');
                            //order_back(":"+strr[1]);
                        }
                        else if (str.Contains("!"))
                        {
                            string[] strr = str.Split('!', '$');
                            //order_back("!" + strr[1]);
                        }
                    }
                    main_sp.DiscardInBuffer();
                }
            }
            catch
            {
                return;
            }
        }
Example #9
0
 void DrawMark(double value, coordinate coord, int x0, int y0, double angle2, Graphics g)
 {
     int l = 2;
     switch (coord)
     {
         case coordinate.X:
             g.DrawLine(Pens.RoyalBlue, x0 - (int)(Math.Sin(angle2) * value), y0 - (int)(Math.Cos(angle2) * value) - l, x0 - (int)(Math.Sin(angle2) * value), y0 - (int)(Math.Cos(angle2) * value) + l);
             break;
         case coordinate.Y:
             g.DrawLine(Pens.RoyalBlue, x0 + (int)(Math.Sin(angle2) * value), y0 - (int)(Math.Cos(angle2) * value) - l, x0 + (int)(Math.Sin(angle2) * value), y0 - (int)(Math.Cos(angle2) * value) + l);
             break;
         default:
             break;
     }
 }
Example #10
0
    void spawnTreasure()
    {
        List <coordinate> tempList = new List <coordinate>();

        for (int i = 1; i < boardSize - 1; i++)
        {
            for (int j = 1; j < boardSize - 1; j++)
            {
                bool       proximity = true;
                coordinate coord_    = new coordinate(i, j);
                foreach (var coord in treasureCoords)
                {
                    if (euclideanDistance(coord_, coord) <= 4.0f)
                    {
                        proximity = false;
                    }
                    if (euclideanDistance(coord_, spawnCoord) <= 4.0f)
                    {
                        proximity = false;
                    }
                    if (coord_.i == spawnCoord.i && coord_.j == spawnCoord.j)
                    {
                        proximity = false;
                    }
                }

                if (!proximity)
                {
                    continue;
                }
                else
                {
                    int   count = 0;
                    int[] axis  = new int[2] {
                        -1, 1
                    };

                    foreach (var k in axis)
                    {
                        count += board[i + k, j].getIsWall();
                        count += board[i, j + k].getIsWall();
                    }

                    if (count == 3 && board[i, j].getIsWall() == 0)
                    {
                        treasureCoords.Add(new coordinate(i, j));
                    }
                }
            }
        }

        foreach (var coord in treasureCoords)
        {
            foreach (var coord2 in treasureCoords)
            {
                if (!coord.Equals(coord2) && !tempList.Contains(coord) && !tempList.Contains(coord2) && (euclideanDistance(coord, coord2) <= Mathf.Sqrt(18)) && (treasureCoords.Count - tempList.Count) > Mathf.Floor(boardSize / 10f))
                {
                    tempList.Add(coord2);
                }
            }
        }
        while (treasureCoords.Count - tempList.Count > Mathf.Floor(boardSize / 10) + 5)
        {
            int        rnd    = Random.Range(0, treasureCoords.Count);
            coordinate remove = treasureCoords[rnd];
            if (!tempList.Contains(remove))
            {
                tempList.Add(remove);
            }
        }
        foreach (var coord in tempList)
        {
            treasureCoords.Remove(coord);
        }
        foreach (var coord in treasureCoords)
        {
            board[coord.i, (int)coord.j].instantiateTreasure(treasure, coord.i, coord.j);
        }
    }
Example #11
0
 public bool equal(coordinate a)
 {
     return(this.x == a.x && this.y == a.y);
 }
Example #12
0
 Vector3 GetPositionByID(coordinate co)
 {
     return(new Vector3(co.x * ChessPieceScale.x - weight * ChessPieceScale.x * 0.5f + ChessPieceScale.x * 0.5f,
                        height * ChessPieceScale.y * 0.5f - co.y * ChessPieceScale.y - ChessPieceScale.y * 0.5f,
                        0));
 }
Example #13
0
 void setPositionByID(coordinate co)
 {
     chess[co.x, co.y].transform.localPosition = GetPositionByID(co);
 }
Example #14
0
 TrySavingSector(coordinate, source.GetChunck(coordinate));
Example #15
0
 public bool Equals(coordinate comp)
 {
     return(this.i == comp.i && this.j == comp.j);
 }
Example #16
0
 Usage The class is designed for use in Unity's GUI coordinates. With this coordinate system, the origin is at the top left of the screen, x values increase from left to right, and y values increase as you go DOWN the screen. If you do some careful coordinate transformations, you probably should be able to use the code with an arbitrary coordinate system, but this was not the usage I designed it for, so beware. 
Example #17
0
    // Update is called once per frame
    new void Update()
    {
        base.Update(); // Do human things

        if (isHome)    // Case 1. NPC is currently at home
        {
            Debug.Log("Home");
            if (getCash() > 300)            // Only leave the house if npc can afford it
            {
                if (getImmuneSystem() < 40) // If health gets critical, go to hospital
                {
                    Debug.Log("Immune'nt");
                    isHome   = false; // NPC is no longer home
                    isMoving = true;

                    // Set target to go to random hospital
                    if (Random.Range(0, 10) >= 5)
                    {
                        target = getCoord(hospital1.transform.position);
                    }
                    else
                    {
                        target = getCoord(hospital2.transform.position);
                    }


                    setTarget(target);
                }

                else if (getHunger() < 60)   // If hunger gets low, go to grocery store
                {
                    Debug.Log("Foodn't");
                    isHome   = false; // NPC is no longer home
                    isMoving = true;
                    Debug.Log("calling store");
                    target = getCoord(grocery_store.transform.position); // Set target to go to grocery store
                    //target = new coordinate(0, 14);

                    setTarget(target);

                    // targetPath = Pathfinder.aStar(getCoord(this.transform.position), target);
                    // Debug.Log(targetPath);
                }
            }
        }

        else
        {
            isMoving = true;

            if (!isMoving)   // Case 2. NPC is at hospital or grocery store

            {
                if (getImmuneSystem() >= 100 || getHunger() >= 100)   // If player is done healing / eating
                {
                    Debug.Log("Not Moving");
                    target = getCoord(home.transform.position); // Set target to go home
                    setTarget(target);
                    isMoving = true;
                    return;
                }
            }

            else
            {
                this.move(target, targetPath);  // Case 3. NPC is mid route. Inch NPC a tiny bit closer towards target
            }
        }
    }
Example #18
0
    public coordinate[] requireToChange(int side, int angle, coordinate co)
    {
        coordinate[] coo = new coordinate[5];
        coo[0] = co;
        if (side == 1 && angle == 1)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, -1, 0);
            coo[2] = StaticMethodLib.onlyDoOffset(co, -2, 0);
            coo[3] = StaticMethodLib.onlyDoOffset(co, 0, -1);
            coo[4] = StaticMethodLib.onlyDoOffset(co, -3, 0);
        }
        else if (side == 1 && angle == 2)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, 0, -1);
            coo[2] = StaticMethodLib.onlyDoOffset(co, 0, -2);
            coo[3] = StaticMethodLib.onlyDoOffset(co, 1, 0);
            coo[4] = StaticMethodLib.onlyDoOffset(co, 0, -3);
        }
        else if (side == 1 && angle == 3)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, 1, 0);
            coo[2] = StaticMethodLib.onlyDoOffset(co, 2, 0);
            coo[3] = StaticMethodLib.onlyDoOffset(co, 0, 1);
            coo[4] = StaticMethodLib.onlyDoOffset(co, 3, 0);
        }
        else if (side == 1 && angle == 4)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, 0, 1);
            coo[2] = StaticMethodLib.onlyDoOffset(co, 0, 2);
            coo[3] = StaticMethodLib.onlyDoOffset(co, -1, 0);
            coo[4] = StaticMethodLib.onlyDoOffset(co, 0, 3);
        }
        else if (side == 2 && angle == 1)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, -1, 0);
            coo[2] = StaticMethodLib.onlyDoOffset(co, -2, 0);
            coo[3] = StaticMethodLib.onlyDoOffset(co, 0, 1);
            coo[4] = StaticMethodLib.onlyDoOffset(co, -3, 0);
        }
        else if (side == 2 && angle == 2)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, 0, 1);
            coo[2] = StaticMethodLib.onlyDoOffset(co, 0, 2);
            coo[3] = StaticMethodLib.onlyDoOffset(co, 1, 0);
            coo[4] = StaticMethodLib.onlyDoOffset(co, 0, 3);
        }
        else if (side == 2 && angle == 3)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, 1, 0);
            coo[2] = StaticMethodLib.onlyDoOffset(co, 2, 0);
            coo[3] = StaticMethodLib.onlyDoOffset(co, 0, -1);
            coo[4] = StaticMethodLib.onlyDoOffset(co, 3, 0);
        }
        else if (side == 2 && angle == 4)
        {
            coo[1] = StaticMethodLib.onlyDoOffset(co, 0, -1);
            coo[2] = StaticMethodLib.onlyDoOffset(co, 0, -2);
            coo[3] = StaticMethodLib.onlyDoOffset(co, -1, 0);
            coo[4] = StaticMethodLib.onlyDoOffset(co, 0, -3);
        }

        return(coo);
    }
Example #19
0
        private coordinate getCoor(City in_city)
        {
            int x = (int)(Math.Floor((in_city.X * SCALE_FACTOR - min_x) / x_inc));
            int y = (int)(Math.Floor((in_city.Y * SCALE_FACTOR - min_y) / y_inc));
            coordinate coor = new coordinate(x, y);

            return coor;
        }