コード例 #1
0
ファイル: GameOnField.cs プロジェクト: kristinaerm/Puzzle
        public GameOnField(string id, string game_m, string rec, string log, string form, bool fromSavedGame)
        {
            ConnDatabase bd = new ConnDatabase();

            InitializeComponent();
            ControlMover.Owner = this;

            //верхние пикчербоксы
            List <Bitmap> top = new List <Bitmap>();
            //нижние пикчербоксы
            List <Bitmap> bottom = new List <Bitmap>();
            //верхние пикчербоксы
            List <PicBox> top_pic = new List <PicBox>();
            //нижние пикчербоксы
            List <PicBox> bottom_pic = new List <PicBox>();
            //верхние номера
            List <int> top_num = new List <int>();
            //нижние номера
            List <int> bottom_num = new List <int>();


            if (form.Equals("треугольник"))
            {
                triangle = true;
            }

            id_puzzle = id;
            game_mode = game_m;
            record    = rec;
            login     = log;
            fromGame  = fromSavedGame;
            this.form = form;

            id_picture = bd.selectIdPictureByIdPuzzle(id_puzzle);
            string        path    = bd.selectPathByIdPicture(id_picture);
            List <string> picture = bd.selectSizeAndComplexityFromPuzzleByIdPuzzle(id_puzzle);

            verticalCountOfPieces   = Convert.ToInt32(picture[0]);
            horisontalCountOfPieces = Convert.ToInt32(picture[1]);

            if (bd.cutExcessSpace(picture[2]).Equals("1"))
            {
                complexityKoeff = 10;
            }
            else if (bd.cutExcessSpace(picture[2]).Equals("2"))
            {
                complexityKoeff = 50;
            }
            else
            {
                complexityKoeff = 100;
            }
            if (bd.cutExcessSpace(record).Equals("На очки"))
            {
                //в движениях
                betweenGreatAndNormal = 5;
                betweenNormalAndBad   = 15;
                //потом при подсчете результата число мувов делю на верт*горизонт и сравниваю и домнажаю на бэднормалгуд и на комплексити
            }
            else
            {
                //в секундах
                betweenGreatAndNormal = 20;
                betweenNormalAndBad   = 60;
            }

            int count = 0;

            btm1 = new List <Bitmap>();                                                //нормальный список кусочков пазл
            btm1 = Section.RectangleSection(path, picture[1], picture[0], id_picture); //разрезаем картинку на кусочки
            if (triangle)
            {
                btm = new List <Bitmap>();
                Bitmap[] mass = new Bitmap[2];
                for (int i = 0; i < horisontalCountOfPieces * verticalCountOfPieces; i++)
                {
                    mass = Section.TriangularSection(btm1[i], false);
                    top.Add(mass[0]);
                    bottom.Add(mass[1]);
                }
                count = horisontalCountOfPieces * verticalCountOfPieces * 2;
            }
            else
            {
                btm   = btm1;
                count = horisontalCountOfPieces * verticalCountOfPieces;
            }

            if (fromGame)
            {
                try
                {
                    List <string> saved = bd.selectAllAboutGameByLoginAndIdPuzzle(login, id_puzzle);
                    if (bd.cutExcessSpace(record).Equals("На очки"))
                    {
                        currentmoves = Convert.ToInt32(saved[2]);
                    }
                    else
                    {
                        string hh = "";
                        hh += saved[2][0];
                        hh += saved[2][1];
                        string mm = "";
                        mm += saved[2][3];
                        mm += saved[2][4];
                        string ss = "";
                        ss      += saved[2][6];
                        ss      += saved[2][7];
                        fromSave = new TimeSpan(Convert.ToInt32(hh), Convert.ToInt32(mm), Convert.ToInt32(ss));
                    }
                    //загрузить все кусочки в виде номер - координаты текущие
                    pieces = bd.selectPuzzlePiecesByPuzzleIdAndLogin(id_puzzle, login);
                    string[] buff;
                    //отсортировать по номеру
                    for (int i = 0; i < count; i++)
                    {
                        for (int j = 0; j < count - 1; j++)
                        {
                            if (Convert.ToInt32(pieces[j][0]) > Convert.ToInt32(pieces[j + 1][0]))
                            {
                                buff          = pieces[j];
                                pieces[j]     = pieces[j + 1];
                                pieces[j + 1] = buff;
                            }
                        }
                    }
                }
                catch
                {
                    if (MessageBox.Show("Не удалось загрузить пазл!") == DialogResult.OK)
                    {
                        this.Close();
                    }
                }
            }
            h = btm1[0].Height;
            w = btm1[0].Width;

            int newHeightOfForm = this.Size.Height + h + 30;

            buttonLeft.Location  = new Point(buttonLeft.Location.X, newHeightOfForm - 15 - (h / 2) - buttonLeft.Height - 38);
            buttonRight.Location = new Point(buttonRight.Location.X, newHeightOfForm - 15 - (h / 2) - buttonRight.Height - 38);

            currentLocationOfStripZoneBottomRight = new Point(this.Width - 50, newHeightOfForm - 15 - 38);
            currentLocationOfStripZoneTopLeft     = new Point(50, newHeightOfForm - h - buttonLeft.Height - 38);



            int currH = 0;
            int currW = 0;

            PicBox p  = new PicBox();
            PicBox p1 = new PicBox();

            object[] obj;
            object[] obj1;
            Graphics gr = this.CreateGraphics();

            gr.DrawEllipse(Pens.Black, currW * (w + 1) + 5, currH * (h + 1) + 25, 10, 10);
            Point pp;

            for (int i = 0; i < verticalCountOfPieces * horisontalCountOfPieces; i++)
            {
                if (triangle)
                {
                    p          = new PicBox();
                    p.Size     = new Size(w, h);
                    p.SizeMode = PictureBoxSizeMode.StretchImage;
                    p.Image    = (Image)top[i];
                    top_pic.Add(p);

                    p1          = new PicBox();
                    p1.Size     = new Size(w, h);
                    p1.SizeMode = PictureBoxSizeMode.StretchImage;
                    p1.Image    = (Image)bottom[i];
                    bottom_pic.Add(p1);
                }
                else
                {
                    p          = new PicBox();
                    p.Size     = new Size(w, h);
                    p.SizeMode = PictureBoxSizeMode.StretchImage;
                    p.Image    = (Image)btm[i];
                    pb.Add(p);
                }

                obj  = new object[2];
                obj1 = new object[2];
                if (game_mode == "На ленте")
                {
                    if (fromGame)
                    {
                        if (triangle)
                        {
                            if (((Convert.ToInt32(pieces[i * 2][1]) == 0) && (Convert.ToInt32(pieces[i * 2][2]) == 0)) || (currentLocationOfStripZoneTopLeft.X < Convert.ToInt32(pieces[i * 2][1])) && (Convert.ToInt32(pieces[i * 2][1]) < currentLocationOfStripZoneBottomRight.X) && (currentLocationOfStripZoneTopLeft.Y < Convert.ToInt32(pieces[i * 2][2])) && (Convert.ToInt32(pieces[i * 2][2]) < currentLocationOfStripZoneBottomRight.Y))
                            {
                                //в ленте
                                p.Visible = false;
                                obj[1]    = 'n';
                            }
                            else
                            {
                                p.Visible = true;
                                obj[1]    = 'f';
                            }
                            if (((Convert.ToInt32(pieces[i + i + 1][1]) == 0) && (Convert.ToInt32(pieces[i + i + 1][2]) == 0)) || (currentLocationOfStripZoneTopLeft.X < Convert.ToInt32(pieces[i + i + 1][1])) && (Convert.ToInt32(pieces[i + i + 1][1]) < currentLocationOfStripZoneBottomRight.X) && (currentLocationOfStripZoneTopLeft.Y < Convert.ToInt32(pieces[i + i + 1][2])) && (Convert.ToInt32(pieces[i + i + 1][2]) < currentLocationOfStripZoneBottomRight.Y))
                            {
                                //в ленте
                                p1.Visible = false;
                                obj1[1]    = 'n';
                            }
                            else
                            {
                                p1.Visible = true;
                                obj1[1]    = 'f';
                            }
                        }
                        else
                        {
                            if (((Convert.ToInt32(pieces[i][1]) == 0) && (Convert.ToInt32(pieces[i][2]) == 0)) || (currentLocationOfStripZoneTopLeft.X < Convert.ToInt32(pieces[i][1])) && (Convert.ToInt32(pieces[i][1]) < currentLocationOfStripZoneBottomRight.X) && (currentLocationOfStripZoneTopLeft.Y < Convert.ToInt32(pieces[i][2])) && (Convert.ToInt32(pieces[i][2]) < currentLocationOfStripZoneBottomRight.Y))
                            {
                                //в ленте
                                p.Visible = false;
                                obj[1]    = 'n';
                            }
                            else
                            {
                                p.Visible = true;
                                obj[1]    = 'f';
                            }
                        }
                    }
                    else
                    {
                        if (triangle)
                        {
                            p.Visible  = false;
                            p1.Visible = false;
                        }
                        else
                        {
                            p.Visible = false;
                        }
                        obj[1] = 'n';
                    }
                }
                else
                {
                    if (triangle)
                    {
                        obj[1]  = ' ';
                        obj1[1] = ' ';
                    }
                    else
                    {
                        obj[1] = ' ';
                    }
                }
                pp      = new Point(currW * (w + 1) + 5, currH * (h + 1) + 25);
                obj[0]  = pp;
                obj1[0] = pp;
                if (triangle)
                {
                    p.Tag  = obj;
                    p1.Tag = obj1;
                }
                else
                {
                    p.Tag = obj;
                    serial_number.Add(i);
                }

                if (fromGame)
                {
                    if (triangle)
                    {
                        p.Location = new Point(Convert.ToInt32(pieces[i * 2][1]), Convert.ToInt32(pieces[i * 2][2]));
                        if ((pp.X == p.Location.X) && (pp.Y == p.Location.Y))
                        {
                            p.BorderStyle = BorderStyle.Fixed3D;
                            p.Enabled     = false;
                        }

                        p1.Location = new Point(Convert.ToInt32(pieces[i + i + 1][1]), Convert.ToInt32(pieces[i + i + 1][2]));
                        if ((pp.X == p1.Location.X) && (pp.Y == p1.Location.Y))
                        {
                            p1.BorderStyle = BorderStyle.Fixed3D;
                            p1.Enabled     = false;
                        }
                    }
                    else
                    {
                        p.Location = new Point(Convert.ToInt32(pieces[i][1]), Convert.ToInt32(pieces[i][2]));
                        if ((pp.X == p.Location.X) && (pp.Y == p.Location.Y))
                        {
                            p.BorderStyle = BorderStyle.Fixed3D;
                            p.Enabled     = false;
                        }
                    }
                }

                if (triangle)
                {
                    this.Controls.Add(p);
                    ControlMover.Add(p);
                    this.Controls.Add(p1);
                    ControlMover.Add(p1);
                }
                else
                {
                    this.Controls.Add(pb[i]);
                    ControlMover.Add(pb[i]);
                }
                currW++;
                if (currW == horisontalCountOfPieces)
                {
                    currH++;
                    currW = 0;
                }
            }



            if (!fromGame)
            {
                //тут шафл массива пикчеров и номеров синхронно
                if (triangle)
                {
                    for (int i = 0; i < 2 * verticalCountOfPieces * horisontalCountOfPieces; i += 2)
                    {
                        top_num.Add(i);
                        bottom_num.Add(i + 1);
                    }
                    //перемешивание верхних кусочков
                    syncShuffle <PicBox, int>(top_pic, top_num);
                    //нижние
                    syncShuffle <PicBox, int>(bottom_pic, bottom_num);
                    //объединение
                }
                else
                {
                    syncShuffle <PicBox, int>(pb, serial_number);
                }


                currH = 0;
                currW = 0;

                if (game_mode == "На поле")
                {
                    for (int i = 0; i < verticalCountOfPieces * horisontalCountOfPieces; i++)
                    {
                        if (triangle)
                        {
                            top_pic[i].Location    = new Point(currW * (w + 1) + 5, currH * (h + 1) + 25);
                            bottom_pic[i].Location = new Point(currW * (w + 1) + 5, currH * (h + 1) + 25);
                        }
                        else
                        {
                            pb[i].Location = new Point(currW * (w + 1) + 5, currH * (h + 1) + 25);
                        }

                        currW++;
                        if (currW == horisontalCountOfPieces)
                        {
                            currH++;
                            currW = 0;
                        }
                    }
                    pb.AddRange(top_pic);
                    pb.AddRange(bottom_pic);
                    serial_number.AddRange(top_num);
                    serial_number.AddRange(bottom_num);
                }
                else if (game_mode == "В куче")
                {
                    if (triangle)
                    {
                        for (int i = 0; i < verticalCountOfPieces * horisontalCountOfPieces; i++)
                        {
                            top_pic[i].Location    = new Point(currW * (w + 1) + 5, currH * (h + 1) + 25);
                            bottom_pic[i].Location = new Point(currW * (w + 1) + 5, currH * (h + 1) + 25);


                            currW++;
                            if (currW == horisontalCountOfPieces)
                            {
                                currH++;
                                currW = 0;
                            }
                        }
                        pb.AddRange(top_pic);
                        pb.AddRange(bottom_pic);
                        serial_number.AddRange(top_num);
                        serial_number.AddRange(bottom_num);
                    }
                    else
                    {
                        Random r = new Random();
                        for (int i = 0; i < verticalCountOfPieces * horisontalCountOfPieces; i++)
                        {
                            if (triangle)
                            {
                                top_pic[i].Location    = new Point(r.Next(50, 300), r.Next(50, 300));
                                bottom_pic[i].Location = new Point(r.Next(50, 300), r.Next(50, 300));
                            }
                            else
                            {
                                pb[i].Location = new Point(r.Next(50, 300), r.Next(50, 300));
                            }
                        }
                        pb.AddRange(top_pic);
                        pb.AddRange(bottom_pic);
                        serial_number.AddRange(top_num);
                        serial_number.AddRange(bottom_num);
                    }
                }
                else if (game_mode == "На ленте")
                {
                    pb.AddRange(top_pic);
                    pb.AddRange(bottom_pic);
                    serial_number.AddRange(top_num);
                    serial_number.AddRange(bottom_num);

                    buttonLeft.Enabled = true;
                    buttonLeft.Visible = true;

                    buttonRight.Enabled = true;
                    buttonRight.Visible = true;

                    this.Size = new Size(this.Width, newHeightOfForm);

                    //столько кусочков уместится на ленте
                    countOfPiecesOnStrip = (currentLocationOfStripZoneBottomRight.X - currentLocationOfStripZoneTopLeft.X - 5) / (w + 5);

                    for (int i = 0; i < countOfPiecesOnStrip; i++)
                    {
                        pb[i].Location = new Point(5 + currentLocationOfStripZoneTopLeft.X + i * (w + 5), currentLocationOfStripZoneTopLeft.Y);

                        object[] o = new object[2];
                        o[0]      = ((Point)((object[])pb[i].Tag)[0]);
                        o[1]      = 's';
                        pb[i].Tag = o;

                        pb[i].Visible = true;
                    }
                    currentFirstElementOnStrip = 0;
                }
            }
            else
            {
                if (game_mode == "На ленте")
                {
                    if (triangle)
                    {
                        for (int i = 0; i < 2 * verticalCountOfPieces * horisontalCountOfPieces; i += 2)
                        {
                            top_num.Add(i);
                            bottom_num.Add(i + 1);
                        }
                    }
                    pb.AddRange(top_pic);
                    pb.AddRange(bottom_pic);
                    serial_number.AddRange(top_num);
                    serial_number.AddRange(bottom_num);
                    syncShuffle <PicBox, int>(pb, serial_number);

                    buttonLeft.Enabled = true;
                    buttonLeft.Visible = true;

                    buttonRight.Enabled = true;
                    buttonRight.Visible = true;

                    this.Size = new Size(this.Width, newHeightOfForm);

                    //столько кусочков уместится на ленте
                    countOfPiecesOnStrip = (currentLocationOfStripZoneBottomRight.X - currentLocationOfStripZoneTopLeft.X - 5) / (w + 5);

                    updateStrip();
                }
            }

            //ЗАПУТАЛАСЬ С РАЗМЕТКОЙ ШО ДЕЛАТЬ
            this.Paint   += new PaintEventHandler(Form1_Paint);
            hint          = new PicBox();
            hint.SizeMode = PictureBoxSizeMode.StretchImage;
            hint.Size     = new Size((btm1[0].Width + 1) * horisontalCountOfPieces, (btm1[0].Height + 1) * verticalCountOfPieces);
            hint.Location = new Point(5, 25);
            hint.Image    = Image.FromFile(path);
            hhh           = new Hint(hint);
        }