About the idea of encircling a neuro-cat

Source: Internet
Author: User

See friends crazy turn this game inside Circle, then hit a bit, say own idea.


First, the map is a 9*9 matrix, because there is no alignment, so later, when the wide search will be some trouble, but, after all, relative to 41 directions, the alignment is not correct in 61 directions, making the game very big change.
The appearance of cats appears in the middle of the map. The default barriers should also be randomly generated, so there's no difficulty.
The most troublesome place to feel is the way the cat escapes. The first thought was a run in 6 directions, but later found it was the shortest route to escape. Because I am more vegetables. So we use the wide search to achieve. The starting point is the location of the cat, and the end point is the coordinates beyond the map.

Wide search is the most important wide search. No pruning. There is no two-way (9*9 also want these only a pound of onion talent dry out it.

。。 Then each extension to a node records the location of his previous node used as the escape path, wide search inside the visit array I set two. One is clicked, one is temporary in the search. Then, when searching, let's say you're not interviewed in two arrays. Exploring adjacent nodes costs a bit. Because of the relative to the matrix, this coordinate is not able to use two layers for the fix, I want to push the mathematical formula, pushed for a half a day cumbersome not to say, code to see a uncomfortable ... Then suddenly remembered a catty of the constant array of onions. Easy to solve ~ So the code is only 1/3 of the original length. And it looks much better than the original.

。。

As for the outcome. Suppose the old cat at the border of the map that is lost, assuming that the above the wide search did not search out the path then the player won. PS: In fact such a situation in the game inside the old cat is in situ Akira. It is not the end, but how the implementation of the back has been very easy, did not write. The return ( -1,-1) cannot be moved without searching ( -2,-2).

Because there is no talent for designing the UI. So the interface is simply replaced with a label, circular label Baidu a bit. Seemingly able to achieve here will not repeat.


And then... There's nothing, then? The last Code

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing;    Using system.linq;using system.text;using system.windows.forms;namespace trapthecat{public partial class Form1:form        {private int mapheight = 9;        private int mapwidth = 9;        Private bool[,] boolmap;//cannot walk the node private int startnum = 30;        Private gamelabel[,] gamemap;        private int catposx;//cat de location private int catposy;  Private int[,] Guidey = {{0, 1,-1, 1, 0, 1}, {-1, 0,-1, 1,-1, 0}};//These two arrays are used to explore adjacent nodes in deep search private int[] Guidex        = {-1,-1, 0, 0, 1, 1}; The struct pos//is used to store the path {public Pos (int x, int y) {this).                x = x; This.            y = y;            } public int X;        public int Y;            } public Form1 () {InitializeComponent ();            Gamestart ();        Gameshow (); } private void Gamestart ()Generate map {int I, J;            Catposx = MAPHEIGHT/2;            Catposy = MAPWIDTH/2;            Boolmap = new bool[mapheight, mapwidth];            Gamemap = new gamelabel[mapheight, mapwidth];            Random rand = new Random (); for (i = 0; i < mapheight; i++) for (j = 0; J < Mapwidth; J + +) Boolmap[i, j] = Fal            Se                for (i = 0; i < Startnum; i++) {int x, y; x = Rand.                Next (Mapheight); y = rand.                Next (Mapwidth); while (boolmap[x, y] | |                (x = = Mapheight/2 && y = = MAPWIDTH/2)) {x = rand.                    Next (Mapheight); y = rand.                Next (Mapwidth);            } boolmap[x, Y] = true;                    } for (i = 0; i < mapheight; i++) for (j = 0; J < Mapwidth; J + +) {             Gamemap[i, j] = new Gamelabel (i, j);       if (Boolmap[i, j]) Gamemap[i, j].                    BackColor = Color.orange; Else Gamemap[i, J].                    BackColor = Color.White; Gamemap[i, J].                    Width = 20; Gamemap[i, J].                    Height = 20; if (i% 2 = = 0) {gamemap[i, j].                    Left = 25 + J *; } else {gamemap[i, j].                    left = + J * 25; } Gamemap[i, J].                Top = + I * 25; } gamemap[catposx, Catposy].            Text = "Cat";        BOOLMAP[CATPOSX, Catposy] = false;            } private void gameshow ()//display interface {int i, J; for (i = 0; i < mapheight; i++) for (j = 0; J < Mapwidth; J + +) {Ga Memap[i, J].                    Click + = Label_click; This.                Controls.Add (Gamemap[i, j]); }} PrivaTe Pos?            Catmove ()//The path of the cat escape, return to which direction the next step should run {int X = CATPOSX;            int Y = Catposy;            int[,] list = new int[400, 2];            bool[,] visited = new bool[mapheight, mapwidth];            int Top = 0;            int Tail = 0;            int I, J; for (i = 0; i < mapheight; i++) for (j = 0; J < Mapwidth; J + +) Visited[i, j] = Fal            Se            pos[,] Prev = new pos[mapheight, mapwidth]; Prev[x, Y].            x = x; Prev[x, Y].            y = y;            List[top, 0] = X;            List[top, 1] = Y;            top++; while (Top! = Tail | |                Top = = 0) {X = List[tail, 0];                Y = List[tail, 1];                tail++;                    for (i = 0; i < 6; i++) {int tempx, tempy;                    Tempy = Y + guidey[x% 2, I];                    TEMPX = X + guidex[i]; if (tempx < 0 | | TEMPX >= Mapheight | | Tempy < 0 | |                        Tempy >= mapwidth) {Pos p; while (Prev[x, Y]. X! = Catposx | | Prev[x, Y].                            Y! = catposy) {p = prev[x, y];                            X = p.x;                        Y = P.Y;                    } return new Pos (X, Y); } if (! VISITED[TEMPX, Tempy] &&!                        BOOLMAP[TEMPX, Tempy]) {list[top, 0] = tempx;                        List[top, 1] = Tempy; PREV[TEMPX, Tempy].                        x = x; PREV[TEMPX, Tempy].                        y = y;                        top++;                    VISITED[TEMPX, Tempy] = true;            }}} MessageBox.Show ("no!\n" + tail.tostring ());        return null;      private void Label_click (object sender, EventArgs e)//label Click event {      Gamelabel label = (gamelabel) sender; Boolmap[label. X, label.            Y] = true; Label.            BackColor = Color.orange; if (Catposx = = 0 | | Catposx = = MapHeight-1 | | Catposy = = 0 | |                Catposy = = MapWidth-1) {MessageBox.Show ("you lose!");            Return } Pos?            p = catmove (); if (p.hasvalue) {gamemap[catposx, Catposy].                Text = ""; Gamemap[p.value.x, P.value.y].                Text = "Cat";                Catposx = p.value.x;            Catposy = P.value.y;            } else {MessageBox.Show ("you win!");        }}}} class gamelabel:label//is displayed with the label {private int PosX;        public int X {get {return PosX;}        } private int PosY;        public int Y {get {return PosY;} } public Gamelabel (int PosX, int. PosY) {this. PosX = PosX This.        PosY = PosY; }    }}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

About the idea of encircling a neuro-cat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.