C# :: Aufgabe #197
3 Lösungen

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.
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.
Lösungen:

<Window x:Class="KartenMischen.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:KartenMischen" mc:Ignorable="d" Title="MainWindow" Height="580" Width="1080" Loaded="Window_Loaded" > <DockPanel> <UniformGrid DockPanel.Dock="Bottom" Rows="1" Height="Auto" HorizontalAlignment="Right"> <Button Name="btn_Reset" Click="btn_Reset_Click">Reset</Button> <Button Name="btn_Shuffle" Click="btn_Shuffle_Click">Mischen</Button> </UniformGrid> <ScrollViewer DockPanel.Dock="Top"> <ItemsControl Name="Board"> <ItemsControl.ItemTemplate> <DataTemplate> <Image Source="{Binding}" Width="80"/> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </ScrollViewer> </DockPanel> </Window>

using System; using System.Collections.ObjectModel; using System.Windows; using System.Collections; namespace KartenMischen { /// <summary> /// Interaktionslogik für MainWindow.xaml /// </summary> public partial class MainWindow : Window { readonly Random _random = new Random(); public MainWindow() { InitializeComponent(); Reset(); } private void Window_Loaded(object sender, RoutedEventArgs e) { Shuffle(); } private void btn_Reset_Click(object sender, RoutedEventArgs e) { Reset(); } void Reset() { Board.ItemsSource = CreateDeck(); } ObservableCollection<string> CreateDeck() { ObservableCollection<string> Deck = new ObservableCollection<string>(); foreach(string suite in new string[] { "Clubs", "Spades", "Hearts", "Diamonds" }) { Deck.Add($"pack://application:,,,/KartenMischen;component/Resources/Cards/{suite}/Ace.png"); for(int i = 2; i <= 10; i++) { Deck.Add($"pack://application:,,,/KartenMischen;component/Resources/Cards/{suite}/{i.ToString("00")}.png"); } Deck.Add($"pack://application:,,,/KartenMischen;component/Resources/Cards/{suite}/Jack.png"); Deck.Add($"pack://application:,,,/KartenMischen;component/Resources/Cards/{suite}/Queen.png"); Deck.Add($"pack://application:,,,/KartenMischen;component/Resources/Cards/{suite}/King.png"); } return Deck; } private void btn_Shuffle_Click(object sender, RoutedEventArgs e) { Shuffle(); } void Shuffle() { for(int i = ((IList)Board.ItemsSource).Count - 1; i > -1; i--) { var Item = ((IList)Board.ItemsSource)[_random.Next(i)]; ((IList)Board.ItemsSource).Remove(Item); ((IList)Board.ItemsSource).Add(Item); } } } }
/*
* Karten-Bilder müssen im Ordner "./cards" abgelegt werden
*/
Form1.cs:
C#-Code
Form1.Designer.cs:
C#-Code
* Karten-Bilder müssen im Ordner "./cards" abgelegt werden
*/
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.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:

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; } }
Die Kartenbilder müssen in einem Ordner @"pics\" liegen und mit
image000.png ... image012.png
image100.png ... image112.png
image200.png ... image212.png
image300.png ... image312.png
bezeichnet sein.
Die Kartenrückseite ist @"pics\back.png".
C#-Code
C#-Code
C#-Code
image000.png ... image012.png
image100.png ... image112.png
image200.png ... image212.png
image300.png ... image312.png
bezeichnet sein.
Die Kartenrückseite ist @"pics\back.png".

