C# Schachfeld



  • Sehr geehrte Community,

    diesmal habe ich ein Problem mit C# (ich programmier sehr selten in C#, muss es aber diesmal)

    Es ist eine Windows Forms Anwendung

    Form1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SchachVerwaltung
    {
    
        public partial class Form1 : Form
        {
    
            //ChessField m = new ChessField(400, 20, 20,listBox1);
            public Form1()
            {
                InitializeComponent();
                this.BackColor = System.Drawing.Color.Yellow;
                //m.Visible = true;
                this.Controls.Add(new ChessField(400, 20,20,listBox1) { });
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            }
    
        }
    
    }
    

    Class1.cs //class ChessField

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SchachVerwaltung
    {
        public class ChessField : Panel
        {
            private string[] fieldArray = new string[8 * 8];
            private string fieldmem = "";
            private string whatmem = "";
            private datastruct mystruct = new datastruct();
            private enum state
            {
                black,
                white
            }
    
            protected void setFields()
            {
                int i = 0;
                fieldArray[i++] = "A8";     /*1*/
                fieldArray[i++] = "B8";     /*2*/
                fieldArray[i++] = "C8";
                fieldArray[i++] = "D8";
                fieldArray[i++] = "E8";
                fieldArray[i++] = "F8";
                fieldArray[i++] = "G8";
                fieldArray[i++] = "H8";
                fieldArray[i++] = "A7";
                fieldArray[i++] = "B7";     /*10*/
                fieldArray[i++] = "C7";
                fieldArray[i++] = "D7";
                fieldArray[i++] = "E7";
                fieldArray[i++] = "F7";
                fieldArray[i++] = "G7";
                fieldArray[i++] = "H7";
                fieldArray[i++] = "A6";
                fieldArray[i++] = "B6";
                fieldArray[i++] = "C6";
                fieldArray[i++] = "D6";     /*20*/
                fieldArray[i++] = "E6";
                fieldArray[i++] = "F6";
                fieldArray[i++] = "G6";
                fieldArray[i++] = "H6";
                fieldArray[i++] = "A5";
                fieldArray[i++] = "B5";
                fieldArray[i++] = "C5";
                fieldArray[i++] = "D5";
                fieldArray[i++] = "E5";
                fieldArray[i++] = "F5";     /*30*/
                fieldArray[i++] = "G5";
                fieldArray[i++] = "H5";
                fieldArray[i++] = "A4";
                fieldArray[i++] = "B4";
                fieldArray[i++] = "C4";
                fieldArray[i++] = "D4";
                fieldArray[i++] = "E4";
                fieldArray[i++] = "F4";
                fieldArray[i++] = "G4";
                fieldArray[i++] = "H4";     /*40*/
                fieldArray[i++] = "A3";
                fieldArray[i++] = "B3";
                fieldArray[i++] = "C3";
                fieldArray[i++] = "D3";
                fieldArray[i++] = "E3";
                fieldArray[i++] = "F3";
                fieldArray[i++] = "G3";
                fieldArray[i++] = "H3";
                fieldArray[i++] = "A2";
                fieldArray[i++] = "B2";     /*50*/
                fieldArray[i++] = "C2";
                fieldArray[i++] = "D2";
                fieldArray[i++] = "E2";
                fieldArray[i++] = "F2";
                fieldArray[i++] = "G2";
                fieldArray[i++] = "H2";
                fieldArray[i++] = "A1";
                fieldArray[i++] = "B1";
                fieldArray[i++] = "C1";
                fieldArray[i++] = "D1";     /*60*/
                fieldArray[i++] = "E1";
                fieldArray[i++] = "F1";
                fieldArray[i++] = "G1";
                fieldArray[i++] = "H1";     /*64*/
            }
    
            Dictionary<string, ChessButton> Btn_Dict;
    
            public ChessField(int size, int x,int y, ListBox lb)
            {
                base.Width = size;
                base.Location = new Point(x, y);
                base.Width = (base.Width / 8) * 8;
                base.Height = base.Width;
                base.BackColor = Color.Gray;
                mystruct.reflv = lb;
                //this.draw();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                int s = base.Width / 8;
                state g = state.black;
                int cnt = 0;
                for(int y = 0; y<8;y++)
                {
                    for(int x = 0; x<8;x++)
                    {
                        if (g == state.black)
                        {
    
                            base.Controls.Add(new ChessButton(s,x*s,y*s,Color.White, fieldArray[cnt++],"WHITE PAWN",ChessClick ));
                            g = state.white;
    
                        }
                        else
                        {
    
                            base.Controls.Add(new ChessButton(s, x * s, y * s, Color.Black, fieldArray[cnt++], "NULL", ChessClick));
                            g = state.black;
    
                        }
                    }
                    if (g == state.black)
                        g = state.white;
                    else
                        g = state.black;
               }
    
            }
            public void ChessClick(object sender, EventArgs e)
            {
                if ((((ChessButton)sender).figure != "NULL") && this.whatmem.Length == 0)
                { 
                    this.fieldmem = ((ChessButton)sender).field;
                    this.whatmem = ((ChessButton)sender).figure;
                    ((ChessButton)sender).figure = "NULL";
                    this.mystruct.figure = this.whatmem + " " + this.fieldmem;
                }
                if (((((ChessButton)sender).figure == "NULL") && this.whatmem.Length == 0))
                {
                    ((ChessButton)sender).figure = this.whatmem;
                    this.whatmem = "";
                    this.fieldmem = "";
                }
            }
    
        }
    }
    

    ChessButton.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SchachVerwaltung
    {
        class ChessButton : Button
        {
            private string _figure;
            private string _field;
            Dictionary<string, System.Drawing.Image> img = new Dictionary<string,System.Drawing.Image>();
            public ChessButton(int size, int x, int y, System.Drawing.Color back, string mfield, string figure, System.EventHandler function) : base()
            {
                this.setImg();
                base.BackColor = back;
                base.Width = base.Height = size;
                base.Location = new System.Drawing.Point(x, y);
                this._field = mfield;
                this.figure = figure;
                base.Click += function;
            }
    
            protected void setImg()
            {
                //White
                string tmp = "WHITE ";
                img.Add(tmp + "PAWN", System.Drawing.Image.FromFile(@"e:\wPawn.png"));
                img.Add(tmp + "ROUGE", System.Drawing.Image.FromFile(@"e:\wRouge.png"));
                img.Add(tmp + "BISHOP", System.Drawing.Image.FromFile(@"e:\wBishop.png"));
                img.Add(tmp + "Knight", System.Drawing.Image.FromFile(@"e:\wKnight.png"));
                img.Add(tmp + "Queen", System.Drawing.Image.FromFile(@"e:\wQueen.png"));
                img.Add(tmp + "King", System.Drawing.Image.FromFile(@"e:\wKing.png"));
                //Black
                tmp = "BLACK ";
                img.Add(tmp + "PAWN", System.Drawing.Image.FromFile(@"e:\bPawn.png"));
                img.Add(tmp + "ROUGE", System.Drawing.Image.FromFile(@"e:\bRouge.png"));
                img.Add(tmp + "BISHOP", System.Drawing.Image.FromFile(@"e:\bBishop.png"));
                img.Add(tmp + "Knight", System.Drawing.Image.FromFile(@"e:\bKnight.png"));
                img.Add(tmp + "Queen", System.Drawing.Image.FromFile(@"e:\bQueen.png"));
                img.Add(tmp + "King", System.Drawing.Image.FromFile(@"e:\bKing.png"));
    
            }
    
            public string figure
            {
                set
                {
                    //string[] cont = value.Split(' ');
                    if(value!="NULL")
                        base.Image = img[value];
                    this._figure = value;
                }
                get
                {
                    base.Image = null;
                    return this._figure;
                }
            }
            public string field
            {
                get //Hier entsteht ein Fehler beim drücken von den Buttons
                {
                    return field; 
                }
            }
        }
    }
    

    datastruct.cs //sollte eigentlich eine Struct zur Datenübertragung werden, habe mich dann aber für eine Klasse entschieden, aber den Namen behalten

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SchachVerwaltung
    {
        class datastruct
        {
            public datastruct()
            {
    
            }
            System.Windows.Forms.ListBox lv;
            public System.Windows.Forms.ListBox reflv
            {
                set
                {
                    this.lv = value;
                }
                get
                {
                    return this.reflv;
                }
            }
            private string _figure;
            public string figure
            {
                set
                {
                    this.lv.Items.Add(value);
                    this._figure = value;
                }
                get
                {
                    return this._figure;
                }
            }
            private string _field;
            public string field
            {
                set
                {
                    this.lv.Items.Add(value);
                    this._field = value;
                }
                get
                {
                    return this._field;
                }
            }
    
        }
    }
    

    Warum wird das Schachfeld nicht komplett aufgebaut?

    Wenn man auf einen Knopf drückt, soll er kontrollieren, ob im Knopf eine Figur drin ist (leer = "NULL") und ob in der Zwischenablage whatmem was drin ist.

    Mit freundlichen Grüßen



  • Hallo,

    Du hast bisher eine Endlosrekursion erzeugt. Lösung:

    public string field
    {
        get
        {
            return _field; // <- Zugriff auf privates Feld
        }
    }
    

    Du hast aber so viele Unschönheiten in deinem Programm, da ist es schwer dein Code-Aufbau zu verstehen, z.B.
    - Namensbenennung entspricht nicht dem MS C# Styleguide (z.B. Klassen-/Strukturnamen sowie Eigenschaften und Methoden sollten mit einem Großbuchstaben anfangen)
    - Array-Initialisierung über Schleifen (fieldArray!!!)
    - warum "NULL" anstatt null (bzw. String.Empty)?
    - Vermischung von GUI und Logik/Daten
    - Warum hat jeder ChessButton das (selbe) Image-Dictionary?
    - Packe besser die Images in die Ressourcen (anstatt über absoluten Pfad anzusprechen)
    - ...



  • Th69 schrieb:

    Hallo,

    Du hast bisher eine Endlosrekursion erzeugt. Lösung:

    public string field
    {
        get
        {
            return _field; // <- Zugriff auf privates Feld
        }
    }
    

    Du hast aber so viele Unschönheiten in deinem Programm, da ist es schwer dein Code-Aufbau zu verstehen, z.B.
    - Namensbenennung entspricht nicht dem MS C# Styleguide (z.B. Klassen-/Strukturnamen sowie Eigenschaften und Methoden sollten mit einem Großbuchstaben anfangen)
    - Array-Initialisierung über Schleifen (fieldArray!!!)
    - warum "NULL" anstatt null (bzw. String.Empty)?
    - Vermischung von GUI und Logik/Daten
    - Warum hat jeder ChessButton das (selbe) Image-Dictionary?
    - Packe besser die Images in die Ressourcen (anstatt über absoluten Pfad anzusprechen)
    - ...

    Sollte vielleicht das Image-Dictionary Static machen, so dass es nur einmal existiert.

    In die Resource packe ich erst alles rein, wenn ich die Bilder habe, im Moment ist alles ein Weißer Bauer.

    Sollte ich stattdessen fieldArray so initialisieren

    private string[]fieldArray = {"A8","B8", ...} ?

    Korrigierte Version, es klappt für zwei mal, danach bricht er ab

    class1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SchachVerwaltung
    {
        public class ChessField : Panel
        {
            private string[] fieldArray = new string[8 * 8];
            private string fieldmem = "";
            private string whatmem = "";
            private datastruct mystruct = new datastruct();
            private enum state
            {
                black,
                white
            }
    
            protected void setFields()
            {
                int i = 0;
                fieldArray[i++] = "A8";     /*1*/
                fieldArray[i++] = "B8";     /*2*/
                fieldArray[i++] = "C8";
                fieldArray[i++] = "D8";
                fieldArray[i++] = "E8";
                fieldArray[i++] = "F8";
                fieldArray[i++] = "G8";
                fieldArray[i++] = "H8";
                fieldArray[i++] = "A7";
                fieldArray[i++] = "B7";     /*10*/
                fieldArray[i++] = "C7";
                fieldArray[i++] = "D7";
                fieldArray[i++] = "E7";
                fieldArray[i++] = "F7";
                fieldArray[i++] = "G7";
                fieldArray[i++] = "H7";
                fieldArray[i++] = "A6";
                fieldArray[i++] = "B6";
                fieldArray[i++] = "C6";
                fieldArray[i++] = "D6";     /*20*/
                fieldArray[i++] = "E6";
                fieldArray[i++] = "F6";
                fieldArray[i++] = "G6";
                fieldArray[i++] = "H6";
                fieldArray[i++] = "A5";
                fieldArray[i++] = "B5";
                fieldArray[i++] = "C5";
                fieldArray[i++] = "D5";
                fieldArray[i++] = "E5";
                fieldArray[i++] = "F5";     /*30*/
                fieldArray[i++] = "G5";
                fieldArray[i++] = "H5";
                fieldArray[i++] = "A4";
                fieldArray[i++] = "B4";
                fieldArray[i++] = "C4";
                fieldArray[i++] = "D4";
                fieldArray[i++] = "E4";
                fieldArray[i++] = "F4";
                fieldArray[i++] = "G4";
                fieldArray[i++] = "H4";     /*40*/
                fieldArray[i++] = "A3";
                fieldArray[i++] = "B3";
                fieldArray[i++] = "C3";
                fieldArray[i++] = "D3";
                fieldArray[i++] = "E3";
                fieldArray[i++] = "F3";
                fieldArray[i++] = "G3";
                fieldArray[i++] = "H3";
                fieldArray[i++] = "A2";
                fieldArray[i++] = "B2";     /*50*/
                fieldArray[i++] = "C2";
                fieldArray[i++] = "D2";
                fieldArray[i++] = "E2";
                fieldArray[i++] = "F2";
                fieldArray[i++] = "G2";
                fieldArray[i++] = "H2";
                fieldArray[i++] = "A1";
                fieldArray[i++] = "B1";
                fieldArray[i++] = "C1";
                fieldArray[i++] = "D1";     /*60*/
                fieldArray[i++] = "E1";
                fieldArray[i++] = "F1";
                fieldArray[i++] = "G1";
                fieldArray[i++] = "H1";     /*64*/
            }
    
            Dictionary<string, ChessButton> Btn_Dict;
    
            public ChessField(int size, int x,int y, ListBox lb)
            {
                this.setFields();
                base.Width = size;
                base.Location = new Point(x, y);
                base.Width = (base.Width / 8) * 8;
                base.Height = base.Width;
                base.BackColor = Color.Gray;
                mystruct.reflv = lb;
                //this.draw();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                int s = base.Width / 8;
                state g = state.black;
                int cnt = 0;
                for(int y = 0; y<8;y++)
                {
                    for(int x = 0; x<8;x++)
                    {
                        if (g == state.black)
                        {
    
                            base.Controls.Add(new ChessButton(s,x*s,y*s,Color.White, fieldArray[cnt++],"WHITE PAWN",ChessClick ));
                            g = state.white;
    
                        }
                        else
                        {
    
                            base.Controls.Add(new ChessButton(s, x * s, y * s, Color.Black, fieldArray[cnt++], "NULL", ChessClick));
                            g = state.black;
    
                        }
                    }
                    if (g == state.black)
                        g = state.white;
                    else
                        g = state.black;
               }
    
            }
            public void ChessClick(object sender, EventArgs e)
            { //hier bricht er nach dem 2 oder dritten Klick ab
                if ((((ChessButton)sender).figure != "NULL") && this.whatmem.Length == 0)
                { 
                    this.fieldmem = ((ChessButton)sender).field;
                    this.whatmem = ((ChessButton)sender).figure;
                    ((ChessButton)sender).figure = "NULL";
                    this.mystruct.figure = this.whatmem + " " + this.fieldmem;
                }
                else
                if (((((ChessButton)sender).figure == "NULL") && this.whatmem.Length != 0))
                {
                    ((ChessButton)sender).figure = this.whatmem;
                    this.whatmem = null;
                    this.fieldmem = null;
                }
            }
    
        }
    }
    

    ChessButton.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SchachVerwaltung
    {
        class ChessButton : Button
        {
            private string _figure;
            private string _field;
            Dictionary<string, System.Drawing.Image> img = new Dictionary<string,System.Drawing.Image>();
            public ChessButton(int size, int x, int y, System.Drawing.Color back, string mfield, string figure, System.EventHandler function) : base()
            {
                this.setImg();
                base.BackColor = back;
                base.Width = base.Height = size;
                base.Location = new System.Drawing.Point(x, y);
                this._field = mfield;
                this.figure = figure;
                base.Click += function;
            }
    
            protected void setImg()
            {
                //White
                string tmp = "WHITE ";
                img.Add(tmp + "PAWN", (System.Drawing.Image)Resource1.WHITEPAWN);
                img.Add(tmp + "ROUGE", System.Drawing.Image.FromFile(@"e:\wRouge.png"));
                img.Add(tmp + "BISHOP", System.Drawing.Image.FromFile(@"e:\wBishop.png"));
                img.Add(tmp + "Knight", System.Drawing.Image.FromFile(@"e:\wKnight.png"));
                img.Add(tmp + "Queen", System.Drawing.Image.FromFile(@"e:\wQueen.png"));
                img.Add(tmp + "King", System.Drawing.Image.FromFile(@"e:\wKing.png"));
                //Black
                tmp = "BLACK ";
                img.Add(tmp + "PAWN", System.Drawing.Image.FromFile(@"e:\bPawn.png"));
                img.Add(tmp + "ROUGE", System.Drawing.Image.FromFile(@"e:\bRouge.png"));
                img.Add(tmp + "BISHOP", System.Drawing.Image.FromFile(@"e:\bBishop.png"));
                img.Add(tmp + "Knight", System.Drawing.Image.FromFile(@"e:\bKnight.png"));
                img.Add(tmp + "Queen", System.Drawing.Image.FromFile(@"e:\bQueen.png"));
                img.Add(tmp + "King", System.Drawing.Image.FromFile(@"e:\bKing.png"));
    
            }
    
            public string figure
            {
                set
                {
                    //string[] cont = value.Split(' ');
                    if(value!="NULL")
                        base.Image = img[value];
                    if (value == "NULL")
                        base.Image = null;
                    this._figure = value;
                }
                get
                {
                    base.Image = null;
                    return this._figure;
                }
            }
            public string field
            {
                get
                {
                    return _field;
                }
            }
        }
    }
    

    Vielen Dank Th69



  • hallo

    Leider Doppelpost, aber ich habe jetzt eine weitere verbesserte Version

    class1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SchachVerwaltung
    {
        public class ChessField : Panel
        {
            private string[] fieldArray = new string[8 * 8];
            private string fieldmem = null;
            private string whatmem = null;
            private datastruct mystruct = new datastruct();
            private Dictionary<string, string> list = new Dictionary<string, string>();
            private enum state
            {
                black,
                white
            }
    
            protected void setFields()
            {
                int i = 0;
                fieldArray[i++] = "A8";     /*1*/
                fieldArray[i++] = "B8";     /*2*/
                fieldArray[i++] = "C8";
                fieldArray[i++] = "D8";
                fieldArray[i++] = "E8";
                fieldArray[i++] = "F8";
                fieldArray[i++] = "G8";
                fieldArray[i++] = "H8";
                fieldArray[i++] = "A7";
                fieldArray[i++] = "B7";     /*10*/
                fieldArray[i++] = "C7";
                fieldArray[i++] = "D7";
                fieldArray[i++] = "E7";
                fieldArray[i++] = "F7";
                fieldArray[i++] = "G7";
                fieldArray[i++] = "H7";
                fieldArray[i++] = "A6";
                fieldArray[i++] = "B6";
                fieldArray[i++] = "C6";
                fieldArray[i++] = "D6";     /*20*/
                fieldArray[i++] = "E6";
                fieldArray[i++] = "F6";
                fieldArray[i++] = "G6";
                fieldArray[i++] = "H6";
                fieldArray[i++] = "A5";
                fieldArray[i++] = "B5";
                fieldArray[i++] = "C5";
                fieldArray[i++] = "D5";
                fieldArray[i++] = "E5";
                fieldArray[i++] = "F5";     /*30*/
                fieldArray[i++] = "G5";
                fieldArray[i++] = "H5";
                fieldArray[i++] = "A4";
                fieldArray[i++] = "B4";
                fieldArray[i++] = "C4";
                fieldArray[i++] = "D4";
                fieldArray[i++] = "E4";
                fieldArray[i++] = "F4";
                fieldArray[i++] = "G4";
                fieldArray[i++] = "H4";     /*40*/
                fieldArray[i++] = "A3";
                fieldArray[i++] = "B3";
                fieldArray[i++] = "C3";
                fieldArray[i++] = "D3";
                fieldArray[i++] = "E3";
                fieldArray[i++] = "F3";
                fieldArray[i++] = "G3";
                fieldArray[i++] = "H3";
                fieldArray[i++] = "A2";
                fieldArray[i++] = "B2";     /*50*/
                fieldArray[i++] = "C2";
                fieldArray[i++] = "D2";
                fieldArray[i++] = "E2";
                fieldArray[i++] = "F2";
                fieldArray[i++] = "G2";
                fieldArray[i++] = "H2";
                fieldArray[i++] = "A1";
                fieldArray[i++] = "B1";
                fieldArray[i++] = "C1";
                fieldArray[i++] = "D1";     /*60*/
                fieldArray[i++] = "E1";
                fieldArray[i++] = "F1";
                fieldArray[i++] = "G1";
                fieldArray[i++] = "H1";     /*64*/
            }
    
            //Dictionary<string, ChessButton> Btn_Dict;
    
            public ChessField(int size, int x,int y, ListBox lb)
            {
                this.setFields();
                base.Width = size;
                base.Location = new Point(x, y);
                base.Width = (base.Width / 8) * 8;
                base.Height = base.Width;
                base.BackColor = Color.Gray;
                mystruct.reflv = lb;
                ChessButton.setImg(); 
                //this.draw();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                int s = base.Width / 8;
                state g = state.black;
                int cnt = 0;
                this.createStart();
                for(int y = 0; y<8;y++)
                {
                    for(int x = 0; x<8;x++)
                    {
                        if (g == state.black)
                        {
    
                            base.Controls.Add(new ChessButton(s,x*s,y*s,Color.White, fieldArray[cnt],list[fieldArray[cnt++]],ChessClick ));
                            g = state.white;
    
                        }
                        else
                        {
    
                            base.Controls.Add(new ChessButton(s, x * s, y * s, Color.Black, fieldArray[cnt], list[fieldArray[cnt++]], ChessClick));
                            g = state.black;
    
                        }
                    }
                    if (g == state.black)
                        g = state.white;
                    else
                        g = state.black;
               }
               // this.createStart();
            }
            public void ChessClick(object sender, EventArgs e)
            {
                if ((((ChessButton)sender).figure != "NULL") && this.whatmem == null)
                { 
                    this.fieldmem = ((ChessButton)sender).field;
                    this.whatmem = ((ChessButton)sender).figure;
                    ((ChessButton)sender).figure = "NULL";
                    this.mystruct.figure = this.whatmem + " " + this.fieldmem;
                }
                else
                if (((((ChessButton)sender).figure == "NULL") && this.whatmem == null))
                {
                    ((ChessButton)sender).figure = this.whatmem;
                    this.whatmem = null;
                    this.fieldmem = null;
                }
            }
    
            public void createStart()
            {
    
                //Black Figures
                list.Add("A8","BLACK ROUGE");
                list.Add("B8","BLACK KNIGHT");
                list.Add("C8","BLACK BISHOP");
                list.Add("D8","BLACK QUEEN");
                list.Add("E8","BLACK KING");
                list.Add("F8","BLACK BISHOP");
                list.Add("G8","BLACK KNIGHT");
                list.Add("H8","BLACK ROUGE");
                list.Add("A7", "BLACK PAWN");
                list.Add("B7", "BLACK PAWN");
                list.Add("C7", "BLACK PAWN");
                list.Add("D7", "BLACK PAWN");
                list.Add("E7", "BLACK PAWN");
                list.Add("F7", "BLACK PAWN");
                list.Add("G7", "BLACK PAWN");
                list.Add("H7", "BLACK PAWN");
    
                //NULL the rest
    
                list.Add("A6", "NULL");
                list.Add("B6", "NULL");
                list.Add("C6", "NULL");
                list.Add("D6", "NULL");
                list.Add("E6", "NULL");
                list.Add("F6", "NULL");
                list.Add("G6", "NULL");
                list.Add("H6", "NULL");
                list.Add("A5", "NULL");
                list.Add("B5", "NULL");
                list.Add("C5", "NULL");
                list.Add("D5", "NULL");
                list.Add("E5", "NULL");
                list.Add("F5", "NULL");
                list.Add("G5", "NULL");
                list.Add("H5", "NULL");
                list.Add("A4", "NULL");
                list.Add("B4", "NULL");
                list.Add("C4", "NULL");
                list.Add("D4", "NULL");
                list.Add("E4", "NULL");
                list.Add("F4", "NULL");
                list.Add("G4", "NULL");
                list.Add("H4", "NULL");
                list.Add("A3", "NULL");
                list.Add("B3", "NULL");
                list.Add("C3", "NULL");
                list.Add("D3", "NULL");
                list.Add("E3", "NULL");
                list.Add("F3", "NULL");
                list.Add("G3", "NULL");
                list.Add("H3", "NULL");
                //White Figures
                list.Add("A1","WHITE ROUGE");
                list.Add("B1","WHITE KNIGHT");
                list.Add("C1","WHITE BISHOP");
                list.Add("D1","WHITE QUEEN");
                list.Add("E1","WHITE KING");
                list.Add("F1","WHITE BISHOP");
                list.Add("G1","WHITE KNIGHT");
                list.Add("H1","WHITE ROUGE");
                list.Add("A2", "WHITE PAWIN");
                list.Add("B2", "WHITE PAWIN");
                list.Add("C2", "WHITE PAWIN");
                list.Add("D2", "WHITE PAWIN");
                list.Add("E2", "WHITE PAWIN");
                list.Add("F2", "WHITE PAWIN");
                list.Add("G2", "WHITE PAWIN");
                list.Add("H2", "WHITE PAWIN");
            }
        }
    }
    

    ChessButton.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SchachVerwaltung
    {
        class ChessButton : Button
        {
            private string _figure;
            private string _field;
            static Dictionary<string, System.Drawing.Image> img = new Dictionary<string,System.Drawing.Image>();
            public ChessButton(int size, int x, int y, System.Drawing.Color back, string mfield, string figure, System.EventHandler function) : base()
            {
                //this.setImg();
                base.BackColor = back;
                base.Width = base.Height = size;
                base.Location = new System.Drawing.Point(x, y);
                this._field = mfield;
                this.figure = figure;
                base.Click += function;
            }
    
           public static void setImg()
            {
                //White
                string tmp = "WHITE ";
                ChessButton.img.Add(tmp + "PAWN", System.Drawing.Image.FromFile(@"e:\wPawn.png"));
                ChessButton.img.Add(tmp + "ROUGE", System.Drawing.Image.FromFile(@"e:\wRouge.png"));
                ChessButton.img.Add(tmp + "BISHOP", System.Drawing.Image.FromFile(@"e:\wBishop.png"));
                ChessButton.img.Add(tmp + "KNIGHT", System.Drawing.Image.FromFile(@"e:\wKnight.png"));
                ChessButton.img.Add(tmp + "QUEEN", System.Drawing.Image.FromFile(@"e:\wQueen.png"));
                ChessButton.img.Add(tmp + "KING", System.Drawing.Image.FromFile(@"e:\wKing.png"));
                //Black
                tmp = "BLACK ";
                ChessButton.img.Add(tmp + "PAWN", System.Drawing.Image.FromFile(@"e:\bPawn.png"));
                ChessButton.img.Add(tmp + "ROUGE", System.Drawing.Image.FromFile(@"e:\bRouge.png"));
                ChessButton.img.Add(tmp + "BISHOP", System.Drawing.Image.FromFile(@"e:\bBishop.png"));
                ChessButton.img.Add(tmp + "KNIGHT", System.Drawing.Image.FromFile(@"e:\bKnight.png"));
                ChessButton.img.Add(tmp + "QUEEN", System.Drawing.Image.FromFile(@"e:\bQueen.png"));
                ChessButton.img.Add(tmp + "KING", System.Drawing.Image.FromFile(@"e:\bKing.png"));
    
            }
    
            public string figure
            {
                set
                {
                    //string[] cont = value.Split(' ');
                    if(value!="NULL" && value != null)
                        base.Image = ChessButton.img[value];
                    if (value == "NULL")
                        base.Image = null;
                    this._figure = value;
                }
                get
                {
                    base.Image = null;
                    return this._figure;
                }
            }
            public string field
            {
                get
                {
                    return _field;
                }
            }
        }
    }
    

    Diesmal ist img Dictionary ein Statisches Objekt für alle Gleich

    Er will aber nicht die Weißen Figuren stellen 😞

    Weiß jemand wieso er genau abbricht?

    Vielen Dank



  • Es ist mit Sicherheit keine gute Idee, in OnPaint immer wieder neue Buttons anzulegen.

    Außerdem heißt es Rook. Rouge tut man sich auf die Backen.



  • Soll ich die Buttons in den Konstruktor packen?

    Habe den Fehler gefunden und onPaint entfernt. Es heißt jetzt createChessField() und wird im Constructor ausgeführt.



  • Für die Feldinitialisierung z.B.

    private string[,] fieldArray = new string[8, 8]; // rectangle array
    
    for(int y = 0; y<8; y++)
        for(int x = 0; x<8; x++)
            fieldArray[x, y} = String.Format("{0}{1}", (char)('A'+ x), y+1);
    

    Wobei ich mich frage, warum du überhaupt diese Strings benötigt (denn diese sind ja direkt aus der Position (x,y) berechenbar)?

    Sinnvoller wäre ersteinmal überhaupt die interne Datenstruktur richtig zu definieren (anstatt schon direkt den GUI-Code).

    Dein Code könnte z.B. so aussehen:

    enum ColorType { Empty, W, B };
    enum FigureType { None, P, N, B, R, Q, K };
    
    struct Figure
    {
      public static readonly Figure Null = new Figure(ColorType.Empty, FigureType.None);
    
      public Figure(ColorType color, FigureType figure)
          : this()
      {
        ColorType = color;
        FigureType = figure;
      }
    
      public ColorType ColorType { get; private set; }
    
      public FigureType FigureType { get; private set; }
    }
    
    // as shortcuts for field initialization
    static readonly ColorType W = ColorType.W;
    static readonly ColorType B = ColorType.B;
    
    Figure[,] field = new Figure[8, 8]
    {
      { new Figure(W, FigureType.R), new Figure(W, FigureType.N), /* ... */ },
    
      { Figure.Null, /* ... */ },
    
      { new Figure(B, FigureType.R), new Figure(B, FigureType.N), /* ... */ }
    };
    

    (oder evtl. durch eine Methode mithilfe von Schleifen erzeugen)

    Dies würdest du dann in eine eigene Klasse packen und dann entsprechende Methoden definieren, um vom GUI-Code aus darauf zuzugreifen.

    Der GUI-Code ist nur noch für die Darstellung dar (nicht mehr für die Datenhaltung etc.).


Log in to reply