Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Informatik in der Schule (http://www.informatikerboard.de/board/board.php?boardid=21)
--- C# Geeignetes Key - Ereignis (http://www.informatikerboard.de/board/thread.php?threadid=1437)


Geschrieben von eulerscheZahl am 03.04.2013 um 21:30:

 

Irgendwo ist noch ein kleiner Fehler drin, aber ich denke man kann erkennen, wie es im Prinzip funktioniert:
evtl. in Zeile29 den Pfad ändern.

edit: ich sollte abends nicht mehr programmieren... Läuft jetzt.
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
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;
using System.Drawing.Imaging;

namespace Slider
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public Bitmap CutImage(Bitmap original, int startX, int startY, int breiteX, int breiteY)
        {
            return original.Clone(new Rectangle(startX, startY, breiteX, breiteY), PixelFormat.DontCare);
        }

        const int teileX = 3;
        const int teileY = 3;
        PictureBox[,] pic = new PictureBox[teileX, teileY];
        Point freiesFeld = new Point(teileX - 1, teileY - 1);
        int[,] wasIstWo = new int[teileX, teileY];

        private void Form1_Load(object sender, EventArgs e)
        {
            Random r = new Random();
            string[] auswahl = { "Chrysanthemum", "Desert", "Hydrangeas", "Jellyfish", "Koala", "Lighthouse", "Penguins", "Tulips" };
            string bildPfad = @"C:\Users\Public\Pictures\Sample Pictures\" + auswahl[r.Next(auswahl.Length)] + ".jpg";
            Bitmap bild = new Bitmap(bildPfad);
            this.KeyPreview = true;
            for (int i = 0; i < teileX; i++)
                for (int j = 0; j < teileY; j++)
                {
                    pic[i, j] = new PictureBox();
                    pic[i, j].Parent = this;
                    pic[i, j].Left = i * (0 + bild.Width / teileX);
                    pic[i, j].Top = j * (0 + bild.Height / teileY);
                    pic[i, j].Width = bild.Width / teileX;
                    pic[i, j].Height = bild.Height / teileY;
                    pic[i, j].Image = CutImage(bild, bild.Width / teileX * i, bild.Height / teileY * j, bild.Width / teileX, bild.Height / teileY);
                    wasIstWo[i, j] = i * teileY + j;
                }
            pic[teileX - 1, teileY - 1].Visible = false;
            mischen(teileX * teileX * teileY * teileY);
        }

        private void mischen(int p)
        {
            Random r = new Random();
            for (int i = 0; i < p; i++)
                bewegen(r.Next(4));
        }

        public enum richtung { LEFT, RIGHT, UP, DOWN }

        private void bewegen(int richt)
        {
            int tmp, nachbarX, nachbarY;
            int[,] offsetVector = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
            switch (richt)
            {
                case (int)richtung.LEFT:
                    if (freiesFeld.X == teileX - 1) return;
                    break;
                case (int)richtung.RIGHT:
                    if (freiesFeld.X == 0) return;
                    break;
                case (int)richtung.UP:
                    if (freiesFeld.Y == teileY - 1) return;
                    break;
                case (int)richtung.DOWN:
                    if (freiesFeld.Y == 0) return;
                    break;
            }
            nachbarX = wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]] / teileY;
            nachbarY = wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]] % teileY;
            if (richt >= 2)
            {
                tmp = pic[nachbarX, nachbarY].Top;
                pic[nachbarX, nachbarY].Top = pic[teileX - 1, teileY - 1].Top;
                pic[teileX - 1, teileY - 1].Top = tmp;
            }
            else
            {
                tmp = pic[nachbarX, nachbarY].Left;
                pic[nachbarX, nachbarY].Left = pic[teileX - 1, teileY - 1].Left;
                pic[teileX - 1, teileY - 1].Left = tmp;
            }
            tmp = wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]];
            wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]] = wasIstWo[freiesFeld.X, freiesFeld.Y];
            wasIstWo[freiesFeld.X, freiesFeld.Y] = tmp;
            freiesFeld.X += offsetVector[richt, 0];
            freiesFeld.Y += offsetVector[richt, 1];

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left: //von links in das freie Feld schieben
                    bewegen((int)richtung.LEFT);
                    break;
                case Keys.Right:
                    bewegen((int)richtung.RIGHT);
                    break;
                case Keys.Up:
                    bewegen((int)richtung.UP);
                    break;
                case Keys.Down:
                    bewegen((int)richtung.DOWN);
                    break;
            }
            pic[teileX - 1, teileY - 1].Visible = TestGeloest();
        }

        private bool TestGeloest()
        {
            for (int i = 0; i < teileX; i++)
                for (int j = 0; j < teileY; j++)
                    if (wasIstWo[i, j] != i * teileY + j) return false;
            return true;
        }
    }
}



Geschrieben von InformaTiger am 06.04.2013 um 12:13:

 

Nun das Problem daran ist mal natürlich wieder, dass ich das leider nicht verstehe... vielleicht wäre es mit Kommentare übersichtlicher. Mein Auge ist so von meinen Programmierkonventionen geprägt, dass ich andern Code leider kaum verstehen kann. unglücklich
Zumal verstehe ich nicht was enum ist. Vom Code verstanden habe ich nur eines: Dass der Code die Bilder durch Zufall auswählt, man Sie durch bewegen(); verschieben kann und sonst leider nicht viel...

PS: Wenn es möglich ist kannst du mir das gegebenenfalls über Email erklären. Nächste Woche bin ich leider voll ausgebucht -.-

Lg
InformaTiger


Forensoftware: Burning Board, entwickelt von WoltLab GmbH