C# :: Aufgabe #197 :: Lösung #2

3 Lösungen Lösungen öffentlich
#197

Mischen von Spielkarten

Anfänger - C# von hollst - 14.01.2018 um 16:42 Uhr
Man schreibe eine GUI-Anwendung, die einen 52er Kartensatz mischt und die entstandene Reihenfolge anzeigt (BILD 2).
Die Anwendung soll zwei Befehlsknöpfe haben: MISCHEN und RESET. Mit RESET soll die Default-Reihenfolge angezeigt werden (BILD 1).

Einen Satz von Image-Dateien der Spielkarten könnt ihr z. B. unter http://www.nongnu.org/cardpics/cardpics.en.html herunterladen und darüber frei verfügen, sofern ihr eure Lösung nicht für kommerzielle Zwecke vermarkten wollt.
#2
vote_ok
von hyWse (50 Punkte) - 08.02.2018 um 22:21 Uhr
/*
* Karten-Bilder müssen im Ordner "./cards" abgelegt werden
*/

Form1.cs:
Quellcode ausblenden C#-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 AUFGABEN {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }

        private String[] _files;
        private Image[] _cards;

        private Random _rnd;

        private void Form1_Load(object sender, EventArgs e) {
            _rnd = new Random();

            LoadCardsFromDirectory();
            ReloadCards();
            ReloadDisplay();
        }

        /*
         * Funktionen
         */
        public void LoadCardsFromDirectory() {
            _files = System.IO.Directory.GetFiles("cards");
        }

        public void ReloadCards() {
            if (_files == null) return;

            _cards = new Image[_files.Length];

            for (int i = 0; i < _files.Length; i++) {
                _cards[i] = Image.FromFile(_files[i]);
            }
        }
        public void ReloadDisplay() {
            if (_cards == null) return;

            flPnlCards.Controls.Clear();

            for (int i = 0; i < _cards.Length; i++) {
                PictureBox pictureBox = new PictureBox();
                pictureBox.Size = new Size(79, 123);
                pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox.Image = _cards[i];

                flPnlCards.Controls.Add(pictureBox);

            }
        }
        public void MixCards() {
            _files = _files.OrderBy(x => _rnd.Next()).ToArray();
        }
  
        /*
         * Buttons
         */
        private void btnMixCards_Click(object sender, EventArgs e) {
            MixCards();
            ReloadCards();
            ReloadDisplay();
        }

        private void btnReset_Click(object sender, EventArgs e) {
            LoadCardsFromDirectory();
            ReloadCards();
            ReloadDisplay();
        }
    }
}


Form1.Designer.cs:
Quellcode ausblenden C#-Code
namespace AUFGABEN {
    partial class Form1 {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.flPnlCards = new System.Windows.Forms.FlowLayoutPanel();
            this.btnMixCards = new System.Windows.Forms.Button();
            this.btnReset = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox1.Controls.Add(this.flPnlCards);
            this.groupBox1.Location = new System.Drawing.Point(12, 45);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(600, 289);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Karten";
            // 
            // flPnlCards
            // 
            this.flPnlCards.AutoScroll = true;
            this.flPnlCards.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.flPnlCards.Dock = System.Windows.Forms.DockStyle.Fill;
            this.flPnlCards.Location = new System.Drawing.Point(3, 16);
            this.flPnlCards.Name = "flPnlCards";
            this.flPnlCards.Size = new System.Drawing.Size(594, 270);
            this.flPnlCards.TabIndex = 0;
            // 
            // btnMixCards
            // 
            this.btnMixCards.Location = new System.Drawing.Point(12, 12);
            this.btnMixCards.Name = "btnMixCards";
            this.btnMixCards.Size = new System.Drawing.Size(75, 23);
            this.btnMixCards.TabIndex = 1;
            this.btnMixCards.Text = "Mischen";
            this.btnMixCards.UseVisualStyleBackColor = true;
            this.btnMixCards.Click += new System.EventHandler(this.btnMixCards_Click);
            // 
            // btnReset
            // 
            this.btnReset.Location = new System.Drawing.Point(93, 12);
            this.btnReset.Name = "btnReset";
            this.btnReset.Size = new System.Drawing.Size(75, 23);
            this.btnReset.TabIndex = 2;
            this.btnReset.Text = "Reset";
            this.btnReset.UseVisualStyleBackColor = true;
            this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(624, 346);
            this.Controls.Add(this.btnReset);
            this.Controls.Add(this.btnMixCards);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.FlowLayoutPanel flPnlCards;
        private System.Windows.Forms.Button btnMixCards;
        private System.Windows.Forms.Button btnReset;
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben