C# :: Aufgabe #197 :: Lösung #3
3 Lösungen
#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.
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.
#3
von hollst (13980 Punkte)
- 14.02.2018 um 11:58 Uhr
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();
}
}
}
}
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