<Window x:Class="ShuffleCards.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ShuffleCards" mc:Ignorable="d" Title="ShuffleCards" Height="700" Width="1050" FontFamily="Courier New" WindowState="Normal" WindowStartupLocation="CenterScreen"> <Grid Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}"> <Grid.RowDefinitions> <RowDefinition Height="35"/> <RowDefinition/> </Grid.RowDefinitions> <StackPanel Margin="0" Orientation="Horizontal"> <Button x:Name="button0" Content=" reset " Width="75" Margin="5" Click="button0_Click"/> <Button x:Name="button1" Content=" shuffle " Width="75" Margin="5" Click="button1_Click"/> <Label x:Name="lb_version" Content=" version 2018/01/14 2018/01/17" VerticalContentAlignment="Center" FontWeight="Bold" </StackPanel> <GroupBox x:Name="groupBox" Header=" card ground " Grid.Row="1" Margin="5"> <Grid Margin="5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Image x:Name="image000" Margin="5" Grid.Row="0" Grid.Column="0" /> <Image x:Name="image001" Margin="5" Grid.Row="0" Grid.Column="1" /> <Image x:Name="image002" Margin="5" Grid.Row="0" Grid.Column="2" /> <Image x:Name="image003" Margin="5" Grid.Row="0" Grid.Column="3" /> <Image x:Name="image004" Margin="5" Grid.Row="0" Grid.Column="4" /> <Image x:Name="image005" Margin="5" Grid.Row="0" Grid.Column="5" /> <Image x:Name="image006" Margin="5" Grid.Row="0" Grid.Column="6" /> <Image x:Name="image007" Margin="5" Grid.Row="0" Grid.Column="7" /> <Image x:Name="image008" Margin="5" Grid.Row="0" Grid.Column="8" /> <Image x:Name="image009" Margin="5" Grid.Row="0" Grid.Column="9" /> <Image x:Name="image010" Margin="5" Grid.Row="0" Grid.Column="10" /> <Image x:Name="image011" Margin="5" Grid.Row="0" Grid.Column="11" /> <Image x:Name="image012" Margin="5" Grid.Row="0" Grid.Column="12" /> <Image x:Name="image100" Margin="5" Grid.Row="1" Grid.Column="0" /> <Image x:Name="image101" Margin="5" Grid.Row="1" Grid.Column="1" /> <Image x:Name="image102" Margin="5" Grid.Row="1" Grid.Column="2" /> <Image x:Name="image103" Margin="5" Grid.Row="1" Grid.Column="3" /> <Image x:Name="image104" Margin="5" Grid.Row="1" Grid.Column="4" /> <Image x:Name="image105" Margin="5" Grid.Row="1" Grid.Column="5" /> <Image x:Name="image106" Margin="5" Grid.Row="1" Grid.Column="6" /> <Image x:Name="image107" Margin="5" Grid.Row="1" Grid.Column="7" /> <Image x:Name="image108" Margin="5" Grid.Row="1" Grid.Column="8" /> <Image x:Name="image109" Margin="5" Grid.Row="1" Grid.Column="9" /> <Image x:Name="image110" Margin="5" Grid.Row="1" Grid.Column="10" /> <Image x:Name="image111" Margin="5" Grid.Row="1" Grid.Column="11" /> <Image x:Name="image112" Margin="5" Grid.Row="1" Grid.Column="12" /> <Image x:Name="image200" Margin="5" Grid.Row="2" Grid.Column="0" /> <Image x:Name="image201" Margin="5" Grid.Row="2" Grid.Column="1" /> <Image x:Name="image202" Margin="5" Grid.Row="2" Grid.Column="2" /> <Image x:Name="image203" Margin="5" Grid.Row="2" Grid.Column="3" /> <Image x:Name="image204" Margin="5" Grid.Row="2" Grid.Column="4" /> <Image x:Name="image205" Margin="5" Grid.Row="2" Grid.Column="5" /> <Image x:Name="image206" Margin="5" Grid.Row="2" Grid.Column="6" /> <Image x:Name="image207" Margin="5" Grid.Row="2" Grid.Column="7" /> <Image x:Name="image208" Margin="5" Grid.Row="2" Grid.Column="8" /> <Image x:Name="image209" Margin="5" Grid.Row="2" Grid.Column="9" /> <Image x:Name="image210" Margin="5" Grid.Row="2" Grid.Column="10" /> <Image x:Name="image211" Margin="5" Grid.Row="2" Grid.Column="11" /> <Image x:Name="image212" Margin="5" Grid.Row="2" Grid.Column="12" /> <Image x:Name="image300" Margin="5" Grid.Row="3" Grid.Column="0" /> <Image x:Name="image301" Margin="5" Grid.Row="3" Grid.Column="1" /> <Image x:Name="image302" Margin="5" Grid.Row="3" Grid.Column="2" /> <Image x:Name="image303" Margin="5" Grid.Row="3" Grid.Column="3" /> <Image x:Name="image304" Margin="5" Grid.Row="3" Grid.Column="4" /> <Image x:Name="image305" Margin="5" Grid.Row="3" Grid.Column="5" /> <Image x:Name="image306" Margin="5" Grid.Row="3" Grid.Column="6" /> <Image x:Name="image307" Margin="5" Grid.Row="3" Grid.Column="7" /> <Image x:Name="image308" Margin="5" Grid.Row="3" Grid.Column="8" /> <Image x:Name="image309" Margin="5" Grid.Row="3" Grid.Column="9" /> <Image x:Name="image310" Margin="5" Grid.Row="3" Grid.Column="10" /> <Image x:Name="image311" Margin="5" Grid.Row="3" Grid.Column="11" /> <Image x:Name="image312" Margin="5" Grid.Row="3" Grid.Column="12" /> </Grid> </GroupBox> </Grid> </Window>

using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; namespace ShuffleCards { public partial class MainWindow : Window { public enum Cases { BackSide, Reset, Mix }; public void set_images(Cases icase) { Image[] Im = new Image[] { this.image000, this.image001, this.image002, this.image003, this.image004, this.image005, this.image006, this.image007, this.image008, this.image009, this.image010, this.image011, this.image012, this.image100, this.image101, this.image102, this.image103, this.image104, this.image105, this.image106, this.image107, this.image108, this.image109, this.image110, this.image111, this.image112, this.image200, this.image201, this.image202, this.image203, this.image204, this.image205, this.image206, this.image207, this.image208, this.image209, this.image210, this.image211, this.image212, this.image300, this.image301, this.image302, this.image303, this.image304, this.image305, this.image306, this.image307, this.image308, this.image309, this.image310, this.image311, this.image312 }; int count_cards = Im.Length; Shuffle shufflecards = null; switch (icase) { case Cases.BackSide: string s0 = @"pics\back.png"; ImageSource imageSource = new BitmapImage(new Uri(s0, UriKind.RelativeOrAbsolute)); foreach (Image im in Im) im.Source = imageSource; return; case Cases.Reset: shufflecards = new Shuffle(count_cards, bo_reset: true); break; case Cases.Mix: shufflecards = new Shuffle(count_cards, bo_reset: false); break; }; string[] uris = new string[count_cards]; string s00 = @"pics\"; int zeiger = 0; for (var i = 0; i < uris.Length; i++) { if (i == 11 || i == 24 || i == 37 || i == 50) zeiger++; uris[i] = s00 + zeiger.ToString("00") + ".png"; zeiger++; }; for (var i = 0; i < uris.Length; i++) Im[i].Source = new BitmapImage(new Uri(uris[shufflecards.Mix[i]], UriKind.RelativeOrAbsolute)); } public MainWindow() { InitializeComponent(); set_images((int)Cases.BackSide); } private void button0_Click(object sender, RoutedEventArgs e) => set_images(Cases.Reset); private void button1_Click(object sender, RoutedEventArgs e) => set_images(Cases.Mix); } }

using System; using System.Collections.Generic; using System.Text; namespace ShuffleCards { public class Shuffle { private Random rand; private int count_cards; private bool bo_reset; public int[] Mix { get; private set; } //Fisher - Yates shuffle public Shuffle(int count_cards, bool bo_reset = false) { rand = new Random(); this.count_cards = count_cards; this.bo_reset = bo_reset; this.Mix = new int[count_cards]; for (var i = 0; i < count_cards; i++) this.Mix[i] = i; if (!bo_reset) mix(); } private void mix() { Stack<int> stack = new Stack<int>(); for (var j = Mix.Length - 1; j > 0; j--) { int jj = rand.Next(j + 1); stack.Push(Mix[jj]); Mix[jj] = Mix[j]; Mix[j] = stack.Pop(); } } } }